author | Tero Marttila <terom@fixme.fi> |
Mon, 30 Mar 2009 01:31:27 +0300 | |
changeset 87 | f0db6ebf18b9 |
parent 78 | 941bb8379d3d |
child 89 | 68345a9b99a3 |
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 |
/** |
|
87 | 29 |
* High-level IRC channel callbacks for convenience. |
30 |
* |
|
31 |
* These are based on the normal irc_chan::handlers interface, and you can use both freely. |
|
32 |
* |
|
33 |
* The on_self_* handlers are for actions that affect our own status on the channel, the rest are only for actions |
|
34 |
* 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
|
35 |
* |
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
|
36 |
* Where callbacks have irc_nm arguments, these are MAY be given as NULL if the prefix could not be parsed. |
26 | 37 |
*/ |
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
|
38 |
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
|
39 |
/** 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
|
40 |
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
|
41 |
|
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
|
42 |
/** 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
|
43 |
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
|
44 |
|
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
45 |
/** 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
|
46 |
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
|
47 |
|
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
|
48 |
/** 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
|
49 |
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
|
50 |
|
78
941bb8379d3d
implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents:
74
diff
changeset
|
51 |
/** 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
|
52 |
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
|
53 |
|
38
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
54 |
/** 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
|
55 |
void (*on_msg) (struct irc_chan *chan, const struct irc_nm *source, const char *msg, void *arg); |
26 | 56 |
}; |
57 |
||
58 |
/** |
|
87 | 59 |
* Per-channel-user state. This is used to store the list of irc_user's on an irc_chan. |
60 |
* |
|
61 |
* The nickname is accessible via user->nickname. |
|
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
62 |
*/ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
63 |
struct 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
|
64 |
/** The per-network user info */ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
65 |
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
|
66 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
67 |
/** The irc_chan list */ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
68 |
LIST_ENTRY(irc_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
|
69 |
}; |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
70 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
71 |
/** |
87 | 72 |
* Persistent IRC channel state, part of irc_net. |
73 |
* |
|
74 |
* This stores the channel's info, status flags, users list, and handlers/callbacks. |
|
75 |
* |
|
76 |
* Create/get channels using the irc_net_add_chan()/irc_net_get_chan() interface, and then attach your own |
|
77 |
* callbacks/handlers using irc_chan_add_callbacks()/irc_chan::handlers and irc_cmd_add(). |
|
78 |
* |
|
79 |
* Note that irc_net will propagate all relevant events for each channel. |
|
26 | 80 |
*/ |
81 |
struct irc_chan { |
|
87 | 82 |
/** The network we're part of */ |
26 | 83 |
struct irc_net *net; |
84 |
||
85 |
/** Our identifying info */ |
|
86 |
struct irc_chan_info info; |
|
87 |
||
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
88 |
/** |
87 | 89 |
* @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
|
90 |
* @{ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
91 |
*/ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
92 |
/** 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
|
93 |
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
|
94 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
95 |
/** 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
|
96 |
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
|
97 |
|
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
|
98 |
/** 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
|
99 |
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
|
100 |
|
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
|
101 |
// @} |
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
|
102 |
|
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
103 |
/** 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
|
104 |
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
|
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 |
/** 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
|
107 |
irc_cmd_handlers_t handlers; |
38
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
108 |
|
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
109 |
/** High-level user callbacks */ |
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
110 |
struct chain_list callbacks; |
87 | 111 |
|
112 |
/** The irc_net::channels list */ |
|
113 |
TAILQ_ENTRY(irc_chan) net_channels; |
|
26 | 114 |
}; |
115 |
||
116 |
/** |
|
117 |
* Return the channel's name |
|
118 |
*/ |
|
119 |
const char* irc_chan_name (struct irc_chan *chan); |
|
120 |
||
121 |
/** |
|
122 |
* Build/initialize a new irc_chan struct. This does not have any external side-effects. |
|
123 |
* |
|
124 |
* The channel will be in the IRC_CHAN_INIT state after this. |
|
125 |
* |
|
126 |
* @param chan_ptr the new irc_chan is returned via this pointer |
|
127 |
* @param net the irc_net this channel is on |
|
128 |
* @param info the channel's identifying information |
|
129 |
* @param err error codes are returned via this |
|
130 |
*/ |
|
131 |
err_t irc_chan_create (struct irc_chan **chan_ptr, struct irc_net *net, const struct irc_chan_info *info, struct error_info *err); |
|
132 |
||
133 |
/** |
|
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
|
134 |
* 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
|
135 |
*/ |
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
|
136 |
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
|
137 |
|
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
|
138 |
/** |
87 | 139 |
* 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
|
140 |
*/ |
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
141 |
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
|
142 |
|
0c2e0cb46c3a
implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
143 |
/** |
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
|
144 |
* 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
|
145 |
*/ |
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
|
146 |
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
|
147 |
|
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
|
148 |
/** |
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
149 |
* 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
|
150 |
* |
87 | 151 |
* Be careful not to hold on to this, as the irc_chan_user struct may be released if the user leaves the channel. |
152 |
* |
|
72
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
153 |
* @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
|
154 |
* @param nickname the user's current nickname |
87 | 155 |
* @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
|
156 |
*/ |
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
157 |
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
|
158 |
|
43084f103c2a
add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents:
69
diff
changeset
|
159 |
/** |
26 | 160 |
* Send the initial JOIN message. |
161 |
* |
|
162 |
* @param chan the channel to JOIN |
|
163 |
*/ |
|
164 |
err_t irc_chan_join (struct irc_chan *chan); |
|
165 |
||
166 |
#endif |