src/irc_chan.c
changeset 37 4fe4a3c4496e
parent 26 aec062af155d
child 38 0c2e0cb46c3a
equal deleted inserted replaced
36:791d7a5532e2 37:4fe4a3c4496e
     1 #include "irc_chan.h"
     1 #include "irc_chan.h"
       
     2 #include "irc_nm.h"
       
     3 #include "log.h"
     2 
     4 
     3 #include <stdlib.h>
     5 #include <stdlib.h>
     4 #include <assert.h>
     6 #include <assert.h>
     5 
     7 
     6 const char* irc_chan_name (struct irc_chan *chan)
     8 const char* irc_chan_name (struct irc_chan *chan)
     7 {
     9 {
     8     return chan->info.channel;
    10     return chan->info.channel;
     9 }
    11 }
       
    12 
       
    13 /**
       
    14  * :nm JOIN <channel>
       
    15  */ 
       
    16 static void irc_chan_on_JOIN (const struct irc_line *line, void *arg)
       
    17 {
       
    18     struct irc_chan *chan = arg;
       
    19 
       
    20     // us?
       
    21     if (irc_prefix_cmp_nick(line->prefix, chan->net->conn->nickname) == 0) {
       
    22         // twiddle state
       
    23         chan->state.joining = false;
       
    24         chan->state.joined = true;
       
    25 
       
    26         log_info("joined channel: %s", chan->info.channel);
       
    27 
       
    28         // TODO: callbacks
       
    29     }
       
    30 }
       
    31 
       
    32 /**
       
    33  * Core command handlers
       
    34  */
       
    35 struct irc_cmd_handler _cmd_handlers[] = {
       
    36     {   "JOIN",     &irc_chan_on_JOIN   },
       
    37     {   NULL,       NULL                }
       
    38 };
    10 
    39 
    11 err_t irc_chan_create (struct irc_chan **chan_ptr, struct irc_net *net, const struct irc_chan_info *info, struct error_info *err)
    40 err_t irc_chan_create (struct irc_chan **chan_ptr, struct irc_net *net, const struct irc_chan_info *info, struct error_info *err)
    12 {
    41 {
    13     struct irc_chan *chan;
    42     struct irc_chan *chan;
    14 
    43 
    17         return SET_ERROR(err, ERR_CALLOC);
    46         return SET_ERROR(err, ERR_CALLOC);
    18 
    47 
    19     // store
    48     // store
    20     chan->net = net;
    49     chan->net = net;
    21     chan->info = *info;
    50     chan->info = *info;
    22     chan->state = IRC_CHAN_INIT;
    51 
       
    52     // init
       
    53     irc_cmd_init(&chan->handlers);
       
    54     
       
    55     // add handlers
       
    56     if ((ERROR_CODE(err) = irc_cmd_add(&chan->handlers, _cmd_handlers, chan)))
       
    57         goto error;
    23 
    58 
    24     // ok
    59     // ok
    25     *chan_ptr = chan;
    60     *chan_ptr = chan;
    26 
    61 
    27     return SUCCESS;
    62     return SUCCESS;
       
    63 
       
    64 error:
       
    65     // cleanup
       
    66     irc_chan_destroy(chan);
       
    67 
       
    68     return ERROR_CODE(err);
       
    69 }
       
    70 
       
    71 void irc_chan_destroy (struct irc_chan *chan)
       
    72 {
       
    73     // free command handlers
       
    74     irc_cmd_free(&chan->handlers);
       
    75 
       
    76     // free chan itself
       
    77     free(chan);
    28 }
    78 }
    29 
    79 
    30 err_t irc_chan_join (struct irc_chan *chan)
    80 err_t irc_chan_join (struct irc_chan *chan)
    31 {
    81 {
    32     err_t err;
    82     err_t err;
    33 
    83 
    34     // XXX: error instead?
    84     // XXX: error instead?
    35     assert(chan->state == IRC_CHAN_INIT);
    85     assert(!chan->state.joining && !chan->state.joined);
    36 
    86 
    37     // send JOIN message on the appropriate connection
    87     // send JOIN message on the appropriate connection
    38     if ((err = irc_conn_JOIN(chan->net->conn, chan->info.channel)))
    88     if ((err = irc_conn_JOIN(chan->net->conn, chan->info.channel)))
    39         // XXX: state?
    89         // XXX: error state?
    40         return err;
    90         return err;
    41 
    91 
    42     // ok, joining
    92     // ok
    43     chan->state = IRC_CHAN_JOINING;
    93     chan->state.joining = true;
    44 
    94 
    45     // done
       
    46     return SUCCESS;
    95     return SUCCESS;
    47 }
    96 }
    48 
    97