src/irc.c
changeset 9 aff6726b8b87
parent 6 7eb12be31bb5
child 10 311ea5fa60dd
equal deleted inserted replaced
8:3152de03758e 9:aff6726b8b87
    94 
    94 
    95     if (stream == NULL)
    95     if (stream == NULL)
    96         return IRC_GENERAL_ERROR;
    96         return IRC_GENERAL_ERROR;
    97 
    97 
    98     fprintf (stream, "NICK %s\r\n", nick);
    98     fprintf (stream, "NICK %s\r\n", nick);
    99 
       
   100     fprintf (stream, "USER %s 0 %s %s\r\n", user, server, pwd->pw_gecos);
    99     fprintf (stream, "USER %s 0 %s %s\r\n", user, server, pwd->pw_gecos);
   101 
       
   102     fprintf (stream, "PRIVMSG NICKSERV :IDENTIFY %s\r\n", password);
   100     fprintf (stream, "PRIVMSG NICKSERV :IDENTIFY %s\r\n", password);
   103 
   101 
   104     for (;;) {
   102     for (;;) {
   105         fgets (msg, 512, stream);
   103         fgets (msg, 512, stream);
   106         if ((user = irc_parsemessage (msg, &message)) != NULL)
   104         if ((user = irc_parsemessage (msg, &message)) != NULL)
   167     message->line = strtok (NULL, "\r\n");
   165     message->line = strtok (NULL, "\r\n");
   168 
   166 
   169     return message->command;
   167     return message->command;
   170 }
   168 }
   171 
   169 
   172 /*
   170 /**
   173  * Main prints ("%10s %s %s\n", MSG->command MSG->channel MSG->line).
   171  * Main prints ("%10s %s %s\n", MSG->command MSG->channel MSG->line).
   174  * This functions makes sure, that there will be someting to print...
   172  * This functions makes sure, that there will be someting to print...
   175  */
   173  */
   176 
       
   177 char *irc_parsemessage (const char *line, MSG * message)
   174 char *irc_parsemessage (const char *line, MSG * message)
   178 {
   175 {
   179     int i;
   176     int i;
   180     char *command;
   177     char *command;
   181     char *ptr;
   178     char *ptr;
   184     i = 0;
   181     i = 0;
   185     if ((command = irc_getmessage (line, message)) != NULL) {
   182     if ((command = irc_getmessage (line, message)) != NULL) {
   186         while (IRC_Commands[i] != NULL) {
   183         while (IRC_Commands[i] != NULL) {
   187             if (strcmp (IRC_Commands[i], command) == 0) {
   184             if (strcmp (IRC_Commands[i], command) == 0) {
   188                 switch (i) {
   185                 switch (i) {
   189 
       
   190                 case 0:        /* NOTICE */
   186                 case 0:        /* NOTICE */
   191                     if (message->line == NULL) {
   187                     if (message->line == NULL) {
   192                         message->channel = "*";
   188                         message->channel = "*";
   193                         message->line = "*";
   189                         message->line = "*";
   194                     } else {
   190                     } else {
   212                     if ((message->line = strtok (NULL, "\r\n")))
   208                     if ((message->line = strtok (NULL, "\r\n")))
   213                         ++message->line;
   209                         ++message->line;
   214                     return command;
   210                     return command;
   215                 case 5:        /* PING */
   211                 case 5:        /* PING */
   216 #ifdef DEBUG
   212 #ifdef DEBUG
       
   213                     /*
       
   214                      * DONT NERVE WITH PING PONG MESSAGES
       
   215                      */
   217                     printf ("%10s %s localhost\n", "PING", message->line);
   216                     printf ("%10s %s localhost\n", "PING", message->line);
   218 #endif
   217 #endif
   219                     fprintf (message->stream, "PONG :%s\r\n", message->line);
   218                     fprintf (message->stream, "PONG :%s\r\n", message->line);
   220                     message->channel = "localhost";
   219                     message->channel = "localhost";
   221 #ifdef DEBUG
   220 #ifdef DEBUG
   223 #else
   222 #else
   224                     return NULL;
   223                     return NULL;
   225 #endif
   224 #endif
   226                 case 6:        /* ENOMEM */
   225                 case 6:        /* ENOMEM */
   227                 case 7:        /* ERROR */
   226                 case 7:        /* ERROR */
   228                     exit (-1);
   227                     return command;
   229                     break;
       
   230                 case 8:        /* VERSION */
   228                 case 8:        /* VERSION */
   231                     if ((ptr = strchr (message->user, ' ')))
   229                     if ((ptr = strchr (message->user, ' ')))
   232                         *ptr = 0;
   230                         *ptr = 0;
   233                     return command;
   231                     return command;
   234                 case 9:        /* PRIVMSG */
   232                 case 9:        /* PRIVMSG */
   236                         ++message->email;
   234                         ++message->email;
   237                     if ((ptr = strchr (message->user, '!')))
   235                     if ((ptr = strchr (message->user, '!')))
   238                         *ptr = 0;
   236                         *ptr = 0;
   239 
   237 
   240                     message->channel = strtok (message->line, " ");
   238                     message->channel = strtok (message->line, " ");
       
   239                     message->current_channel = message->channel;
   241                     message->line = strtok (NULL, "\r\n");
   240                     message->line = strtok (NULL, "\r\n");
   242                     message->line++;
   241                     message->line++;
   243                     printf ("%10s %s %s :%s\n", "READ", message->command, message->channel, message->line);
   242                     printf ("%10s %s %s :%s\n", "READ", message->command, message->channel, message->line);
   244                     return NULL;
   243                     return NULL;
   245                 case 10:       /* QUIT */
   244                 case 10:       /* QUIT */
   269             case 266:
   268             case 266:
   270                 /*
   269                 /*
   271                  * prints as is in irc_login
   270                  * prints as is in irc_login
   272                  */
   271                  */
   273                 return command;
   272                 return command;
   274                 break;
       
   275             case 311:
   273             case 311:
   276             case 312:
   274             case 312:
   277             case 315:          /* END OF WHO */
   275             case 315:          /* END OF WHO */
   278             case 318:
   276             case 318:
   279                 message->channel = strtok (message->line, " ");
   277                 message->channel = strtok (message->line, " ");
   280                 message->line = strtok (NULL, "\r\n");
   278                 message->line = strtok (NULL, "\r\n");
   281                 return command;
   279                 return command;
   282             case 319:
   280             case 319:
   283                 message->channel = strtok (message->line, " ");
   281                 message->channel = strtok (message->line, " ");
   284                 message->line = strtok (NULL, "\r\n");
   282                 message->line = strtok (NULL, "\r\n");
   285                 fprintf (message->stream, "PRIVMSG %s :%s\r\n", "#mcbot", message->line);
   283                 fprintf (message->stream, "PRIVMSG %s :%s\r\n", message->current_channel, message->line);
   286                 return command;
   284                 return command;
   287             case 320:
   285             case 320:
   288             case 332:          /* TOPIC OF CHANNEL */
   286             case 332:          /* TOPIC OF CHANNEL */
   289             case 333:          /* NAMES IN CHANNEL */
   287             case 333:          /* NAMES IN CHANNEL */
   290                 message->channel = strtok (message->line, " ");
   288                 message->channel = strtok (message->line, " ");
   299                 message->line = strtok (NULL, "\r\n");
   297                 message->line = strtok (NULL, "\r\n");
   300                 /*
   298                 /*
   301                  * MORE THAN 3 LINES AND YOU WILL be KICKED
   299                  * MORE THAN 3 LINES AND YOU WILL be KICKED
   302                  */
   300                  */
   303                 return command;
   301                 return command;
   304                 break;
       
   305             case 353:
   302             case 353:
   306             case 365:
   303             case 365:
   307             case 366:          /* END OF NAMES */
   304             case 366:          /* END OF NAMES */
   308                 message->channel = strtok (message->line, " ");
   305                 message->channel = strtok (message->line, " ");
   309                 message->line = strtok (NULL, "\r\n");
   306                 message->line = strtok (NULL, "\r\n");
   310                 return command;
   307                 return command;
   311                 break;
       
   312             case 372:          /* MOTD MESSAGES */
   308             case 372:          /* MOTD MESSAGES */
   313             case 375:
   309             case 375:
   314             case 376:          /* END OF MOTD */
   310             case 376:          /* END OF MOTD */
   315                 return command;
   311                 return command;
   316                 break;
   312                 break;
   317             case 401:          /* NO SUCH NICK/CHANNEL */
   313             case 401:          /* NO SUCH NICK/CHANNEL */
   318             case 403:          /* That CHANNEL doesnt exist */
   314             case 403:          /* That CHANNEL doesnt exist */
   319                 return command;
   315             case 441:          /* They aren't on this channel */
   320                 break;
   316                 return command;
   321             case 474:
   317             case 474:
   322             case 475:
   318             case 475:
   323             case 476:
   319             case 476:
   324             case 477:
   320             case 477:
   325                 message->channel = strtok (message->line, " ");
   321             case 482:
   326                 message->line = strtok (NULL, "\r\n");
   322                 message->channel = strtok (message->line, " ");
   327                 return command;
   323                 message->line = strtok (NULL, "\r\n");
   328                 break;
   324                 return command;
   329             case 901:          /* notify or some crap */
   325             case 901:          /* notify or some crap */
   330                 message->channel = strtok (message->line, " ");
   326                 message->channel = strtok (message->line, " ");
   331                 message->line = strtok (NULL, "\r\n");
   327                 message->line = strtok (NULL, "\r\n");
   332                 return command;
   328                 return command;
   333                 break;
       
   334             default:
   329             default:
   335                 printf ("DEBUG   %s", line);
   330                 printf ("DEBUG   %s", line);
   336                 printf ("Unknown Value: %d\n", value);
   331                 printf ("Unknown Command Value: %d\n", value);
   337             }
   332             }
   338         }
   333         }
   339         printf ("DEBUG   %s", line);
   334         printf ("DEBUG   %s", line);
   340         printf ("Unknown Command: %s\n", command);
   335         printf ("Unknown Command: %s\n", command);
   341         return command;
   336         return command;
   342     }
   337     }
   343     printf ("DEBUG   %s", line);
   338     printf ("NOT PARSEABLE   %s", line);
   344     return NULL;
   339     return NULL;
   345 }
   340 }