| 880 | } |
| 881 | |
| 882 | int startBot(char *createdb_query, int argc, char **argv, int flags, TBRequestCallback req_callback, TBCronCallback cron_callback, char **triggers) { |
| 883 | srand(time(NULL)); |
| 884 | |
| 885 | Bot.debug = 0; |
| 886 | Bot.verbose = 0; |
| 887 | Bot.dbfile = "./mybot.sqlite"; |
| 888 | Bot.triggers = triggers; |
| 889 | Bot.apikey = NULL; |
| 890 | Bot.req_callback = req_callback; |
| 891 | Bot.cron_callback = cron_callback; |
| 892 | |
| 893 | /* Parse options. */ |
| 894 | for (int j = 1; j < argc; j++) { |
| 895 | int morearg = argc-j-1; |
| 896 | if (!strcmp(argv[j],"--debug")) { |
| 897 | Bot.debug++; |
| 898 | Bot.verbose = 1; |
| 899 | } else if (!strcmp(argv[j],"--verbose")) { |
| 900 | Bot.verbose = 1; |
| 901 | } else if (!strcmp(argv[j],"--apikey") && morearg) { |
| 902 | Bot.apikey = sdsnew(argv[++j]); |
| 903 | } else if (!strcmp(argv[j],"--dbfile") && morearg) { |
| 904 | Bot.dbfile = argv[++j]; |
| 905 | } else if (!(flags & TB_FLAGS_IGNORE_BAD_ARG)) { |
| 906 | printf( |
| 907 | "Usage: %s [--apikey <apikey>] [--debug] [--verbose] " |
| 908 | "[--dbfile <filename>]" |
| 909 | "\n",argv[0]); |
| 910 | exit(1); |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | /* Initializations. Note that we don't redefine the SQLite allocator, |
| 915 | * since SQLite errors are always handled by Stonky anyway. */ |
| 916 | curl_global_init(CURL_GLOBAL_DEFAULT); |
| 917 | if (Bot.apikey == NULL) readApiKeyFromFile(); |
| 918 | if (Bot.apikey == NULL) { |
| 919 | printf("Provide a bot API key via --apikey or storing a file named " |
| 920 | "apikey.txt in the bot working directory.\n"); |
| 921 | exit(1); |
| 922 | } |
| 923 | resetBotStats(); |
| 924 | DbHandle = dbInit(createdb_query); |
| 925 | if (DbHandle == NULL) exit(1); |
| 926 | cJSON_Hooks jh = {.malloc_fn = xmalloc, .free_fn = xfree}; |
| 927 | cJSON_InitHooks(&jh); |
| 928 | |
| 929 | /* Enter the infinite loop handling the bot. */ |
| 930 | botMain(); |
| 931 | return 0; |
| 932 | } |
no test coverage detected