fixed crash management
[NeonServV5.git] / src / main.c
1 /* main.c - NeonServ v5.4
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "main.h"
19 #include "signal.h"
20 #include "ClientSocket.h"
21 #include "UserNode.h"
22 #include "ChanNode.h"
23 #include "IRCEvents.h"
24 #include "IRCParser.h"
25 #include "modcmd.h"
26 #include "WHOHandler.h"
27 #include "bots.h"
28 #include "mysqlConn.h"
29 #include "HandleInfoHandler.h"
30 #include "lang.h"
31 #include "tools.h"
32 #include "timeq.h"
33 #include "EventLogger.h"
34 #include "ModeNode.h"
35 #include "IRCQueue.h"
36 #include "DBHelper.h"
37 #include "ConfigParser.h"
38 #include "ssl.h"
39 #include "QServer.h"
40 #include "version.h"
41 #include "modules.h"
42 #include "module_commands.h"
43
44 time_t start_time;
45 static int running, hard_restart;
46 static int statistics_requested_lusers = 0;
47 int statistics_enabled;
48 TIMEQ_CALLBACK(main_statistics);
49 TIMEQ_CALLBACK(main_checkauths);
50 static int daemonized = 0;
51 static int print_loglevel = 0;
52 static FILE *log_fptr = NULL;
53 static int process_argc;
54 static char **process_argv;
55 #ifdef HAVE_THREADS
56 int running_threads;
57 pthread_mutex_t cache_sync;
58 pthread_mutex_t whohandler_sync, whohandler_mass_sync;
59 static pthread_mutex_t log_sync;
60 #endif
61
62 static void check_firstrun();
63
64 void cleanup() {
65     stop_modules();
66     free_sockets();
67     qserver_free();
68     free_parser();
69     free_UserNode();
70     free_ChanNode();
71     free_bind();
72     free_modcmd();
73     free_whoqueue();
74     free_mysql();
75     free_handleinfohandler();
76     free_lang();
77 }
78
79 static int load_mysql_config() {
80     char *mysql_host, *mysql_user, *mysql_pass, *mysql_base;
81     int mysql_serverport;
82     
83     mysql_host = get_string_field("MySQL.host");
84     if(!mysql_host) {
85         perror("invalid neonserv.conf: missing MySQL.host");
86         return 0;
87     }
88     mysql_serverport = get_int_field("MySQL.port");
89     if(!mysql_serverport)
90         mysql_serverport = 3306;
91     mysql_user = get_string_field("MySQL.user");
92     if(!mysql_user) {
93         perror("invalid neonserv.conf: missing MySQL.user");
94         return 0;
95     }
96     mysql_pass = get_string_field("MySQL.pass");
97     if(!mysql_pass) {
98         perror("invalid neonserv.conf: missing MySQL.pass");
99         return 0;
100     }
101     mysql_base = get_string_field("MySQL.base");
102     if(!mysql_base) {
103         perror("invalid neonserv.conf: missing MySQL base");
104         return 0;
105     }
106     init_mysql(mysql_host, mysql_serverport, mysql_user, mysql_pass, mysql_base);
107     return 1;
108 }
109
110 #ifdef HAVE_THREADS
111 pthread_t *current_threads = NULL;
112
113 void * thread_main(void *arg) {
114     time_t socket_wait;
115     while(running) {
116         socket_wait = time(0) + SOCKET_SELECT_TIME;
117         do {
118             if(!socket_loop(SOCKET_SELECT_TIME)) {
119                 if(!running) break;
120                 putlog(LOGLEVEL_ERROR, "No more active Bots... shutting down.\n");
121                 cleanup();
122                 exit(0);
123             }
124         } while(time(0) < socket_wait);
125         if(!running) break;
126         clearTempUsers();
127         destroyEvents();
128         mysql_free();
129     }
130     running_threads--;
131     return NULL;
132 }
133
134 int getCurrentThreadID() {
135     if(!current_threads) return 0;
136     int i;
137     unsigned int my_tid = (unsigned int) pthread_self_tid();
138     for(i = 0; i < running_threads; i++) {
139         #ifdef WIN32
140         if((unsigned int) current_threads[i].p == my_tid)
141         #else
142         if((unsigned int) current_threads[i] == my_tid)
143         #endif
144             return i+1;
145     }
146     return 0;
147 }
148
149 #endif
150
151 void exit_daemon() {
152     running = 0;
153     if(daemonized) {
154         remove(PID_FILE);
155     }
156     if(log_fptr) {
157         fclose(log_fptr);
158         log_fptr = NULL;
159     }
160 }
161
162 int main(int argc, char *argv[]) {
163     int run_as_daemon = 1;
164     int c;
165     process_argv = argv;
166     process_argc = argc;
167     printf("NeonServ v%s\n\n", NEONSERV_VERSION);
168     struct option options[] = {
169         {"show", 1, 0, 's'},
170         {"foreground", 0, 0, 'f'},
171         {"help", 0, 0, 'h'},
172         {"version", 0, 0, 'v'},
173         {0, 0, 0, 0}
174     };
175     while ((c = getopt_long(argc, argv, "s:fvh", options, NULL)) != -1) {
176         switch (c) {
177         case 's':
178             print_loglevel = atoi(optarg);
179             break;
180         case 'f':
181             run_as_daemon = 0;
182             break;
183         case 'v':
184             printf("Version: %s.%d (%s)\n", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"));
185             printf("Build: #%s %s (%s lines, " COMPILER ")\n", compilation, creation, codelines);
186             exit(0);
187             break;
188         case 'h':
189             printf("Usage: ./neonserv [-s loglevel] [-f] [-h|-v]\n");
190             printf(" -s, --show           show log lines matching loglevel in stdout.\n");
191             printf(" -f, --foreground     run NeonServ in the foreground.\n");
192             printf(" -h, --help           prints this usage message.\n");
193             printf(" -v, --version        prints this program's version.\n");
194             exit(0);
195             break;
196         }
197     }
198     #ifndef WIN32
199     if(geteuid() == 0 || getuid() == 0) {
200         fprintf(stderr, "NeonServ may not be run with super user privileges.\n");
201         exit(0);
202     }
203     #endif
204     #ifdef ENABLE_MEMORY_DEBUG
205     initMemoryDebug();
206     #endif
207     if(!loadConfig(CONF_FILE)) {
208         fprintf(stderr, "Unable to load " CONF_FILE "\n");
209         exit(0);
210     }
211     #if HAVE_THREADS
212     THREAD_MUTEX_INIT(log_sync);
213     #endif
214     if(!load_mysql_config()) {
215         fprintf(stderr, "Unable to connect to MySQL\n");
216         exit(0);
217     }
218     check_firstrun();
219     if (run_as_daemon) {
220         #ifndef WIN32
221         /* Attempt to fork into the background if daemon mode is on. */
222         pid_t pid = fork();
223         if (pid < 0) {
224             fprintf(stderr, "Unable to fork: %s\n", strerror(errno));
225         } else if (pid > 0) {
226             printf("Forking into the background (pid: %d)...\n", pid);
227             putlog(LOGLEVEL_INFO, "Forking into the background (pid: %d)...\n", pid);
228             exit(0);
229         }
230         setsid();
231         daemonized = 1;
232         atexit(exit_daemon);
233         FILE *pidfile = fopen(PID_FILE, "w");
234         if (pidfile == NULL) {
235             fprintf(stderr, "Unable to create PID file: %s\n", strerror(errno));
236             putlog(LOGLEVEL_ERROR, "Unable to create PID file: %s\n", strerror(errno));
237         } else {
238             fprintf(pidfile, "%i\n", (int)getpid());
239             fclose(pidfile);
240         }
241         fclose(stdin); fopen("/dev/null", "r");
242         fclose(stdout); fopen("/dev/null", "w");
243         fclose(stderr); fopen("/dev/null", "w");
244         #endif
245     }
246     
247 main:
248     signal(SIGABRT, sighandler);
249     signal(SIGFPE, sighandler);
250     signal(SIGILL, sighandler);
251     signal(SIGINT, sighandler);
252     signal(SIGSEGV, sighandler);
253     signal(SIGTERM, sighandler);
254     
255     start_time = time(0);
256     
257     #ifdef WIN32
258     int res;
259     WSADATA wsadata;
260     // Start Windows Sockets.
261     res = WSAStartup(MAKEWORD(2, 0), &wsadata);
262     if (res)
263     {
264         perror("Unable to start Windows Sockets");
265         return 0;
266     }
267     #endif
268     
269     statistics_enabled = get_int_field("statistics.enable");
270     
271     #ifdef HAVE_THREADS
272     THREAD_MUTEX_INIT(cache_sync);
273     THREAD_MUTEX_INIT(whohandler_sync);
274     THREAD_MUTEX_INIT(whohandler_mass_sync);
275     #endif
276     
277     queue_init();
278     init_sockets();
279     init_timeq();
280     init_lang();
281     ssl_init();
282     init_parser();
283     init_UserNode();
284     init_ChanNode();
285     init_ModeNode();
286     init_bind();
287         init_modcmd();
288     register_module_commands();
289     init_handleinfohandler();
290     init_tools();
291     loadModules();
292     init_bots();
293     init_DBHelper();
294     qserver_init();
295     
296     load_languages();
297     int update_minutes = get_int_field("statistics.frequency");
298     if(!update_minutes) update_minutes = 2;
299     timeq_add(update_minutes * 60 + 10, 0, main_statistics, NULL);
300     
301     timeq_add(90, 0, main_checkauths, NULL);
302     
303     int worker_threads = get_int_field("General.worker_threads");
304     if(!worker_threads) worker_threads = 1;
305     running = 1;
306     #ifdef HAVE_THREADS
307     int tid_id = 0;
308     current_threads = calloc(worker_threads, sizeof(*current_threads));
309     for(tid_id = 0; tid_id < worker_threads; tid_id++) {
310         running_threads++;
311         pthread_create(&current_threads[tid_id], NULL, thread_main, NULL);
312     }
313     int usleep_delay = 1000000 / TICKS_PER_SECOND;
314     while(running) {
315         timeq_tick();
316         loop_modules();
317         qserver_loop();
318         queue_loop();
319         mysql_free();
320         usleep(usleep_delay);
321     }
322     for(tid_id = 0; tid_id < worker_threads; tid_id++) {
323         pthread_join(current_threads[tid_id], NULL);
324     }
325     running_threads = 0;
326     #else
327     time_t socket_wait;
328     while(running) {
329         socket_wait = time(0) + SOCKET_SELECT_TIME;
330         do {
331             if(!socket_loop(SOCKET_SELECT_TIME)) {
332                 putlog(LOGLEVEL_ERROR, "No more active Bots... shutting down.\n");
333                 cleanup();
334                 exit(0);
335             }
336         } while(time(0) < socket_wait);
337         timeq_tick();
338         loop_modules();
339         clearTempUsers();
340         destroyEvents();
341         qserver_loop();
342         queue_loop();
343         mysql_free();
344     }
345     #endif
346     cleanup();
347     if(hard_restart) {
348         restart_process();
349     }
350     goto main;
351 }
352
353 void restart_process() {
354     /* Append a NULL to the end of argv[]. */
355     char **restart_argv = (char **)alloca((process_argc + 1) * sizeof(char *));
356     memcpy(restart_argv, process_argv, process_argc * sizeof(char *));
357     restart_argv[process_argc] = NULL;
358     #ifdef WIN32
359     execv(process_argv[0], (const char * const*)restart_argv);
360     #else
361     execv(process_argv[0], restart_argv);
362     #endif
363 }
364
365 int stricmp (const char *s1, const char *s2)
366 {
367    if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
368    if (s2 == NULL) return *s1;
369    char c1, c2;
370    while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
371    {
372      if (*s1 == '\0') break;
373      ++s1; ++s2;
374    }
375    return c1 - c2;
376 }
377
378 int stricmplen (const char *s1, const char *s2, int len)
379 {
380    if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
381    if (s2 == NULL) return *s1;
382    char c1, c2;
383    int i = 0;
384    while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
385    {
386      i++;
387      if (*s1 == '\0') break;
388      ++s1; ++s2;
389      if(i == len) break;
390    }
391    return c1 - c2;
392 }
393
394 void restart_bot(int do_hard_restart) {
395     hard_restart = do_hard_restart;
396     running = 0;
397 }
398
399 void stop_bot() {
400     cleanup();
401     exit(0);
402 }
403
404 void reload_config() {
405     loadConfig(CONF_FILE);
406 }
407
408 static int getCurrentSecondsOfDay() {
409     time_t now = time(0);
410     struct tm *timeofday = localtime(&now);
411     int seconds = 0;
412     seconds += timeofday->tm_hour * 3600;
413     seconds += timeofday->tm_min * 60;
414     seconds += timeofday->tm_sec;
415     return seconds;
416 }
417
418 static AUTHLOOKUP_CALLBACK(main_checkauths_callback) {
419     //check if registered is still valid
420     MYSQL_RES *res;
421     MYSQL_ROW row;
422     printf_mysql_query("SELECT `user_id`, `user_registered` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
423     res = mysql_use();
424     if ((row = mysql_fetch_row(res)) != NULL) {
425         if(!exists || (strcmp(row[1], "0") && registered != atoi(row[1]))) {
426             //User is no longer valid! Delete it...
427             deleteUser(atoi(row[0]));
428             char *alertchan = get_string_field("General.CheckAuths.alertchan");
429             if(alertchan) {
430                 struct ChanNode *alertchan_chan = getChanByName(alertchan);
431                 struct ClientSocket *alertclient;
432                 if(alertchan_chan && (alertclient = getChannelBot(alertchan_chan, 0)) != NULL) {
433                     putsock(alertclient, "PRIVMSG %s :Deleted User %s", alertchan_chan->name, auth);
434                 }
435             }
436         } else if(exists && !strcmp(row[1], "0")) {
437             printf_mysql_query("UPDATE `users` SET `user_registered` = '%lu', `user_lastcheck` = UNIX_TIMESTAMP() WHERE `user_id` = '%s'", (unsigned long) registered, row[0]);
438         } else {
439             printf_mysql_query("UPDATE `users` SET `user_lastcheck` = UNIX_TIMESTAMP() WHERE `user_id` = '%s'", row[0]);
440         }
441     }
442 }
443
444 TIMEQ_CALLBACK(main_checkauths) {
445     int next_call = 600;
446     if(get_int_field("General.CheckAuths.enabled")) {
447         int check_start_time = get_int_field("General.CheckAuths.start_time") * 3600;
448         int duration = get_int_field("General.CheckAuths.duration") * 60;
449         int now = getCurrentSecondsOfDay();
450         if(now < check_start_time && check_start_time+duration >= 86400) {
451             check_start_time -= 86400;
452         }
453         if(now >= check_start_time && now < (check_start_time + duration)) {
454             next_call = get_int_field("General.CheckAuths.interval");
455             //get the "longest-unchecked-user"
456             MYSQL_RES *res;
457             MYSQL_ROW row;
458             int lastcheck;
459             time_t unixtime = time(0);
460             int min_unckecked = get_int_field("General.CheckAuths.min_unckecked");
461             printf_mysql_query("SELECT `user_user`, `user_lastcheck` FROM `users` ORDER BY `user_lastcheck` ASC LIMIT 1");
462             res = mysql_use();
463             if ((row = mysql_fetch_row(res)) != NULL) {
464                 lastcheck = atoi(row[1]);
465                 if(!lastcheck || unixtime - lastcheck >= min_unckecked) {
466                     lookup_authname(row[0], 0, main_checkauths_callback, NULL);
467                 } else 
468                     next_call = 300;
469             }
470         } else {
471             int pending;
472             if(now > check_start_time)
473                 pending = 86400 - now + check_start_time;
474             else
475                 pending = check_start_time - now;
476             if(pending < 600)
477                 next_call = pending;
478         }
479         
480     }
481     timeq_add(next_call, 0, main_checkauths, NULL);
482 }
483
484 TIMEQ_CALLBACK(main_statistics) {
485     int update_minutes = get_int_field("statistics.frequency");
486     if(!update_minutes) update_minutes = 2;
487     timeq_add(update_minutes * 60, 0, main_statistics, NULL);
488     if(get_int_field("statistics.enable")) {
489         statistics_enabled = 1;
490         statistics_requested_lusers = 1;
491         if(get_int_field("statistics.include_lusers")) {
492             struct ClientSocket *bot, *lusersbot = NULL;
493             for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
494                 if(bot->flags & SOCKET_FLAG_PREFERRED)
495                     lusersbot = bot;
496             }
497             bot = lusersbot;
498             if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
499             putsock(bot, "LUSERS");
500         } else {
501             statistics_update();
502         }
503     } else
504         statistics_enabled = 0;
505 }
506
507 void statistics_update() {
508     if(get_int_field("statistics.enable") && statistics_requested_lusers && get_string_field("statistics.execute")) {
509         statistics_requested_lusers = 0;
510         char command[MAXLEN];
511         /* parameters:
512          - visible users
513          - visible chanusers
514          - visible channels
515          - privmsg per minute
516          - commands per minute
517          - network users
518          - network channels
519         */
520         sprintf(command, "%s %d %d %d %d %d %d %d", get_string_field("statistics.execute"), getUserCount(), getChanUserCount(), getChannelCount(), statistics_privmsg, statistics_commands, statistics_network_users, statistics_network_channels);
521         statistics_privmsg = 0;
522         statistics_commands = 0;
523         system(command);
524     }
525 }
526
527 time_t getStartTime() {
528     return start_time;
529 }
530
531 int getRunningThreads() {
532     return running_threads;
533 }
534
535 void write_log(int loglevel, const char *line, int len) {
536     SYNCHRONIZE(log_sync);
537     if(!daemonized && (print_loglevel & loglevel)) {
538         printf("%s", line);
539     } else if(!daemonized && loglevel == LOGLEVEL_ERROR) {
540         fprintf(stderr, "%s", line);
541     }
542     if(get_int_field("log.loglevel") & loglevel) {
543         if(!log_fptr) {
544             log_fptr = fopen(LOG_FILE, "a");
545             if(!log_fptr) goto write_log_end;
546         }
547         time_t rawtime;
548         struct tm *timeinfo;
549         time(&rawtime);
550         timeinfo = localtime(&rawtime);
551         char timestr[20];
552         int timepos = strftime(timestr, 20, "%x %X ", timeinfo);
553         fwrite(timestr, 1, timepos, log_fptr);
554         fwrite(line, 1, len, log_fptr);
555     }
556     write_log_end:
557     DESYNCHRONIZE(log_sync);
558 }
559
560 void putlog(int loglevel, const char *text, ...) {
561     va_list arg_list;
562     char logBuf[MAXLOGLEN];
563     int pos;
564     logBuf[0] = '\0';
565     va_start(arg_list, text);
566     pos = vsnprintf(logBuf, MAXLOGLEN - 1, text, arg_list);
567     va_end(arg_list);
568     if (pos < 0 || pos > (MAXLOGLEN - 1)) pos = MAXLOGLEN - 1;
569     logBuf[pos] = '\0';
570     write_log(loglevel, logBuf, pos);
571 }
572
573 static void check_firstrun() {
574     MYSQL_RES *res;
575     MYSQL_ROW row;
576     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_access` = 1000 LIMIT 1");
577     res = mysql_use();
578     if (mysql_fetch_row(res) == NULL) {
579         //first run!
580         printf("No superuser found...\n");
581         check_firstrun_admin:
582         printf("AuthServ account name of admin user: ");
583         char *ptr;
584         char adminuser[31];
585         fgets(adminuser, 30, stdin);
586         for(ptr = adminuser; *ptr; ptr++) { if(*ptr == '\n' || *ptr == '\r') *ptr = '\0'; }
587         if(strlen(adminuser) < 2) goto check_firstrun_admin;
588         printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(adminuser));
589         res = mysql_use();
590         if ((row = mysql_fetch_row(res)) != NULL)
591             printf_mysql_query("UPDATE `users` SET `user_access` = 1000 WHERE `user_id` = '%s'", row[0]);
592         else
593             printf_mysql_query("INSERT INTO `users` (`user_user`, `user_access`) VALUES ('%s', 1000)", escape_string(adminuser));
594     }
595     printf_mysql_query("SELECT `id` FROM `bots` WHERE `active` = 1 LIMIT 1");
596     res = mysql_use();
597     if (mysql_fetch_row(res) == NULL) {
598         //no bot active
599         printf("No active bot found...\n\n");
600         printf("ADD NEW BOT\n");
601         char *ptr;
602         char bot_nick[31];
603         check_firstrun_bot_nick:
604         printf("Nick: ");
605         fgets(bot_nick, 30, stdin);
606         for(ptr = bot_nick; *ptr; ptr++) { if(*ptr == '\n' || *ptr == '\r') *ptr = '\0'; }
607         if(strlen(bot_nick) < 2) goto check_firstrun_bot_nick;
608         char bot_ident[16];
609         check_firstrun_bot_ident:
610         printf("Ident: ");
611         fgets(bot_ident, 15, stdin);
612         for(ptr = bot_ident; *ptr; ptr++) { if(*ptr == '\n' || *ptr == '\r') *ptr = '\0'; }
613         if(strlen(bot_ident) < 2) goto check_firstrun_bot_ident;
614         char bot_realname[101];
615         check_firstrun_bot_realname:
616         printf("Realname: ");
617         fgets(bot_realname, 100, stdin);
618         for(ptr = bot_realname; *ptr; ptr++) { if(*ptr == '\n' || *ptr == '\r') *ptr = '\0'; }
619         if(strlen(bot_realname) < 2) goto check_firstrun_bot_realname;
620         char bot_server[101];
621         check_firstrun_bot_server:
622         printf("Server: [irc.onlinegamesnet.net] ");
623         fgets(bot_server, 100, stdin);
624         for(ptr = bot_server; *ptr; ptr++) { if(*ptr == '\n' || *ptr == '\r') *ptr = '\0'; }
625         if(*bot_server && strlen(bot_nick) < 5) goto check_firstrun_bot_server;
626         if(!*bot_server)
627             strcpy(bot_server, "irc.onlinegamesnet.net");
628         int bot_port;
629         char bot_port_buf[7];
630         printf("Port: [6667] ");
631         fgets(bot_port_buf, 6, stdin);
632         for(ptr = bot_port_buf; *ptr; ptr++) { if(*ptr == '\n' || *ptr == '\r') *ptr = '\0'; }
633         if(!*bot_port_buf)
634             bot_port = 6667;
635         else
636             bot_port = atoi(bot_port_buf);
637         int bot_ssl;
638         char bot_ssl_buf[5];
639         check_firstrun_bot_ssl:
640         printf("SSL: [y/N] ");
641         fgets(bot_ssl_buf, 4, stdin);
642         for(ptr = bot_ssl_buf; *ptr; ptr++) { if(*ptr == '\n' || *ptr == '\r') *ptr = '\0'; }
643         if(!*bot_ssl_buf || tolower(*bot_ssl_buf) == 'n')
644             bot_ssl = 0;
645         else if(tolower(*bot_ssl_buf) == 'y')
646             bot_ssl = 1;
647         else
648             goto check_firstrun_bot_ssl;
649         char bot_pass[101];
650         printf("Server Password: [] ");
651         fgets(bot_pass, 100, stdin);
652         for(ptr = bot_pass; *ptr; ptr++) { if(*ptr == '\n' || *ptr == '\r') *ptr = '\0'; }
653         int bot_maxchan;
654         char bot_maxchan_buf[5];
655         printf("MaxChannel: [20] ");
656         fgets(bot_maxchan_buf, 5, stdin);
657         for(ptr = bot_maxchan_buf; *ptr; ptr++) { if(*ptr == '\n' || *ptr == '\r') *ptr = '\0'; }
658         if(*bot_maxchan_buf)
659             bot_maxchan = atoi(bot_maxchan_buf);
660         else
661             bot_maxchan = 20;
662         int bot_queue;
663         char bot_queue_buf[5];
664         check_firstrun_bot_queue:
665         printf("Queue (prevents excess floods): [Y/n] ");
666         fgets(bot_queue_buf, 4, stdin);
667         for(ptr = bot_queue_buf; *ptr; ptr++) { if(*ptr == '\n' || *ptr == '\r') *ptr = '\0'; }
668         if(!*bot_queue_buf || tolower(*bot_queue_buf) == 'y')
669             bot_queue = 1;
670         else if(tolower(*bot_queue_buf) == 'n')
671             bot_queue = 0;
672         else
673             goto check_firstrun_bot_queue;
674         printf_mysql_query("INSERT INTO `bots` (`active`, `nick`, `server`, `port`, `pass`, `ssl`, `ident`, `realname`, `botclass`, `textbot`, `queue`, `defaulttrigger`, `max_channels`) VALUES ('1', '%s', '%s', '%d', '%s', '%d', '%s', '%s', '1', '1', '%d', '+', '%d')", escape_string(bot_nick), escape_string(bot_server), bot_port, escape_string(bot_pass), bot_ssl, escape_string(bot_ident), escape_string(bot_realname), bot_queue, bot_maxchan);
675     }
676 }
677