src/irc_chan.c
author Tero Marttila <terom@fixme.fi>
Fri, 27 Mar 2009 17:22:43 +0200
changeset 83 c8e2dac08207
parent 82 bc767e01648d
child 87 f0db6ebf18b9
permissions -rw-r--r--
add config module and modify irc_log/nexus to use it
26
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "irc_chan.h"
39
a4891d71aca9 rename irc_nm to irc_proto, and move numerics from irc_cmd.h
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
     2
#include "irc_proto.h"
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
     3
#include "log.h"
26
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
#include <stdlib.h>
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
     6
#include <string.h>
26
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
#include <assert.h>
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
const char* irc_chan_name (struct irc_chan *chan)
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
{
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
    return chan->info.channel;
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
}
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    14
/**
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    15
 * Add or update a nickname to the irc_chan.users list.
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    16
 *
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    17
 * If the given nickname already exists in our users list, this does nothing. Otherwise, an irc_user is aquired using
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    18
 * irc_net_get_user, and a new irc_chan_user struct added to our users list.
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    19
 */
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    20
static err_t irc_chan_add_user (struct irc_chan *chan, const char *nickname)
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    21
{
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    22
    struct irc_user *user;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    23
    struct irc_chan_user *chan_user;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    24
    err_t err;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    25
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    26
    // skip if already listed
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    27
    if (irc_chan_get_user(chan, nickname))
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    28
        return SUCCESS;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    29
    
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    30
    // lookup/create the irc_user state, incrementing the refcount
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    31
    if ((err = irc_net_get_user(chan->net, &user, nickname)))
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    32
        return err;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    33
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    34
    // alloc the new irc_chan_user
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    35
    if ((chan_user = calloc(1, sizeof(*chan_user))) == NULL) {
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    36
        // XXX: release irc_user
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    37
        return ERR_CALLOC;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    38
    }
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    39
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    40
    // store
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    41
    chan_user->user = user;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    42
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    43
    // add to users list
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    44
    LIST_INSERT_HEAD(&chan->users, chan_user, chan_users);
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    45
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    46
    // ok
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    47
    return SUCCESS;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    48
}
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    49
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    50
/**
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    51
 * Remove an irc_chan_user previously added by irc_chan_add_user().
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    52
 */
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    53
static void irc_chan_remove_user (struct irc_chan *chan, struct irc_chan_user *chan_user)
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    54
{
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    55
    // put the irc_user reference back, decrementing refcount
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    56
    irc_net_put_user(chan->net, chan_user->user);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    57
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    58
    // remove from list
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    59
    LIST_REMOVE(chan_user, chan_users);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    60
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    61
    // free
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    62
    free(chan_user);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    63
}
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    64
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    65
/**
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    66
 * :nm JOIN <channel>
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    67
 */ 
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    68
static void irc_chan_on_JOIN (const struct irc_line *line, void *arg)
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    69
{
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    70
    struct irc_chan *chan = arg;
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    71
    err_t err;
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    72
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    73
    // us?
75
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
    74
    if (irc_cmp_nick(line->source->nickname, chan->net->conn->nickname) == 0) {
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    75
        // twiddle state
45
71e65564afd2 remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    76
        chan->joining = false;
71e65564afd2 remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    77
        chan->joined = true;
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    78
38
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
    79
        // invoke callback
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
    80
        IRC_CHAN_INVOKE_CALLBACK(chan, on_self_join);
45
71e65564afd2 remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    81
71e65564afd2 remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    82
    } else {
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    83
        // add them
75
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
    84
        if ((err = irc_chan_add_user(chan, line->source->nickname)))
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
    85
            return log_warn("irc_chan_add_user(%s, %s): %s", irc_chan_name(chan), line->source->nickname, error_name(err));
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    86
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
    87
        // invoke callback (source)
75
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
    88
        IRC_CHAN_INVOKE_CALLBACK(chan, on_join, line->source);
38
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
    89
    }
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
    90
}
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    91
38
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
    92
/**
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    93
 * Someone, or us ourselves, left a channel
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    94
 *
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    95
 * :nm PART <channel> [<message>]
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    96
 */
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    97
static void irc_chan_on_PART (const struct irc_line *line, void *arg)
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    98
{
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
    99
    struct irc_chan *chan = arg;
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   100
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   101
    const char *msg = line->args[1];
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   102
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   103
    // us?
75
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   104
    if (irc_cmp_nick(line->source->nickname, chan->net->conn->nickname) == 0) {
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   105
        // twiddle state
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   106
        chan->joined = false;
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   107
        chan->parted = true;
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   108
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   109
        // invoke callback
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   110
        IRC_CHAN_INVOKE_CALLBACK(chan, on_self_part);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   111
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   112
    } else {
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   113
        // someone else
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   114
        struct irc_chan_user *chan_user;
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   115
        
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   116
        // invoke callback (source, msg)
75
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   117
        IRC_CHAN_INVOKE_CALLBACK(chan, on_part, line->source, msg);
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   118
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   119
        // look up the irc_chan_user
75
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   120
        if ((chan_user = irc_chan_get_user(chan, line->source->nickname)) == NULL)
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   121
            return log_warn("PART'd user not on channel: %s, %s", irc_chan_name(chan), line->source->nickname);
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   122
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   123
        // remove them
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   124
        irc_chan_remove_user(chan, chan_user);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   125
    }
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   126
}
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   127
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   128
/**
38
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
   129
 * :nm PRIVMSG <channel> <message>
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
   130
 */
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
   131
