author | Tero Marttila <terom@fixme.fi> |
Tue, 31 Mar 2009 20:57:07 +0300 | |
changeset 97 | d3bc82ee76cb |
parent 89 | 68345a9b99a3 |
child 127 | 94e6c3b4230f |
permissions | -rw-r--r-- |
26 | 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 | 4 |
|
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 | 7 |
#include <assert.h> |
8 |
||
9 |
const char* irc_chan_name (struct irc_chan *chan) |
|
10 |
{ |
|
11 |
return chan->info.channel; |
|
12 |
} |
|
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 |
/** |
87 | 15 |
* Invoke the named irc_chan_callbacks field with the given args |
16 |
*/ |
|
17 |
#define IRC_CHAN_INVOKE_CALLBACK(chan, _cb_name_, ...) \ |
|
18 |
do { \ |
|
19 |
struct chain_head *head; \ |
|
20 |
\ |
|
21 |
CHAIN_FOREACH(&(chan)->callbacks, head) { \ |
|
22 |
const struct irc_chan_callbacks *callbacks = head->chain; \ |
|
23 |
\ |
|
24 |
if (callbacks->_cb_name_) \ |
|
25 |
callbacks->_cb_name_((chan), ## __VA_ARGS__, head->arg); \ |
|
26 |
} \ |
|
27 |
} while (0); |
|
28 |
||
29 |
/** |
|
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
30 |
* 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
|
31 |
* |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
32 |
* 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
|
33 |
* 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
|
34 |
*/ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
35 |
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
|
36 |
{ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
37 |
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
|
38 |
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
|
39 |
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
|
40 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
41 |
// 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
|
42 |
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
|
43 |
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
|
44 |
|
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
|
45 |
// 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
|
46 |
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
|
47 |
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
|
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 |
// 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
|
50 |
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
|
51 |
// 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
|
52 |
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
|
53 |
} |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
54 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
55 |
// store |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
56 |
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
|
57 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
58 |
// 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
|
59 |
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
|
60 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
61 |
// ok |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
62 |
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
|
63 |
} |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
64 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
65 |
/** |
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
|
66 |
* 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
|
67 |
*/ |
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
|
68 |
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
|
69 |
{ |
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
|
70 |
// 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
|
71 |
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
|
72 |
|
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
|
73 |
// 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
|
74 |
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
|
75 |
|
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
|
76 |
// 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
|
77 |
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
|
78 |
} |
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
|
79 |
|
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
|
80 |
/** |
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
|
81 |
* :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
|
82 |
*/ |
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
|
83 |
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
|
84 |
{ |
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
|
85 |
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
|
86 |
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
|
87 |
|
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
|
88 |
// us? |
75
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
74
diff
changeset
|
89 |
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
|
90 |
// 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
|
91 |
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
|
92 |
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
|
93 |
|
38
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
94 |
// invoke callback |
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
95 |
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
|
96 |
|
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
|
97 |
} 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
|
98 |
// add them |
75
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
74
diff
changeset
|
99 |
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
|
100 |
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
|
101 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
102 |
// invoke callback (source) |
75
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
74
diff
changeset
|
103 |
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
|
104 |
} |
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
105 |
} |
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
|
106 |
|
38
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
107 |
/** |
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
108 |
* :nm PRIVMSG <channel> <message> |
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
109 |
*/ |
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
110 |
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
|
111 |
{ |
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
112 |
struct irc_chan *chan = arg; |
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
113 |
|
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
|
114 |
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
|
115 |
|
75
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
74
diff
changeset
|
116 |
// invoke callback (source, message) |
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
74
diff
changeset
|
117 |
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
|
118 |
} |
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
|
119 |
|
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
|
120 |
/** |
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
121 |
* 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
|
122 |
* |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
123 |
* @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
|
124 |
*/ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
125 |
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
|
126 |
{ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
127 |
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
|
128 |
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
|
129 |
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
|
130 |
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
|
131 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
132 |
// 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
|
133 |
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
|
134 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
135 |
// 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
|
136 |
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
|
137 |
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
|
138 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
139 |
// 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
|
140 |
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
|
141 |
// 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
|
142 |
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
|
143 |
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
|
144 |
|
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
145 |
// 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
|
146 |
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
|
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 |
// 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
|
149 |
// 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
|
150 |
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
|
151 |
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
|
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 |
} |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
154 |
|
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 |
* 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
|
157 |
* |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
158 |
* @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
|
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 |
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
|
161 |
{ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
162 |
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
|
163 |
|
97
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
164 |
(void) line; |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
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 |
// 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
|
167 |
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
|
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 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
170 |
/** |
89 | 171 |
* Someone, or us ourselves, left a channel |
172 |
* |
|
173 |
* :nm PART <channel> [<message>] |
|
174 |
*/ |
|
175 |
static void irc_chan_on_PART (const struct irc_line *line, void *arg) |
|
176 |
{ |
|
177 |
struct irc_chan *chan = arg; |
|
178 |
||
179 |
const char *msg = line->args[1]; |
|
180 |
||
181 |
// us? |
|
182 |
if (irc_cmp_nick(line->source->nickname, chan->net->conn->nickname) == 0) { |
|
183 |
// twiddle state |
|
184 |
chan->joined = false; |
|
185 |
chan->parted = true; |
|
186 |
||
187 |
// invoke callback |
|
188 |
IRC_CHAN_INVOKE_CALLBACK(chan, on_self_part); |
|
189 |
||
190 |
// XXX: cleanup |
|
191 |
||
192 |
} else { |
|
193 |
// someone else |
|
194 |
struct irc_chan_user *chan_user; |
|
195 |
||
196 |
// invoke callback (source, msg) |
|
197 |
IRC_CHAN_INVOKE_CALLBACK(chan, on_part, line->source, msg); |
|
198 |
||
199 |
// look up the irc_chan_user |
|
200 |
if ((chan_user = irc_chan_get_user(chan, line->source->nickname)) == NULL) |
|
201 |
return log_warn("PART'd user not on channel: %s, %s", irc_chan_name(chan), line->source->nickname); |
|
202 |
||
203 |
// remove them |
|
204 |
irc_chan_remove_user(chan, chan_user); |
|
205 |
} |
|
206 |
} |
|
207 |
||
208 |
/** |
|
209 |
* :nm KICK <channel> <target> [<reason>] |
|
210 |
* |
|
211 |
* User kicked some other user, might be us |
|
212 |
*/ |
|
213 |
static void irc_chan_on_KICK (const struct irc_line *line, void *arg) |
|
214 |
{ |
|
215 |
struct irc_chan *chan = arg; |
|
216 |
struct irc_chan_user *target_user; |
|
217 |
||
218 |
const char *target = line->args[1]; |
|
219 |
const char *msg = line->args[2]; |
|
220 |
||
221 |
// us? |
|
222 |
if (irc_cmp_nick(target, chan->net->conn->nickname) == 0) { |
|
223 |
// twiddle state |
|
224 |
chan->joined = false; |
|
225 |
chan->kicked = true; |
|
226 |
||
227 |
// invoke callback (source, msg) |
|
228 |
IRC_CHAN_INVOKE_CALLBACK(chan, on_self_kicked, line->source, msg); |
|
229 |
||
230 |
// XXX: cleanup |
|
231 |
||
232 |
} else { |
|
233 |
// look up the target irc_chan_user |
|
234 |
if ((target_user = irc_chan_get_user(chan, target)) == NULL) |
|
235 |
return log_warn("KICK'd user not on channel: %s, %s", irc_chan_name(chan), target); |
|
236 |
||
237 |
// invoke callback (source, target_user, msg) |
|
238 |
IRC_CHAN_INVOKE_CALLBACK(chan, on_kick, line->source, target_user, msg); |
|
239 |
||
240 |
// remove them |
|
241 |
irc_chan_remove_user(chan, target_user); |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
/** |
|
78
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
246 |
* :nm QUIT [<message>] |
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
247 |
* |
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
248 |
* 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
|
249 |
*/ |
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
250 |
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
|
251 |
{ |
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
252 |
struct irc_chan *chan = arg; |
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
253 |
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
|
254 |
|
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
255 |
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
|
256 |
|
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
257 |
// invoke callback (source, msg) |
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
258 |
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
|
259 |
|
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
260 |
// 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
|
261 |
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
|
262 |
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
|
263 |
|
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
264 |
// remove them |
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
265 |
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
|
266 |
} |
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
267 |
|
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
268 |
/** |
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
|
269 |
* 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
|
270 |
*/ |
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
|
271 |
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
|
272 |
{ "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
|
273 |
{ 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
|
274 |
{ 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
|
275 |
{ "PRIVMSG", &irc_chan_on_PRIVMSG }, |
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
276 |
{ "PART", &irc_chan_on_PART }, |
89 | 277 |
{ "KICK", &irc_chan_on_KICK }, |
78
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
278 |
{ "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
|
279 |
{ 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
|
280 |
}; |
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
|
281 |
|
26 | 282 |
err_t irc_chan_create (struct irc_chan **chan_ptr, struct irc_net *net, const struct irc_chan_info *info, struct error_info *err) |
283 |
{ |
|
284 |
struct irc_chan *chan; |
|
285 |
||
286 |
// allocate |
|
287 |
if ((chan = calloc(1, sizeof(*chan))) == NULL) |
|
288 |
return SET_ERROR(err, ERR_CALLOC); |
|
289 |
||
290 |
// store |
|
291 |
chan->net = net; |
|
292 |
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
|
293 |
|
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
|
294 |
// 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
|
295 |
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
|
296 |
irc_cmd_init(&chan->handlers); |
38
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
297 |
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
|
298 |
|
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
|
299 |
// 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
|
300 |
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
|
301 |
goto error; |
26 | 302 |
|
303 |
// ok |
|
304 |
*chan_ptr = chan; |
|
305 |
||
306 |
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
|
307 |
|
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
|
308 |
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
|
309 |
// 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
|
310 |
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
|
311 |
|
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
|
312 |
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
|
313 |
} |
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
|
314 |
|
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
|
315 |
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
|
316 |
{ |
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
|
317 |
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
|
318 |
|
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
|
319 |
// 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
|
320 |
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
|
321 |
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
|
322 |
} |
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
323 |
|
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
|
324 |
// 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
|
325 |
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
|
326 |
|
38
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
327 |
// free callbacks |
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
328 |
chain_free(&chan->callbacks); |
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
329 |
|
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
|
330 |
// 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
|
331 |
free(chan); |
26 | 332 |
} |
333 |
||
38
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
334 |
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
|
335 |
{ |
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
336 |
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
|
337 |
} |
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
338 |
|
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
|
339 |
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
|
340 |
{ |
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
|
341 |
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
|
342 |
} |
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
|
343 |
|
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
344 |
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
|
345 |
{ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
346 |
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
|
347 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
348 |
// 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
|
349 |
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
|
350 |
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
|
351 |
// found |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
352 |
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
|
353 |
} |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
354 |
} |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
355 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
356 |
// 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
|
357 |
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
|
358 |
} |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
359 |
|
26 | 360 |
err_t irc_chan_join (struct irc_chan *chan) |
361 |
{ |
|
362 |
err_t err; |
|
97
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
363 |
|
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
364 |
// correct state |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
365 |
if (chan->joining || chan->joined) |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
366 |
return ERR_IRC_CHAN_STATE; |
26 | 367 |
|
97
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
368 |
if (!chan->net->conn) |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
369 |
return ERR_IRC_NET_STATE; |
26 | 370 |
|
371 |
// send JOIN message on the appropriate connection |
|
372 |
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
|
373 |
// XXX: error state? |
26 | 374 |
return err; |
375 |
||
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
|
376 |
// 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
|
377 |
chan->joining = true; |
26 | 378 |
|
379 |
return SUCCESS; |
|
380 |
} |
|
381 |
||
97
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
382 |
err_t irc_chan_PRIVMSG (struct irc_chan *chan, const char *message) |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
383 |
{ |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
384 |
// correct state |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
385 |
if (!chan->joined) |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
386 |
return ERR_IRC_CHAN_STATE; |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
387 |
|
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
388 |
if (!chan->net->conn) |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
389 |
return ERR_IRC_NET_STATE; |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
390 |
|
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
391 |
// send the PRIVMSG message |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
392 |
return irc_conn_PRIVMSG(chan->net->conn, chan->info.channel, message); |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
393 |
} |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
394 |