[5ebff60] | 1 | /********************************************************************\ |
---|
[b7d3cc34] | 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[79bb7e4] | 4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* MSN module - Switchboard server callbacks and utilities */ |
---|
| 8 | |
---|
| 9 | /* |
---|
| 10 | This program is free software; you can redistribute it and/or modify |
---|
| 11 | it under the terms of the GNU General Public License as published by |
---|
| 12 | the Free Software Foundation; either version 2 of the License, or |
---|
| 13 | (at your option) any later version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, |
---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | GNU General Public License for more details. |
---|
| 19 | |
---|
| 20 | You should have received a copy of the GNU General Public License with |
---|
| 21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
[6f10697] | 22 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[b7d3cc34] | 24 | */ |
---|
| 25 | |
---|
| 26 | #include <ctype.h> |
---|
| 27 | #include "nogaim.h" |
---|
| 28 | #include "msn.h" |
---|
| 29 | #include "md5.h" |
---|
[e5a8118] | 30 | #include "soap.h" |
---|
[b7d3cc34] | 31 | |
---|
[5ebff60] | 32 | static gboolean msn_sb_callback(gpointer data, gint source, b_input_condition cond); |
---|
| 33 | static int msn_sb_command(struct msn_handler_data *handler, char **cmd, int num_parts); |
---|
| 34 | static int msn_sb_message(struct msn_handler_data *handler, char *msg, int msglen, char **cmd, int num_parts); |
---|
[b7d3cc34] | 35 | |
---|
[5ebff60] | 36 | int msn_sb_write(struct msn_switchboard *sb, const char *fmt, ...) |
---|
[b7d3cc34] | 37 | { |
---|
[64768d4] | 38 | va_list params; |
---|
| 39 | char *out; |
---|
| 40 | size_t len; |
---|
[b7d3cc34] | 41 | int st; |
---|
[5ebff60] | 42 | |
---|
| 43 | va_start(params, fmt); |
---|
| 44 | out = g_strdup_vprintf(fmt, params); |
---|
| 45 | va_end(params); |
---|
| 46 | |
---|
| 47 | if (getenv("BITLBEE_DEBUG")) { |
---|
| 48 | fprintf(stderr, "->SB%d:%s\n", sb->fd, out); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | len = strlen(out); |
---|
| 52 | st = write(sb->fd, out, len); |
---|
| 53 | g_free(out); |
---|
| 54 | if (st != len) { |
---|
| 55 | msn_sb_destroy(sb); |
---|
[64768d4] | 56 | return 0; |
---|
[b7d3cc34] | 57 | } |
---|
[5ebff60] | 58 | |
---|
[64768d4] | 59 | return 1; |
---|
[b7d3cc34] | 60 | } |
---|
| 61 | |
---|
[5ebff60] | 62 | int msn_sb_write_msg(struct im_connection *ic, struct msn_message *m) |
---|
[a830512] | 63 | { |
---|
| 64 | struct msn_data *md = ic->proto_data; |
---|
| 65 | struct msn_switchboard *sb; |
---|
| 66 | |
---|
| 67 | /* FIXME: *CHECK* the reliability of using spare sb's! */ |
---|
[5ebff60] | 68 | if ((sb = msn_sb_spare(ic))) { |
---|
| 69 | debug("Trying to use a spare switchboard to message %s", m->who); |
---|
| 70 | |
---|
| 71 | sb->who = g_strdup(m->who); |
---|
| 72 | if (msn_sb_write(sb, "CAL %d %s\r\n", ++sb->trId, m->who)) { |
---|
[a830512] | 73 | /* He/She should join the switchboard soon, let's queue the message. */ |
---|
[5ebff60] | 74 | sb->msgq = g_slist_append(sb->msgq, m); |
---|
| 75 | return(1); |
---|
[a830512] | 76 | } |
---|
| 77 | } |
---|
[5ebff60] | 78 | |
---|
| 79 | debug("Creating a new switchboard to message %s", m->who); |
---|
| 80 | |
---|
[a830512] | 81 | /* If we reach this line, there was no spare switchboard, so let's make one. */ |
---|
[5ebff60] | 82 | if (!msn_ns_write(ic, -1, "XFR %d SB\r\n", ++md->trId)) { |
---|
| 83 | g_free(m->who); |
---|
| 84 | g_free(m->text); |
---|
| 85 | g_free(m); |
---|
| 86 | |
---|
| 87 | return(0); |
---|
[a830512] | 88 | } |
---|
[5ebff60] | 89 | |
---|
[a830512] | 90 | /* And queue the message to md. We'll pick it up when the switchboard comes up. */ |
---|
[5ebff60] | 91 | md->msgq = g_slist_append(md->msgq, m); |
---|
| 92 | |
---|
[a830512] | 93 | /* FIXME: If the switchboard creation fails, the message will not be sent. */ |
---|
[5ebff60] | 94 | |
---|
| 95 | return(1); |
---|
[a830512] | 96 | } |
---|
| 97 | |
---|
[5ebff60] | 98 | struct msn_switchboard *msn_sb_create(struct im_connection *ic, char *host, int port, char *key, int session) |
---|
[b7d3cc34] | 99 | { |
---|
[0da65d5] | 100 | struct msn_data *md = ic->proto_data; |
---|
[5ebff60] | 101 | struct msn_switchboard *sb = g_new0(struct msn_switchboard, 1); |
---|
| 102 | |
---|
| 103 | sb->fd = proxy_connect(host, port, msn_sb_connected, sb); |
---|
| 104 | if (sb->fd < 0) { |
---|
| 105 | g_free(sb); |
---|
| 106 | return(NULL); |
---|
[b7d3cc34] | 107 | } |
---|
[5ebff60] | 108 | |
---|
[0da65d5] | 109 | sb->ic = ic; |
---|
[5ebff60] | 110 | sb->key = g_strdup(key); |
---|
[b7d3cc34] | 111 | sb->session = session; |
---|
[5ebff60] | 112 | |
---|
| 113 | msn_switchboards = g_slist_append(msn_switchboards, sb); |
---|
| 114 | md->switchboards = g_slist_append(md->switchboards, sb); |
---|
| 115 | |
---|
| 116 | return(sb); |
---|
[b7d3cc34] | 117 | } |
---|
| 118 | |
---|
[5ebff60] | 119 | struct msn_switchboard *msn_sb_by_handle(struct im_connection *ic, const char *handle) |
---|
[b7d3cc34] | 120 | { |
---|
[0da65d5] | 121 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 122 | struct msn_switchboard *sb; |
---|
| 123 | GSList *l; |
---|
[5ebff60] | 124 | |
---|
| 125 | for (l = md->switchboards; l; l = l->next) { |
---|
[b7d3cc34] | 126 | sb = l->data; |
---|
[5ebff60] | 127 | if (sb->who && strcmp(sb->who, handle) == 0) { |
---|
| 128 | return(sb); |
---|
| 129 | } |
---|
[b7d3cc34] | 130 | } |
---|
[5ebff60] | 131 | |
---|
| 132 | return(NULL); |
---|
[b7d3cc34] | 133 | } |
---|
| 134 | |
---|
[5ebff60] | 135 | struct msn_switchboard *msn_sb_by_chat(struct groupchat *c) |
---|
[b7d3cc34] | 136 | { |
---|
[0da65d5] | 137 | struct msn_data *md = c->ic->proto_data; |
---|
[b7d3cc34] | 138 | struct msn_switchboard *sb; |
---|
| 139 | GSList *l; |
---|
[5ebff60] | 140 | |
---|
| 141 | for (l = md->switchboards; l; l = l->next) { |
---|
[b7d3cc34] | 142 | sb = l->data; |
---|
[5ebff60] | 143 | if (sb->chat == c) { |
---|
| 144 | return(sb); |
---|
| 145 | } |
---|
[b7d3cc34] | 146 | } |
---|
[5ebff60] | 147 | |
---|
| 148 | return(NULL); |
---|
[b7d3cc34] | 149 | } |
---|
| 150 | |
---|
[5ebff60] | 151 | struct msn_switchboard *msn_sb_spare(struct im_connection *ic) |
---|
[b7d3cc34] | 152 | { |
---|
[0da65d5] | 153 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 154 | struct msn_switchboard *sb; |
---|
| 155 | GSList *l; |
---|
[5ebff60] | 156 | |
---|
| 157 | for (l = md->switchboards; l; l = l->next) { |
---|
[b7d3cc34] | 158 | sb = l->data; |
---|
[5ebff60] | 159 | if (!sb->who && !sb->chat) { |
---|
| 160 | return(sb); |
---|
| 161 | } |
---|
[b7d3cc34] | 162 | } |
---|
[5ebff60] | 163 | |
---|
| 164 | return(NULL); |
---|
[b7d3cc34] | 165 | } |
---|
| 166 | |
---|
[5ebff60] | 167 | int msn_sb_sendmessage(struct msn_switchboard *sb, char *text) |
---|
[b7d3cc34] | 168 | { |
---|
[5ebff60] | 169 | if (sb->ready) { |
---|
[64768d4] | 170 | char *buf; |
---|
[b7d3cc34] | 171 | int i, j; |
---|
[5ebff60] | 172 | |
---|
[bd28e6a] | 173 | /* Build the message. Convert LF to CR-LF for normal messages. */ |
---|
[5ebff60] | 174 | if (strcmp(text, TYPING_NOTIFICATION_MESSAGE) == 0) { |
---|
| 175 | i = strlen(MSN_TYPING_HEADERS) + strlen(sb->ic->acc->user); |
---|
| 176 | buf = g_new0(char, i); |
---|
| 177 | i = g_snprintf(buf, i, MSN_TYPING_HEADERS, sb->ic->acc->user); |
---|
| 178 | } else if (strcmp(text, NUDGE_MESSAGE) == 0) { |
---|
| 179 | buf = g_strdup(MSN_NUDGE_HEADERS); |
---|
| 180 | i = strlen(buf); |
---|
| 181 | } else if (strcmp(text, SB_KEEPALIVE_MESSAGE) == 0) { |
---|
| 182 | buf = g_strdup(MSN_SB_KEEPALIVE_HEADERS); |
---|
| 183 | i = strlen(buf); |
---|
| 184 | } else { |
---|
| 185 | buf = g_new0(char, sizeof(MSN_MESSAGE_HEADERS) + strlen(text) * 2 + 1); |
---|
| 186 | i = strlen(MSN_MESSAGE_HEADERS); |
---|
| 187 | |
---|
| 188 | strcpy(buf, MSN_MESSAGE_HEADERS); |
---|
| 189 | for (j = 0; text[j]; j++) { |
---|
| 190 | if (text[j] == '\n') { |
---|
[b7d3cc34] | 191 | buf[i++] = '\r'; |
---|
[5ebff60] | 192 | } |
---|
| 193 | |
---|
[b7d3cc34] | 194 | buf[i++] = text[j]; |
---|
| 195 | } |
---|
| 196 | } |
---|
[5ebff60] | 197 | |
---|
[bd28e6a] | 198 | /* Build the final packet (MSG command + the message). */ |
---|
[5ebff60] | 199 | if (msn_sb_write(sb, "MSG %d N %d\r\n%s", ++sb->trId, i, buf)) { |
---|
| 200 | g_free(buf); |
---|
[64768d4] | 201 | return 1; |
---|
[5ebff60] | 202 | } else { |
---|
| 203 | g_free(buf); |
---|
[64768d4] | 204 | return 0; |
---|
[b7d3cc34] | 205 | } |
---|
[5ebff60] | 206 | } else if (sb->who) { |
---|
| 207 | struct msn_message *m = g_new0(struct msn_message, 1); |
---|
| 208 | |
---|
| 209 | m->who = g_strdup(""); |
---|
| 210 | m->text = g_strdup(text); |
---|
| 211 | sb->msgq = g_slist_append(sb->msgq, m); |
---|
| 212 | |
---|
| 213 | return(1); |
---|
| 214 | } else { |
---|
| 215 | return(0); |
---|
[b7d3cc34] | 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
[5ebff60] | 219 | struct groupchat *msn_sb_to_chat(struct msn_switchboard *sb) |
---|
[b7d3cc34] | 220 | { |
---|
[0da65d5] | 221 | struct im_connection *ic = sb->ic; |
---|
[e5abfd4] | 222 | struct groupchat *c = NULL; |
---|
[b7d3cc34] | 223 | char buf[1024]; |
---|
[5ebff60] | 224 | |
---|
[b7d3cc34] | 225 | /* Create the groupchat structure. */ |
---|
[5ebff60] | 226 | g_snprintf(buf, sizeof(buf), "MSN groupchat session %d", sb->session); |
---|
| 227 | if (sb->who) { |
---|
| 228 | c = bee_chat_by_title(ic->bee, ic, sb->who); |
---|
| 229 | } |
---|
| 230 | if (c && !msn_sb_by_chat(c)) { |
---|
[e5abfd4] | 231 | sb->chat = c; |
---|
[5ebff60] | 232 | } else { |
---|
| 233 | sb->chat = imcb_chat_new(ic, buf); |
---|
| 234 | } |
---|
| 235 | |
---|
[b7d3cc34] | 236 | /* Populate the channel. */ |
---|
[5ebff60] | 237 | if (sb->who) { |
---|
| 238 | imcb_chat_add_buddy(sb->chat, sb->who); |
---|
| 239 | } |
---|
| 240 | imcb_chat_add_buddy(sb->chat, ic->acc->user); |
---|
| 241 | |
---|
[b7d3cc34] | 242 | /* And make sure the switchboard doesn't look like a regular chat anymore. */ |
---|
[5ebff60] | 243 | if (sb->who) { |
---|
| 244 | g_free(sb->who); |
---|
[b7d3cc34] | 245 | sb->who = NULL; |
---|
| 246 | } |
---|
[5ebff60] | 247 | |
---|
[fa29d093] | 248 | return sb->chat; |
---|
[b7d3cc34] | 249 | } |
---|
| 250 | |
---|
[5ebff60] | 251 | void msn_sb_destroy(struct msn_switchboard *sb) |
---|
[b7d3cc34] | 252 | { |
---|
[0da65d5] | 253 | struct im_connection *ic = sb->ic; |
---|
| 254 | struct msn_data *md = ic->proto_data; |
---|
[5ebff60] | 255 | |
---|
| 256 | debug("Destroying switchboard: %s", sb->who ? sb->who : sb->key ? sb->key : ""); |
---|
| 257 | |
---|
| 258 | msn_msgq_purge(ic, &sb->msgq); |
---|
| 259 | msn_sb_stop_keepalives(sb); |
---|
| 260 | |
---|
| 261 | if (sb->key) { |
---|
| 262 | g_free(sb->key); |
---|
| 263 | } |
---|
| 264 | if (sb->who) { |
---|
| 265 | g_free(sb->who); |
---|
[b7d3cc34] | 266 | } |
---|
[5ebff60] | 267 | |
---|
| 268 | if (sb->chat) { |
---|
| 269 | imcb_chat_free(sb->chat); |
---|
[b7d3cc34] | 270 | } |
---|
[5ebff60] | 271 | |
---|
| 272 | if (sb->handler) { |
---|
| 273 | if (sb->handler->rxq) { |
---|
| 274 | g_free(sb->handler->rxq); |
---|
| 275 | } |
---|
| 276 | if (sb->handler->cmd_text) { |
---|
| 277 | g_free(sb->handler->cmd_text); |
---|
| 278 | } |
---|
| 279 | g_free(sb->handler); |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | if (sb->inp) { |
---|
| 283 | b_event_remove(sb->inp); |
---|
| 284 | } |
---|
| 285 | closesocket(sb->fd); |
---|
| 286 | |
---|
| 287 | msn_switchboards = g_slist_remove(msn_switchboards, sb); |
---|
| 288 | md->switchboards = g_slist_remove(md->switchboards, sb); |
---|
| 289 | g_free(sb); |
---|
[b7d3cc34] | 290 | } |
---|
| 291 | |
---|
[5ebff60] | 292 | gboolean msn_sb_connected(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 293 | { |
---|
| 294 | struct msn_switchboard *sb = data; |
---|
[0da65d5] | 295 | struct im_connection *ic; |
---|
[080c43a] | 296 | struct msn_data *md; |
---|
[b7d3cc34] | 297 | char buf[1024]; |
---|
[5ebff60] | 298 | |
---|
[b7d3cc34] | 299 | /* Are we still alive? */ |
---|
[5ebff60] | 300 | if (!g_slist_find(msn_switchboards, sb)) { |
---|
[ba9edaa] | 301 | return FALSE; |
---|
[5ebff60] | 302 | } |
---|
| 303 | |
---|
[0da65d5] | 304 | ic = sb->ic; |
---|
[080c43a] | 305 | md = ic->proto_data; |
---|
[5ebff60] | 306 | |
---|
| 307 | if (source != sb->fd) { |
---|
| 308 | debug("Error %d while connecting to switchboard server", 1); |
---|
| 309 | msn_sb_destroy(sb); |
---|
[ba9edaa] | 310 | return FALSE; |
---|
[b7d3cc34] | 311 | } |
---|
[5ebff60] | 312 | |
---|
[b7d3cc34] | 313 | /* Prepare the callback */ |
---|
[5ebff60] | 314 | sb->handler = g_new0(struct msn_handler_data, 1); |
---|
[b7d3cc34] | 315 | sb->handler->fd = sb->fd; |
---|
[5ebff60] | 316 | sb->handler->rxq = g_new0(char, 1); |
---|
[b7d3cc34] | 317 | sb->handler->data = sb; |
---|
| 318 | sb->handler->exec_command = msn_sb_command; |
---|
| 319 | sb->handler->exec_message = msn_sb_message; |
---|
[5ebff60] | 320 | |
---|
| 321 | if (sb->session == MSN_SB_NEW) { |
---|
| 322 | g_snprintf(buf, sizeof(buf), "USR %d %s;{%s} %s\r\n", ++sb->trId, ic->acc->user, md->uuid, sb->key); |
---|
| 323 | } else { |
---|
| 324 | g_snprintf(buf, sizeof(buf), "ANS %d %s;{%s} %s %d\r\n", ++sb->trId, ic->acc->user, md->uuid, sb->key, |
---|
| 325 | sb->session); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | if (msn_sb_write(sb, "%s", buf)) { |
---|
| 329 | sb->inp = b_input_add(sb->fd, B_EV_IO_READ, msn_sb_callback, sb); |
---|
| 330 | } else { |
---|
| 331 | debug("Error %d while connecting to switchboard server", 2); |
---|
| 332 | } |
---|
| 333 | |
---|
[ba9edaa] | 334 | return FALSE; |
---|
[b7d3cc34] | 335 | } |
---|
| 336 | |
---|
[5ebff60] | 337 | static gboolean msn_sb_callback(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 338 | { |
---|
| 339 | struct msn_switchboard *sb = data; |
---|
[59f527b6] | 340 | struct im_connection *ic = sb->ic; |
---|
| 341 | struct msn_data *md = ic->proto_data; |
---|
[5ebff60] | 342 | |
---|
| 343 | if (msn_handler(sb->handler) != -1) { |
---|
[f8cb76d] | 344 | return TRUE; |
---|
[5ebff60] | 345 | } |
---|
| 346 | |
---|
| 347 | if (sb->msgq != NULL) { |
---|
| 348 | time_t now = time(NULL); |
---|
| 349 | |
---|
| 350 | if (now - md->first_sb_failure > 600) { |
---|
[59f527b6] | 351 | /* It's not really the first one, but the start of this "series". |
---|
| 352 | With this, the warning below will be shown only if this happens |
---|
| 353 | at least three times in ten minutes. This algorithm isn't |
---|
| 354 | perfect, but for this purpose it will do. */ |
---|
| 355 | md->first_sb_failure = now; |
---|
| 356 | md->sb_failures = 0; |
---|
| 357 | } |
---|
[5ebff60] | 358 | |
---|
| 359 | debug("Error: Switchboard died"); |
---|
| 360 | if (++md->sb_failures >= 3) { |
---|
| 361 | imcb_log(ic, "Warning: Many switchboard failures on MSN connection. " |
---|
| 362 | "There might be problems delivering your messages."); |
---|
[f8cb76d] | 363 | } |
---|
[5ebff60] | 364 | |
---|
| 365 | if (md->msgq == NULL) { |
---|
| 366 | md->msgq = sb->msgq; |
---|
| 367 | } else { |
---|
[f8cb76d] | 368 | GSList *l; |
---|
[5ebff60] | 369 | |
---|
| 370 | for (l = md->msgq; l->next; l = l->next) { |
---|
| 371 | ; |
---|
| 372 | } |
---|
[f8cb76d] | 373 | l->next = sb->msgq; |
---|
[59f527b6] | 374 | } |
---|
[f8cb76d] | 375 | sb->msgq = NULL; |
---|
[5ebff60] | 376 | |
---|
| 377 | debug("Moved queued messages back to the main queue, " |
---|
| 378 | "creating a new switchboard to retry."); |
---|
| 379 | if (!msn_ns_write(ic, -1, "XFR %d SB\r\n", ++md->trId)) { |
---|
[f8cb76d] | 380 | return FALSE; |
---|
[5ebff60] | 381 | } |
---|
[59f527b6] | 382 | } |
---|
[5ebff60] | 383 | |
---|
| 384 | msn_sb_destroy(sb); |
---|
[f8cb76d] | 385 | return FALSE; |
---|
[b7d3cc34] | 386 | } |
---|
| 387 | |
---|
[5ebff60] | 388 | static int msn_sb_command(struct msn_handler_data *handler, char **cmd, int num_parts) |
---|
[b7d3cc34] | 389 | { |
---|
[bae0617] | 390 | struct msn_switchboard *sb = handler->data; |
---|
[0da65d5] | 391 | struct im_connection *ic = sb->ic; |
---|
[5ebff60] | 392 | |
---|
| 393 | if (!num_parts) { |
---|
[b7d3cc34] | 394 | /* Hrrm... Empty command...? Ignore? */ |
---|
[5ebff60] | 395 | return(1); |
---|
[b7d3cc34] | 396 | } |
---|
[5ebff60] | 397 | |
---|
| 398 | if (strcmp(cmd[0], "XFR") == 0) { |
---|
| 399 | imcb_error(ic, |
---|
| 400 | "Received an XFR from a switchboard server, unable to comply! This is likely to be a bug, please report it!"); |
---|
| 401 | imc_logout(ic, TRUE); |
---|
| 402 | return(0); |
---|
| 403 | } else if (strcmp(cmd[0], "USR") == 0) { |
---|
| 404 | if (num_parts < 5) { |
---|
| 405 | msn_sb_destroy(sb); |
---|
| 406 | return(0); |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | if (strcmp(cmd[2], "OK") != 0) { |
---|
| 410 | msn_sb_destroy(sb); |
---|
| 411 | return(0); |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | if (sb->who) { |
---|
| 415 | return msn_sb_write(sb, "CAL %d %s\r\n", ++sb->trId, sb->who); |
---|
| 416 | } else { |
---|
| 417 | debug("Just created a switchboard, but I don't know what to do with it."); |
---|
| 418 | } |
---|
| 419 | } else if (strcmp(cmd[0], "IRO") == 0) { |
---|
[b7d3cc34] | 420 | int num, tot; |
---|
[5ebff60] | 421 | |
---|
| 422 | if (num_parts < 6) { |
---|
| 423 | msn_sb_destroy(sb); |
---|
| 424 | return(0); |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | num = atoi(cmd[2]); |
---|
| 428 | tot = atoi(cmd[3]); |
---|
| 429 | |
---|
| 430 | if (tot <= 0) { |
---|
| 431 | msn_sb_destroy(sb); |
---|
| 432 | return(0); |
---|
| 433 | } else if (tot > 1) { |
---|
[b7d3cc34] | 434 | char buf[1024]; |
---|
[5ebff60] | 435 | |
---|
[080c43a] | 436 | /* For as much as I understand this MPOP stuff now, a |
---|
| 437 | switchboard has two (or more) roster entries per |
---|
| 438 | participant. One "bare JID" and one JID;UUID. Ignore |
---|
| 439 | the latter. */ |
---|
[5ebff60] | 440 | if (!strchr(cmd[4], ';')) { |
---|
[e9caacd] | 441 | /* HACK: Since even 1:1 chats now have >2 participants |
---|
| 442 | (ourselves included) it gets hard to tell them apart |
---|
| 443 | from rooms. Let's hope this is enough: */ |
---|
[5ebff60] | 444 | if (sb->chat == NULL && num != tot) { |
---|
| 445 | g_snprintf(buf, sizeof(buf), "MSN groupchat session %d", sb->session); |
---|
| 446 | sb->chat = imcb_chat_new(ic, buf); |
---|
| 447 | |
---|
| 448 | g_free(sb->who); |
---|
[e9caacd] | 449 | sb->who = NULL; |
---|
| 450 | } |
---|
[5ebff60] | 451 | |
---|
| 452 | if (sb->chat) { |
---|
| 453 | imcb_chat_add_buddy(sb->chat, cmd[4]); |
---|
| 454 | } |
---|
[e9caacd] | 455 | } |
---|
[5ebff60] | 456 | |
---|
[e9caacd] | 457 | /* We have the full roster, start showing the channel to |
---|
| 458 | the user. */ |
---|
[5ebff60] | 459 | if (num == tot && sb->chat) { |
---|
| 460 | imcb_chat_add_buddy(sb->chat, ic->acc->user); |
---|
[b7d3cc34] | 461 | } |
---|
| 462 | } |
---|
[5ebff60] | 463 | } else if (strcmp(cmd[0], "ANS") == 0) { |
---|
| 464 | if (num_parts < 3) { |
---|
| 465 | msn_sb_destroy(sb); |
---|
| 466 | return(0); |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | if (strcmp(cmd[2], "OK") != 0) { |
---|
| 470 | debug("Switchboard server sent a negative ANS reply"); |
---|
| 471 | msn_sb_destroy(sb); |
---|
| 472 | return(0); |
---|
| 473 | } |
---|
| 474 | |
---|
[b7d3cc34] | 475 | sb->ready = 1; |
---|
[5ebff60] | 476 | |
---|
| 477 | msn_sb_start_keepalives(sb, FALSE); |
---|
| 478 | } else if (strcmp(cmd[0], "CAL") == 0) { |
---|
| 479 | if (num_parts < 4 || !g_ascii_isdigit(cmd[3][0])) { |
---|
| 480 | msn_sb_destroy(sb); |
---|
| 481 | return(0); |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | sb->session = atoi(cmd[3]); |
---|
| 485 | } else if (strcmp(cmd[0], "JOI") == 0) { |
---|
| 486 | if (num_parts < 3) { |
---|
| 487 | msn_sb_destroy(sb); |
---|
| 488 | return(0); |
---|
| 489 | } |
---|
| 490 | |
---|
[080c43a] | 491 | /* See IRO above. Handle "bare JIDs" only. */ |
---|
[5ebff60] | 492 | if (strchr(cmd[1], ';')) { |
---|
[080c43a] | 493 | return 1; |
---|
[5ebff60] | 494 | } |
---|
| 495 | |
---|
| 496 | if (sb->who && g_strcasecmp(cmd[1], sb->who) == 0) { |
---|
[b7d3cc34] | 497 | /* The user we wanted to talk to is finally there, let's send the queued messages then. */ |
---|
| 498 | struct msn_message *m; |
---|
| 499 | GSList *l; |
---|
| 500 | int st = 1; |
---|
[5ebff60] | 501 | |
---|
| 502 | debug("%s arrived in the switchboard session, now sending queued message(s)", cmd[1]); |
---|
| 503 | |
---|
[b7d3cc34] | 504 | /* Without this, sendmessage() will put everything back on the queue... */ |
---|
| 505 | sb->ready = 1; |
---|
[5ebff60] | 506 | |
---|
| 507 | while ((l = sb->msgq)) { |
---|
[b7d3cc34] | 508 | m = l->data; |
---|
[5ebff60] | 509 | if (st) { |
---|
[b7d3cc34] | 510 | /* This hack is meant to convert a regular new chat into a groupchat */ |
---|
[5ebff60] | 511 | if (strcmp(m->text, GROUPCHAT_SWITCHBOARD_MESSAGE) == 0) { |
---|
| 512 | msn_sb_to_chat(sb); |
---|
| 513 | } else { |
---|
| 514 | st = msn_sb_sendmessage(sb, m->text); |
---|
| 515 | } |
---|
[b7d3cc34] | 516 | } |
---|
[05816dd] | 517 | sb->msgq = g_slist_remove(sb->msgq, m); |
---|
[5ebff60] | 518 | g_free(m->text); |
---|
| 519 | g_free(m->who); |
---|
| 520 | g_free(m); |
---|
[b7d3cc34] | 521 | } |
---|
[5ebff60] | 522 | |
---|
| 523 | msn_sb_start_keepalives(sb, FALSE); |
---|
| 524 | |
---|
| 525 | return(st); |
---|
| 526 | } else if (strcmp(cmd[1], ic->acc->user) == 0) { |
---|
[080c43a] | 527 | /* Well, gee thanks. Thanks for letting me know I've arrived.. */ |
---|
[5ebff60] | 528 | } else if (sb->who) { |
---|
| 529 | debug("Converting chat with %s to a groupchat because %s joined the session.", sb->who, cmd[1]); |
---|
| 530 | |
---|
[b7d3cc34] | 531 | /* This SB is a one-to-one chat right now, but someone else is joining. */ |
---|
[5ebff60] | 532 | msn_sb_to_chat(sb); |
---|
| 533 | |
---|
| 534 | imcb_chat_add_buddy(sb->chat, cmd[1]); |
---|
| 535 | } else if (sb->chat) { |
---|
| 536 | imcb_chat_add_buddy(sb->chat, cmd[1]); |
---|
[b7d3cc34] | 537 | sb->ready = 1; |
---|
[5ebff60] | 538 | } else { |
---|
[b7d3cc34] | 539 | /* PANIC! */ |
---|
| 540 | } |
---|
[5ebff60] | 541 | } else if (strcmp(cmd[0], "MSG") == 0) { |
---|
| 542 | if (num_parts < 4) { |
---|
| 543 | msn_sb_destroy(sb); |
---|
| 544 | return(0); |
---|
[3585c5a] | 545 | } |
---|
[5ebff60] | 546 | |
---|
| 547 | sb->handler->msglen = atoi(cmd[3]); |
---|
| 548 | |
---|
| 549 | if (sb->handler->msglen <= 0) { |
---|
| 550 | debug("Received a corrupted message on the switchboard, the switchboard will be closed"); |
---|
| 551 | msn_sb_destroy(sb); |
---|
| 552 | return(0); |
---|
| 553 | } |
---|
| 554 | } else if (strcmp(cmd[0], "NAK") == 0) { |
---|
| 555 | if (sb->who) { |
---|
| 556 | imcb_log(ic, "The MSN servers could not deliver one of your messages to %s.", sb->who); |
---|
| 557 | } else { |
---|
| 558 | imcb_log(ic, |
---|
| 559 | "The MSN servers could not deliver one of your groupchat messages to all participants."); |
---|
| 560 | } |
---|
| 561 | } else if (strcmp(cmd[0], "BYE") == 0) { |
---|
| 562 | if (num_parts < 2) { |
---|
| 563 | msn_sb_destroy(sb); |
---|
| 564 | return(0); |
---|
[3585c5a] | 565 | } |
---|
[5ebff60] | 566 | |
---|
[b7d3cc34] | 567 | /* if( cmd[2] && *cmd[2] == '1' ) -=> Chat is being cleaned up because of idleness */ |
---|
[5ebff60] | 568 | |
---|
| 569 | if (sb->who) { |
---|
| 570 | msn_sb_stop_keepalives(sb); |
---|
| 571 | |
---|
[b7d3cc34] | 572 | /* This is a single-person chat, and the other person is leaving. */ |
---|
[5ebff60] | 573 | g_free(sb->who); |
---|
[b7d3cc34] | 574 | sb->who = NULL; |
---|
| 575 | sb->ready = 0; |
---|
[5ebff60] | 576 | |
---|
| 577 | debug("Person %s left the one-to-one switchboard connection. Keeping it around as a spare...", |
---|
| 578 | cmd[1]); |
---|
| 579 | |
---|
[b7d3cc34] | 580 | /* We could clean up the switchboard now, but keeping it around |
---|
| 581 | as a spare for a next conversation sounds more sane to me. |
---|
| 582 | The server will clean it up when it's idle for too long. */ |
---|
[5ebff60] | 583 | } else if (sb->chat && !strchr(cmd[1], ';')) { |
---|
| 584 | imcb_chat_remove_buddy(sb->chat, cmd[1], ""); |
---|
| 585 | } else { |
---|
[b7d3cc34] | 586 | /* PANIC! */ |
---|
| 587 | } |
---|
[5ebff60] | 588 | } else if (g_ascii_isdigit(cmd[0][0])) { |
---|
| 589 | int num = atoi(cmd[0]); |
---|
| 590 | const struct msn_status_code *err = msn_status_by_number(num); |
---|
| 591 | |
---|
[bc090f0] | 592 | /* If the person is offline, send an offline message instead, |
---|
| 593 | and don't report an error. */ |
---|
[5ebff60] | 594 | if (num == 217) { |
---|
| 595 | msn_ns_oim_send_queue(ic, &sb->msgq); |
---|
| 596 | } else { |
---|
| 597 | imcb_error(ic, "Error reported by switchboard server: %s", err->text); |
---|
[b7d3cc34] | 598 | } |
---|
[5ebff60] | 599 | |
---|
| 600 | if (err->flags & STATUS_SB_FATAL) { |
---|
| 601 | msn_sb_destroy(sb); |
---|
[6048744] | 602 | return 0; |
---|
[5ebff60] | 603 | } else if (err->flags & STATUS_FATAL) { |
---|
| 604 | imc_logout(ic, TRUE); |
---|
| 605 | return 0; |
---|
| 606 | } else if (err->flags & STATUS_SB_IM_SPARE) { |
---|
| 607 | if (sb->who) { |
---|
[3b9390b] | 608 | /* Apparently some invitation failed. We might want to use this |
---|
| 609 | board later, so keep it as a spare. */ |
---|
[5ebff60] | 610 | g_free(sb->who); |
---|
[3b9390b] | 611 | sb->who = NULL; |
---|
[5ebff60] | 612 | |
---|
[3b9390b] | 613 | /* Also clear the msgq, otherwise someone else might get them. */ |
---|
[5ebff60] | 614 | msn_msgq_purge(ic, &sb->msgq); |
---|
[3b9390b] | 615 | } |
---|
[5ebff60] | 616 | |
---|
[6048744] | 617 | /* Do NOT return 0 here, we want to keep this sb. */ |
---|
[3b9390b] | 618 | } |
---|
[5ebff60] | 619 | } else { |
---|
[8ff0a61] | 620 | /* debug( "Received unknown command from switchboard server: %s", cmd[0] ); */ |
---|
[b7d3cc34] | 621 | } |
---|
[5ebff60] | 622 | |
---|
| 623 | return(1); |
---|
[b7d3cc34] | 624 | } |
---|
| 625 | |
---|
[5ebff60] | 626 | static int msn_sb_message(struct msn_handler_data *handler, char *msg, int msglen, char **cmd, int num_parts) |
---|
[b7d3cc34] | 627 | { |
---|
[bae0617] | 628 | struct msn_switchboard *sb = handler->data; |
---|
[0da65d5] | 629 | struct im_connection *ic = sb->ic; |
---|
[b7d3cc34] | 630 | char *body; |
---|
[5ebff60] | 631 | |
---|
| 632 | if (!num_parts) { |
---|
| 633 | return(1); |
---|
| 634 | } |
---|
| 635 | |
---|
| 636 | if ((body = strstr(msg, "\r\n\r\n"))) { |
---|
[b7d3cc34] | 637 | body += 4; |
---|
[5ebff60] | 638 | } |
---|
| 639 | |
---|
| 640 | if (strcmp(cmd[0], "MSG") == 0) { |
---|
| 641 | char *ct = get_rfc822_header(msg, "Content-Type:", msglen); |
---|
| 642 | |
---|
| 643 | if (!ct) { |
---|
| 644 | return(1); |
---|
| 645 | } |
---|
| 646 | |
---|
| 647 | if (g_strncasecmp(ct, "text/plain", 10) == 0) { |
---|
| 648 | g_free(ct); |
---|
| 649 | |
---|
| 650 | if (!body) { |
---|
| 651 | return(1); |
---|
[b7d3cc34] | 652 | } |
---|
[5ebff60] | 653 | |
---|
| 654 | if (sb->who) { |
---|
| 655 | imcb_buddy_msg(ic, cmd[1], body, 0, 0); |
---|
| 656 | } else if (sb->chat) { |
---|
| 657 | imcb_chat_msg(sb->chat, cmd[1], body, 0, 0); |
---|
| 658 | } else { |
---|
[b7d3cc34] | 659 | /* PANIC! */ |
---|
| 660 | } |
---|
| 661 | } |
---|
[5ebff60] | 662 | else if (g_strncasecmp(ct, "application/x-msnmsgrp2p", 24) == 0) { |
---|
[9b1d2d6] | 663 | /* Not currently implemented. Don't warn about it since |
---|
| 664 | this seems to be used for avatars now. */ |
---|
[5ebff60] | 665 | g_free(ct); |
---|
| 666 | } else if (g_strncasecmp(ct, "text/x-msmsgscontrol", 20) == 0) { |
---|
| 667 | char *who = get_rfc822_header(msg, "TypingUser:", msglen); |
---|
| 668 | |
---|
| 669 | if (who) { |
---|
| 670 | imcb_buddy_typing(ic, who, OPT_TYPING); |
---|
| 671 | g_free(who); |
---|
[b7d3cc34] | 672 | } |
---|
[5ebff60] | 673 | |
---|
| 674 | g_free(ct); |
---|
| 675 | } else { |
---|
| 676 | g_free(ct); |
---|
[b7d3cc34] | 677 | } |
---|
| 678 | } |
---|
[5ebff60] | 679 | |
---|
| 680 | return(1); |
---|
[b7d3cc34] | 681 | } |
---|
[9bf2481] | 682 | |
---|
[5ebff60] | 683 | static gboolean msn_sb_keepalive(gpointer data, gint source, b_input_condition cond) |
---|
[9bf2481] | 684 | { |
---|
| 685 | struct msn_switchboard *sb = data; |
---|
[5ebff60] | 686 | |
---|
| 687 | return sb->ready && msn_sb_sendmessage(sb, SB_KEEPALIVE_MESSAGE); |
---|
[9bf2481] | 688 | } |
---|
[bb839e8] | 689 | |
---|
[5ebff60] | 690 | void msn_sb_start_keepalives(struct msn_switchboard *sb, gboolean initial) |
---|
[bb839e8] | 691 | { |
---|
[f924563] | 692 | bee_user_t *bu; |
---|
[5ebff60] | 693 | |
---|
| 694 | if (sb && sb->who && sb->keepalive == 0 && |
---|
| 695 | (bu = bee_user_by_handle(sb->ic->bee, sb->ic, sb->who)) && |
---|
| 696 | !(bu->flags & BEE_USER_ONLINE) && |
---|
| 697 | set_getbool(&sb->ic->acc->set, "switchboard_keepalives")) { |
---|
| 698 | if (initial) { |
---|
| 699 | msn_sb_keepalive(sb, 0, 0); |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | sb->keepalive = b_timeout_add(20000, msn_sb_keepalive, sb); |
---|
[bb839e8] | 703 | } |
---|
| 704 | } |
---|
| 705 | |
---|
[5ebff60] | 706 | void msn_sb_stop_keepalives(struct msn_switchboard *sb) |
---|
[bb839e8] | 707 | { |
---|
[5ebff60] | 708 | if (sb && sb->keepalive > 0) { |
---|
| 709 | b_event_remove(sb->keepalive); |
---|
[bb839e8] | 710 | sb->keepalive = 0; |
---|
| 711 | } |
---|
| 712 | } |
---|