static void irc_chan_on_PRIVMSG (const struct irc_line *line, void *arg)
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
   132
{
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
   133
    struct irc_chan *chan = arg;
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
   134
45
71e65564afd2 remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   135
    const char *msg = line->args[1];
71e65564afd2 remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   136
75
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   137
    // invoke callback (source, message)
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   138
    IRC_CHAN_INVOKE_CALLBACK(chan, on_msg, line->source, msg);
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   139
}
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   140
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   141
/**
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   142
 * Add/update nicknames to users list using irc_chan_add_user
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   143
 *
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   144
 * @see IRC_RPL_NAMREPLY
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   145
 */
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   146
static void irc_chan_on_RPL_NAMREPLY (const struct irc_line *line, void *arg)
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   147
{
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   148
    struct irc_chan *chan = arg;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   149
    char chanflags[IRC_CHANFLAGS_MAX];
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   150
    const char *nickname;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   151
    err_t err;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   152
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   153
    // the args
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   154
    const char *arg_names = line->args[3];
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   155
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   156
    // copy the nicklist to a mutable buffer
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   157
    char names_buf[strlen(arg_names) + 1], *names = names_buf;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   158
    strcpy(names, arg_names);
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   159
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   160
    // iterate over each name
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   161
    while ((nickname = strsep(&names, " "))) {
82
bc767e01648d fix a valgrind'd bug with irc_nick_chanflags being given an empty string
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   162
        // skip empty token at end
bc767e01648d fix a valgrind'd bug with irc_nick_chanflags being given an empty string
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   163
        if (strlen(nickname) == 0)
bc767e01648d fix a valgrind'd bug with irc_nick_chanflags being given an empty string
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   164
            continue;
bc767e01648d fix a valgrind'd bug with irc_nick_chanflags being given an empty string
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   165
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   166
        // parse off the channel flags
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   167
        nickname = irc_nick_chanflags(nickname, chanflags);
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   168
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   169
        // add/update
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   170
        // XXX: do something with chanflags
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   171
        if ((err = irc_chan_add_user(chan, nickname)))
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   172
            log_warn("irc_chan_add_user(%s, %s): %s", irc_chan_name(chan), nickname, error_name(err));
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   173
    }
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   174
}
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   175
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   176
/**
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   177
 * Channel join sequence complete
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   178
 *
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   179
 * @see IRC_RPL_ENDOFNAMES
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   180
 */
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   181
static void irc_chan_on_RPL_ENDOFNAMES (const struct irc_line *line, void *arg)
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   182
{
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   183
    struct irc_chan *chan = arg;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   184
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   185
    // XXX: update state
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   186
    log_info("channel join sync complete");
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   187
}
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   188
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   189
/**
78
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   190
 * :nm QUIT [<message>]
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   191
 *
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   192
 * User quit, so remove them from our users list
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   193
 */
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   194
static void irc_chan_on_QUIT (const struct irc_line *line, void *arg)
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   195
{
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   196
    struct irc_chan *chan = arg;
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   197
    struct irc_chan_user *chan_user;
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   198
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   199
    const char *msg = line->args[1];
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   200
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   201
    // invoke callback (source, msg)
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   202
    IRC_CHAN_INVOKE_CALLBACK(chan, on_quit, line->source, msg);
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   203
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   204
    // look up the irc_chan_user
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   205
    if ((chan_user = irc_chan_get_user(chan, line->source->nickname)) == NULL)
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   206
        return log_warn("QUIT'd user not on channel: %s, %s", irc_chan_name(chan), line->source->nickname);
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   207
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   208
    // remove them
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   209
    irc_chan_remove_user(chan, chan_user);
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   210
}
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   211
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   212
/**
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   213
 * Core command handlers
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   214
 */
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   215
struct irc_cmd_handler _cmd_handlers[] = {
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   216
    {   "JOIN",             &irc_chan_on_JOIN           },
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   217
    {   IRC_RPL_NAMREPLY,   &irc_chan_on_RPL_NAMREPLY   },
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   218
    {   IRC_RPL_ENDOFNAMES, &irc_chan_on_RPL_ENDOFNAMES },
78
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   219
    {   "PRIVMSG",          &irc_chan_on_PRIVMSG        },
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   220
    {   "PART",             &irc_chan_on_PART           },
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   221
    {   "QUIT",             &irc_chan_on_QUIT           },
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   222
    {   NULL,       NULL                                }
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   223
};
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   224
26
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   225
err_t irc_chan_create (struct irc_chan **chan_ptr, struct irc_net *net, const struct irc_chan_info *info, struct error_info *err)
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   226
{
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   227
    struct irc_chan *chan;
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   228
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   229
    // allocate
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   230
    if ((chan = calloc(1, sizeof(*chan))) == NULL)
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   231
        return SET_ERROR(err, ERR_CALLOC);
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   232
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   233
    // store
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   234
    chan->net = net;
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   235
    chan->info = *info;
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   236
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   237
    // init
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   238
    LIST_INIT(&chan->users);
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   239
    irc_cmd_init(&chan->handlers);
38
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
   240
    CHAIN_INIT(&chan->callbacks);
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   241
    
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   242
    // add handlers
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   243
    if ((ERROR_CODE(err) = irc_cmd_add(&chan->handlers, _cmd_handlers, chan)))
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   244
        goto error;
26
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   245
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   246
    // ok
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   247
    *chan_ptr = chan;
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   248
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   249
    return SUCCESS;
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   250
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   251
error:
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   252
    // cleanup
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   253
    irc_chan_destroy(chan);
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   254
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   255
    return ERROR_CODE(err);
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   256
}
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   257
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   258
void irc_chan_destroy (struct irc_chan *chan)
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   259
{
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   260
    struct irc_chan_user *chan_user;
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   261
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   262
    // free users
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   263
    while ((chan_user = LIST_FIRST(&chan->users))) {
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   264
        irc_chan_remove_user(chan, chan_user);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   265
    }
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   266
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   267
    // free command handlers
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   268
    irc_cmd_free(&chan->handlers);
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   269
38
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
   270
    // free callbacks
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
   271
    chain_free(&chan->callbacks);
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
   272
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   273
    // free chan itself
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   274
    free(chan);
26
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   275
}
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   276
38
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
   277
