src/irc.c
changeset 10 311ea5fa60dd
parent 9 aff6726b8b87
child 11 a769385a59c6
equal deleted inserted replaced
9:aff6726b8b87 10:311ea5fa60dd
    23 
    23 
    24 const char *IRC_Commands[] = {
    24 const char *IRC_Commands[] = {
    25     "NOTICE", "MODE", "JOIN", "PART",
    25     "NOTICE", "MODE", "JOIN", "PART",
    26     "TOPIC", "PING", "ENOMEM", "ERROR",
    26     "TOPIC", "PING", "ENOMEM", "ERROR",
    27     "VERSION", "PRIVMSG", "QUIT", "NICK",
    27     "VERSION", "PRIVMSG", "QUIT", "NICK",
    28     NULL,
       
    29 };
    28 };
    30 
    29 
    31 FILE *irc_connect (char *server, unsigned int port)
    30 FILE *irc_connect (char *server, unsigned int port)
    32 {
    31 {
    33     struct hostent *he;
    32     struct hostent *he;
    34     char *ip;
    33     char *ip;
    35     struct sockaddr_in ca;
    34     struct sockaddr_in ca;
    36     int csocket;
    35     int csocket;
    37     FILE *stream = NULL;
    36     FILE *stream;
    38 
    37 
    39     he = gethostbyname (server);
    38     he = gethostbyname (server);
    40     if (he == NULL) {
    39     if (he == NULL) {
    41         perror ("GETHOSTBYNAME");
    40         perror ("GETHOSTBYNAME");
    42         return NULL;
    41         return NULL;
    43     }
    42     }
    44 
    43 
    45     if ((ip = inet_ntoa (*((struct in_addr *)he->h_addr_list[0]))) == NULL) {
    44     if ((ip = inet_ntoa (*((struct in_addr *)he->h_addr_list[0]))) == NULL) {
    46         perror ("GETHOSTBYNAME");
    45         perror ("INET_NTOA");
    47         return NULL;
    46         return NULL;
    48     } else
    47     } else
    49         printf ("IP: %s\n", ip);
    48         printf ("IP: %s\n", ip);
    50 
    49 
    51     ca.sin_family = AF_INET;
    50     ca.sin_family = AF_INET;
    78     return stream;
    77     return stream;
    79 }
    78 }
    80 
    79 
    81 int irc_login (FILE * stream, char *server, char *nick, char *password)
    80 int irc_login (FILE * stream, char *server, char *nick, char *password)
    82 {
    81 {
    83     MSG message;
    82     MSG message = { NULL, 0, 0, 0, 0, 0, 0 };
    84     char msg[513];
    83     char msg[513];
    85     char *user;
    84     char *user;
    86     struct passwd *pwd = NULL;
    85     struct passwd *pwd;
    87 
    86 
    88     if ((user = getenv ("USER")) != NULL)
    87     if ((user = getenv ("USER")) == NULL)
    89         if ((pwd = getpwnam (user)) == NULL)
    88         return IRC_GENERAL_ERROR;
    90             return IRC_GENERAL_ERROR;
    89 
       
    90     if ((pwd = getpwnam (user)) == NULL)
       
    91         return IRC_GENERAL_ERROR;
    91 
    92 
    92     if (password == NULL)
    93     if (password == NULL)
    93         return IRC_LOGIN_ERROR;
    94         return IRC_LOGIN_ERROR;
    94 
    95 
    95     if (stream == NULL)
    96     if (stream == NULL)
    96         return IRC_GENERAL_ERROR;
    97         return IRC_GENERAL_ERROR;
       
    98     else
       
    99         message.stream = stream;
    97 
   100 
    98     fprintf (stream, "NICK %s\r\n", nick);
   101     fprintf (stream, "NICK %s\r\n", nick);
    99     fprintf (stream, "USER %s 0 %s %s\r\n", user, server, pwd->pw_gecos);
   102     fprintf (stream, "USER %s 0 %s %s\r\n", user, server, pwd->pw_gecos);
   100     fprintf (stream, "PRIVMSG NICKSERV :IDENTIFY %s\r\n", password);
   103     fprintf (stream, "PRIVMSG NICKSERV :IDENTIFY %s\r\n", password);
   101 
   104 
   102     for (;;) {
   105     for (;;) {
       
   106         *msg = 0;
   103         fgets (msg, 512, stream);
   107         fgets (msg, 512, stream);
   104         if ((user = irc_parsemessage (msg, &message)) != NULL)
   108         if ((user = irc_parsemessage (msg, &message)) != NULL)
   105             printf ("%10s %s\n", user, message.line);
   109             printf ("%10s %s\n", user, message.line);
   106 
   110 
   107         if (strstr (msg, "VERSION") != NULL) {
   111         if (strstr (msg, "VERSION") != NULL) {
   281                 message->channel = strtok (message->line, " ");
   285                 message->channel = strtok (message->line, " ");
   282                 message->line = strtok (NULL, "\r\n");
   286                 message->line = strtok (NULL, "\r\n");
   283                 fprintf (message->stream, "PRIVMSG %s :%s\r\n", message->current_channel, message->line);
   287                 fprintf (message->stream, "PRIVMSG %s :%s\r\n", message->current_channel, message->line);
   284                 return command;
   288                 return command;
   285             case 320:
   289             case 320:
       
   290             case 328:          /* INFORMATION */
   286             case 332:          /* TOPIC OF CHANNEL */
   291             case 332:          /* TOPIC OF CHANNEL */
   287             case 333:          /* NAMES IN CHANNEL */
   292             case 333:          /* NAMES IN CHANNEL */
   288                 message->channel = strtok (message->line, " ");
   293                 message->channel = strtok (message->line, " ");
   289                 message->line = strtok (NULL, "\r\n");
   294                 message->line = strtok (NULL, "\r\n");
   290                 return command;
   295                 return command;
   309             case 375:
   314             case 375:
   310             case 376:          /* END OF MOTD */
   315             case 376:          /* END OF MOTD */
   311                 return command;
   316                 return command;
   312                 break;
   317                 break;
   313             case 401:          /* NO SUCH NICK/CHANNEL */
   318             case 401:          /* NO SUCH NICK/CHANNEL */
   314             case 403:          /* That CHANNEL doesnt exist */
   319             case 403:          /* THAT CHANNEL DOESN'T EXIST */
   315             case 441:          /* They aren't on this channel */
   320             case 441:          /* THEY AREN'T ON THIS CHANNEL */
   316                 return command;
   321                 return command;
   317             case 474:
   322             case 474:
   318             case 475:
   323             case 475:
   319             case 476:
   324             case 476:
   320             case 477:
   325             case 477: