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