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