src/irc.c
changeset 15 f19c1f9b4cd3
parent 13 d3554afaa768
child 18 4435146391ae
equal deleted inserted replaced
14:fe8adc56b109 15:f19c1f9b4cd3
    17 
    17 
    18 #include <pwd.h>
    18 #include <pwd.h>
    19 
    19 
    20 #include <compat.h>
    20 #include <compat.h>
    21 #include <irc.h>
    21 #include <irc.h>
       
    22 
       
    23 #define  NOTICE  0
       
    24 #define    MODE  1
       
    25 #define    JOIN  2
       
    26 #define    PART  3
       
    27 #define   TOPIC  4
       
    28 #define    PING  5
       
    29 #define  ENOMEM  6
       
    30 #define   ERROR  7
       
    31 #define VERSION  8
       
    32 #define PRIVMSG  9
       
    33 #define    QUIT 10
       
    34 #define    NICK 11
    22 
    35 
    23 #define VERSION_STRING "MCBOT on GNU/LINUX"
    36 #define VERSION_STRING "MCBOT on GNU/LINUX"
    24 
    37 
    25 const char *IRC_Commands[] = {
    38 const char *IRC_Commands[] = {
    26     "NOTICE", "MODE", "JOIN", "PART",
    39     "NOTICE", "MODE", "JOIN", "PART",
   161     message->user = message->email = NULL;
   174     message->user = message->email = NULL;
   162     message->command = NULL;
   175     message->command = NULL;
   163     message->channel = message->line = NULL;
   176     message->channel = message->line = NULL;
   164 
   177 
   165     token = strtok (theLine, " ");
   178     token = strtok (theLine, " ");
   166     if (*token != ':') {
   179     if (*token != ':') {        /* SERVER MESSAGES */
   167         /*
       
   168          * SERVER MESSAGES
       
   169          */
       
   170         if ((ptr = strtok (NULL, "\r\n")) != NULL)
   180         if ((ptr = strtok (NULL, "\r\n")) != NULL)
   171             ++ptr;
   181             ++ptr;
   172         message->line = ptr;
   182         message->line = ptr;
   173         return token;
   183         return token;
   174     }
   184     }
   175 
   185 
   176     message->user = ++token;
   186     message->user = ++token;
   177     message->command = strtok (NULL, " ");
   187     message->command = strtok (NULL, " ");
   178 
       
   179     message->line = strtok (NULL, "\r\n");
   188     message->line = strtok (NULL, "\r\n");
   180 
   189 
   181     return message->command;
   190     return message->command;
   182 }
   191 }
   183 
   192 
   195     i = 0;
   204     i = 0;
   196     if ((command = irc_getmessage (line, message)) != NULL) {
   205     if ((command = irc_getmessage (line, message)) != NULL) {
   197         while (IRC_Commands[i] != NULL) {
   206         while (IRC_Commands[i] != NULL) {
   198             if (strcmp (IRC_Commands[i], command) == 0) {
   207             if (strcmp (IRC_Commands[i], command) == 0) {
   199                 switch (i) {
   208                 switch (i) {
   200                 case 0:        /* NOTICE */
   209                 case NOTICE:
   201                     if (message->line == NULL) {
   210                     if (message->line == NULL) {
   202                         message->channel = "*";
   211                         message->channel = "*";
   203                         message->line = "*";
   212                         message->line = "*";
   204                     } else {
   213                     } else {
   205                         message->channel = strtok (message->line, " ");
   214                         message->channel = strtok (message->line, " ");
   206                         if ((message->line = strtok (NULL, "\r\n")) != NULL)
   215                         if ((message->line = strtok (NULL, "\r\n")) != NULL)
   207                             ++message->line;
   216                             ++message->line;
   208                     }
   217                     }
   209                     return command;
   218                     return command;
   210                 case 1:        /* MODE */
   219                 case MODE:
   211                     message->channel = strtok (message->line, " ");
   220                     message->channel = strtok (message->line, " ");
   212                     message->line = strtok (NULL, "\r\n");
   221                     message->line = strtok (NULL, "\r\n");
   213                     return command;
   222                     return command;
   214                 case 2:        /* JOIN */
   223                 case JOIN:
   215                 case 3:        /* PART */
   224                 case PART:
   216                     if ((message->channel = strchr (message->line, ':')))
   225                     if ((message->channel = strchr (message->line, ':')))
   217                         ++message->channel;
   226                         ++message->channel;
   218                     message->line = message->user;
   227                     message->line = message->user;
   219                     return command;
   228                     return command;
   220                 case 4:        /* TOPIC */
   229                 case TOPIC:
   221                     message->channel = strtok (message->line, " ");
   230                     message->channel = strtok (message->line, " ");
   222                     if ((message->line = strtok (NULL, "\r\n")))
   231                     if ((message->line = strtok (NULL, "\r\n")))
   223                         ++message->line;
   232                         ++message->line;
   224                     return command;
   233                     return command;
   225                 case 5:        /* PING */
   234                 case PING:
   226 #ifdef DEBUG
   235 #ifdef DEBUG
   227                     /*
   236                     /*
   228                      * DONT NERVE WITH PING PONG MESSAGES
   237                      * DONT NERVE WITH PING PONG MESSAGES
   229                      */
   238                      */
   230                     printf ("%10s %s localhost\n", "PING", message->line);
   239                     printf ("%10s %s localhost\n", "PING", message->line);
   234 #ifdef DEBUG
   243 #ifdef DEBUG
   235                     return "PONG";
   244                     return "PONG";
   236 #else
   245 #else
   237                     return NULL;
   246                     return NULL;
   238 #endif
   247 #endif
   239                 case 6:        /* ENOMEM */
   248                 case ENOMEM:
   240                 case 7:        /* ERROR */
   249                 case ERROR:
   241                     return command;
   250                     return command;
   242                 case 8:        /* VERSION */
   251                 case VERSION:
   243                     if ((ptr = strchr (message->user, ' ')))
   252                     if ((ptr = strchr (message->user, ' ')))
   244                         *ptr = '\0';
   253                         *ptr = '\0';
   245                     return command;
   254                     return command;
   246                 case 9:        /* PRIVMSG */
   255                 case PRIVMSG:
   247                     if ((message->email = strchr (message->user, '=')))
   256                     if ((message->email = strchr (message->user, '=')))
   248                         ++message->email;
   257                         ++message->email;
   249                     if ((ptr = strchr (message->user, '!')))
   258                     if ((ptr = strchr (message->user, '!')))
   250                         *ptr = '\0';
   259                         *ptr = '\0';
   251 
   260 
   252                     message->channel = strtok (message->line, " ");
   261                     message->channel = strtok (message->line, " ");
   253                     message->current_channel = message->channel;
   262                     strncpy (message->current_channel, message->channel, 40);
   254                     message->line = strtok (NULL, "\r\n");
   263                     message->line = strtok (NULL, "\r\n");
   255                     message->line++;
   264                     message->line++;
   256                     printf ("%10s %s %s :%s\n", "READ", message->command, message->channel, message->line);
   265                     printf ("%10s %s %s :%s\n", "READ", message->command, message->channel, message->line);
   257                     return NULL;
   266                     return NULL;
   258                 case 10:       /* QUIT */
   267                 case QUIT:
   259                     message->channel = message->user;
   268                     message->channel = message->user;
   260                     return command;
   269                     return command;
   261                 case 11:       /* NICK */
   270                 case NICK:
   262                     message->channel = message->user;
   271                     message->channel = message->user;
   263                     return command;
   272                     return command;
   264                 }
   273                 }
   265             }
   274             }
   266             i++;
   275             i++;
   298                 return command;
   307                 return command;
   299             case 320:
   308             case 320:
   300             case 328:          /* INFORMATION */
   309             case 328:          /* INFORMATION */
   301             case 332:          /* TOPIC OF CHANNEL */
   310             case 332:          /* TOPIC OF CHANNEL */
   302             case 333:          /* NAMES IN CHANNEL */
   311             case 333:          /* NAMES IN CHANNEL */
   303                 message->channel = strtok (message->line, " ");
       
   304                 message->line = strtok (NULL, "\r\n");
       
   305                 return command;
       
   306             case 351:          /* SVERSION */
   312             case 351:          /* SVERSION */
   307                 message->channel = strtok (message->line, " ");
       
   308                 message->line = strtok (NULL, "\r\n");
       
   309                 return command;
       
   310             case 352:          /* WHO LIST */
   313             case 352:          /* WHO LIST */
   311                 message->channel = strtok (message->line, " ");
       
   312                 message->line = strtok (NULL, "\r\n");
       
   313                 /*
       
   314                  * MORE THAN 3 LINES AND YOU WILL be KICKED
       
   315                  */
       
   316                 return command;
       
   317             case 353:
   314             case 353:
   318             case 365:
   315             case 365:
   319             case 366:          /* END OF NAMES */
   316             case 366:          /* END OF NAMES */
   320                 message->channel = strtok (message->line, " ");
   317                 message->channel = strtok (message->line, " ");
   321                 message->line = strtok (NULL, "\r\n");
   318                 message->line = strtok (NULL, "\r\n");
   326                 return command;
   323                 return command;
   327             case 401:          /* NO SUCH NICK/CHANNEL */
   324             case 401:          /* NO SUCH NICK/CHANNEL */
   328             case 403:          /* THAT CHANNEL DOESN'T EXIST */
   325             case 403:          /* THAT CHANNEL DOESN'T EXIST */
   329             case 412:          /* NO TEXT TO SEND */
   326             case 412:          /* NO TEXT TO SEND */
   330             case 441:          /* THEY AREN'T ON THIS CHANNEL */
   327             case 441:          /* THEY AREN'T ON THIS CHANNEL */
       
   328                 message->channel = strtok (message->line, " ");
       
   329                 message->line = strtok (NULL, "\r\n");
       
   330                 fprintf (message->stream, "PRIVMSG %s :%s\r\n", message->current_channel, message->line);
   331                 return command;
   331                 return command;
   332             case 474:
   332             case 474:
   333             case 475:
   333             case 475:
   334             case 476:
   334             case 476:
   335             case 477:
   335             case 477:
   336             case 482:
   336             case 482:
   337                 message->channel = strtok (message->line, " ");
       
   338                 message->line = strtok (NULL, "\r\n");
       
   339                 return command;
       
   340             case 901:          /* notify or some crap */
   337             case 901:          /* notify or some crap */
   341                 message->channel = strtok (message->line, " ");
   338                 message->channel = strtok (message->line, " ");
   342                 message->line = strtok (NULL, "\r\n");
   339                 message->line = strtok (NULL, "\r\n");
       
   340                 fprintf (message->stream, "PRIVMSG %s :%s\r\n", message->current_channel, message->line);
   343                 return command;
   341                 return command;
   344             default:
   342             default:
   345                 printf ("DEBUG   %s", line);
   343                 printf ("DEBUG   %s", line);
   346                 printf ("Unknown Command Value: %d\n", value);
   344                 printf ("Unknown Command Value: %d\n", value);
   347             }
   345             }