src/parse.c
changeset 0 586472add385
child 3 a82a978f23f7
equal deleted inserted replaced
-1:000000000000 0:586472add385
       
     1 /**
       
     2  *  $Id: parse.c 153 2008-04-27 07:26:15Z mbroeker $
       
     3  * $URL: http://localhost/svn/c/mcbot/trunk/src/parse.c $
       
     4  *
       
     5  */
       
     6 
       
     7 #include <stdio.h>
       
     8 #include <stdlib.h>
       
     9 #include <string.h>
       
    10 #include <time.h>
       
    11 
       
    12 #include <mcbot.h>
       
    13 #include <database.h>
       
    14 
       
    15 #include <locale.h>
       
    16 #include <libintl.h>
       
    17 
       
    18 #ifndef DATABASE_FILE
       
    19 #define DATABASE_FILE "/var/lib/nobody/data/mcbot.dat"
       
    20 #endif
       
    21 
       
    22 const
       
    23 char *COMMAND_LIST[] = {
       
    24     "!help      Known Commands: join(1), leave(2), add(3), replace(4), delete(5), list(6), search(7), info(8)\r\n",
       
    25     "!join:     Joins a new channel\r\n",
       
    26     "!leave:    Parts from the current channel\r\n",
       
    27     "!add:      adds an entry\r\n",
       
    28     "!replace:  replaces an entry\r\n",
       
    29     "!delete:   deletes an entry\r\n",
       
    30     "!list:     lists the number of stored values\r\n",
       
    31     "!search:   searches an entry up\r\n",
       
    32     "!info:     Prints the current Bot-Version\r\n",
       
    33     "!ping:     pings an host\r\n",
       
    34     "!on:       enables autolearning mode\r\n",
       
    35     "!off:      disables autolearning\r\n",
       
    36     "!debug:    prints some debug infos\r\n",
       
    37     "!vaccuum:  reorganizes the database\r\n",
       
    38     "!logout:   Protected logout function\r\n",
       
    39     NULL,
       
    40 };
       
    41 
       
    42 const
       
    43 char ITEMS = 14;
       
    44 
       
    45 const
       
    46 char *Bot_Commands[] = {
       
    47     "!help", "!join", "!leave",
       
    48     "!add", "!replace", "!delete",
       
    49     "!list", "!search", "!info",
       
    50     "!ping", "!on", "!off",
       
    51     "!debug", "!vaccuum", "!logout",
       
    52     "!who", "!whois", "!time", "!tell",
       
    53     "!op",
       
    54     NULL,
       
    55 };
       
    56 
       
    57 char *parse (MSG * message, short *active)
       
    58 {
       
    59     static char msg[513];
       
    60     int cmd = -1;
       
    61     int i;
       
    62     char *token;
       
    63     char *parameters;
       
    64 
       
    65     time_t t;
       
    66     struct tm *timeptr;
       
    67     static int counter = 0;
       
    68 
       
    69     /*
       
    70      * default message
       
    71      */
       
    72     snprintf (msg, 512, "PRIVMSG %s :%s.\r\n", message->channel, gettext ("Request cannot be performed"));
       
    73 
       
    74     /*
       
    75      * PRIVATE MESSAGES
       
    76      */
       
    77     if (!strcmp (message->channel, message->nick))
       
    78         message->channel = message->user;
       
    79 
       
    80     if (strstr (message->line, message->nick)) {
       
    81         if (*message->line != '!') {
       
    82             /*
       
    83              * DEAD - LOCK - CHECK
       
    84              */
       
    85             if (strcmp (message->user, message->nick)) {
       
    86                 snprintf (msg, 512, "PRIVMSG %s :%s, %s?\r\n", message->channel, gettext ("What's up"), message->user);
       
    87                 if (counter++ > 3)
       
    88                     return NULL;
       
    89                 return msg;
       
    90             }
       
    91             /*
       
    92              * DEAD - LOCK - CHECK ENDS
       
    93              */
       
    94         }
       
    95         return NULL;
       
    96     }
       
    97 
       
    98     counter = 0;
       
    99 
       
   100     /*
       
   101      * NO BOT Commands
       
   102      */
       
   103     if (*message->line != '!') {
       
   104         return NULL;
       
   105     }
       
   106 
       
   107     i = 0;
       
   108     token = strtok (message->line, " ");
       
   109     while (Bot_Commands[i]) {
       
   110         if (!strcmp (token, Bot_Commands[i])) {
       
   111             switch (i) {
       
   112             case 0:            /* !help */
       
   113                 if ((token = strtok (NULL, "\r\n")))
       
   114                     cmd = atoi (token);
       
   115                 if ((cmd > 0) && (cmd < ITEMS))
       
   116                     snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[cmd]);
       
   117                 else
       
   118                     snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[0]);
       
   119                 return msg;
       
   120 
       
   121             case 1:            /* !join */
       
   122                 if ((token = strtok (NULL, "\r\n")))
       
   123                     snprintf (msg, 512, "JOIN %s\r\n", token);
       
   124                 return msg;
       
   125 
       
   126             case 2:            /* !leave */
       
   127                 if (*message->channel != '#')
       
   128                     return NULL;
       
   129                 snprintf (msg, 512, "PART %s :Leaving.\r\n", message->channel);
       
   130                 return msg;
       
   131 
       
   132             case 3:            /* !add */
       
   133                 if ((token = strtok (NULL, " "))) {
       
   134                     if ((parameters = strtok (NULL, "\r\n"))) {
       
   135                         snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
       
   136                                   message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 0));
       
   137                     } else {
       
   138                         snprintf (msg, 512,
       
   139                                   "PRIVMSG %s :%s, %s!\r\n",
       
   140                                   message->channel, gettext ("I need more parameters to add"), message->user);
       
   141                     }
       
   142                 }
       
   143                 return msg;
       
   144 
       
   145             case 4:            /* !replace */
       
   146                 if ((token = strtok (NULL, " "))) {
       
   147                     if ((parameters = strtok (NULL, "\r\n"))) {
       
   148                         snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
       
   149                                   message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 1));
       
   150                     } else {
       
   151                         snprintf (msg, 512,
       
   152                                   "PRIVMSG %s :%s, %s!\r\n",
       
   153                                   message->channel, gettext ("I need more parameters to replace"), message->user);
       
   154                     }
       
   155                 }
       
   156                 return msg;
       
   157 
       
   158             case 5:            /* !delete */
       
   159                 if ((token = strtok (NULL, "\r\n"))) {
       
   160                     snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
       
   161                               message->channel, message->user, db_remove (DATABASE_FILE, token));
       
   162                 } else {
       
   163                     snprintf (msg, 512,
       
   164                               "PRIVMSG %s :%s, %s!\r\n",
       
   165                               message->channel, gettext ("I need a key to delete"), message->user);
       
   166                 }
       
   167                 return msg;
       
   168 
       
   169             case 6:            /* !count */
       
   170                 snprintf (msg, 512, "PRIVMSG %s :%s %s\r\n", message->channel,
       
   171                           db_elements (DATABASE_FILE), db_lookup(DATABASE_FILE, "mcbot.cgi"));
       
   172                 return msg;
       
   173 
       
   174             case 7:            /* !search */
       
   175                 if ((token = strtok (NULL, "\r\n"))) {
       
   176                     snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
       
   177                               message->channel, message->user, db_lookup (DATABASE_FILE, token));
       
   178                 } else {
       
   179                     snprintf (msg, 512,
       
   180                               "PRIVMSG %s :%s, %s!\r\n",
       
   181                               message->channel, gettext ("I need a key to lookup"), message->user);
       
   182                 }
       
   183                 return msg;
       
   184 
       
   185             case 8:            /* !info */
       
   186                 snprintf (msg, 512,
       
   187                           "PRIVMSG %s :I am MCBot-%1.2f and my source code can be found on %s\r\n",
       
   188                           message->channel, VERSION, "http://largo.homelinux.org/svn/c/mcbot/trunk");
       
   189                 return msg;
       
   190 
       
   191             case 9:            /* !ping */
       
   192                 if ((token = strtok (NULL, "\r\n")))
       
   193                     snprintf (msg, 512, "PRIVMSG %s :PING 0815\r\n", token);
       
   194                 return msg;
       
   195 
       
   196             case 10:           /* !on */
       
   197                 snprintf (msg, 512,
       
   198                           "PRIVMSG %s :%s %s.\r\n",
       
   199                           message->user, gettext ("Autolearn enabled for channel"), message->channel);
       
   200                 return msg;
       
   201 
       
   202             case 11:           /* !off */
       
   203                 snprintf (msg, 512,
       
   204                           "PRIVMSG %s :%s %s.\r\n",
       
   205                           message->user, gettext ("Autolearn disabled for channel"), message->channel);
       
   206                 return msg;
       
   207 
       
   208             case 12:           /* !debug */
       
   209                 snprintf (msg, 512,
       
   210                           "PRIVMSG %s :USER: %s EMAIL: %s CHANNEL: %s LINE: %s\r\n",
       
   211                           message->channel, message->user, message->email, message->channel, message->line);
       
   212                 return msg;
       
   213 
       
   214             case 13:           /* !vaccum */
       
   215                 snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, db_vaccuum (DATABASE_FILE));
       
   216                 return msg;
       
   217 
       
   218             case 14:           /* !logout */
       
   219                 if (strstr (message->user, db_lookup(DATABASE_FILE, "mcbot.user"))) {
       
   220                     if (strstr (message->email, db_lookup(DATABASE_FILE, "mcbot.email"))) {
       
   221                         snprintf (msg, 512,
       
   222                                   "PRIVMSG %s :%s!\r\nQUIT\r\n", message->channel, gettext ("Bye, have a nice day!"));
       
   223                         *active = 0;
       
   224                     }
       
   225                 }
       
   226                 /*
       
   227                  * the returned message is either the default one or the generated one
       
   228                  */
       
   229                 return msg;
       
   230 
       
   231             case 15:           /* !who */
       
   232                 if ((token = strtok (NULL, "\r\n")) != NULL) {
       
   233                     snprintf (msg, 512, "WHO %s\r\n", token);
       
   234                 }
       
   235                 return msg;
       
   236                 break;
       
   237 
       
   238             case 16:           /* !whois */
       
   239                 if ((token = strtok (NULL, "\r\n")) != NULL) {
       
   240                     snprintf (msg, 512, "WHOIS %s\r\n", token);
       
   241                 }
       
   242                 return msg;
       
   243                 break;
       
   244             case 17:           /* time */
       
   245                 t = time (NULL);
       
   246                 timeptr = localtime (&t);
       
   247                 if ((token = malloc (81))) {
       
   248                     strftime (token, 80, "%I:%M:%S %p", timeptr);
       
   249                     snprintf (msg, 512, "PRIVMSG %s :%s %s, %s!\r\n",
       
   250                               message->channel, gettext ("It is"), token, message->user);
       
   251                     free (token);
       
   252                 }
       
   253                 return msg;
       
   254                 break;
       
   255             case 18:           /* tell */
       
   256                 if ((token = strtok (NULL, " "))) {
       
   257                     if ((parameters = strtok (NULL, "\r\n"))) {
       
   258                         snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
       
   259                                   (*token ==
       
   260                                    '*') ? ++token : message->channel, token, db_lookup (DATABASE_FILE, parameters));
       
   261                     }
       
   262                 }
       
   263                 return msg;
       
   264                 break;
       
   265             case 19:           /* op */
       
   266                 if ((token = strtok (NULL, "\r\n")) != NULL) {
       
   267                     if (strstr (message->email, db_lookup(DATABASE_FILE, "mcbot.email")))
       
   268                         snprintf (msg, 512, "MODE %s +o %s\r\n", message->channel, token);
       
   269                 }
       
   270                 return msg;
       
   271                 break;
       
   272             }
       
   273         }
       
   274         i++;
       
   275     }
       
   276 
       
   277     return NULL;
       
   278 }