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