src/irc_net.c
changeset 72 43084f103c2a
parent 68 591a574f390e
child 73 2780a73c71f3
equal deleted inserted replaced
71:0a13030f795d 72:43084f103c2a
     1 #include "irc_net.h"
     1 #include "irc_net.h"
     2 #include "log.h"
     2 #include "log.h"
     3 
     3 
     4 #include <stdlib.h>
     4 #include <stdlib.h>
     5 #include <string.h>
     5 #include <string.h>
       
     6 
       
     7 const char* irc_net_name (struct irc_net *net)
       
     8 {
       
     9     return net->info.network;
       
    10 }
     6 
    11 
     7 /**
    12 /**
     8  * Something happaned which caused our irc_conn to fail. Destroy it, and recover. XXX: somehow
    13  * Something happaned which caused our irc_conn to fail. Destroy it, and recover. XXX: somehow
     9  */
    14  */
    10 static void irc_net_error (struct irc_net *net, struct error_info *err)
    15 static void irc_net_error (struct irc_net *net, struct error_info *err)
   106     
   111     
   107     irc_net_propagate_chan(net, line, line->args[0]);
   112     irc_net_propagate_chan(net, line, line->args[0]);
   108 }
   113 }
   109 
   114 
   110 /**
   115 /**
       
   116  * Propagate line to irc_chan based on args[1]
       
   117  */ 
       
   118 static void irc_net_on_chan1 (const struct irc_line *line, void *arg)
       
   119 {
       
   120     struct irc_net *net = arg;
       
   121     
       
   122     irc_net_propagate_chan(net, line, line->args[1]);
       
   123 }
       
   124 
       
   125 /**
       
   126  * Propagate line to irc_chan based on args[2]
       
   127  */ 
       
   128 static void irc_net_on_chan2 (const struct irc_line *line, void *arg)
       
   129 {
       
   130     struct irc_net *net = arg;
       
   131     
       
   132     irc_net_propagate_chan(net, line, line->args[2]);
       
   133 }
       
   134 
       
   135 /**
   111  * :nm (PRIVMSG|NOTICE) <target> <message>
   136  * :nm (PRIVMSG|NOTICE) <target> <message>
   112  *
   137  *
   113  * Either propagate to channel if found, otherwise handle as a privmsg
   138  * Either propagate to channel if found, otherwise handle as a privmsg
   114  */
   139  */
   115 static void irc_net_on_msg (const struct irc_line *line, void *arg)
   140 static void irc_net_on_msg (const struct irc_line *line, void *arg)
   129 
   154 
   130 /**
   155 /**
   131  * Our irc_cmd handler list
   156  * Our irc_cmd handler list
   132  */
   157  */
   133 static struct irc_cmd_handler _cmd_handlers[] = {
   158 static struct irc_cmd_handler _cmd_handlers[] = {
   134     {   "JOIN",     &irc_net_on_chan0       },
   159     {   "JOIN",             &irc_net_on_chan0       },
   135     {   "PART",     &irc_net_on_chan0       },
   160     {   "PART",             &irc_net_on_chan0       },
   136     {   "MODE",     &irc_net_on_chan0       },
   161     {   "MODE",             &irc_net_on_chan0       },
   137     {   "TOPIC",    &irc_net_on_chan0       },
   162     {   "TOPIC",            &irc_net_on_chan0       },
   138     {   "KICK",     &irc_net_on_chan0       },
   163     {   "KICK",             &irc_net_on_chan0       },
   139 
   164 
   140     {   "PRIVMSG",  &irc_net_on_msg         },
   165     {   "PRIVMSG",          &irc_net_on_msg         },
   141     {   "NOTICE",   &irc_net_on_msg         },
   166     {   "NOTICE",           &irc_net_on_msg         },
   142 
   167 
   143     // XXX: NICK/QUIT
   168     // XXX: NICK/QUIT
   144 
   169 
   145     {   NULL,       NULL                    }
   170     // numerics
       
   171     {   IRC_RPL_NAMREPLY,   &irc_net_on_chan2       },
       
   172     {   IRC_RPL_ENDOFNAMES, &irc_net_on_chan1       },
       
   173 
       
   174     {   NULL,               NULL                    }
   146 };
   175 };
   147 
   176 
   148 err_t irc_net_create (struct irc_net **net_ptr, const struct irc_net_info *info, struct error_info *err)
   177 err_t irc_net_create (struct irc_net **net_ptr, const struct irc_net_info *info, struct error_info *err)
   149 {
   178 {
   150     struct irc_net *net;
   179     struct irc_net *net;
   155         return SET_ERROR(err, ERR_CALLOC);
   184         return SET_ERROR(err, ERR_CALLOC);
   156 
   185 
   157     // initialize
   186     // initialize
   158     net->info = *info;
   187     net->info = *info;
   159     TAILQ_INIT(&net->channels);
   188     TAILQ_INIT(&net->channels);
       
   189     LIST_INIT(&net->users);
   160 
   190 
   161     if (info->raw_sock) {
   191     if (info->raw_sock) {
   162         log_info("connected using raw socket: %p", info->raw_sock);
   192         log_info("connected using raw socket: %p", info->raw_sock);
   163 
   193 
   164         sock = info->raw_sock;
   194         sock = info->raw_sock;
   222         chan = next;
   252         chan = next;
   223         next = TAILQ_NEXT(chan, node);
   253         next = TAILQ_NEXT(chan, node);
   224 
   254 
   225         irc_chan_destroy(chan);
   255         irc_chan_destroy(chan);
   226     }
   256     }
       
   257 
       
   258     // XXX: our users
   227 
   259 
   228     // ourselves
   260     // ourselves
   229     free(net);
   261     free(net);
   230 }
   262 }
   231 
   263 
   267 
   299 
   268     // no such channel
   300     // no such channel
   269     return NULL;
   301     return NULL;
   270 }
   302 }
   271 
   303 
       
   304 err_t irc_net_get_user (struct irc_net *net, struct irc_user **user_ptr, const char *nickname)
       
   305 {
       
   306     struct irc_user *user = NULL;
       
   307     err_t err;
       
   308 
       
   309     // look for it...
       
   310     LIST_FOREACH(user, &net->users, net_users) {
       
   311         if (irc_cmp_nick(nickname, user->nickname) == 0) {
       
   312             // found existing
       
   313             *user_ptr = user;
       
   314 
       
   315             return SUCCESS;
       
   316         }
       
   317     }
       
   318     
       
   319     // create a new user struct
       
   320     if ((err = irc_user_create(&user, net, nickname)))
       
   321         return err;
       
   322 
       
   323     // add to our list
       
   324     LIST_INSERT_HEAD(&net->users, user, net_users);
       
   325 
       
   326     // ok
       
   327     *user_ptr = user;
       
   328 
       
   329     return SUCCESS;
       
   330 }
       
   331 
   272 err_t irc_net_quit (struct irc_net *net, const char *message)
   332 err_t irc_net_quit (struct irc_net *net, const char *message)
   273 {
   333 {
   274    if (!net->conn)
   334    if (!net->conn)
   275        return ERR_IRC_NET_QUIT_STATE;
   335        return ERR_IRC_NET_QUIT_STATE;
   276     
   336