[f06894d] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Jabber module - Main file * |
---|
| 5 | * * |
---|
[0e788f5] | 6 | * Copyright 2006-2013 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
[f06894d] | 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 | #include <glib.h> |
---|
| 25 | #include <string.h> |
---|
| 26 | #include <unistd.h> |
---|
| 27 | #include <ctype.h> |
---|
| 28 | #include <stdio.h> |
---|
| 29 | |
---|
[21167d2] | 30 | #include "ssl_client.h" |
---|
[f06894d] | 31 | #include "xmltree.h" |
---|
| 32 | #include "bitlbee.h" |
---|
| 33 | #include "jabber.h" |
---|
[e14b47b8] | 34 | #include "oauth.h" |
---|
[608f8cf] | 35 | #include "md5.h" |
---|
[f06894d] | 36 | |
---|
[b5c8a34] | 37 | GSList *jabber_connections; |
---|
| 38 | |
---|
[7f69740] | 39 | /* First enty is the default */ |
---|
| 40 | static const int jabber_port_list[] = { |
---|
| 41 | 5222, |
---|
| 42 | 5223, |
---|
| 43 | 5220, |
---|
| 44 | 5221, |
---|
| 45 | 5224, |
---|
| 46 | 5225, |
---|
| 47 | 5226, |
---|
| 48 | 5227, |
---|
| 49 | 5228, |
---|
| 50 | 5229, |
---|
| 51 | 80, |
---|
| 52 | 443, |
---|
| 53 | 0 |
---|
| 54 | }; |
---|
| 55 | |
---|
[5ebff60] | 56 | static void jabber_init(account_t *acc) |
---|
[f06894d] | 57 | { |
---|
| 58 | set_t *s; |
---|
[7f69740] | 59 | char str[16]; |
---|
[5ebff60] | 60 | |
---|
| 61 | s = set_add(&acc->set, "activity_timeout", "600", set_eval_int, acc); |
---|
| 62 | |
---|
| 63 | s = set_add(&acc->set, "oauth", "false", set_eval_oauth, acc); |
---|
| 64 | |
---|
| 65 | s = set_add(&acc->set, "display_name", NULL, NULL, acc); |
---|
| 66 | |
---|
| 67 | g_snprintf(str, sizeof(str), "%d", jabber_port_list[0]); |
---|
| 68 | s = set_add(&acc->set, "port", str, set_eval_int, acc); |
---|
[f06894d] | 69 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[1c3008a] | 70 | |
---|
[5ebff60] | 71 | s = set_add(&acc->set, "priority", "0", set_eval_priority, acc); |
---|
| 72 | |
---|
| 73 | s = set_add(&acc->set, "proxy", "<local>;<auto>", NULL, acc); |
---|
| 74 | |
---|
| 75 | s = set_add(&acc->set, "resource", "BitlBee", NULL, acc); |
---|
[ebe7b36] | 76 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[5ebff60] | 77 | |
---|
| 78 | s = set_add(&acc->set, "resource_select", "activity", NULL, acc); |
---|
| 79 | |
---|
| 80 | s = set_add(&acc->set, "sasl", "true", set_eval_bool, acc); |
---|
[ce199b7] | 81 | s->flags |= ACC_SET_OFFLINE_ONLY | SET_HIDDEN_DEFAULT; |
---|
[5ebff60] | 82 | |
---|
| 83 | s = set_add(&acc->set, "server", NULL, set_eval_account, acc); |
---|
[bb5ce568] | 84 | s->flags |= SET_NOSAVE | ACC_SET_OFFLINE_ONLY | SET_NULL_OK; |
---|
[5ebff60] | 85 | |
---|
| 86 | s = set_add(&acc->set, "ssl", "false", set_eval_bool, acc); |
---|
[f06894d] | 87 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[5ebff60] | 88 | |
---|
| 89 | s = set_add(&acc->set, "tls", "true", set_eval_tls, acc); |
---|
[f06894d] | 90 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[5ebff60] | 91 | |
---|
| 92 | s = set_add(&acc->set, "tls_verify", "true", set_eval_bool, acc); |
---|
[486ddb5] | 93 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[34ded90] | 94 | |
---|
[5ebff60] | 95 | s = set_add(&acc->set, "user_agent", "BitlBee", NULL, acc); |
---|
| 96 | |
---|
| 97 | s = set_add(&acc->set, "xmlconsole", "false", set_eval_bool, acc); |
---|
[a6df0b5] | 98 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[5ebff60] | 99 | |
---|
[06eef80] | 100 | acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE | |
---|
| 101 | ACC_FLAG_HANDLE_DOMAINS; |
---|
[f06894d] | 102 | } |
---|
| 103 | |
---|
[5ebff60] | 104 | static void jabber_generate_id_hash(struct jabber_data *jd); |
---|
[608f8cf] | 105 | |
---|
[5ebff60] | 106 | static void jabber_login(account_t *acc) |
---|
[f06894d] | 107 | { |
---|
[5ebff60] | 108 | struct im_connection *ic = imcb_new(acc); |
---|
| 109 | struct jabber_data *jd = g_new0(struct jabber_data, 1); |
---|
[4a5d885] | 110 | char *s; |
---|
[5ebff60] | 111 | |
---|
[b5c8a34] | 112 | /* For now this is needed in the _connected() handlers if using |
---|
| 113 | GLib event handling, to make sure we're not handling events |
---|
| 114 | on dead connections. */ |
---|
[5ebff60] | 115 | jabber_connections = g_slist_prepend(jabber_connections, ic); |
---|
| 116 | |
---|
[0da65d5] | 117 | jd->ic = ic; |
---|
| 118 | ic->proto_data = jd; |
---|
[5ebff60] | 119 | |
---|
| 120 | jabber_set_me(ic, acc->user); |
---|
| 121 | |
---|
[de03374] | 122 | jd->fd = jd->r_inpa = jd->w_inpa = -1; |
---|
[5ebff60] | 123 | |
---|
| 124 | if (jd->server == NULL) { |
---|
| 125 | imcb_error(ic, "Incomplete account name (format it like <username@jabberserver.name>)"); |
---|
| 126 | imc_logout(ic, FALSE); |
---|
[21167d2] | 127 | return; |
---|
| 128 | } |
---|
[5ebff60] | 129 | |
---|
| 130 | if ((s = strchr(jd->server, '/'))) { |
---|
[3b3cd693] | 131 | *s = 0; |
---|
[5ebff60] | 132 | set_setstr(&acc->set, "resource", s + 1); |
---|
| 133 | |
---|
[3b3cd693] | 134 | /* Also remove the /resource from the original variable so we |
---|
| 135 | won't have to do this again every time. */ |
---|
[5ebff60] | 136 | s = strchr(acc->user, '/'); |
---|
[3b3cd693] | 137 | *s = 0; |
---|
| 138 | } |
---|
[5ebff60] | 139 | |
---|
| 140 | jd->node_cache = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, jabber_cache_entry_free); |
---|
| 141 | jd->buddies = g_hash_table_new(g_str_hash, g_str_equal); |
---|
| 142 | |
---|
| 143 | if (set_getbool(&acc->set, "oauth")) { |
---|
[e14b47b8] | 144 | GSList *p_in = NULL; |
---|
| 145 | const char *tok; |
---|
[5ebff60] | 146 | |
---|
[f138bd2] | 147 | jd->fd = jd->r_inpa = jd->w_inpa = -1; |
---|
[5ebff60] | 148 | |
---|
| 149 | if (strstr(jd->server, ".facebook.com")) { |
---|
[18c6d36] | 150 | jd->oauth2_service = &oauth2_service_facebook; |
---|
[5ebff60] | 151 | } else { |
---|
[18c6d36] | 152 | jd->oauth2_service = &oauth2_service_google; |
---|
[5ebff60] | 153 | } |
---|
| 154 | |
---|
| 155 | oauth_params_parse(&p_in, ic->acc->pass); |
---|
| 156 | |
---|
[64b6635] | 157 | /* First see if we have a refresh token, in which case any |
---|
| 158 | access token we *might* have has probably expired already |
---|
| 159 | anyway. */ |
---|
[5ebff60] | 160 | if ((tok = oauth_params_get(&p_in, "refresh_token"))) { |
---|
| 161 | sasl_oauth2_refresh(ic, tok); |
---|
[64b6635] | 162 | } |
---|
| 163 | /* If we don't have a refresh token, let's hope the access |
---|
| 164 | token is still usable. */ |
---|
[5ebff60] | 165 | else if ((tok = oauth_params_get(&p_in, "access_token"))) { |
---|
| 166 | jd->oauth2_access_token = g_strdup(tok); |
---|
| 167 | jabber_connect(ic); |
---|
[64b6635] | 168 | } |
---|
| 169 | /* If we don't have any, start the OAuth process now. Don't |
---|
| 170 | even open an XMPP connection yet. */ |
---|
[5ebff60] | 171 | else { |
---|
| 172 | sasl_oauth2_init(ic); |
---|
[f988ad3] | 173 | ic->flags |= OPT_SLOW_LOGIN; |
---|
| 174 | } |
---|
[5ebff60] | 175 | |
---|
| 176 | oauth_params_free(&p_in); |
---|
| 177 | } else { |
---|
| 178 | jabber_connect(ic); |
---|
[4a5d885] | 179 | } |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /* Separate this from jabber_login() so we can do OAuth first if necessary. |
---|
| 183 | Putting this in io.c would probably be more correct. */ |
---|
[5ebff60] | 184 | void jabber_connect(struct im_connection *ic) |
---|
[4a5d885] | 185 | { |
---|
| 186 | account_t *acc = ic->acc; |
---|
| 187 | struct jabber_data *jd = ic->proto_data; |
---|
| 188 | int i; |
---|
| 189 | char *connect_to; |
---|
| 190 | struct ns_srv_reply **srvl = NULL, *srv = NULL; |
---|
[5ebff60] | 191 | |
---|
[36e9f62] | 192 | /* Figure out the hostname to connect to. */ |
---|
[5ebff60] | 193 | if (acc->server && *acc->server) { |
---|
[36e9f62] | 194 | connect_to = acc->server; |
---|
[5ebff60] | 195 | } else if ((srvl = srv_lookup("xmpp-client", "tcp", jd->server)) || |
---|
| 196 | (srvl = srv_lookup("jabber-client", "tcp", jd->server))) { |
---|
[ffdf2e7] | 197 | /* Find the lowest-priority one. These usually come |
---|
| 198 | back in random/shuffled order. Not looking at |
---|
| 199 | weights etc for now. */ |
---|
| 200 | srv = *srvl; |
---|
[5ebff60] | 201 | for (i = 1; srvl[i]; i++) { |
---|
| 202 | if (srvl[i]->prio < srv->prio) { |
---|
[ffdf2e7] | 203 | srv = srvl[i]; |
---|
[5ebff60] | 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
[36e9f62] | 207 | connect_to = srv->name; |
---|
[5ebff60] | 208 | } else { |
---|
[36e9f62] | 209 | connect_to = jd->server; |
---|
[5ebff60] | 210 | } |
---|
| 211 | |
---|
| 212 | imcb_log(ic, "Connecting"); |
---|
| 213 | |
---|
| 214 | for (i = 0; jabber_port_list[i] > 0; i++) { |
---|
| 215 | if (set_getint(&acc->set, "port") == jabber_port_list[i]) { |
---|
[7f69740] | 216 | break; |
---|
[5ebff60] | 217 | } |
---|
| 218 | } |
---|
[7f69740] | 219 | |
---|
[5ebff60] | 220 | if (jabber_port_list[i] == 0) { |
---|
| 221 | imcb_log(ic, "Illegal port number"); |
---|
| 222 | imc_logout(ic, FALSE); |
---|
[0f4c1bb5] | 223 | return; |
---|
| 224 | } |
---|
[5ebff60] | 225 | |
---|
[36e9f62] | 226 | /* For non-SSL connections we can try to use the port # from the SRV |
---|
| 227 | reply, but let's not do that when using SSL, SSL usually runs on |
---|
| 228 | non-standard ports... */ |
---|
[5ebff60] | 229 | if (set_getbool(&acc->set, "ssl")) { |
---|
| 230 | jd->ssl = ssl_connect(connect_to, set_getint(&acc->set, "port"), set_getbool(&acc->set, |
---|
| 231 | "tls_verify"), jabber_connected_ssl, |
---|
| 232 | ic); |
---|
| 233 | jd->fd = jd->ssl ? ssl_getfd(jd->ssl) : -1; |
---|
| 234 | } else { |
---|
| 235 | jd->fd = proxy_connect(connect_to, srv ? srv->port : set_getint(&acc->set, |
---|
| 236 | "port"), jabber_connected_plain, ic); |
---|
| 237 | } |
---|
| 238 | srv_free(srvl); |
---|
| 239 | |
---|
| 240 | if (jd->fd == -1) { |
---|
| 241 | imcb_error(ic, "Could not connect to server"); |
---|
| 242 | imc_logout(ic, TRUE); |
---|
| 243 | |
---|
[fb4ebcc5] | 244 | return; |
---|
[35f6677] | 245 | } |
---|
[5ebff60] | 246 | |
---|
| 247 | if (set_getbool(&acc->set, "xmlconsole")) { |
---|
[a6df0b5] | 248 | jd->flags |= JFLAG_XMLCONSOLE; |
---|
| 249 | /* Shouldn't really do this at this stage already, maybe. But |
---|
| 250 | I think this shouldn't break anything. */ |
---|
[5ebff60] | 251 | imcb_add_buddy(ic, JABBER_XMLCONSOLE_HANDLE, NULL); |
---|
[a6df0b5] | 252 | } |
---|
[5ebff60] | 253 | |
---|
| 254 | jabber_generate_id_hash(jd); |
---|
[608f8cf] | 255 | } |
---|
| 256 | |
---|
[89d736a] | 257 | /* This generates an unfinished md5_state_t variable. Every time we generate |
---|
| 258 | an ID, we finish the state by adding a sequence number and take the hash. */ |
---|
[5ebff60] | 259 | static void jabber_generate_id_hash(struct jabber_data *jd) |
---|
[608f8cf] | 260 | { |
---|
[89d736a] | 261 | md5_byte_t binbuf[4]; |
---|
[608f8cf] | 262 | char *s; |
---|
[5ebff60] | 263 | |
---|
| 264 | md5_init(&jd->cached_id_prefix); |
---|
| 265 | md5_append(&jd->cached_id_prefix, (unsigned char *) jd->username, strlen(jd->username)); |
---|
| 266 | md5_append(&jd->cached_id_prefix, (unsigned char *) jd->server, strlen(jd->server)); |
---|
| 267 | s = set_getstr(&jd->ic->acc->set, "resource"); |
---|
| 268 | md5_append(&jd->cached_id_prefix, (unsigned char *) s, strlen(s)); |
---|
| 269 | random_bytes(binbuf, 4); |
---|
| 270 | md5_append(&jd->cached_id_prefix, binbuf, 4); |
---|
[f06894d] | 271 | } |
---|
| 272 | |
---|
[5ebff60] | 273 | static void jabber_logout(struct im_connection *ic) |
---|
[f06894d] | 274 | { |
---|
[0da65d5] | 275 | struct jabber_data *jd = ic->proto_data; |
---|
[4ac647d] | 276 | |
---|
[5ebff60] | 277 | while (jd->filetransfers) { |
---|
| 278 | imcb_file_canceled(ic, (( struct jabber_transfer *) jd->filetransfers->data)->ft, "Logging out"); |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | while (jd->streamhosts) { |
---|
[4ac647d] | 282 | jabber_streamhost_t *sh = jd->streamhosts->data; |
---|
[5ebff60] | 283 | jd->streamhosts = g_slist_remove(jd->streamhosts, sh); |
---|
| 284 | g_free(sh->jid); |
---|
| 285 | g_free(sh->host); |
---|
| 286 | g_free(sh); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | if (jd->fd >= 0) { |
---|
| 290 | jabber_end_stream(ic); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | while (ic->groupchats) { |
---|
| 294 | jabber_chat_free(ic->groupchats->data); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | if (jd->r_inpa >= 0) { |
---|
| 298 | b_event_remove(jd->r_inpa); |
---|
| 299 | } |
---|
| 300 | if (jd->w_inpa >= 0) { |
---|
| 301 | b_event_remove(jd->w_inpa); |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | if (jd->ssl) { |
---|
| 305 | ssl_disconnect(jd->ssl); |
---|
| 306 | } |
---|
| 307 | if (jd->fd >= 0) { |
---|
| 308 | closesocket(jd->fd); |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | if (jd->tx_len) { |
---|
| 312 | g_free(jd->txq); |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | if (jd->node_cache) { |
---|
| 316 | g_hash_table_destroy(jd->node_cache); |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | jabber_buddy_remove_all(ic); |
---|
| 320 | |
---|
| 321 | xt_free(jd->xt); |
---|
| 322 | |
---|
| 323 | md5_free(&jd->cached_id_prefix); |
---|
| 324 | |
---|
| 325 | g_free(jd->oauth2_access_token); |
---|
| 326 | g_free(jd->away_message); |
---|
| 327 | g_free(jd->internal_jid); |
---|
| 328 | g_free(jd->username); |
---|
| 329 | g_free(jd->me); |
---|
| 330 | g_free(jd); |
---|
| 331 | |
---|
| 332 | jabber_connections = g_slist_remove(jabber_connections, ic); |
---|
[f06894d] | 333 | } |
---|
| 334 | |
---|
[5ebff60] | 335 | static int jabber_buddy_msg(struct im_connection *ic, char *who, char *message, int flags) |
---|
[f06894d] | 336 | { |
---|
[0da65d5] | 337 | struct jabber_data *jd = ic->proto_data; |
---|
[a21a8ac] | 338 | struct jabber_buddy *bud; |
---|
[cc2cb2d] | 339 | struct xt_node *node; |
---|
[b9f8b87] | 340 | char *s; |
---|
[4a0614e] | 341 | int st; |
---|
[5ebff60] | 342 | |
---|
| 343 | if (g_strcasecmp(who, JABBER_XMLCONSOLE_HANDLE) == 0) { |
---|
| 344 | return jabber_write(ic, message, strlen(message)); |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | if (g_strcasecmp(who, JABBER_OAUTH_HANDLE) == 0 && |
---|
| 348 | !(jd->flags & OPT_LOGGED_IN) && jd->fd == -1) { |
---|
| 349 | if (sasl_oauth2_get_refresh_token(ic, message)) { |
---|
[4a5d885] | 350 | return 1; |
---|
[5ebff60] | 351 | } else { |
---|
| 352 | imcb_error(ic, "OAuth failure"); |
---|
| 353 | imc_logout(ic, TRUE); |
---|
[911d97a] | 354 | return 0; |
---|
[4a5d885] | 355 | } |
---|
| 356 | } |
---|
[5ebff60] | 357 | |
---|
| 358 | if ((s = strchr(who, '=')) && jabber_chat_by_jid(ic, s + 1)) { |
---|
| 359 | bud = jabber_buddy_by_ext_jid(ic, who, 0); |
---|
| 360 | } else { |
---|
| 361 | bud = jabber_buddy_by_jid(ic, who, GET_BUDDY_BARE_OK); |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | node = xt_new_node("body", message, NULL); |
---|
| 365 | node = jabber_make_packet("message", "chat", bud ? bud->full_jid : who, node); |
---|
| 366 | |
---|
| 367 | if (bud && (jd->flags & JFLAG_WANT_TYPING) && |
---|
| 368 | ((bud->flags & JBFLAG_DOES_XEP85) || |
---|
| 369 | !(bud->flags & JBFLAG_PROBED_XEP85))) { |
---|
[a21a8ac] | 370 | struct xt_node *act; |
---|
[5ebff60] | 371 | |
---|
[a21a8ac] | 372 | /* If the user likes typing notification and if we don't know |
---|
[788a1af] | 373 | (and didn't probe before) if this resource supports XEP85, |
---|
[abbd8ed] | 374 | include a probe in this packet now. Also, if we know this |
---|
| 375 | buddy does support XEP85, we have to send this <active/> |
---|
| 376 | tag to tell that the user stopped typing (well, that's what |
---|
| 377 | we guess when s/he pressed Enter...). */ |
---|
[5ebff60] | 378 | act = xt_new_node("active", NULL, NULL); |
---|
| 379 | xt_add_attr(act, "xmlns", XMLNS_CHATSTATES); |
---|
| 380 | xt_add_child(node, act); |
---|
| 381 | |
---|
[a21a8ac] | 382 | /* Just make sure we do this only once. */ |
---|
[788a1af] | 383 | bud->flags |= JBFLAG_PROBED_XEP85; |
---|
[a21a8ac] | 384 | } |
---|
[5ebff60] | 385 | |
---|
| 386 | st = jabber_write_packet(ic, node); |
---|
| 387 | xt_free_node(node); |
---|
| 388 | |
---|
[4a0614e] | 389 | return st; |
---|
[f06894d] | 390 | } |
---|
| 391 | |
---|
[5ebff60] | 392 | static GList *jabber_away_states(struct im_connection *ic) |
---|
[dd788bb] | 393 | { |
---|
[5e202b0] | 394 | static GList *l = NULL; |
---|
| 395 | int i; |
---|
[5ebff60] | 396 | |
---|
| 397 | if (l == NULL) { |
---|
| 398 | for (i = 0; jabber_away_state_list[i].full_name; i++) { |
---|
| 399 | l = g_list_append(l, (void *) jabber_away_state_list[i].full_name); |
---|
| 400 | } |
---|
| 401 | } |
---|
| 402 | |
---|
[5e202b0] | 403 | return l; |
---|
[dd788bb] | 404 | } |
---|
| 405 | |
---|
[5ebff60] | 406 | static void jabber_get_info(struct im_connection *ic, char *who) |
---|
[038d17f] | 407 | { |
---|
[6a1128d] | 408 | struct jabber_buddy *bud; |
---|
[5ebff60] | 409 | |
---|
| 410 | bud = jabber_buddy_by_jid(ic, who, GET_BUDDY_FIRST); |
---|
| 411 | |
---|
| 412 | while (bud) { |
---|
| 413 | imcb_log(ic, "Buddy %s (%d) information:", bud->full_jid, bud->priority); |
---|
| 414 | if (bud->away_state) { |
---|
| 415 | imcb_log(ic, "Away state: %s", bud->away_state->full_name); |
---|
| 416 | } |
---|
| 417 | imcb_log(ic, "Status message: %s", bud->away_message ? bud->away_message : "(none)"); |
---|
| 418 | |
---|
[6a1128d] | 419 | bud = bud->next; |
---|
| 420 | } |
---|
[5ebff60] | 421 | |
---|
| 422 | jabber_get_vcard(ic, bud ? bud->full_jid : who); |
---|
[038d17f] | 423 | } |
---|
| 424 | |
---|
[5ebff60] | 425 | static void jabber_set_away(struct im_connection *ic, char *state_txt, char *message) |
---|
[dd788bb] | 426 | { |
---|
[0da65d5] | 427 | struct jabber_data *jd = ic->proto_data; |
---|
[5ebff60] | 428 | |
---|
[840bba8] | 429 | /* state_txt == NULL -> Not away. |
---|
| 430 | Unknown state -> fall back to the first defined away state. */ |
---|
[5ebff60] | 431 | if (state_txt == NULL) { |
---|
[daae10f] | 432 | jd->away_state = NULL; |
---|
[5ebff60] | 433 | } else if ((jd->away_state = jabber_away_state_by_name(state_txt)) == NULL) { |
---|
[daae10f] | 434 | jd->away_state = jabber_away_state_list; |
---|
[5ebff60] | 435 | } |
---|
| 436 | |
---|
| 437 | g_free(jd->away_message); |
---|
| 438 | jd->away_message = (message && *message) ? g_strdup(message) : NULL; |
---|
| 439 | |
---|
| 440 | presence_send_update(ic); |
---|
[deff040] | 441 | } |
---|
| 442 | |
---|
[5ebff60] | 443 | static void jabber_add_buddy(struct im_connection *ic, char *who, char *group) |
---|
[cfbb3a6] | 444 | { |
---|
[bb95d43] | 445 | struct jabber_data *jd = ic->proto_data; |
---|
[5ebff60] | 446 | |
---|
| 447 | if (g_strcasecmp(who, JABBER_XMLCONSOLE_HANDLE) == 0) { |
---|
[bb95d43] | 448 | jd->flags |= JFLAG_XMLCONSOLE; |
---|
[5ebff60] | 449 | imcb_add_buddy(ic, JABBER_XMLCONSOLE_HANDLE, NULL); |
---|
[bb95d43] | 450 | return; |
---|
| 451 | } |
---|
[5ebff60] | 452 | |
---|
| 453 | if (jabber_add_to_roster(ic, who, NULL, group)) { |
---|
| 454 | presence_send_request(ic, who, "subscribe"); |
---|
| 455 | } |
---|
[cfbb3a6] | 456 | } |
---|
| 457 | |
---|
[5ebff60] | 458 | static void jabber_remove_buddy(struct im_connection *ic, char *who, char *group) |
---|
[cfbb3a6] | 459 | { |
---|
[bb95d43] | 460 | struct jabber_data *jd = ic->proto_data; |
---|
[5ebff60] | 461 | |
---|
| 462 | if (g_strcasecmp(who, JABBER_XMLCONSOLE_HANDLE) == 0) { |
---|
[bb95d43] | 463 | jd->flags &= ~JFLAG_XMLCONSOLE; |
---|
[a3d5766] | 464 | /* Not necessary for now. And for now the code isn't too |
---|
| 465 | happy if the buddy is completely gone right after calling |
---|
| 466 | this function already. |
---|
[998b103] | 467 | imcb_remove_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL ); |
---|
[a3d5766] | 468 | */ |
---|
[bb95d43] | 469 | return; |
---|
| 470 | } |
---|
[5ebff60] | 471 | |
---|
[788a1af] | 472 | /* We should always do this part. Clean up our administration a little bit. */ |
---|
[5ebff60] | 473 | jabber_buddy_remove_bare(ic, who); |
---|
| 474 | |
---|
| 475 | if (jabber_remove_from_roster(ic, who)) { |
---|
| 476 | presence_send_request(ic, who, "unsubscribe"); |
---|
| 477 | } |
---|
[cfbb3a6] | 478 | } |
---|
| 479 | |
---|
[5ebff60] | 480 | static struct groupchat *jabber_chat_join_(struct im_connection *ic, const char *room, const char *nick, |
---|
| 481 | const char *password, set_t **sets) |
---|
[e35d1a1] | 482 | { |
---|
[441a67e] | 483 | struct jabber_data *jd = ic->proto_data; |
---|
[fcb2c2e] | 484 | char *final_nick; |
---|
[5ebff60] | 485 | |
---|
[fcb2c2e] | 486 | /* Ignore the passed nick parameter if we have our own default */ |
---|
[5ebff60] | 487 | if (!(final_nick = set_getstr(sets, "nick")) && |
---|
| 488 | !(final_nick = set_getstr(&ic->acc->set, "display_name"))) { |
---|
[fcb2c2e] | 489 | /* Well, whatever, actually use the provided default, then */ |
---|
| 490 | final_nick = (char *) nick; |
---|
| 491 | } |
---|
| 492 | |
---|
[5ebff60] | 493 | if (strchr(room, '@') == NULL) { |
---|
| 494 | imcb_error(ic, "%s is not a valid Jabber room name. Maybe you mean %s@conference.%s?", |
---|
| 495 | room, room, jd->server); |
---|
| 496 | } else if (jabber_chat_by_jid(ic, room)) { |
---|
| 497 | imcb_error(ic, "Already present in chat `%s'", room); |
---|
| 498 | } else { |
---|
| 499 | return jabber_chat_join(ic, room, final_nick, set_getstr(sets, "password")); |
---|
| 500 | } |
---|
| 501 | |
---|
[e35d1a1] | 502 | return NULL; |
---|
| 503 | } |
---|
| 504 | |
---|
[5ebff60] | 505 | static struct groupchat *jabber_chat_with_(struct im_connection *ic, char *who) |
---|
[fc0640e] | 506 | { |
---|
[5ebff60] | 507 | return jabber_chat_with(ic, who); |
---|
[fc0640e] | 508 | } |
---|
| 509 | |
---|
[5ebff60] | 510 | static void jabber_chat_msg_(struct groupchat *c, char *message, int flags) |
---|
[43671b9] | 511 | { |
---|
[5ebff60] | 512 | if (c && message) { |
---|
| 513 | jabber_chat_msg(c, message, flags); |
---|
| 514 | } |
---|
[43671b9] | 515 | } |
---|
| 516 | |
---|
[5ebff60] | 517 | static void jabber_chat_topic_(struct groupchat *c, char *topic) |
---|
[ef5c185] | 518 | { |
---|
[5ebff60] | 519 | if (c && topic) { |
---|
| 520 | jabber_chat_topic(c, topic); |
---|
| 521 | } |
---|
[ef5c185] | 522 | } |
---|
| 523 | |
---|
[5ebff60] | 524 | static void jabber_chat_leave_(struct groupchat *c) |
---|
[e35d1a1] | 525 | { |
---|
[5ebff60] | 526 | if (c) { |
---|
| 527 | jabber_chat_leave(c, NULL); |
---|
| 528 | } |
---|
[e35d1a1] | 529 | } |
---|
| 530 | |
---|
[5ebff60] | 531 | static void jabber_chat_invite_(struct groupchat *c, char *who, char *msg) |
---|
[c058ff9] | 532 | { |
---|
[68286eb] | 533 | struct jabber_data *jd = c->ic->proto_data; |
---|
[c058ff9] | 534 | struct jabber_chat *jc = c->data; |
---|
| 535 | gchar *msg_alt = NULL; |
---|
| 536 | |
---|
[5ebff60] | 537 | if (msg == NULL) { |
---|
| 538 | msg_alt = g_strdup_printf("%s invited you to %s", jd->me, jc->name); |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | if (c && who) { |
---|
| 542 | jabber_chat_invite(c, who, msg ? msg : msg_alt); |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | g_free(msg_alt); |
---|
[c058ff9] | 546 | } |
---|
| 547 | |
---|
[5ebff60] | 548 | static void jabber_keepalive(struct im_connection *ic) |
---|
[deff040] | 549 | { |
---|
| 550 | /* Just any whitespace character is enough as a keepalive for XMPP sessions. */ |
---|
[5ebff60] | 551 | if (!jabber_write(ic, "\n", 1)) { |
---|
[38ff846] | 552 | return; |
---|
[5ebff60] | 553 | } |
---|
| 554 | |
---|
[038d17f] | 555 | /* This runs the garbage collection every minute, which means every packet |
---|
| 556 | is in the cache for about a minute (which should be enough AFAIK). */ |
---|
[5ebff60] | 557 | jabber_cache_clean(ic); |
---|
[dd788bb] | 558 | } |
---|
| 559 | |
---|
[5ebff60] | 560 | static int jabber_send_typing(struct im_connection *ic, char *who, int typing) |
---|
[a21a8ac] | 561 | { |
---|
[0da65d5] | 562 | struct jabber_data *jd = ic->proto_data; |
---|
[a21a8ac] | 563 | struct jabber_buddy *bud; |
---|
[5ebff60] | 564 | |
---|
[a21a8ac] | 565 | /* Enable typing notification related code from now. */ |
---|
| 566 | jd->flags |= JFLAG_WANT_TYPING; |
---|
[5ebff60] | 567 | |
---|
| 568 | if ((bud = jabber_buddy_by_jid(ic, who, 0)) == NULL) { |
---|
[788a1af] | 569 | /* Sending typing notifications to unknown buddies is |
---|
| 570 | unsupported for now. Shouldn't be a problem, I think. */ |
---|
| 571 | return 0; |
---|
| 572 | } |
---|
[5ebff60] | 573 | |
---|
| 574 | if (bud->flags & JBFLAG_DOES_XEP85) { |
---|
[a21a8ac] | 575 | /* We're only allowed to send this stuff if we know the other |
---|
| 576 | side supports it. */ |
---|
[5ebff60] | 577 | |
---|
[a21a8ac] | 578 | struct xt_node *node; |
---|
| 579 | char *type; |
---|
| 580 | int st; |
---|
[5ebff60] | 581 | |
---|
| 582 | if (typing & OPT_TYPING) { |
---|
[a21a8ac] | 583 | type = "composing"; |
---|
[5ebff60] | 584 | } else if (typing & OPT_THINKING) { |
---|
[df1fb67] | 585 | type = "paused"; |
---|
[5ebff60] | 586 | } else { |
---|
[df1fb67] | 587 | type = "active"; |
---|
[5ebff60] | 588 | } |
---|
| 589 | |
---|
| 590 | node = xt_new_node(type, NULL, NULL); |
---|
| 591 | xt_add_attr(node, "xmlns", XMLNS_CHATSTATES); |
---|
| 592 | node = jabber_make_packet("message", "chat", bud->full_jid, node); |
---|
| 593 | |
---|
| 594 | st = jabber_write_packet(ic, node); |
---|
| 595 | xt_free_node(node); |
---|
| 596 | |
---|
[a21a8ac] | 597 | return st; |
---|
| 598 | } |
---|
[5ebff60] | 599 | |
---|
[a21a8ac] | 600 | return 1; |
---|
| 601 | } |
---|
| 602 | |
---|
[5ebff60] | 603 | void jabber_chat_add_settings(account_t *acc, set_t **head) |
---|
[6d544a1] | 604 | { |
---|
| 605 | /* Meh. Stupid room passwords. Not trying to obfuscate/hide |
---|
| 606 | them from the user for now. */ |
---|
[5ebff60] | 607 | set_add(head, "password", NULL, NULL, NULL); |
---|
[6d544a1] | 608 | } |
---|
| 609 | |
---|
[5ebff60] | 610 | void jabber_chat_free_settings(account_t *acc, set_t **head) |
---|
[6d544a1] | 611 | { |
---|
[5ebff60] | 612 | set_del(head, "password"); |
---|
[6d544a1] | 613 | } |
---|
| 614 | |
---|
[5ebff60] | 615 | GList *jabber_buddy_action_list(bee_user_t *bu) |
---|
[d88c92a] | 616 | { |
---|
| 617 | static GList *ret = NULL; |
---|
[5ebff60] | 618 | |
---|
| 619 | if (ret == NULL) { |
---|
[a97a336] | 620 | static const struct buddy_action ba[2] = { |
---|
[d88c92a] | 621 | { "VERSION", "Get client (version) information" }, |
---|
| 622 | }; |
---|
[5ebff60] | 623 | |
---|
| 624 | ret = g_list_prepend(ret, (void *) ba + 0); |
---|
[d88c92a] | 625 | } |
---|
[5ebff60] | 626 | |
---|
[d88c92a] | 627 | return ret; |
---|
| 628 | } |
---|
| 629 | |
---|
[5ebff60] | 630 | void *jabber_buddy_action(struct bee_user *bu, const char *action, char * const args[], void *data) |
---|
[d88c92a] | 631 | { |
---|
[5ebff60] | 632 | if (g_strcasecmp(action, "VERSION") == 0) { |
---|
[d88c92a] | 633 | struct jabber_buddy *bud; |
---|
[5ebff60] | 634 | |
---|
| 635 | if ((bud = jabber_buddy_by_ext_jid(bu->ic, bu->handle, 0)) == NULL) { |
---|
| 636 | bud = jabber_buddy_by_jid(bu->ic, bu->handle, GET_BUDDY_FIRST); |
---|
| 637 | } |
---|
| 638 | for (; bud; bud = bud->next) { |
---|
| 639 | jabber_iq_version_send(bu->ic, bud, data); |
---|
| 640 | } |
---|
[d88c92a] | 641 | } |
---|
[5ebff60] | 642 | |
---|
[d88c92a] | 643 | return NULL; |
---|
| 644 | } |
---|
| 645 | |
---|
[5ebff60] | 646 | gboolean jabber_handle_is_self(struct im_connection *ic, const char *who) |
---|
| 647 | { |
---|
[be1efa3] | 648 | struct jabber_data *jd = ic->proto_data; |
---|
[5ebff60] | 649 | |
---|
| 650 | return ((g_strcasecmp(who, ic->acc->user) == 0) || |
---|
| 651 | (jd->internal_jid && |
---|
| 652 | g_strcasecmp(who, jd->internal_jid) == 0)); |
---|
[be1efa3] | 653 | } |
---|
| 654 | |
---|
[0da65d5] | 655 | void jabber_initmodule() |
---|
[f06894d] | 656 | { |
---|
[5ebff60] | 657 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
| 658 | |
---|
[f06894d] | 659 | ret->name = "jabber"; |
---|
[6d544a1] | 660 | ret->mms = 0; /* no limit */ |
---|
[f06894d] | 661 | ret->login = jabber_login; |
---|
[0da65d5] | 662 | ret->init = jabber_init; |
---|
| 663 | ret->logout = jabber_logout; |
---|
[f6c963b] | 664 | ret->buddy_msg = jabber_buddy_msg; |
---|
[dd788bb] | 665 | ret->away_states = jabber_away_states; |
---|
| 666 | ret->set_away = jabber_set_away; |
---|
[f06894d] | 667 | // ret->set_info = jabber_set_info; |
---|
[038d17f] | 668 | ret->get_info = jabber_get_info; |
---|
[cfbb3a6] | 669 | ret->add_buddy = jabber_add_buddy; |
---|
| 670 | ret->remove_buddy = jabber_remove_buddy; |
---|
[43671b9] | 671 | ret->chat_msg = jabber_chat_msg_; |
---|
[ef5c185] | 672 | ret->chat_topic = jabber_chat_topic_; |
---|
[c058ff9] | 673 | ret->chat_invite = jabber_chat_invite_; |
---|
[e35d1a1] | 674 | ret->chat_leave = jabber_chat_leave_; |
---|
| 675 | ret->chat_join = jabber_chat_join_; |
---|
[fc0640e] | 676 | ret->chat_with = jabber_chat_with_; |
---|
[6d544a1] | 677 | ret->chat_add_settings = jabber_chat_add_settings; |
---|
| 678 | ret->chat_free_settings = jabber_chat_free_settings; |
---|
[deff040] | 679 | ret->keepalive = jabber_keepalive; |
---|
[a21a8ac] | 680 | ret->send_typing = jabber_send_typing; |
---|
[f06894d] | 681 | ret->handle_cmp = g_strcasecmp; |
---|
[be1efa3] | 682 | ret->handle_is_self = jabber_handle_is_self; |
---|
[2ff2076] | 683 | ret->transfer_request = jabber_si_transfer_request; |
---|
[d88c92a] | 684 | ret->buddy_action_list = jabber_buddy_action_list; |
---|
| 685 | ret->buddy_action = jabber_buddy_action; |
---|
[f06894d] | 686 | |
---|
[5ebff60] | 687 | register_protocol(ret); |
---|
[f06894d] | 688 | } |
---|