author | Tero Marttila <terom@fixme.fi> |
Tue, 31 Mar 2009 22:09:53 +0300 | |
changeset 98 | f357f835f0d5 |
parent 97 | d3bc82ee76cb |
child 127 | 94e6c3b4230f |
permissions | -rw-r--r-- |
26 | 1 |
#ifndef IRC_CHAN_H |
2 |
#define IRC_CHAN_H |
|
3 |
||
4 |
/** |
|
5 |
* @file |
|
6 |
* |
|
7 |
* Support for IRC channels, including tracking their state and actions on them. |
|
8 |
*/ |
|
9 |
struct irc_chan_info; |
|
10 |
struct irc_chan; |
|
11 |
||
12 |
#include "irc_net.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
|
13 |
#include "irc_user.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
|
14 |
#include "irc_cmd.h" |
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:
38
diff
changeset
|
15 |
#include "irc_proto.h" |
26 | 16 |
#include "error.h" |
17 |
#include <sys/queue.h> |
|
18 |
||
19 |
/** |
|
20 |
* IRC channel info, as required to create an irc_chan |
|
21 |
*/ |
|
22 |
struct irc_chan_info { |
|
23 |
/** Channel name, with the [#&!] prefix */ |
|
24 |
const char *channel; |
|
25 |
||
26 |
}; |
|
27 |
||
28 |
/** |
|
89 | 29 |
* Per-channel-user state. This is used to store the list of irc_user's on an irc_chan. |
30 |
* |
|
31 |
* The nickname is accessible via user->nickname. |
|
32 |
*/ |
|
33 |
struct irc_chan_user { |
|
34 |
/** The per-network user info */ |
|
35 |
struct irc_user *user; |
|
36 |
||
37 |
/** The irc_chan list */ |
|
38 |
LIST_ENTRY(irc_chan_user) chan_users; |
|
39 |
}; |
|
40 |
||
41 |
/** |
|
87 | 42 |
* High-level IRC channel callbacks for convenience. |
43 |
* |
|
44 |
* These are based on the normal irc_chan::handlers interface, and you can use both freely. |
|
45 |
* |
|
46 |
* The on_self_* handlers are for actions that affect our own status on the channel, the rest are only for actions |
|
47 |
* done by other users. |
|
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:
38
diff
changeset
|
48 |
* |
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:
38
diff
changeset
|
49 |
* Where callbacks have irc_nm arguments, these are MAY be given as NULL if the prefix could not be parsed. |
26 | 50 |
*/ |
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
|
51 |
struct irc_chan_callbacks { |
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
|
52 |
/** Joined the 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
|
53 |
void (*on_self_join) (struct irc_chan *chan, void *arg); |
38
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
54 |
|
74
11ec458d1cbf
add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
55 |
/** We parted the channel */ |
11ec458d1cbf
add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
56 |
void (*on_self_part) (struct irc_chan *chan, void *arg); |
11ec458d1cbf
add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
57 |
|
89 | 58 |
/** We were kicked from the channel */ |
59 |
void (*on_self_kicked) (struct irc_chan *chan, const struct irc_nm *source, const char *msg, void *arg); |
|
60 |
||
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
61 |
/** Someone joined the channel */ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
62 |
void (*on_join) (struct irc_chan *chan, const struct irc_nm *source, 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
|
63 |
|
74
11ec458d1cbf
add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
64 |
/** Someone parted the channel. The irc_chan_user is still present */ |
11ec458d1cbf
add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
65 |
void (*on_part) (struct irc_chan *chan, const struct irc_nm *source, const char *msg, void *arg); |
11ec458d1cbf
add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
66 |
|
89 | 67 |
/** Someone was kicked from the channel. The irc_chan_user is still present */ |
68 |
void (*on_kick) (struct irc_chan *chan, const struct irc_nm *source, struct irc_chan_user *target, const char *msg, void *arg); |
|
69 |
||
78
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
74
diff
changeset
|
70 |
/** Someone quit the channel. The irc_chan_user is still present */ |
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
74
diff
changeset
|
71 |
void (*on_quit) (struct irc_chan *chan, const struct irc_nm *source, const char *msg, void *arg); |
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
74
diff
changeset
|
72 |
|
38
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
73 |
/** Someone sent a message to the channel */ |
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:
38
diff
changeset
|
74 |
void (*on_msg) (struct irc_chan *chan, const struct irc_nm *source, const char *msg, void *arg); |
26 | 75 |
}; |
76 |
||
77 |
/** |
|
87 | 78 |
* Persistent IRC channel state, part of irc_net. |
79 |
* |
|
80 |
* This stores the channel's info, status flags, users list, and handlers/callbacks. |
|
81 |
* |
|
82 |
* Create/get channels using the irc_net_add_chan()/irc_net_get_chan() interface, and then attach your own |
|
83 |
* callbacks/handlers using irc_chan_add_callbacks()/irc_chan::handlers and irc_cmd_add(). |
|
84 |
* |
|
85 |
* Note that irc_net will propagate all relevant events for each channel. |
|
26 | 86 |
*/ |
87 |
struct irc_chan { |
|
87 | 88 |
/** The network we're part of */ |
26 | 89 |
struct irc_net *net; |
90 |
||
91 |
/** Our identifying info */ |
|
92 |
struct irc_chan_info info; |
|
93 |
||
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
94 |
/** |
87 | 95 |
* @defgroup irc_chan_state State flags |
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
96 |
* @{ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
97 |
*/ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
98 |
/** JOIN request sent, waiting for reply */ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
99 |
bool joining; |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
100 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
101 |
/** Currently joined to channel */ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
102 |
bool joined; |
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
|
103 |
|
74
11ec458d1cbf
add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
104 |
/** We PART'd the channel */ |
11ec458d1cbf
add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
105 |
bool parted; |
11ec458d1cbf
add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
106 |
|
89 | 107 |
/** We were KICK'd from the channel */ |
108 |
bool kicked; |
|
109 |
||
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:
38
diff
changeset
|
110 |
// @} |
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:
38
diff
changeset
|
111 |
|
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
112 |
/** List of users on channel */ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
113 |
LIST_HEAD(irc_chan_users_list, irc_chan_user) users; |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
114 |
|
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
|
115 |
/** General 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
|
116 |
irc_cmd_handlers_t handlers; |
38
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
117 |
|
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
118 |
/** High-level user callbacks */ |
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
119 |
struct chain_list callbacks; |
87 | 120 |
|
121 |
/** The irc_net::channels list */ |
|
122 |
TAILQ_ENTRY(irc_chan) net_channels; |
|
26 | 123 |
}; |
124 |
||
125 |
/** |
|
126 |
* Return the channel's name |
|
127 |
*/ |
|
128 |
const char* irc_chan_name (struct irc_chan *chan); |
|
129 |
||
130 |
/** |
|
131 |
* Build/initialize a new irc_chan struct. This does not have any external side-effects. |
|
132 |
* |
|
133 |
* The channel will be in the IRC_CHAN_INIT state after this. |
|
134 |
* |
|
135 |
* @param chan_ptr the new irc_chan is returned via this pointer |
|
136 |
* @param net the irc_net this channel is on |
|
137 |
* @param info the channel's identifying information |
|
138 |
* @param err error codes are returned via this |
|
139 |
*/ |
|
140 |
err_t irc_chan_create (struct irc_chan **chan_ptr, struct irc_net *net, const struct irc_chan_info *info, struct error_info *err); |
|
141 |
||
142 |
/** |
|
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
|
143 |
* Destroy irc_chan state, without doing anything to the network |
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
|
144 |
*/ |
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
|
145 |
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
|
146 |
|
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
|
147 |
/** |
87 | 148 |
* Add a set of high-level irc_chan callbacks. Not all callbacks need to be implemented. |
38
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
149 |
*/ |
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
150 |
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
|
151 |
|
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
152 |
/** |
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:
45
diff
changeset
|
153 |
* Remove high-level irc_chan callbacks. |
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:
45
diff
changeset
|
154 |
*/ |
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:
45
diff
changeset
|
155 |
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:
45
diff
changeset
|
156 |
|
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:
45
diff
changeset
|
157 |
/** |
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
158 |
* Look up an irc_chan_user struct by nickname for this channel, returning NULL if no such chan_user exists. |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
159 |
* |
87 | 160 |
* Be careful not to hold on to this, as the irc_chan_user struct may be released if the user leaves the channel. |
161 |
* |
|
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
162 |
* @param chan the channel context |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
163 |
* @param nickname the user's current nickname |
87 | 164 |
* @return the irc_chan_user state, or NULL if nickname not found |
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
165 |
*/ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
166 |
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
|
167 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
168 |
/** |
26 | 169 |
* Send the initial JOIN message. |
170 |
* |
|
171 |
* @param chan the channel to JOIN |
|
172 |
*/ |
|
173 |
err_t irc_chan_join (struct irc_chan *chan); |
|
174 |
||
97
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
175 |
/** |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
176 |
* Send a normal PRIVMSG to the channel. If we're being pedantic, one should use NOTICE instead for messages sent in |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
177 |
* reply to PRIVMSG's, but the real world is different. |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
178 |
* |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
179 |
* @param chan the IRC channel |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
180 |
* @param message the message to send |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
181 |
*/ |
d3bc82ee76cb
add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents:
89
diff
changeset
|
182 |
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
|
183 |
|
26 | 184 |
#endif |