1 /***************************************************************************\
2 *                                                                           *
3 *  BitlBee - An IRC to IM gateway                                           *
4 *  Jabber module - Main file                                                *
5 *                                                                           *
6 *  Copyright 2006-2013 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 "bitlbee.h"
30 #include "md5.h"
31 #include "xmltree.h"
32 
33 extern GSList *jabber_connections;
34 
35 typedef enum {
36 	JFLAG_STREAM_STARTED = 1,       /* Set when we detected the beginning of the stream
37 	                                   and want to do auth. */
38 	JFLAG_AUTHENTICATED = 2,        /* Set when we're successfully authenticatd. */
39 	JFLAG_STREAM_RESTART = 4,       /* Set when we want to restart the stream (after
40 	                                   SASL or TLS). */
41 	JFLAG_WANT_SESSION = 8,         /* Set if the server wants a <session/> tag
42 	                                   before we continue. */
43 	JFLAG_WANT_BIND = 16,           /* ... for <bind> tag. */
44 	JFLAG_WANT_TYPING = 32,         /* Set if we ever sent a typing notification, this
45 	                                   activates all XEP-85 related code. */
46 	JFLAG_XMLCONSOLE = 64,          /* If the user added an xmlconsole buddy. */
47 	JFLAG_STARTTLS_DONE = 128,      /* If a plaintext session was converted to TLS. */
48 	JFLAG_GMAILNOTIFY = 256,        /* If gmail notification is enabled */
49 
50 	JFLAG_GTALK =  0x100000,        /* Is Google Talk, as confirmed by iq discovery */
51 	JFLAG_HIPCHAT = 0x200000,       /* Is hipchat, because prpl->name says so */
52 } jabber_flags_t;
53 
54 typedef enum {
55 	JBFLAG_PROBED_XEP85 = 1,        /* Set this when we sent our probe packet to make
56 	                                   sure it gets sent only once. */
57 	JBFLAG_DOES_XEP85 = 2,          /* Set this when the resource seems to support
58 	                                   XEP85 (typing notification shite). */
59 	JBFLAG_IS_CHATROOM = 4,         /* It's convenient to use this JID thingy for
60 	                                   groupchat state info too. */
61 	JBFLAG_IS_ANONYMOUS = 8,        /* For anonymous chatrooms, when we don't have
62 	                                   have a real JID. */
63 	JBFLAG_HIDE_SUBJECT = 16,       /* Hide the subject field since we probably
64 	                                   showed it already. */
65 } jabber_buddy_flags_t;
66 
67 /* Stores a streamhost's (a.k.a. proxy) data */
68 typedef struct {
69 	char *jid;
70 	char *host;
71 	char port[6];
72 } jabber_streamhost_t;
73 
74 typedef enum {
75 	JCFLAG_MESSAGE_SENT = 1,        /* Set this after sending the first message, so
76 	                                   we can detect echoes/backlogs. */
77 	JCFLAG_ALWAYS_USE_NICKS = 2,
78 } jabber_chat_flags_t;
79 
80 struct jabber_data {
81 	struct im_connection *ic;
82 
83 	int fd;
84 	void *ssl;
85 	char *txq;
86 	int tx_len;
87 	int r_inpa, w_inpa;
88 
89 	struct xt_parser *xt;
90 	jabber_flags_t flags;
91 
92 	char *username;         /* USERNAME@server */
93 	char *server;           /* username@SERVER -=> server/domain, not hostname */
94 	char *me;               /* bare jid */
95 	char *internal_jid;
96 
97 	const struct oauth2_service *oauth2_service;
98 	char *oauth2_access_token;
99 
100 	/* After changing one of these two (or the priority setting), call
101 	   presence_send_update() to inform the server about the changes. */
102 	const struct jabber_away_state *away_state;
103 	char *away_message;
104 	guint64 gmail_time;
105 	char *gmail_tid;
106 
107 	md5_state_t cached_id_prefix;
108 	GHashTable *node_cache;
109 	GHashTable *buddies;
110 
111 	GSList *filetransfers;
112 	GSList *streamhosts;
113 	int have_streamhosts;
114 
115 	char *muc_host;
116 };
117 
118 struct jabber_away_state {
119 	char code[5];
120 	char *full_name;
121 };
122 
123 typedef xt_status (*jabber_cache_event) (struct im_connection *ic, struct xt_node *node, struct xt_node *orig);
124 
125 struct jabber_cache_entry {
126 	time_t saved_at;
127 	struct xt_node *node;
128 	jabber_cache_event func;
129 };
130 
131 /* Somewhat messy data structure: We have a hash table with the bare JID as
132    the key and the head of a struct jabber_buddy list as the value. The head
133    is always a bare JID. If the JID has other resources (often the case,
134    except for some transports that don't support multiple resources), those
135    follow. In that case, the bare JID at the beginning doesn't actually
136    refer to a real session and should only be used for operations that
137    support incomplete JIDs. */
138 struct jabber_buddy {
139 	char *bare_jid;
140 	char *full_jid;
141 	char *resource;
142 
143 	char *ext_jid; /* The JID to use in BitlBee. The real JID if possible, */
144 	               /* otherwise something similar to the conference JID. */
145 
146 	int priority;
147 	struct jabber_away_state *away_state;
148 	char *away_message;
149 	GSList *features;
150 
151 	time_t last_msg;
152 	jabber_buddy_flags_t flags;
153 
154 	struct jabber_buddy *next;
155 };
156 
157 struct jabber_chat {
158 	int flags;
159 	char *name;
160 	char *my_full_jid; /* Separate copy because of case sensitivity. */
161 	struct jabber_buddy *me;
162 	char *invite;
163 	char *last_sent_message;
164 };
165 
166 struct jabber_transfer {
167 	/* bitlbee's handle for this transfer */
168 	file_transfer_t *ft;
169 
170 	/* the stream's private handle */
171 	gpointer streamhandle;
172 
173 	/* timeout for discover queries */
174 	gint disco_timeout;
175 	gint disco_timeout_fired;
176 
177 	struct im_connection *ic;
178 
179 	struct jabber_buddy *bud;
180 
181 	int watch_in;
182 	int watch_out;
183 
184 	char *ini_jid;
185 	char *tgt_jid;
186 	char *iq_id;
187 	char *sid;
188 	int accepted;
189 
190 	size_t bytesread, byteswritten;
191 	int fd;
192 	struct sockaddr_storage saddr;
193 };
194 
195 #define JABBER_XMLCONSOLE_HANDLE "_xmlconsole"
196 #define JABBER_OAUTH_HANDLE "jabber_oauth"
197 
198 /* Prefixes to use for packet IDs (mainly for IQ packets ATM). Usually the
199    first one should be used, but when storing a packet in the cache, a
200    "special" kind of ID is assigned to make it easier later to figure out
201    if we have to do call an event handler for the response packet. Also
202    we'll append a hash to make sure we won't trigger on cached packets from
203    other BitlBee users. :-) */
204 #define JABBER_PACKET_ID "BeeP"
205 #define JABBER_CACHED_ID "BeeC"
206 
207 /* The number of seconds to keep cached packets before garbage collecting
208    them. This gc is done on every keepalive (every minute). */
209 #define JABBER_CACHE_MAX_AGE 600
210 
211 /* RFC 392[01] stuff */
212 #define XMLNS_TLS          "urn:ietf:params:xml:ns:xmpp-tls"
213 #define XMLNS_SASL         "urn:ietf:params:xml:ns:xmpp-sasl"
214 #define XMLNS_BIND         "urn:ietf:params:xml:ns:xmpp-bind"
215 #define XMLNS_SESSION      "urn:ietf:params:xml:ns:xmpp-session"
216 #define XMLNS_STANZA_ERROR "urn:ietf:params:xml:ns:xmpp-stanzas"
217 #define XMLNS_STREAM_ERROR "urn:ietf:params:xml:ns:xmpp-streams"
218 #define XMLNS_ROSTER       "jabber:iq:roster"
219 
220 /* Some supported extensions/legacy stuff */
221 #define XMLNS_AUTH         "jabber:iq:auth"                                      /* XEP-0078 */
222 #define XMLNS_VERSION      "jabber:iq:version"                                   /* XEP-0092 */
223 #define XMLNS_TIME_OLD     "jabber:iq:time"                                      /* XEP-0090 */
224 #define XMLNS_TIME         "urn:xmpp:time"                                       /* XEP-0202 */
225 #define XMLNS_PING         "urn:xmpp:ping"                                       /* XEP-0199 */
226 #define XMLNS_RECEIPTS     "urn:xmpp:receipts"                                   /* XEP-0184 */
227 #define XMLNS_VCARD        "vcard-temp"                                          /* XEP-0054 */
228 #define XMLNS_DELAY_OLD    "jabber:x:delay"                                      /* XEP-0091 */
229 #define XMLNS_DELAY        "urn:xmpp:delay"                                      /* XEP-0203 */
230 #define XMLNS_XDATA        "jabber:x:data"                                       /* XEP-0004 */
231 #define XMLNS_GMAILNOTIFY  "google:mail:notify"                                  /* Not a XEP */
232 #define XMLNS_CARBONS      "urn:xmpp:carbons:2"                                  /* XEP-0280 */
233 #define XMLNS_FORWARDING   "urn:xmpp:forward:0"                                  /* XEP-0297 */
234 #define XMLNS_HINTS        "urn:xmpp:hints"                                      /* XEP-0334 */
235 #define XMLNS_CHATSTATES   "http://jabber.org/protocol/chatstates"               /* XEP-0085 */
236 #define XMLNS_DISCO_INFO   "http://jabber.org/protocol/disco#info"               /* XEP-0030 */
237 #define XMLNS_DISCO_ITEMS  "http://jabber.org/protocol/disco#items"              /* XEP-0030 */
238 #define XMLNS_MUC          "http://jabber.org/protocol/muc"                      /* XEP-0045 */
239 #define XMLNS_MUC_USER     "http://jabber.org/protocol/muc#user"                 /* XEP-0045 */
240 #define XMLNS_CAPS         "http://jabber.org/protocol/caps"                     /* XEP-0115 */
241 #define XMLNS_FEATURE      "http://jabber.org/protocol/feature-neg"              /* XEP-0020 */
242 #define XMLNS_SI           "http://jabber.org/protocol/si"                       /* XEP-0095 */
243 #define XMLNS_FILETRANSFER "http://jabber.org/protocol/si/profile/file-transfer" /* XEP-0096 */
244 #define XMLNS_BYTESTREAMS  "http://jabber.org/protocol/bytestreams"              /* XEP-0065 */
245 #define XMLNS_IBB          "http://jabber.org/protocol/ibb"                      /* XEP-0047 */
246 
247 /* Hipchat protocol extensions*/
248 #define XMLNS_HIPCHAT         "http://hipchat.com"
249 #define XMLNS_HIPCHAT_PROFILE "http://hipchat.com/protocol/profile"
250 
251 /* jabber.c */
252 void jabber_connect(struct im_connection *ic);
253 
254 /* iq.c */
255 xt_status jabber_pkt_iq(struct xt_node *node, gpointer data);
256 int jabber_init_iq_auth(struct im_connection *ic);
257 xt_status jabber_pkt_bind_sess(struct im_connection *ic, struct xt_node *node, struct xt_node *orig);
258 int jabber_get_roster(struct im_connection *ic);
259 int jabber_get_vcard(struct im_connection *ic, char *bare_jid);
260 int jabber_add_to_roster(struct im_connection *ic, const char *handle, const char *name, const char *group);
261 int jabber_remove_from_roster(struct im_connection *ic, char *handle);
262 xt_status jabber_iq_query_features(struct im_connection *ic, char *bare_jid);
263 xt_status jabber_iq_query_server(struct im_connection *ic, char *jid, char *xmlns);
264 void jabber_iq_version_send(struct im_connection *ic, struct jabber_buddy *bud, void *data);
265 int jabber_iq_disco_server(struct im_connection *ic);
266 int jabber_iq_disco_muc(struct im_connection *ic, const char *muc_server);
267 
268 /* si.c */
269 int jabber_si_handle_request(struct im_connection *ic, struct xt_node *node, struct xt_node *sinode);
270 void jabber_si_transfer_request(struct im_connection *ic, file_transfer_t *ft, char *who);
271 void jabber_si_free_transfer(file_transfer_t *ft);
272 
273 /* s5bytestream.c */
274 int jabber_bs_recv_request(struct im_connection *ic, struct xt_node *node, struct xt_node *qnode);
275 gboolean jabber_bs_send_start(struct jabber_transfer *tf);
276 gboolean jabber_bs_send_write(file_transfer_t *ft, char *buffer, unsigned int len);
277 
278 /* message.c */
279 xt_status jabber_pkt_message(struct xt_node *node, gpointer data);
280 
281 /* presence.c */
282 xt_status jabber_pkt_presence(struct xt_node *node, gpointer data);
283 int presence_send_update(struct im_connection *ic);
284 int presence_send_request(struct im_connection *ic, char *handle, char *request);
285 
286 /* jabber_util.c */
287 char *set_eval_priority(set_t *set, char *value);
288 char *set_eval_tls(set_t *set, char *value);
289 struct xt_node *jabber_make_packet(char *name, char *type, char *to, struct xt_node *children);
290 struct xt_node *jabber_make_error_packet(struct xt_node *orig, char *err_cond, char *err_type, char *err_code);
291 void jabber_cache_add(struct im_connection *ic, struct xt_node *node, jabber_cache_event func);
292 struct xt_node *jabber_cache_get(struct im_connection *ic, char *id);
293 void jabber_cache_entry_free(gpointer entry);
294 void jabber_cache_clean(struct im_connection *ic);
295 xt_status jabber_cache_handle_packet(struct im_connection *ic, struct xt_node *node);
296 const struct jabber_away_state *jabber_away_state_by_code(char *code);
297 const struct jabber_away_state *jabber_away_state_by_name(char *name);
298 void jabber_buddy_ask(struct im_connection *ic, char *handle);
299 int jabber_compare_jid(const char *jid1, const char *jid2);
300 char *jabber_normalize(const char *orig);
301 
302 typedef enum {
303 	GET_BUDDY_CREAT = 1,    /* Try to create it, if necessary. */
304 	GET_BUDDY_EXACT = 2,    /* Get an exact match (only makes sense with bare JIDs). */
305 	GET_BUDDY_FIRST = 4,    /* No selection, simply get the first resource for this JID. */
306 	GET_BUDDY_BARE = 8,     /* Get the bare version of the JID (possibly inexistent). */
307 	GET_BUDDY_BARE_OK = 16, /* Allow returning a bare JID if that seems better. */
308 } get_buddy_flags_t;
309 
310 struct jabber_error {
311 	char *code, *text, *type;
312 };
313 
314 struct jabber_buddy *jabber_buddy_add(struct im_connection *ic, char *full_jid);
315 struct jabber_buddy *jabber_buddy_by_jid(struct im_connection *ic, char *jid, get_buddy_flags_t flags);
316 struct jabber_buddy *jabber_buddy_by_ext_jid(struct im_connection *ic, char *jid, get_buddy_flags_t flags);
317 int jabber_buddy_remove(struct im_connection *ic, char *full_jid);
318 int jabber_buddy_remove_bare(struct im_connection *ic, char *bare_jid);
319 void jabber_buddy_remove_all(struct im_connection *ic);
320 time_t jabber_get_timestamp(struct xt_node *xt);
321 struct jabber_error *jabber_error_parse(struct xt_node *node, char *xmlns);
322 void jabber_error_free(struct jabber_error *err);
323 gboolean jabber_set_me(struct im_connection *ic, const char *me);
324 char *jabber_get_bare_jid(char *jid);
325 
326 extern const struct jabber_away_state jabber_away_state_list[];
327 
328 /* io.c */
329 int jabber_write_packet(struct im_connection *ic, struct xt_node *node);
330 int jabber_write(struct im_connection *ic, char *buf, int len);
331 gboolean jabber_connected_plain(gpointer data, gint source, b_input_condition cond);
332 gboolean jabber_connected_ssl(gpointer data, int returncode, void *source, b_input_condition cond);
333 gboolean jabber_start_stream(struct im_connection *ic);
334 void jabber_end_stream(struct im_connection *ic);
335 
336 /* sasl.c */
337 xt_status sasl_pkt_mechanisms(struct xt_node *node, gpointer data);
338 xt_status sasl_pkt_challenge(struct xt_node *node, gpointer data);
339 xt_status sasl_pkt_result(struct xt_node *node, gpointer data);
340 gboolean sasl_supported(struct im_connection *ic);
341 void sasl_oauth2_init(struct im_connection *ic);
342 int sasl_oauth2_get_refresh_token(struct im_connection *ic, const char *msg);
343 int sasl_oauth2_refresh(struct im_connection *ic, const char *refresh_token);
344 void sasl_oauth2_got_token(gpointer data, const char *access_token, const char *refresh_token, const char *error);
345 
346 extern const struct oauth2_service oauth2_service_google;
347 
348 /* conference.c */
349 struct groupchat *jabber_chat_join(struct im_connection *ic, const char *room, const char *nick, const char *password,
350                                    gboolean always_use_nicks);
351 struct groupchat *jabber_chat_with(struct im_connection *ic, char *who);
352 struct groupchat *jabber_chat_by_jid(struct im_connection *ic, const char *name);
353 void jabber_chat_free(struct groupchat *c);
354 int jabber_chat_msg(struct groupchat *ic, char *message, int flags);
355 int jabber_chat_topic(struct groupchat *c, char *topic);
356 int jabber_chat_leave(struct groupchat *c, const char *reason);
357 void jabber_chat_pkt_presence(struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node);
358 void jabber_chat_pkt_message(struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node);
359 void jabber_chat_invite(struct groupchat *c, char *who, char *message);
360 
361 /* hipchat.c */
362 int jabber_get_hipchat_profile(struct im_connection *ic);
363 xt_status jabber_parse_hipchat_profile(struct im_connection *ic, struct xt_node *node, struct xt_node *orig);
364 xt_status hipchat_handle_success(struct im_connection *ic, struct xt_node *node);
365 char *hipchat_make_channel_slug(const char *name);
366 char *hipchat_guess_channel_name(struct im_connection *ic, const char *name);
367 
368 #endif
369