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