[5ebff60] | 1 | /********************************************************************\ |
---|
[b7d3cc34] | 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[f9258ae] | 4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* MSN module - Notification server callbacks */ |
---|
| 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> |
---|
[f9258ae] | 27 | #include <sys/utsname.h> |
---|
[b7d3cc34] | 28 | #include "nogaim.h" |
---|
| 29 | #include "msn.h" |
---|
| 30 | #include "md5.h" |
---|
[f9258ae] | 31 | #include "sha1.h" |
---|
[7db65b7] | 32 | #include "soap.h" |
---|
[ca7de3a] | 33 | #include "xmltree.h" |
---|
[b7d3cc34] | 34 | |
---|
[5ebff60] | 35 | static gboolean msn_ns_connected(gpointer data, gint source, b_input_condition cond); |
---|
| 36 | static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond); |
---|
[b7d3cc34] | 37 | |
---|
[5ebff60] | 38 | static void msn_ns_send_adl_start(struct im_connection *ic); |
---|
| 39 | static void msn_ns_send_adl(struct im_connection *ic); |
---|
[b7d3cc34] | 40 | |
---|
[5ebff60] | 41 | int msn_ns_write(struct im_connection *ic, int fd, const char *fmt, ...) |
---|
[64768d4] | 42 | { |
---|
| 43 | struct msn_data *md = ic->proto_data; |
---|
| 44 | va_list params; |
---|
| 45 | char *out; |
---|
| 46 | size_t len; |
---|
| 47 | int st; |
---|
[5ebff60] | 48 | |
---|
| 49 | va_start(params, fmt); |
---|
| 50 | out = g_strdup_vprintf(fmt, params); |
---|
| 51 | va_end(params); |
---|
| 52 | |
---|
| 53 | if (fd < 0) { |
---|
[11e42dc] | 54 | fd = md->fd; |
---|
[5ebff60] | 55 | } |
---|
| 56 | |
---|
| 57 | if (getenv("BITLBEE_DEBUG")) { |
---|
[693aca0] | 58 | fprintf(stderr, "\x1b[91m>>>[NS%d] %s\n\x1b[97m", fd, out); |
---|
[5ebff60] | 59 | } |
---|
| 60 | |
---|
| 61 | len = strlen(out); |
---|
| 62 | st = write(fd, out, len); |
---|
| 63 | g_free(out); |
---|
| 64 | if (st != len) { |
---|
| 65 | imcb_error(ic, "Short write() to main server"); |
---|
| 66 | imc_logout(ic, TRUE); |
---|
[64768d4] | 67 | return 0; |
---|
| 68 | } |
---|
[5ebff60] | 69 | |
---|
[64768d4] | 70 | return 1; |
---|
| 71 | } |
---|
| 72 | |
---|
[11e42dc] | 73 | gboolean msn_ns_connect(struct im_connection *ic, const char *host, int port) |
---|
[b7d3cc34] | 74 | { |
---|
[11e42dc] | 75 | struct msn_data *handler = ic->proto_data; |
---|
| 76 | |
---|
[5ebff60] | 77 | if (handler->fd >= 0) { |
---|
| 78 | closesocket(handler->fd); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | handler->fd = proxy_connect(host, port, msn_ns_connected, handler); |
---|
| 82 | if (handler->fd < 0) { |
---|
| 83 | imcb_error(ic, "Could not connect to server"); |
---|
| 84 | imc_logout(ic, TRUE); |
---|
[ba9edaa] | 85 | return FALSE; |
---|
[b7d3cc34] | 86 | } |
---|
[5ebff60] | 87 | |
---|
[bae0617] | 88 | return TRUE; |
---|
| 89 | } |
---|
| 90 | |
---|
[5ebff60] | 91 | static gboolean msn_ns_connected(gpointer data, gint source, b_input_condition cond) |
---|
[bae0617] | 92 | { |
---|
[11e42dc] | 93 | struct msn_data *md = data; |
---|
| 94 | struct msn_data *handler = md; |
---|
| 95 | struct im_connection *ic = md->ic; |
---|
[5ebff60] | 96 | |
---|
| 97 | if (source == -1) { |
---|
| 98 | imcb_error(ic, "Could not connect to server"); |
---|
| 99 | imc_logout(ic, TRUE); |
---|
[bae0617] | 100 | return FALSE; |
---|
[b7d3cc34] | 101 | } |
---|
[5ebff60] | 102 | |
---|
| 103 | g_free(handler->rxq); |
---|
[bae0617] | 104 | handler->rxlen = 0; |
---|
[5ebff60] | 105 | handler->rxq = g_new0(char, 1); |
---|
| 106 | |
---|
| 107 | if (md->uuid == NULL) { |
---|
[f9258ae] | 108 | struct utsname name; |
---|
| 109 | sha1_state_t sha[1]; |
---|
[5ebff60] | 110 | |
---|
[f9258ae] | 111 | /* UUID == SHA1("BitlBee" + my hostname + MSN username) */ |
---|
[5ebff60] | 112 | sha1_init(sha); |
---|
| 113 | sha1_append(sha, (void *) "BitlBee", 7); |
---|
| 114 | if (uname(&name) == 0) { |
---|
| 115 | sha1_append(sha, (void *) name.nodename, strlen(name.nodename)); |
---|
[f9258ae] | 116 | } |
---|
[5ebff60] | 117 | sha1_append(sha, (void *) ic->acc->user, strlen(ic->acc->user)); |
---|
| 118 | md->uuid = sha1_random_uuid(sha); |
---|
| 119 | memcpy(md->uuid, "b171be3e", 8); /* :-P */ |
---|
[f9258ae] | 120 | } |
---|
[5ebff60] | 121 | |
---|
| 122 | if (msn_ns_write(ic, source, "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER)) { |
---|
| 123 | handler->inpa = b_input_add(handler->fd, B_EV_IO_READ, msn_ns_callback, handler); |
---|
| 124 | imcb_log(ic, "Connected to server, waiting for reply"); |
---|
[b7d3cc34] | 125 | } |
---|
[5ebff60] | 126 | |
---|
[ba9edaa] | 127 | return FALSE; |
---|
[b7d3cc34] | 128 | } |
---|
| 129 | |
---|
[11e42dc] | 130 | void msn_ns_close(struct msn_data *handler) |
---|
[bae0617] | 131 | { |
---|
[5ebff60] | 132 | if (handler->fd >= 0) { |
---|
| 133 | closesocket(handler->fd); |
---|
| 134 | b_event_remove(handler->inpa); |
---|
[bae0617] | 135 | } |
---|
[5ebff60] | 136 | |
---|
[bae0617] | 137 | handler->fd = handler->inpa = -1; |
---|
[5ebff60] | 138 | g_free(handler->rxq); |
---|
| 139 | g_free(handler->cmd_text); |
---|
| 140 | |
---|
[bae0617] | 141 | handler->rxlen = 0; |
---|
| 142 | handler->rxq = NULL; |
---|
| 143 | handler->cmd_text = NULL; |
---|
| 144 | } |
---|
| 145 | |
---|
[5ebff60] | 146 | static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 147 | { |
---|
[11e42dc] | 148 | struct msn_data *handler = data; |
---|
| 149 | struct im_connection *ic = handler->ic; |
---|
[5ebff60] | 150 | |
---|
| 151 | if (msn_handler(handler) == -1) { /* Don't do this on ret == 0, it's already done then. */ |
---|
| 152 | imcb_error(ic, "Error while reading from server"); |
---|
| 153 | imc_logout(ic, TRUE); |
---|
| 154 | |
---|
[ba9edaa] | 155 | return FALSE; |
---|
[5ebff60] | 156 | } else { |
---|
[ba9edaa] | 157 | return TRUE; |
---|
[5ebff60] | 158 | } |
---|
[b7d3cc34] | 159 | } |
---|
| 160 | |
---|
[11e42dc] | 161 | int msn_ns_command(struct msn_data *handler, char **cmd, int num_parts) |
---|
[b7d3cc34] | 162 | { |
---|
[11e42dc] | 163 | struct im_connection *ic = handler->ic; |
---|
| 164 | struct msn_data *md = handler; |
---|
[5ebff60] | 165 | |
---|
| 166 | if (num_parts == 0) { |
---|
[b7d3cc34] | 167 | /* Hrrm... Empty command...? Ignore? */ |
---|
[5ebff60] | 168 | return(1); |
---|
[b7d3cc34] | 169 | } |
---|
[5ebff60] | 170 | |
---|
| 171 | if (strcmp(cmd[0], "VER") == 0) { |
---|
| 172 | if (cmd[2] && strncmp(cmd[2], MSNP_VER, 5) != 0) { |
---|
| 173 | imcb_error(ic, "Unsupported protocol"); |
---|
| 174 | imc_logout(ic, FALSE); |
---|
| 175 | return(0); |
---|
[b7d3cc34] | 176 | } |
---|
[5ebff60] | 177 | |
---|
[254a4da] | 178 | return(msn_ns_write(ic, handler->fd, "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s VmVyc2lvbjogMQ0KWGZyQ291bnQ6IDINClhmclNlbnRVVENUaW1lOiA2MzU2MTQ3OTU5NzgzOTAwMDANCklzR2VvWGZyOiB0cnVlDQo=\r\n", |
---|
[5ebff60] | 179 | ++md->trId, ic->acc->user)); |
---|
| 180 | } else if (strcmp(cmd[0], "CVR") == 0) { |
---|
[b7d3cc34] | 181 | /* We don't give a damn about the information we just received */ |
---|
[5ebff60] | 182 | return msn_ns_write(ic, handler->fd, "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user); |
---|
| 183 | } else if (strcmp(cmd[0], "XFR") == 0) { |
---|
[b7d3cc34] | 184 | char *server; |
---|
| 185 | int port; |
---|
[5ebff60] | 186 | |
---|
| 187 | if (num_parts >= 6 && strcmp(cmd[2], "NS") == 0) { |
---|
| 188 | b_event_remove(handler->inpa); |
---|
[bae0617] | 189 | handler->inpa = -1; |
---|
[5ebff60] | 190 | |
---|
| 191 | server = strchr(cmd[3], ':'); |
---|
| 192 | if (!server) { |
---|
| 193 | imcb_error(ic, "Syntax error"); |
---|
| 194 | imc_logout(ic, TRUE); |
---|
| 195 | return(0); |
---|
[b7d3cc34] | 196 | } |
---|
| 197 | *server = 0; |
---|
[5ebff60] | 198 | port = atoi(server + 1); |
---|
[b7d3cc34] | 199 | server = cmd[3]; |
---|
[5ebff60] | 200 | |
---|
| 201 | imcb_log(ic, "Transferring to other server"); |
---|
[11e42dc] | 202 | return msn_ns_connect(ic, server, port); |
---|
[5ebff60] | 203 | } else { |
---|
| 204 | imcb_error(ic, "Syntax error"); |
---|
| 205 | imc_logout(ic, TRUE); |
---|
| 206 | return(0); |
---|
| 207 | } |
---|
| 208 | } else if (strcmp(cmd[0], "USR") == 0) { |
---|
| 209 | if (num_parts >= 6 && strcmp(cmd[2], "SSO") == 0 && |
---|
| 210 | strcmp(cmd[3], "S") == 0) { |
---|
| 211 | g_free(md->pp_policy); |
---|
| 212 | md->pp_policy = g_strdup(cmd[4]); |
---|
| 213 | msn_soap_passport_sso_request(ic, cmd[5]); |
---|
| 214 | } else if (strcmp(cmd[2], "OK") == 0) { |
---|
[ed0589c] | 215 | /* If the number after the handle is 0, the e-mail |
---|
| 216 | address is unverified, which means we can't change |
---|
| 217 | the display name. */ |
---|
[5ebff60] | 218 | if (cmd[4][0] == '0') { |
---|
[ed0589c] | 219 | md->flags |= MSN_EMAIL_UNVERIFIED; |
---|
[5ebff60] | 220 | } |
---|
| 221 | |
---|
| 222 | imcb_log(ic, "Authenticated, getting buddy list"); |
---|
| 223 | msn_soap_memlist_request(ic); |
---|
| 224 | } else { |
---|
| 225 | imcb_error(ic, "Unknown authentication type"); |
---|
| 226 | imc_logout(ic, FALSE); |
---|
| 227 | return(0); |
---|
| 228 | } |
---|
| 229 | } else if (strcmp(cmd[0], "MSG") == 0) { |
---|
| 230 | if (num_parts < 4) { |
---|
| 231 | imcb_error(ic, "Syntax error"); |
---|
| 232 | imc_logout(ic, TRUE); |
---|
| 233 | return(0); |
---|
[e5854a8] | 234 | } |
---|
[5ebff60] | 235 | |
---|
| 236 | handler->msglen = atoi(cmd[3]); |
---|
| 237 | |
---|
| 238 | if (handler->msglen <= 0) { |
---|
| 239 | imcb_error(ic, "Syntax error"); |
---|
| 240 | imc_logout(ic, TRUE); |
---|
| 241 | return(0); |
---|
| 242 | } |
---|
| 243 | } else if (strcmp(cmd[0], "ADL") == 0) { |
---|
| 244 | if (num_parts >= 3 && strcmp(cmd[2], "OK") == 0) { |
---|
| 245 | msn_ns_send_adl(ic); |
---|
| 246 | return msn_ns_finish_login(ic); |
---|
| 247 | } else if (num_parts >= 3) { |
---|
| 248 | handler->msglen = atoi(cmd[2]); |
---|
| 249 | } |
---|
| 250 | } else if (strcmp(cmd[0], "CHL") == 0) { |
---|
[be7a180] | 251 | char *resp; |
---|
[64768d4] | 252 | int st; |
---|
[5ebff60] | 253 | |
---|
| 254 | if (num_parts < 3) { |
---|
| 255 | imcb_error(ic, "Syntax error"); |
---|
| 256 | imc_logout(ic, TRUE); |
---|
| 257 | return(0); |
---|
[b7d3cc34] | 258 | } |
---|
[5ebff60] | 259 | |
---|
| 260 | resp = msn_p11_challenge(cmd[2]); |
---|
| 261 | |
---|
| 262 | st = msn_ns_write(ic, -1, "QRY %d %s %zd\r\n%s", |
---|
| 263 | ++md->trId, MSNP11_PROD_ID, |
---|
| 264 | strlen(resp), resp); |
---|
| 265 | g_free(resp); |
---|
[64768d4] | 266 | return st; |
---|
[5ebff60] | 267 | } else if (strcmp(cmd[0], "OUT") == 0) { |
---|
[c2fb3809] | 268 | int allow_reconnect = TRUE; |
---|
[5ebff60] | 269 | |
---|
| 270 | if (cmd[1] && strcmp(cmd[1], "OTH") == 0) { |
---|
| 271 | imcb_error(ic, "Someone else logged in with your account"); |
---|
[c2fb3809] | 272 | allow_reconnect = FALSE; |
---|
[5ebff60] | 273 | } else if (cmd[1] && strcmp(cmd[1], "SSD") == 0) { |
---|
| 274 | imcb_error(ic, "Terminating session because of server shutdown"); |
---|
| 275 | } else { |
---|
| 276 | imcb_error(ic, "Session terminated by remote server (%s)", |
---|
| 277 | cmd[1] ? cmd[1] : "reason unknown)"); |
---|
[b7d3cc34] | 278 | } |
---|
[5ebff60] | 279 | |
---|
| 280 | imc_logout(ic, allow_reconnect); |
---|
| 281 | return(0); |
---|
[d550358] | 282 | } else if (strcmp(cmd[0], "GCF") == 0) { |
---|
[5fecede] | 283 | /* Coming up is cmd[2] bytes of stuff we're supposed to |
---|
| 284 | censore. Meh. */ |
---|
[5ebff60] | 285 | handler->msglen = atoi(cmd[2]); |
---|
[11e42dc] | 286 | } else if ((strcmp(cmd[0], "NFY") == 0) || (strcmp(cmd[0], "SDG") == 0)) { |
---|
[254a4da] | 287 | if (num_parts >= 3) { |
---|
| 288 | handler->msglen = atoi(cmd[2]); |
---|
| 289 | } |
---|
[5ebff60] | 290 | } else if (strcmp(cmd[0], "QNG") == 0) { |
---|
[e132b60] | 291 | ic->flags |= OPT_PONGED; |
---|
[5ebff60] | 292 | } else if (g_ascii_isdigit(cmd[0][0])) { |
---|
| 293 | int num = atoi(cmd[0]); |
---|
| 294 | const struct msn_status_code *err = msn_status_by_number(num); |
---|
| 295 | |
---|
| 296 | imcb_error(ic, "Error reported by MSN server: %s", err->text); |
---|
| 297 | |
---|
| 298 | if (err->flags & STATUS_FATAL) { |
---|
| 299 | imc_logout(ic, TRUE); |
---|
| 300 | return(0); |
---|
[b7d3cc34] | 301 | } |
---|
[5ebff60] | 302 | |
---|
[02bb9db] | 303 | /* Oh yes, errors can have payloads too now. Discard them for now. */ |
---|
[5ebff60] | 304 | if (num_parts >= 3) { |
---|
| 305 | handler->msglen = atoi(cmd[2]); |
---|
| 306 | } |
---|
| 307 | } else { |
---|
[d550358] | 308 | imcb_error(ic, "Received unknown command from main server: %s", cmd[0]); |
---|
[b7d3cc34] | 309 | } |
---|
[5ebff60] | 310 | |
---|
| 311 | return(1); |
---|
[b7d3cc34] | 312 | } |
---|
| 313 | |
---|
[11e42dc] | 314 | int msn_ns_message(struct msn_data *handler, char *msg, int msglen, char **cmd, int num_parts) |
---|
[b7d3cc34] | 315 | { |
---|
[11e42dc] | 316 | struct im_connection *ic = handler->ic; |
---|
[b7d3cc34] | 317 | char *body; |
---|
| 318 | int blen = 0; |
---|
[5ebff60] | 319 | |
---|
| 320 | if (!num_parts) { |
---|
| 321 | return(1); |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | if ((body = strstr(msg, "\r\n\r\n"))) { |
---|
[b7d3cc34] | 325 | body += 4; |
---|
[5ebff60] | 326 | blen = msglen - (body - msg); |
---|
[b7d3cc34] | 327 | } |
---|
[5ebff60] | 328 | |
---|
| 329 | if (strcmp(cmd[0], "MSG") == 0) { |
---|
| 330 | if (g_strcasecmp(cmd[1], "Hotmail") == 0) { |
---|
| 331 | char *ct = get_rfc822_header(msg, "Content-Type:", msglen); |
---|
| 332 | |
---|
| 333 | if (!ct) { |
---|
| 334 | return(1); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | if (g_strncasecmp(ct, "application/x-msmsgssystemmessage", 33) == 0) { |
---|
[b7d3cc34] | 338 | char *mtype; |
---|
| 339 | char *arg1; |
---|
[5ebff60] | 340 | |
---|
| 341 | if (!body) { |
---|
| 342 | return(1); |
---|
[b7d3cc34] | 343 | } |
---|
[5ebff60] | 344 | |
---|
| 345 | mtype = get_rfc822_header(body, "Type:", blen); |
---|
| 346 | arg1 = get_rfc822_header(body, "Arg1:", blen); |
---|
| 347 | |
---|
| 348 | if (mtype && strcmp(mtype, "1") == 0) { |
---|
| 349 | if (arg1) { |
---|
| 350 | imcb_log(ic, "The server is going down for maintenance in %s minutes.", |
---|
| 351 | arg1); |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | g_free(arg1); |
---|
| 356 | g_free(mtype); |
---|
| 357 | } else if (g_strncasecmp(ct, "text/x-msmsgsprofile", 20) == 0) { |
---|
[b7d3cc34] | 358 | /* We don't care about this profile for now... */ |
---|
[5ebff60] | 359 | } else if (g_strncasecmp(ct, "text/x-msmsgsinitialemailnotification", 37) == 0) { |
---|
| 360 | if (set_getbool(&ic->acc->set, "mail_notifications")) { |
---|
| 361 | char *inbox = get_rfc822_header(body, "Inbox-Unread:", blen); |
---|
| 362 | char *folders = get_rfc822_header(body, "Folders-Unread:", blen); |
---|
| 363 | |
---|
| 364 | if (inbox && folders) { |
---|
| 365 | imcb_log(ic, |
---|
| 366 | "INBOX contains %s new messages, plus %s messages in other folders.", inbox, |
---|
| 367 | folders); |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | g_free(inbox); |
---|
| 371 | g_free(folders); |
---|
[b7d3cc34] | 372 | } |
---|
[5ebff60] | 373 | } else if (g_strncasecmp(ct, "text/x-msmsgsemailnotification", 30) == 0) { |
---|
| 374 | if (set_getbool(&ic->acc->set, "mail_notifications")) { |
---|
| 375 | char *from = get_rfc822_header(body, "From-Addr:", blen); |
---|
| 376 | char *fromname = get_rfc822_header(body, "From:", blen); |
---|
| 377 | |
---|
| 378 | if (from && fromname) { |
---|
| 379 | imcb_log(ic, "Received an e-mail message from %s <%s>.", fromname, |
---|
| 380 | from); |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | g_free(from); |
---|
| 384 | g_free(fromname); |
---|
[b7d3cc34] | 385 | } |
---|
[5ebff60] | 386 | } else if (g_strncasecmp(ct, "text/x-msmsgsactivemailnotification", 35) == 0) { |
---|
[d550358] | 387 | /* Notification that a message has been read... Ignore it */ |
---|
[5ebff60] | 388 | } else { |
---|
| 389 | debug("Can't handle %s packet from notification server", ct); |
---|
[b7d3cc34] | 390 | } |
---|
[5ebff60] | 391 | |
---|
| 392 | g_free(ct); |
---|
[b7d3cc34] | 393 | } |
---|
[5ebff60] | 394 | } else if (strcmp(cmd[0], "ADL") == 0) { |
---|
[e5854a8] | 395 | struct xt_node *adl, *d, *c; |
---|
[5ebff60] | 396 | |
---|
| 397 | if (!(adl = xt_from_string(msg, msglen))) { |
---|
[e5854a8] | 398 | return 1; |
---|
[5ebff60] | 399 | } |
---|
| 400 | |
---|
| 401 | for (d = adl->children; d; d = d->next) { |
---|
[e5854a8] | 402 | char *dn; |
---|
[5ebff60] | 403 | if (strcmp(d->name, "d") != 0 || |
---|
| 404 | (dn = xt_find_attr(d, "n")) == NULL) { |
---|
[e5854a8] | 405 | continue; |
---|
[5ebff60] | 406 | } |
---|
| 407 | for (c = d->children; c; c = c->next) { |
---|
[e5854a8] | 408 | bee_user_t *bu; |
---|
| 409 | struct msn_buddy_data *bd; |
---|
| 410 | char *cn, *handle, *f, *l; |
---|
| 411 | int flags; |
---|
[5ebff60] | 412 | |
---|
| 413 | if (strcmp(c->name, "c") != 0 || |
---|
| 414 | (l = xt_find_attr(c, "l")) == NULL || |
---|
| 415 | (cn = xt_find_attr(c, "n")) == NULL) { |
---|
[e5854a8] | 416 | continue; |
---|
[5ebff60] | 417 | } |
---|
| 418 | |
---|
[3901b5d] | 419 | /* FIXME: Use "t" here, guess I should just add it |
---|
| 420 | as a prefix like elsewhere in the protocol. */ |
---|
[5ebff60] | 421 | handle = g_strdup_printf("%s@%s", cn, dn); |
---|
| 422 | if (!((bu = bee_user_by_handle(ic->bee, ic, handle)) || |
---|
| 423 | (bu = bee_user_new(ic->bee, ic, handle, 0)))) { |
---|
| 424 | g_free(handle); |
---|
[e5854a8] | 425 | continue; |
---|
| 426 | } |
---|
[5ebff60] | 427 | g_free(handle); |
---|
[e5854a8] | 428 | bd = bu->data; |
---|
[5ebff60] | 429 | |
---|
| 430 | if ((f = xt_find_attr(c, "f"))) { |
---|
| 431 | http_decode(f); |
---|
| 432 | imcb_rename_buddy(ic, bu->handle, f); |
---|
[e5854a8] | 433 | } |
---|
[5ebff60] | 434 | |
---|
| 435 | flags = atoi(l) & 15; |
---|
| 436 | if (bd->flags != flags) { |
---|
[e5854a8] | 437 | bd->flags = flags; |
---|
[5ebff60] | 438 | msn_buddy_ask(bu); |
---|
[e5854a8] | 439 | } |
---|
| 440 | } |
---|
| 441 | } |
---|
[11e42dc] | 442 | } else if (strcmp(cmd[0], "SDG") == 0) { |
---|
| 443 | char **parts = g_strsplit(msg, "\r\n\r\n", 4); |
---|
| 444 | char *from = NULL; |
---|
| 445 | char *mt = NULL; |
---|
| 446 | char *who = NULL; |
---|
| 447 | char *s = NULL; |
---|
[5ebff60] | 448 | |
---|
[11e42dc] | 449 | if ((from = get_rfc822_header(parts[0], "From", 0)) && |
---|
| 450 | (mt = get_rfc822_header(parts[2], "Message-Type", 0)) && |
---|
| 451 | (s = strchr(from, ';'))) { |
---|
[5ebff60] | 452 | |
---|
[11e42dc] | 453 | who = g_strndup(from + 2, s - from - 2); |
---|
| 454 | |
---|
| 455 | if (strcmp(mt, "Control/Typing") == 0) { |
---|
| 456 | imcb_buddy_typing(ic, who, OPT_TYPING); |
---|
| 457 | } else if (strcmp(mt, "Text") == 0) { |
---|
| 458 | imcb_buddy_msg(ic, who, parts[3], 0, 0); |
---|
| 459 | } |
---|
| 460 | } |
---|
| 461 | g_free(from); |
---|
| 462 | g_free(mt); |
---|
| 463 | g_free(who); |
---|
| 464 | return 1; |
---|
[3901b5d] | 465 | } |
---|
[5ebff60] | 466 | |
---|
[3901b5d] | 467 | return 1; |
---|
[b7d3cc34] | 468 | } |
---|
| 469 | |
---|
[5ebff60] | 470 | void msn_auth_got_passport_token(struct im_connection *ic, const char *token, const char *error) |
---|
[b7d3cc34] | 471 | { |
---|
[d84e2a9] | 472 | struct msn_data *md; |
---|
[5ebff60] | 473 | |
---|
[d84e2a9] | 474 | /* Dead connection? */ |
---|
[5ebff60] | 475 | if (g_slist_find(msn_connections, ic) == NULL) { |
---|
[d84e2a9] | 476 | return; |
---|
[b7d3cc34] | 477 | } |
---|
[5ebff60] | 478 | |
---|
| 479 | md = ic->proto_data; |
---|
| 480 | |
---|
| 481 | if (token) { |
---|
| 482 | msn_ns_write(ic, -1, "USR %d SSO S %s %s {%s}\r\n", ++md->trId, md->tokens[0], token, md->uuid); |
---|
| 483 | } else { |
---|
| 484 | imcb_error(ic, "Error during Passport authentication: %s", error); |
---|
| 485 | imc_logout(ic, TRUE); |
---|
[660cb00] | 486 | } |
---|
[b7d3cc34] | 487 | } |
---|
[e3413cc] | 488 | |
---|
[5ebff60] | 489 | void msn_auth_got_contact_list(struct im_connection *ic) |
---|
[ca7de3a] | 490 | { |
---|
| 491 | /* Dead connection? */ |
---|
[5ebff60] | 492 | if (g_slist_find(msn_connections, ic) == NULL) { |
---|
[ca7de3a] | 493 | return; |
---|
[5ebff60] | 494 | } |
---|
| 495 | |
---|
[254a4da] | 496 | msn_ns_send_adl_start(ic); |
---|
| 497 | msn_ns_finish_login(ic); |
---|
[ca7de3a] | 498 | } |
---|
| 499 | |
---|
[5ebff60] | 500 | static gboolean msn_ns_send_adl_1(gpointer key, gpointer value, gpointer data) |
---|
[ca7de3a] | 501 | { |
---|
[254a4da] | 502 | struct xt_node *adl = data, *d, *c, *s; |
---|
[ca7de3a] | 503 | struct bee_user *bu = value; |
---|
| 504 | struct msn_buddy_data *bd = bu->data; |
---|
[5a7af1b] | 505 | struct msn_data *md = bu->ic->proto_data; |
---|
[4f161e3] | 506 | char handle[strlen(bu->handle) + 1]; |
---|
[ca7de3a] | 507 | char *domain; |
---|
| 508 | char l[4]; |
---|
[5ebff60] | 509 | |
---|
| 510 | if ((bd->flags & 7) == 0 || (bd->flags & MSN_BUDDY_ADL_SYNCED)) { |
---|
| 511 | return FALSE; |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | strcpy(handle, bu->handle); |
---|
| 515 | if ((domain = strchr(handle, '@')) == NULL) { /* WTF */ |
---|
[e5854a8] | 516 | return FALSE; |
---|
[5ebff60] | 517 | } |
---|
[ca7de3a] | 518 | *domain = '\0'; |
---|
[5ebff60] | 519 | domain++; |
---|
| 520 | |
---|
| 521 | if ((d = adl->children) == NULL || |
---|
| 522 | g_strcasecmp(xt_find_attr(d, "n"), domain) != 0) { |
---|
| 523 | d = xt_new_node("d", NULL, NULL); |
---|
| 524 | xt_add_attr(d, "n", domain); |
---|
| 525 | xt_insert_child(adl, d); |
---|
[ca7de3a] | 526 | } |
---|
[5ebff60] | 527 | |
---|
| 528 | g_snprintf(l, sizeof(l), "%d", bd->flags & 7); |
---|
| 529 | c = xt_new_node("c", NULL, NULL); |
---|
| 530 | xt_add_attr(c, "n", handle); |
---|
| 531 | xt_add_attr(c, "t", "1"); /* FIXME: Network type, i.e. 32 for Y!MSG */ |
---|
[254a4da] | 532 | s = xt_new_node("s", NULL, NULL); |
---|
| 533 | xt_add_attr(s, "n", "IM"); |
---|
| 534 | xt_add_attr(s, "l", l); |
---|
| 535 | xt_insert_child(c, s); |
---|
[5ebff60] | 536 | xt_insert_child(d, c); |
---|
| 537 | |
---|
[5a7af1b] | 538 | /* Do this in batches of 100. */ |
---|
| 539 | bd->flags |= MSN_BUDDY_ADL_SYNCED; |
---|
| 540 | return (--md->adl_todo % 140) == 0; |
---|
[ca7de3a] | 541 | } |
---|
| 542 | |
---|
[5ebff60] | 543 | static void msn_ns_send_adl(struct im_connection *ic) |
---|
[ca7de3a] | 544 | { |
---|
| 545 | struct xt_node *adl; |
---|
[5a7af1b] | 546 | struct msn_data *md = ic->proto_data; |
---|
[64768d4] | 547 | char *adls; |
---|
[5ebff60] | 548 | |
---|
| 549 | adl = xt_new_node("ml", NULL, NULL); |
---|
| 550 | xt_add_attr(adl, "l", "1"); |
---|
| 551 | g_tree_foreach(md->domaintree, msn_ns_send_adl_1, adl); |
---|
| 552 | if (adl->children == NULL) { |
---|
[5a7af1b] | 553 | /* This tells the caller that we're done now. */ |
---|
| 554 | md->adl_todo = -1; |
---|
[5ebff60] | 555 | xt_free_node(adl); |
---|
[5a7af1b] | 556 | return; |
---|
| 557 | } |
---|
[5ebff60] | 558 | |
---|
| 559 | adls = xt_to_string(adl); |
---|
| 560 | xt_free_node(adl); |
---|
| 561 | msn_ns_write(ic, -1, "ADL %d %zd\r\n%s", ++md->trId, strlen(adls), adls); |
---|
| 562 | g_free(adls); |
---|
[5a7af1b] | 563 | } |
---|
| 564 | |
---|
[5ebff60] | 565 | static void msn_ns_send_adl_start(struct im_connection *ic) |
---|
[5a7af1b] | 566 | { |
---|
| 567 | struct msn_data *md; |
---|
| 568 | GSList *l; |
---|
[5ebff60] | 569 | |
---|
[5a7af1b] | 570 | /* Dead connection? */ |
---|
[5ebff60] | 571 | if (g_slist_find(msn_connections, ic) == NULL) { |
---|
[5a7af1b] | 572 | return; |
---|
[5ebff60] | 573 | } |
---|
| 574 | |
---|
[5a7af1b] | 575 | md = ic->proto_data; |
---|
| 576 | md->adl_todo = 0; |
---|
[5ebff60] | 577 | for (l = ic->bee->users; l; l = l->next) { |
---|
[5a7af1b] | 578 | bee_user_t *bu = l->data; |
---|
| 579 | struct msn_buddy_data *bd = bu->data; |
---|
[5ebff60] | 580 | |
---|
| 581 | if (bu->ic != ic || (bd->flags & 7) == 0) { |
---|
[5a7af1b] | 582 | continue; |
---|
[5ebff60] | 583 | } |
---|
| 584 | |
---|
[5a7af1b] | 585 | bd->flags &= ~MSN_BUDDY_ADL_SYNCED; |
---|
| 586 | md->adl_todo++; |
---|
| 587 | } |
---|
[5ebff60] | 588 | |
---|
| 589 | msn_ns_send_adl(ic); |
---|
[ca7de3a] | 590 | } |
---|
[80175a1] | 591 | |
---|
[5ebff60] | 592 | int msn_ns_finish_login(struct im_connection *ic) |
---|
[80175a1] | 593 | { |
---|
| 594 | struct msn_data *md = ic->proto_data; |
---|
[5ebff60] | 595 | |
---|
| 596 | if (ic->flags & OPT_LOGGED_IN) { |
---|
[80175a1] | 597 | return 1; |
---|
[5ebff60] | 598 | } |
---|
| 599 | |
---|
| 600 | if (md->adl_todo < 0) { |
---|
[80175a1] | 601 | md->flags |= MSN_DONE_ADL; |
---|
[ed0589c] | 602 | } |
---|
[5ebff60] | 603 | |
---|
| 604 | if ((md->flags & MSN_DONE_ADL) && (md->flags & MSN_GOT_PROFILE)) { |
---|
[d550358] | 605 | imcb_connected(ic); |
---|
[5ebff60] | 606 | } |
---|
| 607 | |
---|
[ed0589c] | 608 | return 1; |
---|
[80175a1] | 609 | } |
---|
[bc676ac] | 610 | |
---|
[11e42dc] | 611 | // TODO: typing notifications, nudges lol, etc |
---|
[5ebff60] | 612 | int msn_ns_sendmessage(struct im_connection *ic, bee_user_t *bu, const char *text) |
---|
[bc676ac] | 613 | { |
---|
| 614 | struct msn_data *md = ic->proto_data; |
---|
[11e42dc] | 615 | int retval = 0; |
---|
| 616 | char *buf; |
---|
[5ebff60] | 617 | |
---|
| 618 | if (strncmp(text, "\r\r\r", 3) == 0) { |
---|
[bc676ac] | 619 | /* Err. Shouldn't happen but I guess it can. Don't send others |
---|
| 620 | any of the "SHAKE THAT THING" messages. :-D */ |
---|
| 621 | return 1; |
---|
[5ebff60] | 622 | } |
---|
| 623 | |
---|
[11e42dc] | 624 | buf = g_strdup_printf(MSN_MESSAGE_HEADERS, bu->handle, ic->acc->user, md->uuid, strlen(text), text); |
---|
| 625 | retval = msn_ns_write(ic, -1, "SDG %d %zd\r\n%s", ++md->trId, strlen(buf), buf); |
---|
| 626 | g_free(buf); |
---|
| 627 | return retval; |
---|
[bc676ac] | 628 | } |
---|