src/parse.c
changeset 15 f19c1f9b4cd3
parent 14 fe8adc56b109
child 16 33245bf7873a
equal deleted inserted replaced
14:fe8adc56b109 15:f19c1f9b4cd3
    20 #endif
    20 #endif
    21 
    21 
    22 #ifndef SOURCE_URL
    22 #ifndef SOURCE_URL
    23 #define SOURCE_URL "http://largo.homelinux.org/cgi-bin/gitweb.cgi?p=net/mcbot.git"
    23 #define SOURCE_URL "http://largo.homelinux.org/cgi-bin/gitweb.cgi?p=net/mcbot.git"
    24 #endif
    24 #endif
       
    25 
       
    26 #define    HELP  0
       
    27 #define    JOIN  1
       
    28 #define   LEAVE  2
       
    29 #define     ADD  3
       
    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
    25 
    46 
    26 const
    47 const
    27 char *COMMAND_LIST[] = {
    48 char *COMMAND_LIST[] = {
    28     "!help      Known Commands: join(1), leave(2), add(3), replace(4), delete(5), list(6), search(7), info(8)\r\n",
    49     "!help      Known Commands: join(1), leave(2), add(3), replace(4), delete(5), list(6), search(7), info(8)\r\n",
    29     "!join:     Joins a new channel\r\n",
    50     "!join:     Joins a new channel\r\n",
    41     "!vaccuum:  reorganizes the database\r\n",
    62     "!vaccuum:  reorganizes the database\r\n",
    42     "!logout:   Protected logout function\r\n",
    63     "!logout:   Protected logout function\r\n",
    43 };
    64 };
    44 
    65 
    45 const
    66 const
    46 char ITEMS = 14;
    67 char VISIBLE_ITEMS = 14;
    47 
    68 
    48 const
    69 const
    49 char *Bot_Commands[] = {
    70 char *Bot_Commands[] = {
    50     "!help", "!join", "!leave",
    71     "!help", "!join", "!leave",
    51     "!add", "!replace", "!delete",
    72     "!add", "!replace", "!delete",
   109     i = 0;
   130     i = 0;
   110     token = strtok (message->line, " ");
   131     token = strtok (message->line, " ");
   111     while (Bot_Commands[i]) {
   132     while (Bot_Commands[i]) {
   112         if (!strcmp (token, Bot_Commands[i])) {
   133         if (!strcmp (token, Bot_Commands[i])) {
   113             switch (i) {
   134             switch (i) {
   114             case 0:            /* !help */
   135             case HELP:
   115                 if ((token = strtok (NULL, "\r\n")))
   136                 if ((token = strtok (NULL, "\r\n")))
   116                     cmd = atoi (token);
   137                     cmd = atoi (token);
   117                 if ((cmd > 0) && (cmd < ITEMS))
   138                 if ((cmd > 0) && (cmd < VISIBLE_ITEMS))
   118                     snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[cmd]);
   139                     snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[cmd]);
   119                 else
   140                 else
   120                     snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[0]);
   141                     snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[0]);
   121                 return msg;
   142                 return msg;
   122 
   143 
   123             case 1:            /* !join */
   144             case JOIN:
   124                 if ((token = strtok (NULL, "\r\n")))
   145                 if ((token = strtok (NULL, "\r\n")))
   125                     snprintf (msg, 512, "JOIN %s\r\n", token);
   146                     snprintf (msg, 512, "JOIN %s\r\n", token);
   126                 return msg;
   147                 return msg;
   127 
   148 
   128             case 2:            /* !leave */
   149             case LEAVE:
   129                 if (*message->channel != '#')
   150                 if (*message->channel != '#')
   130                     return NULL;
   151                     return NULL;
   131                 snprintf (msg, 512, "PART %s :Leaving.\r\n", message->channel);
   152                 snprintf (msg, 512, "PART %s :Leaving.\r\n", message->channel);
   132                 return msg;
   153                 return msg;
   133 
   154 
   134             case 3:            /* !add */
   155             case ADD:
   135                 if ((token = strtok (NULL, " "))) {
   156                 if ((token = strtok (NULL, " "))) {
   136                     if ((parameters = strtok (NULL, "\r\n"))) {
   157                     if ((parameters = strtok (NULL, "\r\n"))) {
   137                         snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
   158                         snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
   138                                   message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 0));
   159                                   message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 0));
   139                     } else {
   160                     } else {
   141                                   gettext ("I need more parameters to add"), message->user);
   162                                   gettext ("I need more parameters to add"), message->user);
   142                     }
   163                     }
   143                 }
   164                 }
   144                 return msg;
   165                 return msg;
   145 
   166 
   146             case 4:            /* !replace */
   167             case REPLACE:
   147                 if ((token = strtok (NULL, " "))) {
   168                 if ((token = strtok (NULL, " "))) {
   148                     if ((parameters = strtok (NULL, "\r\n"))) {
   169                     if ((parameters = strtok (NULL, "\r\n"))) {
   149                         snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
   170                         snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
   150                                   message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 1));
   171                                   message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 1));
   151                     } else {
   172                     } else {
   153                                   gettext ("I need more parameters to replace"), message->user);
   174                                   gettext ("I need more parameters to replace"), message->user);
   154                     }
   175                     }
   155                 }
   176                 }
   156                 return msg;
   177                 return msg;
   157 
   178 
   158             case 5:            /* !delete */
   179             case DELETE:
   159                 if ((token = strtok (NULL, "\r\n"))) {
   180                 if ((token = strtok (NULL, "\r\n"))) {
   160                     snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
   181                     snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
   161                               message->channel, message->user, db_remove (DATABASE_FILE, token));
   182                               message->channel, message->user, db_remove (DATABASE_FILE, token));
   162                 } else {
   183                 } else {
   163                     snprintf (msg, 512, "PRIVMSG %s :%s, %s!\r\n", message->channel, gettext ("I need a key to delete"),
   184                     snprintf (msg, 512, "PRIVMSG %s :%s, %s!\r\n", message->channel, gettext ("I need a key to delete"),
   164                               message->user);
   185                               message->user);
   165                 }
   186                 }
   166                 return msg;
   187                 return msg;
   167 
   188 
   168             case 6:            /* !count */
   189             case LIST:
   169                 snprintf (msg, 512, "PRIVMSG %s :%s %s\r\n", message->channel, db_elements (DATABASE_FILE),
   190                 snprintf (msg, 512, "PRIVMSG %s :%s %s\r\n", message->channel, db_elements (DATABASE_FILE),
   170                           db_lookup (DATABASE_FILE, "mcbot.cgi"));
   191                           db_lookup (DATABASE_FILE, "mcbot.cgi"));
   171                 return msg;
   192                 return msg;
   172 
   193 
   173             case 7:            /* !search */
   194             case SEARCH:
   174                 if ((token = strtok (NULL, "\r\n"))) {
   195                 if ((token = strtok (NULL, "\r\n"))) {
   175                     snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", message->channel, message->user,
   196                     snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", message->channel, message->user,
   176                               db_lookup (DATABASE_FILE, token));
   197                               db_lookup (DATABASE_FILE, token));
   177                 } else {
   198                 } else {
   178                     snprintf (msg, 512,
   199                     snprintf (msg, 512,
   179                               "PRIVMSG %s :%s, %s!\r\n", message->channel, gettext ("I need a key to lookup"),
   200                               "PRIVMSG %s :%s, %s!\r\n", message->channel, gettext ("I need a key to lookup"),
   180                               message->user);
   201                               message->user);
   181                 }
   202                 }
   182                 return msg;
   203                 return msg;
   183 
   204 
   184             case 8:            /* !info */
   205             case INFO:
   185                 snprintf (msg, 512, "PRIVMSG %s :I am MCBot-%1.2f and my source code can be found on %s\r\n",
   206                 snprintf (msg, 512, "PRIVMSG %s :I am MCBot-%1.2f and my source code can be found on %s\r\n",
   186                           message->channel, VERSION, SOURCE_URL);
   207                           message->channel, BOT_VERSION, SOURCE_URL);
   187                 return msg;
   208                 return msg;
   188 
   209 
   189             case 9:            /* !ping */
   210             case PING:
   190                 if ((token = strtok (NULL, "\r\n")))
   211                 if ((token = strtok (NULL, "\r\n")))
   191                     snprintf (msg, 512, "PRIVMSG %s :PING 0815\r\n", token);
   212                     snprintf (msg, 512, "PRIVMSG %s :PING 0815\r\n", token);
   192                 return msg;
   213                 return msg;
   193 
   214 
   194             case 10:           /* !on */
   215             case ON:
   195                 snprintf (msg, 512, "PRIVMSG %s :%s %s.\r\n", message->user, gettext ("Autolearn enabled for channel"),
   216                 snprintf (msg, 512, "PRIVMSG %s :%s %s.\r\n", message->user, gettext ("Autolearn enabled for channel"),
   196                           message->channel);
   217                           message->channel);
   197                 return msg;
   218                 return msg;
   198 
   219 
   199             case 11:           /* !off */
   220             case OFF:
   200                 snprintf (msg, 512, "PRIVMSG %s :%s %s.\r\n", message->user, gettext ("Autolearn disabled for channel"),
   221                 snprintf (msg, 512, "PRIVMSG %s :%s %s.\r\n", message->user, gettext ("Autolearn disabled for channel"),
   201                           message->channel);
   222                           message->channel);
   202                 return msg;
   223                 return msg;
   203 
   224 
   204             case 12:           /* !debug */
   225             case DEBUG:
   205                 snprintf (msg, 512, "PRIVMSG %s :USER: %s EMAIL: %s CHANNEL: %s LINE: %s\r\n", message->channel,
   226                 snprintf (msg, 512, "PRIVMSG %s :USER: %s EMAIL: %s CHANNEL: %s LINE: %s\r\n", message->channel,
   206                           message->user, message->email, message->channel, message->line);
   227                           message->user, message->email, message->channel, message->line);
   207                 return msg;
   228                 return msg;
   208 
   229 
   209             case 13:           /* !vaccum */
   230             case VACCUUM:
   210                 snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, db_vaccuum (DATABASE_FILE));
   231                 snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, db_vaccuum (DATABASE_FILE));
   211                 return msg;
   232                 return msg;
   212 
   233 
   213             case 14:           /* !logout */
   234             case LOGOUT:
   214                 if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   235                 if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
   215                     if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) {
   236                     if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) {
   216                         snprintf (msg, 512, "PRIVMSG %s :%s!\r\nQUIT\r\n", message->channel,
   237                         snprintf (msg, 512, "PRIVMSG %s :%s!\r\nQUIT\r\n", message->channel,
   217                                   gettext ("Bye, have a nice day!"));
   238                                   gettext ("Bye, have a nice day!"));
   218                     }
   239                     }
   220                 /*
   241                 /*
   221                  * the returned message is either the default one or the generated one
   242                  * the returned message is either the default one or the generated one
   222                  */
   243                  */
   223                 return msg;
   244                 return msg;
   224 
   245 
   225             case 15:           /* !who */
   246             case WHO:
   226                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   247                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   227                     snprintf (msg, 512, "WHO %s\r\n", token);
   248                     snprintf (msg, 512, "WHO %s\r\n", token);
   228                 }
   249                 }
   229                 return msg;
   250                 return msg;
   230 
   251 
   231             case 16:           /* !whois */
   252             case WHOIS:
   232                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   253                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   233                     snprintf (msg, 512, "WHOIS %s\r\n", token);
   254                     snprintf (msg, 512, "WHOIS %s\r\n", token);
   234                 }
   255                 }
   235                 return msg;
   256                 return msg;
   236 
   257 
   237             case 17:           /* time */
   258             case TIME:
   238                 t = time (NULL);
   259                 t = time (NULL);
   239                 timeptr = localtime (&t);
   260                 timeptr = localtime (&t);
   240                 if ((token = malloc (81))) {
   261                 if ((token = malloc (81))) {
   241                     strftime (token, 80, "%I:%M:%S %p", timeptr);
   262                     strftime (token, 80, "%I:%M:%S %p", timeptr);
   242                     snprintf (msg, 512, "PRIVMSG %s :%s %s, %s!\r\n", message->channel, gettext ("It is"), token,
   263                     snprintf (msg, 512, "PRIVMSG %s :%s %s, %s!\r\n", message->channel, gettext ("It is"), token,
   243                               message->user);
   264                               message->user);
   244                     free (token);
   265                     free (token);
   245                 }
   266                 }
   246                 return msg;
   267                 return msg;
   247 
   268 
   248             case 18:           /* tell */
   269             case TELL:
   249                 if ((token = strtok (NULL, " "))) {
   270                 if ((token = strtok (NULL, " "))) {
   250                     if ((parameters = strtok (NULL, "\r\n"))) {
   271                     if ((parameters = strtok (NULL, "\r\n"))) {
   251                         if (*token == '*')
   272                         if (*token == '*')
   252                             message->channel = ++token;
   273                             message->channel = ++token;
   253                         snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", message->channel, token,
   274                         snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", message->channel, token,
   254                                   db_lookup (DATABASE_FILE, parameters));
   275                                   db_lookup (DATABASE_FILE, parameters));
   255                     }
   276                     }
   256                 }
   277                 }
   257                 return msg;
   278                 return msg;
   258 
   279 
   259             case 19:           /* op */
   280             case OP:
   260                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   281                 if ((token = strtok (NULL, "\r\n")) != NULL) {
   261                     if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   282                     if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
   262                         snprintf (msg, 512, "MODE %s +o %s\r\n", message->channel, token);
   283                         snprintf (msg, 512, "MODE %s +o %s\r\n", message->channel, token);
   263                 }
   284                 }
   264                 return msg;
   285                 return msg;