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