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