diff --git a/src/parse.c b/src/parse.c --- a/src/parse.c +++ b/src/parse.c @@ -11,6 +11,7 @@ #include #include +#include "common.h" #include #include @@ -23,32 +24,10 @@ #define SOURCE_URL "http://largo.homelinux.org/cgi-bin/gitweb.cgi?p=net/mcbot.git" #endif -#define HELP 0 -#define JOIN 1 -#define LEAVE 2 -#define ADD 3 -#define REPLACE 4 -#define DELETE 5 -#define LIST 6 -#define SEARCH 7 -#define INFO 8 -#define PING 9 -#define ON 10 -#define OFF 11 -#define DEBUG 12 -#define VACCUUM 13 -#define LOGOUT 14 -#define WHO 15 -#define WHOIS 16 -#define TIME 17 -#define TELL 18 -#define OP 19 -#define DEOP 20 -#define KICK 21 -#define BAN 22 -#define UNBAN 23 -#define KICKBAN 24 -#define HELLO 25 +enum { + HELP, JOIN, LEAVE, ADD, REPLACE, DELETE, LIST, SEARCH, INFO, PING, ON, OFF, DEBUG, + VACCUUM, LOGOUT, WHO, WHOIS, TIME, TELL, OP, DEOP, KICK, BAN, UNBAN, KICKBAN, HELLO +}; const char *COMMAND_LIST[] = { @@ -89,7 +68,7 @@ char *parse (MSG * message) { - static char msg[513]; + static char msg[DEFAULT_BUF_SIZE]; int cmd = -1; int i; char *token; @@ -101,7 +80,7 @@ /* * default message */ - snprintf (msg, 512, "PRIVMSG %s :%s.\r\n", message->channel, gettext ("Request cannot be performed")); + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s.\r\n", message->channel, gettext ("Request cannot be performed")); /* * PRIVATE MESSAGES @@ -125,16 +104,16 @@ if ((token = strtok (NULL, "\r\n"))) cmd = atoi (token); if ((cmd > 0) && (cmd < VISIBLE_ITEMS)) - snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[cmd]); + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[cmd]); else - snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[0]); + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[0]); return msg; case JOIN: if ((token = strtok (NULL, "\r\n"))) if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) { if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) - snprintf (msg, 512, "JOIN %s\r\n", token); + snprintf (msg, sizeof (msg), "JOIN %s\r\n", token); } return msg; @@ -143,17 +122,17 @@ return NULL; if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) { if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) - snprintf (msg, 512, "PART %s :Leaving.\r\n", message->channel); + snprintf (msg, sizeof (msg), "PART %s :Leaving.\r\n", message->channel); } return msg; case ADD: if ((token = strtok (NULL, " "))) { if ((parameters = strtok (NULL, "\r\n"))) { - snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s\r\n", message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 0)); } else { - snprintf (msg, 512, "PRIVMSG %s :%s, %s!\r\n", message->channel, + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s!\r\n", message->channel, gettext ("I need more parameters to add"), message->user); } } @@ -162,10 +141,10 @@ case REPLACE: if ((token = strtok (NULL, " "))) { if ((parameters = strtok (NULL, "\r\n"))) { - snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s\r\n", message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 1)); } else { - snprintf (msg, 512, "PRIVMSG %s :%s, %s!\r\n", message->channel, + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s!\r\n", message->channel, gettext ("I need more parameters to replace"), message->user); } } @@ -173,63 +152,64 @@ case DELETE: if ((token = strtok (NULL, "\r\n"))) { - snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s\r\n", message->channel, message->user, db_remove (DATABASE_FILE, token)); } else { - snprintf (msg, 512, "PRIVMSG %s :%s, %s!\r\n", message->channel, gettext ("I need a key to delete"), - message->user); + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s!\r\n", message->channel, + gettext ("I need a key to delete"), message->user); } return msg; case LIST: - snprintf (msg, 512, "PRIVMSG %s :%s %s\r\n", message->channel, db_elements (DATABASE_FILE), + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s %s\r\n", message->channel, db_elements (DATABASE_FILE), db_lookup (DATABASE_FILE, "mcbot.cgi")); return msg; case SEARCH: if ((token = strtok (NULL, "\r\n"))) { - snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", message->channel, message->user, + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s\r\n", message->channel, message->user, db_lookup (DATABASE_FILE, token)); } else { - snprintf (msg, 512, + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s!\r\n", message->channel, gettext ("I need a key to lookup"), message->user); } return msg; case INFO: - snprintf (msg, 512, "PRIVMSG %s :I am MCBot-%1.2f Build %s and my source code can be found at %s\r\n", + snprintf (msg, sizeof (msg), + "PRIVMSG %s :I am MCBot-%1.2f Build %s and my source code can be found at %s\r\n", message->channel, BOT_VERSION, BOT_BUILD, SOURCE_URL); return msg; case PING: if ((token = strtok (NULL, "\r\n"))) - snprintf (msg, 512, "PRIVMSG %s :PING 0815\r\n", token); + snprintf (msg, sizeof (msg), "PRIVMSG %s :PING 0815\r\n", token); return msg; case ON: - snprintf (msg, 512, "PRIVMSG %s :%s %s.\r\n", message->user, gettext ("Autolearn enabled for channel"), - message->channel); + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s %s.\r\n", message->user, + gettext ("Autolearn enabled for channel"), message->channel); return msg; case OFF: - snprintf (msg, 512, "PRIVMSG %s :%s %s.\r\n", message->user, gettext ("Autolearn disabled for channel"), - message->channel); + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s %s.\r\n", message->user, + gettext ("Autolearn disabled for channel"), message->channel); return msg; case DEBUG: - snprintf (msg, 512, "PRIVMSG %s :USER: %s EMAIL: %s CHANNEL: %s LINE: %s\r\n", message->channel, - message->user, message->email, message->channel, message->line); + snprintf (msg, sizeof (msg), "PRIVMSG %s :USER: %s EMAIL: %s CHANNEL: %s LINE: %s\r\n", + message->channel, message->user, message->email, message->channel, message->line); return msg; case VACCUUM: - snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, db_vaccuum (DATABASE_FILE)); + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s\r\n", message->channel, db_vaccuum (DATABASE_FILE)); return msg; case LOGOUT: if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) { if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) { - snprintf (msg, 512, "PRIVMSG %s :%s!\r\nQUIT\r\n", message->channel, + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s!\r\nQUIT\r\n", message->channel, gettext ("Bye, have a nice day!")); } } @@ -240,13 +220,13 @@ case WHO: if ((token = strtok (NULL, "\r\n")) != NULL) { - snprintf (msg, 512, "WHO %s\r\n", token); + snprintf (msg, sizeof (msg), "WHO %s\r\n", token); } return msg; case WHOIS: if ((token = strtok (NULL, "\r\n")) != NULL) { - snprintf (msg, 512, "WHOIS %s\r\n", token); + snprintf (msg, sizeof (msg), "WHOIS %s\r\n", token); } return msg; @@ -255,8 +235,8 @@ timeptr = localtime (&t); if ((token = malloc (81))) { strftime (token, 80, "%I:%M:%S %p", timeptr); - snprintf (msg, 512, "PRIVMSG %s :%s %s, %s!\r\n", message->channel, gettext ("It is"), token, - message->user); + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s %s, %s!\r\n", message->channel, gettext ("It is"), + token, message->user); free (token); } return msg; @@ -266,7 +246,7 @@ if ((parameters = strtok (NULL, "\r\n"))) { if (*token == '*') message->channel = ++token; - snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", message->channel, token, + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s\r\n", message->channel, token, db_lookup (DATABASE_FILE, parameters)); } } @@ -276,7 +256,7 @@ if ((token = strtok (NULL, "\r\n")) != NULL) { if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) { if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) - snprintf (msg, 512, "MODE %s +o %s\r\n", message->channel, token); + snprintf (msg, sizeof (msg), "MODE %s +o %s\r\n", message->channel, token); } } return msg; @@ -285,7 +265,7 @@ if ((token = strtok (NULL, "\r\n")) != NULL) { if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) { if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) - snprintf (msg, 512, "MODE %s -o %s\r\n", message->channel, token); + snprintf (msg, sizeof (msg), "MODE %s -o %s\r\n", message->channel, token); } } return msg; @@ -294,7 +274,7 @@ if ((token = strtok (NULL, "\r\n")) != NULL) { if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) { if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) - snprintf (msg, 512, "KICK %s %s\r\n", message->channel, token); + snprintf (msg, sizeof (msg), "KICK %s %s\r\n", message->channel, token); } } return msg; @@ -303,7 +283,7 @@ if ((token = strtok (NULL, "\r\n")) != NULL) { if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) { if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) - snprintf (msg, 512, "MODE %s +b %s\r\n", message->channel, token); + snprintf (msg, sizeof (msg), "MODE %s +b %s\r\n", message->channel, token); } } return msg; @@ -312,7 +292,7 @@ if ((token = strtok (NULL, "\r\n")) != NULL) { if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) { if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) - snprintf (msg, 512, "MODE %s -b %s\r\n", message->channel, token); + snprintf (msg, sizeof (msg), "MODE %s -b %s\r\n", message->channel, token); } } return msg; @@ -321,14 +301,15 @@ if ((token = strtok (NULL, "\r\n")) != NULL) { if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) { if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) - snprintf (msg, 512, "MODE %s +b %s\r\nKICK %s %s\r\n", message->channel, token, + snprintf (msg, sizeof (msg), "MODE %s +b %s\r\nKICK %s %s\r\n", message->channel, token, message->channel, token); } } return msg; case HELLO: - snprintf (msg, 512, "PRIVMSG %s :%s, %s?\r\n", message->channel, gettext ("What's up"), message->user); + snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s?\r\n", message->channel, gettext ("What's up"), + message->user); return msg; } }