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