src/parse.c
changeset 33 56571d34d754
parent 31 bbcb8a3366b4
child 38 c2f62d1b8d76
equal deleted inserted replaced
32:89fb9c6611ca 33:56571d34d754
     9 #include <string.h>
     9 #include <string.h>
    10 #include <time.h>
    10 #include <time.h>
    11 
    11 
    12 #include <mcbot.h>
    12 #include <mcbot.h>
    13 #include <database.h>
    13 #include <database.h>
       
    14 #include "common.h"
    14 
    15 
    15 #include <locale.h>
    16 #include <locale.h>
    16 #include <libintl.h>
    17 #include <libintl.h>
    17 
    18 
    18 #ifndef DATABASE_FILE
    19 #ifndef DATABASE_FILE
    21 
    22 
    22 #ifndef SOURCE_URL
    23 #ifndef SOURCE_URL
    23 #define SOURCE_URL "http://largo.homelinux.org/cgi-bin/gitweb.cgi?p=net/mcbot.git"
    24 #define SOURCE_URL "http://largo.homelinux.org/cgi-bin/gitweb.cgi?p=net/mcbot.git"
    24 #endif
    25 #endif
    25 
    26 
    26 #define    HELP  0
    27 enum {
    27 #define    JOIN  1
    28     HELP, JOIN, LEAVE, ADD, REPLACE, DELETE, LIST, SEARCH, INFO, PING, ON, OFF, DEBUG,
    28 #define   LEAVE  2
    29     VACCUUM, LOGOUT, WHO, WHOIS, TIME, TELL, OP, DEOP, KICK, BAN, UNBAN, KICKBAN, HELLO
    29 #define     ADD  3
    30 };
    30 #define REPLACE  4
       
    31 #define  DELETE  5
       
    32 #define    LIST  6
       
    33 #define  SEARCH  7
       
    34 #define    INFO  8
       
    35 #define    PING  9
       
    36 #define      ON 10
       
    37 #define     OFF 11
       
    38 #define   DEBUG 12
       
    39 #define VACCUUM 13
       
    40 #define  LOGOUT 14
       
    41 #define     WHO 15
       
    42 #define   WHOIS 16
       
    43 #define    TIME 17
       
    44 #define    TELL 18
       
    45 #define      OP 19
       
    46 #define    DEOP 20
       
    47 #define    KICK 21
       
    48 #define     BAN 22
       
    49 #define   UNBAN 23
       
    50 #define KICKBAN 24
       
    51 #define   HELLO 25
       
    52 
    31 
    53 const
    32 const
    54 char *COMMAND_LIST[] = {
    33 char *COMMAND_LIST[] = {
    55     "help      Known Commands: join(1), leave(2), add(3), replace(4), delete(5), list(6), search(7), info(8)\r\n",
    34     "help      Known Commands: join(1), leave(2), add(3), replace(4), delete(5), list(6), search(7), info(8)\r\n",
    56     "join:     Joins a new channel\r\n",
    35     "join:     Joins a new channel\r\n",
    87     NULL
    66     NULL
    88 };
    67 };
    89 
    68 
    90 char *parse (MSG * message)
    69 char *parse (MSG * message)
    91 {
    70 {
    92     static char msg[513];
    71     static char msg[DEFAULT_BUF_SIZE];
    93     int cmd = -1;
    72     int cmd = -1;
    94     int i;
    73     int i;
    95     char *token;
    74     char *token;
    96     char *parameters;
    75     char *parameters;
    97 
    76 
    99     struct tm *timeptr;
    78     struct tm *timeptr;
   100 
    79 
   101     /*
    80     /*
   102      * default message
    81      * default message
   103      */
    82      */
   104     snprintf (msg, 512, "PRIVMSG %s :%s.\r\n", message->channel, gettext ("Request cannot be performed"));
    83     snprintf (msg, sizeof (msg), "PRIVMSG %s :%s.\r\n", message->channel, gettext ("Request cannot be performed"));
   105 
    84 
   106     /*
    85     /*
   107      * PRIVATE MESSAGES
    86      * PRIVATE MESSAGES
   108      */
    87      */
   109     if (!strcmp (message->channel, message->nick))
    88     if (!strcmp (message->channel, message->nick))
   123             switch (i) {
   102             switch (i) {
   124             case HELP:
   103             case HELP:
   125                 if ((token = strtok (NULL, "\r\n")))
   104                 if ((token = strtok (NULL, "\r\n")))
   126                     cmd = atoi (token);
   105                     cmd = atoi (token);
   127                 if ((cmd > 0) && (cmd < VISIBLE_ITEMS))
   106                 if ((cmd > 0) && (cmd < VISIBLE_ITEMS))
   128                     snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[cmd]);
   107                     snprintf (msg, sizeof (msg), "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[cmd]);
   129                 else
   108                 else
   130                     snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[0]);
   109                     snprintf (msg, sizeof (msg), "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[0]);
   131                 return msg;
   110                 return msg;
   132 
   111 
   133             case JOIN:
   112             case JOIN:
   134                 if ((token = strtok (NULL, "\r\n")))
   113                 if ((token = strtok (NULL, "\r\n")))
   135                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   114                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   136                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   115                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   137                             snprintf (msg, 512, "JOIN %s\r\n", token);
   116                             snprintf (msg, sizeof (msg), "JOIN %s\r\n", token);
   138                     }
   117                     }
   139                 return msg;
   118                 return msg;
   140 
   119 
   141             case LEAVE:
   120             case LEAVE:
   142                 if (*message->channel != '#')
   121                 if (*message->channel != '#')
   143                     return NULL;
   122                     return NULL;
   144                 if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   123                 if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   145                     if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   124                     if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   146                         snprintf (msg, 512, "PART %s :Leaving.\r\n", message->channel);
   125                         snprintf (msg, sizeof (msg), "PART %s :Leaving.\r\n", message->channel);
   147                 }
   126                 }
   148                 return msg;
   127                 return msg;
   149 
   128 
   150             case ADD:
   129             case ADD:
   151                 if ((token = strtok (NULL, " "))) {
   130                 if ((token = strtok (NULL, " "))) {
   152                     if ((parameters = strtok (NULL, "\r\n"))) {
   131                     if ((parameters = strtok (NULL, "\r\n"))) {
   153                         snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
   132                         snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s\r\n",
   154                                   message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 0));
   133                                   message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 0));
   155                     } else {
   134                     } else {
   156                         snprintf (msg, 512, "PRIVMSG %s :%s, %s!\r\n", message->channel,
   135                         snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s!\r\n", message->channel,
   157                                   gettext ("I need more parameters to add"), message->user);
   136                                   gettext ("I need more parameters to add"), message->user);
   158                     }
   137                     }
   159                 }
   138                 }
   160                 return msg;
   139                 return msg;
   161 
   140 
   162             case REPLACE:
   141             case REPLACE:
   163                 if ((token = strtok (NULL, " "))) {
   142                 if ((token = strtok (NULL, " "))) {
   164                     if ((parameters = strtok (NULL, "\r\n"))) {
   143                     if ((parameters = strtok (NULL, "\r\n"))) {
   165                         snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
   144                         snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s\r\n",
   166                                   message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 1));
   145                                   message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 1));
   167                     } else {
   146                     } else {
   168                         snprintf (msg, 512, "PRIVMSG %s :%s, %s!\r\n", message->channel,
   147                         snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s!\r\n", message->channel,
   169                                   gettext ("I need more parameters to replace"), message->user);
   148                                   gettext ("I need more parameters to replace"), message->user);
   170                     }
   149                     }
   171                 }
   150                 }
   172                 return msg;
   151                 return msg;
   173 
   152 
   174             case DELETE:
   153             case DELETE:
   175                 if ((token = strtok (NULL, "\r\n"))) {
   154                 if ((token = strtok (NULL, "\r\n"))) {
   176                     snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
   155                     snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s\r\n",
   177                               message->channel, message->user, db_remove (DATABASE_FILE, token));
   156                               message->channel, message->user, db_remove (DATABASE_FILE, token));
   178                 } else {
   157                 } else {
   179                     snprintf (msg, 512, "PRIVMSG %s :%s, %s!\r\n", message->channel, gettext ("I need a key to delete"),
   158                     snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s!\r\n", message->channel,
   180                               message->user);
   159                               gettext ("I need a key to delete"), message->user);
   181                 }
   160                 }
   182                 return msg;
   161                 return msg;
   183 
   162 
   184             case LIST:
   163             case LIST:
   185                 snprintf (msg, 512, "PRIVMSG %s :%s %s\r\n", message->channel, db_elements (DATABASE_FILE),
   164                 snprintf (msg, sizeof (msg), "PRIVMSG %s :%s %s\r\n", message->channel, db_elements (DATABASE_FILE),
   186                           db_lookup (DATABASE_FILE, "mcbot.cgi"));
   165                           db_lookup (DATABASE_FILE, "mcbot.cgi"));
   187                 return msg;
   166                 return msg;
   188 
   167 
   189             case SEARCH:
   168             case SEARCH:
   190                 if ((token = strtok (NULL, "\r\n"))) {
   169                 if ((token = strtok (NULL, "\r\n"))) {
   191                     snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", message->channel, message->user,
   170                     snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s\r\n", message->channel, message->user,
   192                               db_lookup (DATABASE_FILE, token));
   171                               db_lookup (DATABASE_FILE, token));
   193                 } else {
   172                 } else {
   194                     snprintf (msg, 512,
   173                     snprintf (msg, sizeof (msg),
   195                               "PRIVMSG %s :%s, %s!\r\n", message->channel, gettext ("I need a key to lookup"),
   174                               "PRIVMSG %s :%s, %s!\r\n", message->channel, gettext ("I need a key to lookup"),
   196                               message->user);
   175                               message->user);
   197                 }
   176                 }
   198                 return msg;
   177                 return msg;
   199 
   178 
   200             case INFO:
   179             case INFO:
   201                 snprintf (msg, 512, "PRIVMSG %s :I am MCBot-%1.2f Build %s and my source code can be found at %s\r\n",
   180                 snprintf (msg, sizeof (msg),
       
   181                           "PRIVMSG %s :I am MCBot-%1.2f Build %s and my source code can be found at %s\r\n",
   202                           message->channel, BOT_VERSION, BOT_BUILD, SOURCE_URL);
   182                           message->channel, BOT_VERSION, BOT_BUILD, SOURCE_URL);
   203                 return msg;
   183                 return msg;
   204 
   184 
   205             case PING:
   185             case PING:
   206                 if ((token = strtok (NULL, "\r\n")))
   186                 if ((token = strtok (NULL, "\r\n")))
   207                     snprintf (msg, 512, "PRIVMSG %s :PING 0815\r\n", token);
   187                     snprintf (msg, sizeof (msg), "PRIVMSG %s :PING 0815\r\n", token);
   208                 return msg;
   188                 return msg;
   209 
   189 
   210             case ON:
   190             case ON:
   211                 snprintf (msg, 512, "PRIVMSG %s :%s %s.\r\n", message->user, gettext ("Autolearn enabled for channel"),
   191                 snprintf (msg, sizeof (msg), "PRIVMSG %s :%s %s.\r\n", message->user,
   212                           message->channel);
   192                           gettext ("Autolearn enabled for channel"), message->channel);
   213                 return msg;
   193                 return msg;
   214 
   194 
   215             case OFF:
   195             case OFF:
   216                 snprintf (msg, 512, "PRIVMSG %s :%s %s.\r\n", message->user, gettext ("Autolearn disabled for channel"),
   196                 snprintf (msg, sizeof (msg), "PRIVMSG %s :%s %s.\r\n", message->user,
   217                           message->channel);
   197                           gettext ("Autolearn disabled for channel"), message->channel);
   218                 return msg;
   198                 return msg;
   219 
   199 
   220             case DEBUG:
   200             case DEBUG:
   221                 snprintf (msg, 512, "PRIVMSG %s :USER: %s EMAIL: %s CHANNEL: %s LINE: %s\r\n", message->channel,
   201                 snprintf (msg, sizeof (msg), "PRIVMSG %s :USER: %s EMAIL: %s CHANNEL: %s LINE: %s\r\n",
   222                           message->user, message->email, message->channel, message->line);
   202                           message->channel, message->user, message->email, message->channel, message->line);
   223                 return msg;
   203                 return msg;
   224 
   204 
   225             case VACCUUM:
   205             case VACCUUM:
   226                 snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, db_vaccuum (DATABASE_FILE));
   206                 snprintf (msg, sizeof (msg), "PRIVMSG %s :%s\r\n", message->channel, db_vaccuum (DATABASE_FILE));
   227                 return msg;
   207                 return msg;
   228 
   208 
   229             case LOGOUT:
   209             case LOGOUT:
   230                 if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   210                 if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   231                     if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) {
   211                     if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) {
   232                         snprintf (msg, 512, "PRIVMSG %s :%s!\r\nQUIT\r\n", message->channel,
   212                         snprintf (msg, sizeof (msg), "PRIVMSG %s :%s!\r\nQUIT\r\n", message->channel,
   233                                   gettext ("Bye, have a nice day!"));
   213                                   gettext ("Bye, have a nice day!"));
   234                     }
   214                     }
   235                 }
   215                 }
   236                 /*
   216                 /*
   237                  * the returned message is either the default one or the generated one
   217                  * the returned message is either the default one or the generated one
   238                  */
   218                  */
   239                 return msg;
   219                 return msg;
   240 
   220 
   241             case WHO:
   221             case WHO:
   242                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   222                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   243                     snprintf (msg, 512, "WHO %s\r\n", token);
   223                     snprintf (msg, sizeof (msg), "WHO %s\r\n", token);
   244                 }
   224                 }
   245                 return msg;
   225                 return msg;
   246 
   226 
   247             case WHOIS:
   227             case WHOIS:
   248                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   228                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   249                     snprintf (msg, 512, "WHOIS %s\r\n", token);
   229                     snprintf (msg, sizeof (msg), "WHOIS %s\r\n", token);
   250                 }
   230                 }
   251                 return msg;
   231                 return msg;
   252 
   232 
   253             case TIME:
   233             case TIME:
   254                 t = time (NULL);
   234                 t = time (NULL);
   255                 timeptr = localtime (&t);
   235                 timeptr = localtime (&t);
   256                 if ((token = malloc (81))) {
   236                 if ((token = malloc (81))) {
   257                     strftime (token, 80, "%I:%M:%S %p", timeptr);
   237                     strftime (token, 80, "%I:%M:%S %p", timeptr);
   258                     snprintf (msg, 512, "PRIVMSG %s :%s %s, %s!\r\n", message->channel, gettext ("It is"), token,
   238                     snprintf (msg, sizeof (msg), "PRIVMSG %s :%s %s, %s!\r\n", message->channel, gettext ("It is"),
   259                               message->user);
   239                               token, message->user);
   260                     free (token);
   240                     free (token);
   261                 }
   241                 }
   262                 return msg;
   242                 return msg;
   263 
   243 
   264             case TELL:
   244             case TELL:
   265                 if ((token = strtok (NULL, " "))) {
   245                 if ((token = strtok (NULL, " "))) {
   266                     if ((parameters = strtok (NULL, "\r\n"))) {
   246                     if ((parameters = strtok (NULL, "\r\n"))) {
   267                         if (*token == '*')
   247                         if (*token == '*')
   268                             message->channel = ++token;
   248                             message->channel = ++token;
   269                         snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", message->channel, token,
   249                         snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s\r\n", message->channel, token,
   270                                   db_lookup (DATABASE_FILE, parameters));
   250                                   db_lookup (DATABASE_FILE, parameters));
   271                     }
   251                     }
   272                 }
   252                 }
   273                 return msg;
   253                 return msg;
   274 
   254 
   275             case OP:
   255             case OP:
   276                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   256                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   277                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   257                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   278                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   258                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   279                             snprintf (msg, 512, "MODE %s +o %s\r\n", message->channel, token);
   259                             snprintf (msg, sizeof (msg), "MODE %s +o %s\r\n", message->channel, token);
   280                     }
   260                     }
   281                 }
   261                 }
   282                 return msg;
   262                 return msg;
   283 
   263 
   284             case DEOP:
   264             case DEOP:
   285                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   265                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   286                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   266                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   287                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   267                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   288                             snprintf (msg, 512, "MODE %s -o %s\r\n", message->channel, token);
   268                             snprintf (msg, sizeof (msg), "MODE %s -o %s\r\n", message->channel, token);
   289                     }
   269                     }
   290                 }
   270                 }
   291                 return msg;
   271                 return msg;
   292 
   272 
   293             case KICK:
   273             case KICK:
   294                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   274                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   295                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   275                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   296                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   276                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   297                             snprintf (msg, 512, "KICK %s %s\r\n", message->channel, token);
   277                             snprintf (msg, sizeof (msg), "KICK %s %s\r\n", message->channel, token);
   298                     }
   278                     }
   299                 }
   279                 }
   300                 return msg;
   280                 return msg;
   301 
   281 
   302             case BAN:
   282             case BAN:
   303                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   283                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   304                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   284                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   305                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   285                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   306                             snprintf (msg, 512, "MODE %s +b %s\r\n", message->channel, token);
   286                             snprintf (msg, sizeof (msg), "MODE %s +b %s\r\n", message->channel, token);
   307                     }
   287                     }
   308                 }
   288                 }
   309                 return msg;
   289                 return msg;
   310 
   290 
   311             case UNBAN:
   291             case UNBAN:
   312                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   292                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   313                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   293                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   314                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   294                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   315                             snprintf (msg, 512, "MODE %s -b %s\r\n", message->channel, token);
   295                             snprintf (msg, sizeof (msg), "MODE %s -b %s\r\n", message->channel, token);
   316                     }
   296                     }
   317                 }
   297                 }
   318                 return msg;
   298                 return msg;
   319 
   299 
   320             case KICKBAN:
   300             case KICKBAN:
   321                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   301                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   322                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   302                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   323                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   303                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   324                             snprintf (msg, 512, "MODE %s +b %s\r\nKICK %s %s\r\n", message->channel, token,
   304                             snprintf (msg, sizeof (msg), "MODE %s +b %s\r\nKICK %s %s\r\n", message->channel, token,
   325                                       message->channel, token);
   305                                       message->channel, token);
   326                     }
   306                     }
   327                 }
   307                 }
   328                 return msg;
   308                 return msg;
   329 
   309 
   330             case HELLO:
   310             case HELLO:
   331                 snprintf (msg, 512, "PRIVMSG %s :%s, %s?\r\n", message->channel, gettext ("What's up"), message->user);
   311                 snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s?\r\n", message->channel, gettext ("What's up"),
       
   312                           message->user);
   332                 return msg;
   313                 return msg;
   333             }
   314             }
   334         }
   315         }
   335         i++;
   316         i++;
   336     }
   317     }