src/irc.c
changeset 33 56571d34d754
parent 29 08ec6ddd6eea
child 34 900bc2a53fd7
equal deleted inserted replaced
32:89fb9c6611ca 33:56571d34d754
    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 
    22 #include "common.h"
    23 #define  NOTICE  0
    23 
    24 #define    MODE  1
    24 enum {
    25 #define    JOIN  2
    25     NOTICE, MODE, JOIN, PART, TOPIC, PING, ENOMEM, ERROR, VERSION, PRIVMSG, QUIT, NICK, KICK
    26 #define    PART  3
    26 };
    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
       
    35 #define    KICK 12
       
    36 
    27 
    37 #define VERSION_STRING "MCBOT on GNU/LINUX"
    28 #define VERSION_STRING "MCBOT on GNU/LINUX"
    38 
    29 
    39 const char *IRC_Commands[] = {
    30 const char *IRC_Commands[] = {
    40     "NOTICE", "MODE", "JOIN", "PART",
    31     "NOTICE", "MODE", "JOIN", "PART",
    94 }
    85 }
    95 
    86 
    96 int irc_login (FILE * stream, char *server, char *nick, char *password)
    87 int irc_login (FILE * stream, char *server, char *nick, char *password)
    97 {
    88 {
    98     MSG message = { NULL, 0, 0, 0, 0, 0, 0 };
    89     MSG message = { NULL, 0, 0, 0, 0, 0, 0 };
    99     char msg[513];
    90     char msg[DEFAULT_BUF_SIZE];
   100     char *user;
    91     char *user;
   101     struct passwd *pwd;
    92     struct passwd *pwd;
   102 
    93 
   103     if ((user = getenv ("USER")) == NULL)
    94     if ((user = getenv ("USER")) == NULL)
   104         return IRC_GENERAL_ERROR;
    95         return IRC_GENERAL_ERROR;
   116 
   107 
   117     if (password != NULL)
   108     if (password != NULL)
   118         fprintf (stream, "PRIVMSG NICKSERV :IDENTIFY %s\r\n", password);
   109         fprintf (stream, "PRIVMSG NICKSERV :IDENTIFY %s\r\n", password);
   119 
   110 
   120     for (;;) {
   111     for (;;) {
   121         *msg = '\0';
   112         fgets (msg, sizeof (msg), stream);
   122         fgets (msg, 512, stream);
       
   123         if ((user = irc_parsemessage (msg, &message)) != NULL)
   113         if ((user = irc_parsemessage (msg, &message)) != NULL)
   124             printf ("%10s %s\n", user, message.line);
   114             printf ("%10s %s\n", user, message.line);
   125 
   115 
   126         if (strstr (msg, "VERSION") != NULL) {
   116         if (strstr (msg, "VERSION") != NULL) {
   127             printf ("%10s %s", "VERSION", msg);
   117             printf ("%10s %s", "VERSION", msg);
   265                         ++message->email;
   255                         ++message->email;
   266                     if ((ptr = strchr (message->user, '!')))
   256                     if ((ptr = strchr (message->user, '!')))
   267                         *ptr = '\0';
   257                         *ptr = '\0';
   268 
   258 
   269                     message->channel = strtok (message->line, " ");
   259                     message->channel = strtok (message->line, " ");
   270                     strncpy (message->current_channel, message->channel, 40);
   260                     strncpy (message->current_channel, message->channel, sizeof (message->current_channel));
   271                     message->line = strtok (NULL, "\r\n");
   261                     message->line = strtok (NULL, "\r\n");
   272                     message->line++;
   262                     message->line++;
   273                     printf ("%10s %s %s :%s\n", "READ", message->command, message->channel, message->line);
   263                     printf ("%10s %s %s :%s\n", "READ", message->command, message->channel, message->line);
   274                     return NULL;
   264                     return NULL;
   275                 case QUIT:
   265                 case QUIT: