1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Jabber module - Main file * |
---|
5 | * * |
---|
6 | * Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
7 | * * |
---|
8 | * This program is free software; you can redistribute it and/or modify * |
---|
9 | * it under the terms of the GNU General Public License as published by * |
---|
10 | * the Free Software Foundation; either version 2 of the License, or * |
---|
11 | * (at your option) any later version. * |
---|
12 | * * |
---|
13 | * This program is distributed in the hope that it will be useful, * |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
16 | * GNU General Public License for more details. * |
---|
17 | * * |
---|
18 | * You should have received a copy of the GNU General Public License along * |
---|
19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
21 | * * |
---|
22 | \***************************************************************************/ |
---|
23 | |
---|
24 | #ifndef _JABBER_H |
---|
25 | #define _JABBER_H |
---|
26 | |
---|
27 | #include <glib.h> |
---|
28 | |
---|
29 | #include "xmltree.h" |
---|
30 | #include "bitlbee.h" |
---|
31 | |
---|
32 | typedef enum |
---|
33 | { |
---|
34 | JFLAG_STREAM_STARTED = 1, /* Set when we detected the beginning of the stream |
---|
35 | and want to do auth. */ |
---|
36 | JFLAG_AUTHENTICATED = 2, /* Set when we're successfully authenticatd. */ |
---|
37 | JFLAG_STREAM_RESTART = 4, /* Set when we want to restart the stream (after |
---|
38 | SASL or TLS). */ |
---|
39 | JFLAG_WAIT_SESSION = 8, /* Set if we sent a <session> tag and need a reply |
---|
40 | before we continue. */ |
---|
41 | JFLAG_WAIT_BIND = 16, /* ... for <bind> tag. */ |
---|
42 | JFLAG_WANT_TYPING = 32, /* Set if we ever sent a typing notification, this |
---|
43 | activates all XEP-85 related code. */ |
---|
44 | JFLAG_XMLCONSOLE = 64, /* If the user added an xmlconsole buddy. */ |
---|
45 | } jabber_flags_t; |
---|
46 | |
---|
47 | typedef enum |
---|
48 | { |
---|
49 | JBFLAG_PROBED_XEP85 = 1, /* Set this when we sent our probe packet to make |
---|
50 | sure it gets sent only once. */ |
---|
51 | JBFLAG_DOES_XEP85 = 2, /* Set this when the resource seems to support |
---|
52 | XEP85 (typing notification shite). */ |
---|
53 | JBFLAG_IS_CHATROOM = 4, /* It's convenient to use this JID thingy for |
---|
54 | groupchat state info too. */ |
---|
55 | JBFLAG_IS_ANONYMOUS = 8, /* For anonymous chatrooms, when we don't have |
---|
56 | have a real JID. */ |
---|
57 | } jabber_buddy_flags_t; |
---|
58 | |
---|
59 | /* Stores a streamhost's(a.k.a. proxy) data */ |
---|
60 | typedef struct |
---|
61 | { |
---|
62 | char *jid; |
---|
63 | char *host; |
---|
64 | char port[6]; |
---|
65 | } jabber_streamhost_t; |
---|
66 | |
---|
67 | struct jabber_data |
---|
68 | { |
---|
69 | struct im_connection *ic; |
---|
70 | |
---|
71 | int fd; |
---|
72 | void *ssl; |
---|
73 | char *txq; |
---|
74 | int tx_len; |
---|
75 | int r_inpa, w_inpa; |
---|
76 | |
---|
77 | struct xt_parser *xt; |
---|
78 | jabber_flags_t flags; |
---|
79 | |
---|
80 | char *username; /* USERNAME@server */ |
---|
81 | char *server; /* username@SERVER -=> server/domain, not hostname */ |
---|
82 | |
---|
83 | /* After changing one of these two (or the priority setting), call |
---|
84 | presence_send_update() to inform the server about the changes. */ |
---|
85 | struct jabber_away_state *away_state; |
---|
86 | char *away_message; |
---|
87 | |
---|
88 | char *cached_id_prefix; |
---|
89 | GHashTable *node_cache; |
---|
90 | GHashTable *buddies; |
---|
91 | |
---|
92 | GSList *filetransfers; |
---|
93 | GSList *streamhosts; |
---|
94 | int have_streamhosts; |
---|
95 | }; |
---|
96 | |
---|
97 | struct jabber_away_state |
---|
98 | { |
---|
99 | char code[5]; |
---|
100 | char *full_name; |
---|
101 | }; |
---|
102 | |
---|
103 | typedef xt_status (*jabber_cache_event) ( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ); |
---|
104 | |
---|
105 | struct jabber_cache_entry |
---|
106 | { |
---|
107 | struct xt_node *node; |
---|
108 | jabber_cache_event func; |
---|
109 | }; |
---|
110 | |
---|
111 | struct jabber_buddy |
---|
112 | { |
---|
113 | char *bare_jid; |
---|
114 | char *full_jid; |
---|
115 | char *resource; |
---|
116 | |
---|
117 | char *ext_jid; /* The JID to use in BitlBee. The real JID if possible, */ |
---|
118 | /* otherwise something similar to the conference JID. */ |
---|
119 | |
---|
120 | int priority; |
---|
121 | struct jabber_away_state *away_state; |
---|
122 | char *away_message; |
---|
123 | GSList *features; |
---|
124 | |
---|
125 | time_t last_act; |
---|
126 | jabber_buddy_flags_t flags; |
---|
127 | |
---|
128 | struct jabber_buddy *next; |
---|
129 | }; |
---|
130 | |
---|
131 | struct jabber_chat |
---|
132 | { |
---|
133 | int flags; |
---|
134 | char *name; |
---|
135 | char *my_full_jid; /* Separate copy because of case sensitivity. */ |
---|
136 | struct jabber_buddy *me; |
---|
137 | }; |
---|
138 | |
---|
139 | struct jabber_transfer |
---|
140 | { |
---|
141 | /* bitlbee's handle for this transfer */ |
---|
142 | file_transfer_t *ft; |
---|
143 | |
---|
144 | /* the stream's private handle */ |
---|
145 | gpointer streamhandle; |
---|
146 | |
---|
147 | struct im_connection *ic; |
---|
148 | |
---|
149 | int watch_in; |
---|
150 | int watch_out; |
---|
151 | |
---|
152 | char *ini_jid; |
---|
153 | char *tgt_jid; |
---|
154 | char *iq_id; |
---|
155 | char *sid; |
---|
156 | int accepted; |
---|
157 | |
---|
158 | size_t bytesread, byteswritten; |
---|
159 | int fd; |
---|
160 | struct sockaddr_storage saddr; |
---|
161 | }; |
---|
162 | |
---|
163 | #define JABBER_XMLCONSOLE_HANDLE "xmlconsole" |
---|
164 | |
---|
165 | #define JABBER_PORT_DEFAULT "5222" |
---|
166 | #define JABBER_PORT_MIN 5220 |
---|
167 | #define JABBER_PORT_MAX 5229 |
---|
168 | |
---|
169 | /* Prefixes to use for packet IDs (mainly for IQ packets ATM). Usually the |
---|
170 | first one should be used, but when storing a packet in the cache, a |
---|
171 | "special" kind of ID is assigned to make it easier later to figure out |
---|
172 | if we have to do call an event handler for the response packet. Also |
---|
173 | we'll append a hash to make sure we won't trigger on cached packets from |
---|
174 | other BitlBee users. :-) */ |
---|
175 | #define JABBER_PACKET_ID "BeeP" |
---|
176 | #define JABBER_CACHED_ID "BeeC" |
---|
177 | |
---|
178 | /* RFC 392[01] stuff */ |
---|
179 | #define XMLNS_TLS "urn:ietf:params:xml:ns:xmpp-tls" |
---|
180 | #define XMLNS_SASL "urn:ietf:params:xml:ns:xmpp-sasl" |
---|
181 | #define XMLNS_BIND "urn:ietf:params:xml:ns:xmpp-bind" |
---|
182 | #define XMLNS_SESSION "urn:ietf:params:xml:ns:xmpp-session" |
---|
183 | #define XMLNS_STANZA_ERROR "urn:ietf:params:xml:ns:xmpp-stanzas" |
---|
184 | #define XMLNS_STREAM_ERROR "urn:ietf:params:xml:ns:xmpp-streams" |
---|
185 | #define XMLNS_ROSTER "jabber:iq:roster" |
---|
186 | |
---|
187 | /* Some supported extensions/legacy stuff */ |
---|
188 | #define XMLNS_AUTH "jabber:iq:auth" /* XEP-0078 */ |
---|
189 | #define XMLNS_VERSION "jabber:iq:version" /* XEP-0092 */ |
---|
190 | #define XMLNS_TIME "jabber:iq:time" /* XEP-0090 */ |
---|
191 | #define XMLNS_PING "urn:xmpp:ping" /* XEP-0199 */ |
---|
192 | #define XMLNS_VCARD "vcard-temp" /* XEP-0054 */ |
---|
193 | #define XMLNS_DELAY "jabber:x:delay" /* XEP-0091 */ |
---|
194 | #define XMLNS_XDATA "jabber:x:data" /* XEP-0004 */ |
---|
195 | #define XMLNS_CHATSTATES "http://jabber.org/protocol/chatstates" /* XEP-0085 */ |
---|
196 | #define XMLNS_DISCO_INFO "http://jabber.org/protocol/disco#info" /* XEP-0030 */ |
---|
197 | #define XMLNS_DISCO_ITEMS "http://jabber.org/protocol/disco#items" /* XEP-0030 */ |
---|
198 | #define XMLNS_MUC "http://jabber.org/protocol/muc" /* XEP-0045 */ |
---|
199 | #define XMLNS_MUC_USER "http://jabber.org/protocol/muc#user" /* XEP-0045 */ |
---|
200 | #define XMLNS_FEATURE "http://jabber.org/protocol/feature-neg" /* XEP-0020 */ |
---|
201 | #define XMLNS_SI "http://jabber.org/protocol/si" /* XEP-0095 */ |
---|
202 | #define XMLNS_FILETRANSFER "http://jabber.org/protocol/si/profile/file-transfer" /* XEP-0096 */ |
---|
203 | #define XMLNS_BYTESTREAMS "http://jabber.org/protocol/bytestreams" /* XEP-0065 */ |
---|
204 | #define XMLNS_IBB "http://jabber.org/protocol/ibb" /* XEP-0047 */ |
---|
205 | |
---|
206 | /* iq.c */ |
---|
207 | xt_status jabber_pkt_iq( struct xt_node *node, gpointer data ); |
---|
208 | int jabber_init_iq_auth( struct im_connection *ic ); |
---|
209 | xt_status jabber_pkt_bind_sess( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ); |
---|
210 | int jabber_get_roster( struct im_connection *ic ); |
---|
211 | int jabber_get_vcard( struct im_connection *ic, char *bare_jid ); |
---|
212 | int jabber_add_to_roster( struct im_connection *ic, char *handle, char *name ); |
---|
213 | int jabber_remove_from_roster( struct im_connection *ic, char *handle ); |
---|
214 | xt_status jabber_iq_query_features( struct im_connection *ic, char *bare_jid ); |
---|
215 | xt_status jabber_iq_query_server( struct im_connection *ic, char *jid, char *xmlns ); |
---|
216 | |
---|
217 | /* si.c */ |
---|
218 | int jabber_si_handle_request( struct im_connection *ic, struct xt_node *node, struct xt_node *sinode ); |
---|
219 | void jabber_si_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *who ); |
---|
220 | void jabber_si_free_transfer( file_transfer_t *ft); |
---|
221 | |
---|
222 | /* s5bytestream.c */ |
---|
223 | int jabber_bs_recv_request( struct im_connection *ic, struct xt_node *node, struct xt_node *qnode); |
---|
224 | gboolean jabber_bs_send_start( struct jabber_transfer *tf ); |
---|
225 | gboolean jabber_bs_send_write( file_transfer_t *ft, char *buffer, unsigned int len ); |
---|
226 | |
---|
227 | /* message.c */ |
---|
228 | xt_status jabber_pkt_message( struct xt_node *node, gpointer data ); |
---|
229 | |
---|
230 | /* presence.c */ |
---|
231 | xt_status jabber_pkt_presence( struct xt_node *node, gpointer data ); |
---|
232 | int presence_send_update( struct im_connection *ic ); |
---|
233 | int presence_send_request( struct im_connection *ic, char *handle, char *request ); |
---|
234 | |
---|
235 | /* jabber_util.c */ |
---|
236 | char *set_eval_priority( set_t *set, char *value ); |
---|
237 | char *set_eval_tls( set_t *set, char *value ); |
---|
238 | struct xt_node *jabber_make_packet( char *name, char *type, char *to, struct xt_node *children ); |
---|
239 | struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type, char *err_code ); |
---|
240 | void jabber_cache_add( struct im_connection *ic, struct xt_node *node, jabber_cache_event func ); |
---|
241 | struct xt_node *jabber_cache_get( struct im_connection *ic, char *id ); |
---|
242 | void jabber_cache_entry_free( gpointer entry ); |
---|
243 | void jabber_cache_clean( struct im_connection *ic ); |
---|
244 | xt_status jabber_cache_handle_packet( struct im_connection *ic, struct xt_node *node ); |
---|
245 | const struct jabber_away_state *jabber_away_state_by_code( char *code ); |
---|
246 | const struct jabber_away_state *jabber_away_state_by_name( char *name ); |
---|
247 | void jabber_buddy_ask( struct im_connection *ic, char *handle ); |
---|
248 | char *jabber_normalize( const char *orig ); |
---|
249 | |
---|
250 | typedef enum |
---|
251 | { |
---|
252 | GET_BUDDY_CREAT = 1, /* Try to create it, if necessary. */ |
---|
253 | GET_BUDDY_EXACT = 2, /* Get an exact match (only makes sense with bare JIDs). */ |
---|
254 | GET_BUDDY_FIRST = 4, /* No selection, simply get the first resource for this JID. */ |
---|
255 | } get_buddy_flags_t; |
---|
256 | |
---|
257 | struct jabber_error |
---|
258 | { |
---|
259 | char *code, *text, *type; |
---|
260 | }; |
---|
261 | |
---|
262 | struct jabber_buddy *jabber_buddy_add( struct im_connection *ic, char *full_jid ); |
---|
263 | struct jabber_buddy *jabber_buddy_by_jid( struct im_connection *ic, char *jid, get_buddy_flags_t flags ); |
---|
264 | struct jabber_buddy *jabber_buddy_by_ext_jid( struct im_connection *ic, char *jid, get_buddy_flags_t flags ); |
---|
265 | int jabber_buddy_remove( struct im_connection *ic, char *full_jid ); |
---|
266 | int jabber_buddy_remove_bare( struct im_connection *ic, char *bare_jid ); |
---|
267 | time_t jabber_get_timestamp( struct xt_node *xt ); |
---|
268 | struct jabber_error *jabber_error_parse( struct xt_node *node, char *xmlns ); |
---|
269 | void jabber_error_free( struct jabber_error *err ); |
---|
270 | |
---|
271 | extern const struct jabber_away_state jabber_away_state_list[]; |
---|
272 | |
---|
273 | /* io.c */ |
---|
274 | int jabber_write_packet( struct im_connection *ic, struct xt_node *node ); |
---|
275 | int jabber_write( struct im_connection *ic, char *buf, int len ); |
---|
276 | gboolean jabber_connected_plain( gpointer data, gint source, b_input_condition cond ); |
---|
277 | gboolean jabber_connected_ssl( gpointer data, void *source, b_input_condition cond ); |
---|
278 | gboolean jabber_start_stream( struct im_connection *ic ); |
---|
279 | void jabber_end_stream( struct im_connection *ic ); |
---|
280 | |
---|
281 | /* sasl.c */ |
---|
282 | xt_status sasl_pkt_mechanisms( struct xt_node *node, gpointer data ); |
---|
283 | xt_status sasl_pkt_challenge( struct xt_node *node, gpointer data ); |
---|
284 | xt_status sasl_pkt_result( struct xt_node *node, gpointer data ); |
---|
285 | gboolean sasl_supported( struct im_connection *ic ); |
---|
286 | |
---|
287 | /* conference.c */ |
---|
288 | struct groupchat *jabber_chat_join( struct im_connection *ic, char *room, char *nick, char *password ); |
---|
289 | struct groupchat *jabber_chat_by_jid( struct im_connection *ic, const char *name ); |
---|
290 | void jabber_chat_free( struct groupchat *c ); |
---|
291 | int jabber_chat_msg( struct groupchat *ic, char *message, int flags ); |
---|
292 | int jabber_chat_topic( struct groupchat *c, char *topic ); |
---|
293 | int jabber_chat_leave( struct groupchat *c, const char *reason ); |
---|
294 | void jabber_chat_pkt_presence( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node ); |
---|
295 | void jabber_chat_pkt_message( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node ); |
---|
296 | void jabber_chat_invite( struct groupchat *c, char *who, char *message ); |
---|
297 | |
---|
298 | #endif |
---|