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