err_t irc_chan_add_callbacks (struct irc_chan *chan, const struct irc_chan_callbacks *callbacks, void *arg)
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
   278
{
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
   279
    return chain_add(&chan->callbacks, callbacks, arg);
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
   280
}
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
   281
69
6f298b6e0d5f create irc_log_chan function to log a new irc_log_chan, and irc_log_chan_destroy to remove the added callbacks/command handlers
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   282
void irc_chan_remove_callbacks (struct irc_chan *chan, const struct irc_chan_callbacks *callbacks, void *arg)
6f298b6e0d5f create irc_log_chan function to log a new irc_log_chan, and irc_log_chan_destroy to remove the added callbacks/command handlers
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   283
{
6f298b6e0d5f create irc_log_chan function to log a new irc_log_chan, and irc_log_chan_destroy to remove the added callbacks/command handlers
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   284
    chain_remove(&chan->callbacks, callbacks, arg);
6f298b6e0d5f create irc_log_chan function to log a new irc_log_chan, and irc_log_chan_destroy to remove the added callbacks/command handlers
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   285
}
6f298b6e0d5f create irc_log_chan function to log a new irc_log_chan, and irc_log_chan_destroy to remove the added callbacks/command handlers
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   286
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   287
struct irc_chan_user* irc_chan_get_user (struct irc_chan *chan, const char *nickname)
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   288
{
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   289
    struct irc_chan_user *chan_user = NULL;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   290
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   291
    // look for it...
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   292
    LIST_FOREACH(chan_user, &chan->users, chan_users) {
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   293
        if (irc_cmp_nick(nickname, chan_user->user->nickname) == 0) {
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   294
            // found
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   295
            return chan_user;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   296
        }
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   297
    }
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   298
    
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   299
    // not found
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   300
    return NULL;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   301
}
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 69
diff changeset
   302
26
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   303
err_t irc_chan_join (struct irc_chan *chan)
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   304
{
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   305
    err_t err;
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   306
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   307
    // XXX: error instead?
45
71e65564afd2 remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   308
    assert(!chan->joining && !chan->joined);
71e65564afd2 remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   309
    assert(chan->net->conn);
26
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   310
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   311
    // send JOIN message on the appropriate connection
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   312
    if ((err = irc_conn_JOIN(chan->net->conn, chan->info.channel)))
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   313
        // XXX: error state?
26
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   314
        return err;
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   315
37
4fe4a3c4496e change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   316
    // ok
45
71e65564afd2 remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   317
    chan->joining = true;
26
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   318
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   319
    return SUCCESS;
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   320
}
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   321