[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" |
---|
[767b2d1] | 34 | #include "ssl_client.h" |
---|
[b7d3cc34] | 35 | |
---|
[767b2d1] | 36 | static gboolean msn_ns_connected(gpointer data, int source, void *scd, b_input_condition cond); |
---|
[5ebff60] | 37 | static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond); |
---|
[b7d3cc34] | 38 | |
---|
[5ebff60] | 39 | static void msn_ns_send_adl_start(struct im_connection *ic); |
---|
| 40 | static void msn_ns_send_adl(struct im_connection *ic); |
---|
[5fbf815] | 41 | static void msn_ns_structured_message(struct msn_data *md, char *msg, int msglen, char **cmd); |
---|
| 42 | static void msn_ns_sdg(struct msn_data *md, char *who, char **parts, char *action); |
---|
| 43 | static void msn_ns_nfy(struct msn_data *md, char *who, char **parts, char *action, gboolean is_put); |
---|
[b7d3cc34] | 44 | |
---|
[074c9b6] | 45 | int msn_ns_write(struct im_connection *ic, const char *fmt, ...) |
---|
[64768d4] | 46 | { |
---|
| 47 | struct msn_data *md = ic->proto_data; |
---|
| 48 | va_list params; |
---|
| 49 | char *out; |
---|
| 50 | size_t len; |
---|
| 51 | int st; |
---|
[5ebff60] | 52 | |
---|
| 53 | va_start(params, fmt); |
---|
| 54 | out = g_strdup_vprintf(fmt, params); |
---|
| 55 | va_end(params); |
---|
| 56 | |
---|
| 57 | if (getenv("BITLBEE_DEBUG")) { |
---|
[4107fea] | 58 | fprintf(stderr, "\n\x1b[91m>>>[NS] %s\n\x1b[97m", out); |
---|
[5ebff60] | 59 | } |
---|
| 60 | |
---|
| 61 | len = strlen(out); |
---|
[913a663] | 62 | |
---|
| 63 | if (md->is_http) { |
---|
| 64 | st = len; |
---|
| 65 | msn_gw_write(md->gw, out, len); |
---|
| 66 | } else { |
---|
[767b2d1] | 67 | st = ssl_write(md->ssl, out, len); |
---|
[913a663] | 68 | } |
---|
| 69 | |
---|
[5ebff60] | 70 | g_free(out); |
---|
| 71 | if (st != len) { |
---|
| 72 | imcb_error(ic, "Short write() to main server"); |
---|
| 73 | imc_logout(ic, TRUE); |
---|
[64768d4] | 74 | return 0; |
---|
| 75 | } |
---|
[5ebff60] | 76 | |
---|
[64768d4] | 77 | return 1; |
---|
| 78 | } |
---|
| 79 | |
---|
[85dabae] | 80 | int msn_ns_write_cmd(struct im_connection *ic, const char *cmd, const char *params, const char *payload) |
---|
| 81 | { |
---|
| 82 | struct msn_data *md = ic->proto_data; |
---|
| 83 | int trid = ++md->trId; |
---|
| 84 | const char *headers = "\r\n"; /* not needed yet */ |
---|
| 85 | size_t len = strlen(headers) + strlen(payload); |
---|
| 86 | |
---|
| 87 | if (params && *params) { |
---|
| 88 | return msn_ns_write(ic, "%s %d %s %zd\r\n%s%s", cmd, trid, params, len, headers, payload); |
---|
| 89 | } else { |
---|
| 90 | return msn_ns_write(ic, "%s %d %zd\r\n%s%s", cmd, trid, len, headers, payload); |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
[11e42dc] | 94 | gboolean msn_ns_connect(struct im_connection *ic, const char *host, int port) |
---|
[b7d3cc34] | 95 | { |
---|
[5fbf815] | 96 | struct msn_data *md = ic->proto_data; |
---|
[11e42dc] | 97 | |
---|
[5fbf815] | 98 | if (md->fd >= 0) { |
---|
| 99 | closesocket(md->fd); |
---|
[5ebff60] | 100 | } |
---|
| 101 | |
---|
[5fbf815] | 102 | if (md->is_http) { |
---|
[951aefd] | 103 | md->gw = msn_gw_new(ic); |
---|
[5fbf815] | 104 | md->gw->callback = msn_ns_callback; |
---|
[767b2d1] | 105 | msn_ns_connected(md, 0, NULL, B_EV_IO_READ); |
---|
[913a663] | 106 | } else { |
---|
[767b2d1] | 107 | md->ssl = ssl_connect((char *) host, port, TRUE, msn_ns_connected, md); |
---|
| 108 | md->fd = md->ssl ? ssl_getfd(md->ssl) : -1; |
---|
[5fbf815] | 109 | if (md->fd < 0) { |
---|
[913a663] | 110 | imcb_error(ic, "Could not connect to server"); |
---|
| 111 | imc_logout(ic, TRUE); |
---|
| 112 | return FALSE; |
---|
| 113 | } |
---|
[b7d3cc34] | 114 | } |
---|
[5ebff60] | 115 | |
---|
[bae0617] | 116 | return TRUE; |
---|
| 117 | } |
---|
| 118 | |
---|
[767b2d1] | 119 | static gboolean msn_ns_connected(gpointer data, int source, void *scd, b_input_condition cond) |
---|
[bae0617] | 120 | { |
---|
[11e42dc] | 121 | struct msn_data *md = data; |
---|
| 122 | struct im_connection *ic = md->ic; |
---|
[5ebff60] | 123 | |
---|
[767b2d1] | 124 | if (!scd && !md->is_http) { |
---|
| 125 | md->ssl = NULL; |
---|
[5ebff60] | 126 | imcb_error(ic, "Could not connect to server"); |
---|
| 127 | imc_logout(ic, TRUE); |
---|
[bae0617] | 128 | return FALSE; |
---|
[b7d3cc34] | 129 | } |
---|
[5ebff60] | 130 | |
---|
[5fbf815] | 131 | g_free(md->rxq); |
---|
| 132 | md->rxlen = 0; |
---|
| 133 | md->rxq = g_new0(char, 1); |
---|
[5ebff60] | 134 | |
---|
| 135 | if (md->uuid == NULL) { |
---|
[f9258ae] | 136 | struct utsname name; |
---|
| 137 | sha1_state_t sha[1]; |
---|
[5ebff60] | 138 | |
---|
[f9258ae] | 139 | /* UUID == SHA1("BitlBee" + my hostname + MSN username) */ |
---|
[5ebff60] | 140 | sha1_init(sha); |
---|
| 141 | sha1_append(sha, (void *) "BitlBee", 7); |
---|
| 142 | if (uname(&name) == 0) { |
---|
| 143 | sha1_append(sha, (void *) name.nodename, strlen(name.nodename)); |
---|
[f9258ae] | 144 | } |
---|
[5ebff60] | 145 | sha1_append(sha, (void *) ic->acc->user, strlen(ic->acc->user)); |
---|
| 146 | md->uuid = sha1_random_uuid(sha); |
---|
| 147 | memcpy(md->uuid, "b171be3e", 8); /* :-P */ |
---|
[f9258ae] | 148 | } |
---|
[5ebff60] | 149 | |
---|
[4107fea] | 150 | if (msn_ns_write_cmd(ic, "CNT", "CON", "<connect><ver>2</ver><agent><os>winnt</os><osVer>5.2</osVer><proc>x86</proc><lcid>en-us</lcid></agent></connect>")) { |
---|
[5fbf815] | 151 | if (!md->is_http) { |
---|
| 152 | md->inpa = b_input_add(md->fd, B_EV_IO_READ, msn_ns_callback, md); |
---|
[913a663] | 153 | } |
---|
[5ebff60] | 154 | imcb_log(ic, "Connected to server, waiting for reply"); |
---|
[b7d3cc34] | 155 | } |
---|
[5ebff60] | 156 | |
---|
[ba9edaa] | 157 | return FALSE; |
---|
[b7d3cc34] | 158 | } |
---|
| 159 | |
---|
[5fbf815] | 160 | void msn_ns_close(struct msn_data *md) |
---|
[bae0617] | 161 | { |
---|
[5fbf815] | 162 | if (md->gw) { |
---|
[951aefd] | 163 | msn_gw_free(md->gw); |
---|
[913a663] | 164 | } |
---|
[767b2d1] | 165 | |
---|
| 166 | if (md->ssl) { |
---|
| 167 | ssl_disconnect(md->ssl); |
---|
[5fbf815] | 168 | b_event_remove(md->inpa); |
---|
[bae0617] | 169 | } |
---|
[5ebff60] | 170 | |
---|
[767b2d1] | 171 | md->ssl = NULL; |
---|
[5fbf815] | 172 | md->fd = md->inpa = -1; |
---|
[767b2d1] | 173 | |
---|
[5fbf815] | 174 | g_free(md->rxq); |
---|
| 175 | g_free(md->cmd_text); |
---|
[5ebff60] | 176 | |
---|
[5fbf815] | 177 | md->rxlen = 0; |
---|
| 178 | md->rxq = NULL; |
---|
| 179 | md->cmd_text = NULL; |
---|
[bae0617] | 180 | } |
---|
| 181 | |
---|
[5ebff60] | 182 | static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 183 | { |
---|
[5fbf815] | 184 | struct msn_data *md = data; |
---|
| 185 | struct im_connection *ic = md->ic; |
---|
[913a663] | 186 | char *bytes; |
---|
[a4be2f6] | 187 | int st; |
---|
[5ebff60] | 188 | |
---|
[5fbf815] | 189 | if (md->is_http) { |
---|
| 190 | st = msn_gw_read(md->gw, &bytes); |
---|
[913a663] | 191 | } else { |
---|
| 192 | bytes = g_malloc(1024); |
---|
[767b2d1] | 193 | st = ssl_read(md->ssl, bytes, 1024); |
---|
[913a663] | 194 | } |
---|
| 195 | |
---|
[767b2d1] | 196 | if (st == 0 || (st < 0 && (md->is_http || !ssl_sockerr_again(md->ssl)))) { |
---|
[5ebff60] | 197 | imcb_error(ic, "Error while reading from server"); |
---|
| 198 | imc_logout(ic, TRUE); |
---|
[088b070] | 199 | g_free(bytes); |
---|
[ba9edaa] | 200 | return FALSE; |
---|
[5ebff60] | 201 | } |
---|
[a4be2f6] | 202 | |
---|
[5fbf815] | 203 | msn_queue_feed(md, bytes, st); |
---|
[a4be2f6] | 204 | |
---|
[913a663] | 205 | g_free(bytes); |
---|
| 206 | |
---|
[767b2d1] | 207 | if (!md->is_http && ssl_pending(md->ssl)) { |
---|
| 208 | return msn_ns_callback(data, source, cond); |
---|
| 209 | } |
---|
| 210 | |
---|
[088b070] | 211 | return msn_handler(md); |
---|
[b7d3cc34] | 212 | } |
---|
| 213 | |
---|
[d2411a1] | 214 | int msn_ns_command(struct msn_data *md, char **cmd, int num_parts, char *msg, int msglen) |
---|
[b7d3cc34] | 215 | { |
---|
[5fbf815] | 216 | struct im_connection *ic = md->ic; |
---|
[4107fea] | 217 | struct xt_node *xml; |
---|
[5ebff60] | 218 | |
---|
[4107fea] | 219 | if (strcmp(cmd[0], "XFR") == 0) { |
---|
| 220 | struct xt_node *target; |
---|
| 221 | char *server, *p; |
---|
| 222 | int port, st; |
---|
[5ebff60] | 223 | |
---|
[4107fea] | 224 | if (!(xml = xt_from_string(msg + 2, msglen - 2)) || |
---|
| 225 | !(target = xt_find_node(xml->children, "target")) || |
---|
| 226 | !(server = target->text) || |
---|
| 227 | !(p = strchr(server, ':'))) { |
---|
| 228 | return 1; |
---|
[b7d3cc34] | 229 | } |
---|
[4107fea] | 230 | |
---|
| 231 | server = target->text; |
---|
| 232 | *p = 0; |
---|
| 233 | port = atoi(p + 1); |
---|
[5ebff60] | 234 | |
---|
[4107fea] | 235 | b_event_remove(md->inpa); |
---|
| 236 | md->inpa = -1; |
---|
[5ebff60] | 237 | |
---|
[4107fea] | 238 | imcb_log(ic, "Transferring to other server"); |
---|
| 239 | |
---|
| 240 | st = msn_ns_connect(ic, server, port); |
---|
| 241 | |
---|
| 242 | xt_free_node(xml); |
---|
| 243 | |
---|
| 244 | return st; |
---|
| 245 | |
---|
| 246 | } else if (strcmp(cmd[0], "CNT") == 0) { |
---|
| 247 | msn_soap_passport_sso_request(ic); |
---|
| 248 | |
---|
| 249 | /* continues in msn_auth_got_passport_token */ |
---|
| 250 | |
---|
| 251 | } else if (strcmp(cmd[0], "ATH") == 0) { |
---|
| 252 | char *payload; |
---|
| 253 | |
---|
[ef9ee0e] | 254 | if (md->flags & MSN_DONE_BND) { |
---|
| 255 | return 1; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | md->flags |= MSN_DONE_BND; |
---|
| 259 | |
---|
| 260 | // BND |
---|
[4107fea] | 261 | payload = g_markup_printf_escaped( |
---|
| 262 | "<msgr><ver>1</ver><client><name>Skype</name><ver>2/4.3.0.37/174</ver></client>" |
---|
| 263 | "<epid>%s</epid></msgr>\r\n", |
---|
| 264 | md->uuid); |
---|
| 265 | |
---|
| 266 | msn_ns_write_cmd(ic, "BND", "CON\\MSGR", payload); |
---|
| 267 | |
---|
| 268 | g_free(payload); |
---|
| 269 | |
---|
| 270 | } else if (strcmp(cmd[0], "BND") == 0) { |
---|
[ef9ee0e] | 271 | struct xt_node *node; |
---|
| 272 | char *nonce, *resp, *payload; |
---|
| 273 | |
---|
| 274 | if (!(xml = xt_from_string(msg + 2, msglen - 2)) || |
---|
| 275 | !(node = xt_find_node(xml->children, "nonce")) || |
---|
| 276 | !(nonce = node->text)) { |
---|
| 277 | return 1; |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | resp = msn_p11_challenge(nonce); |
---|
| 281 | |
---|
| 282 | // PUT MSGR\CHALLENGE |
---|
| 283 | payload = g_markup_printf_escaped( |
---|
| 284 | "<challenge><appId>%s</appId><response>%s</response></challenge>", |
---|
| 285 | MSNP11_PROD_ID, resp); |
---|
| 286 | |
---|
| 287 | msn_ns_write_cmd(ic, "PUT", "MSGR\\CHALLENGE", payload); |
---|
| 288 | |
---|
[4107fea] | 289 | imcb_log(ic, "Authenticated, getting buddy list"); |
---|
| 290 | msn_soap_memlist_request(ic); |
---|
[5ebff60] | 291 | |
---|
[ef9ee0e] | 292 | xt_free_node(xml); |
---|
| 293 | g_free(payload); |
---|
| 294 | g_free(resp); |
---|
| 295 | |
---|
[5ebff60] | 296 | } else if (strcmp(cmd[0], "ADL") == 0) { |
---|
| 297 | if (num_parts >= 3 && strcmp(cmd[2], "OK") == 0) { |
---|
| 298 | msn_ns_send_adl(ic); |
---|
| 299 | return msn_ns_finish_login(ic); |
---|
| 300 | } |
---|
| 301 | } else if (strcmp(cmd[0], "OUT") == 0) { |
---|
[2fe8297] | 302 | imcb_error(ic, "Session terminated by remote server (%s)", cmd[1] ? cmd[1] : "reason unknown"); |
---|
| 303 | imc_logout(ic, TRUE); |
---|
[5ebff60] | 304 | return(0); |
---|
| 305 | } else if (strcmp(cmd[0], "QNG") == 0) { |
---|
[e132b60] | 306 | ic->flags |= OPT_PONGED; |
---|
[5ebff60] | 307 | } else if (g_ascii_isdigit(cmd[0][0])) { |
---|
| 308 | int num = atoi(cmd[0]); |
---|
| 309 | const struct msn_status_code *err = msn_status_by_number(num); |
---|
| 310 | |
---|
| 311 | imcb_error(ic, "Error reported by MSN server: %s", err->text); |
---|
| 312 | |
---|
| 313 | if (err->flags & STATUS_FATAL) { |
---|
| 314 | imc_logout(ic, TRUE); |
---|
| 315 | return(0); |
---|
[b7d3cc34] | 316 | } |
---|
[95fdf22] | 317 | } else if ((strcmp(cmd[0], "SDG") == 0) || (strcmp(cmd[0], "NFY") == 0)) { |
---|
| 318 | msn_ns_structured_message(md, msg, msglen, cmd); |
---|
[5ebff60] | 319 | } else { |
---|
[d550358] | 320 | imcb_error(ic, "Received unknown command from main server: %s", cmd[0]); |
---|
[b7d3cc34] | 321 | } |
---|
[5ebff60] | 322 | |
---|
| 323 | return(1); |
---|
[b7d3cc34] | 324 | } |
---|
| 325 | |
---|
[5fbf815] | 326 | int msn_ns_message(struct msn_data *md, char *msg, int msglen, char **cmd, int num_parts) |
---|
[b7d3cc34] | 327 | { |
---|
[5fbf815] | 328 | struct im_connection *ic = md->ic; |
---|
[b7d3cc34] | 329 | char *body; |
---|
| 330 | int blen = 0; |
---|
[5ebff60] | 331 | |
---|
| 332 | if ((body = strstr(msg, "\r\n\r\n"))) { |
---|
[b7d3cc34] | 333 | body += 4; |
---|
[5ebff60] | 334 | blen = msglen - (body - msg); |
---|
[b7d3cc34] | 335 | } |
---|
[5ebff60] | 336 | |
---|
| 337 | if (strcmp(cmd[0], "MSG") == 0) { |
---|
| 338 | if (g_strcasecmp(cmd[1], "Hotmail") == 0) { |
---|
| 339 | char *ct = get_rfc822_header(msg, "Content-Type:", msglen); |
---|
| 340 | |
---|
| 341 | if (!ct) { |
---|
| 342 | return(1); |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | if (g_strncasecmp(ct, "application/x-msmsgssystemmessage", 33) == 0) { |
---|
[b7d3cc34] | 346 | char *mtype; |
---|
| 347 | char *arg1; |
---|
[5ebff60] | 348 | |
---|
| 349 | if (!body) { |
---|
| 350 | return(1); |
---|
[b7d3cc34] | 351 | } |
---|
[5ebff60] | 352 | |
---|
| 353 | mtype = get_rfc822_header(body, "Type:", blen); |
---|
| 354 | arg1 = get_rfc822_header(body, "Arg1:", blen); |
---|
| 355 | |
---|
| 356 | if (mtype && strcmp(mtype, "1") == 0) { |
---|
| 357 | if (arg1) { |
---|
| 358 | imcb_log(ic, "The server is going down for maintenance in %s minutes.", |
---|
| 359 | arg1); |
---|
| 360 | } |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | g_free(arg1); |
---|
| 364 | g_free(mtype); |
---|
| 365 | } else if (g_strncasecmp(ct, "text/x-msmsgsprofile", 20) == 0) { |
---|
[b7d3cc34] | 366 | /* We don't care about this profile for now... */ |
---|
[5ebff60] | 367 | } else if (g_strncasecmp(ct, "text/x-msmsgsinitialemailnotification", 37) == 0) { |
---|
| 368 | if (set_getbool(&ic->acc->set, "mail_notifications")) { |
---|
| 369 | char *inbox = get_rfc822_header(body, "Inbox-Unread:", blen); |
---|
| 370 | char *folders = get_rfc822_header(body, "Folders-Unread:", blen); |
---|
| 371 | |
---|
| 372 | if (inbox && folders) { |
---|
[0864a52] | 373 | imcb_notify_email(ic, |
---|
[dd43c62] | 374 | "INBOX contains %s new messages, plus %s messages in other folders.", inbox, |
---|
| 375 | folders); |
---|
[5ebff60] | 376 | } |
---|
| 377 | |
---|
| 378 | g_free(inbox); |
---|
| 379 | g_free(folders); |
---|
[b7d3cc34] | 380 | } |
---|
[5ebff60] | 381 | } else if (g_strncasecmp(ct, "text/x-msmsgsemailnotification", 30) == 0) { |
---|
| 382 | if (set_getbool(&ic->acc->set, "mail_notifications")) { |
---|
| 383 | char *from = get_rfc822_header(body, "From-Addr:", blen); |
---|
| 384 | char *fromname = get_rfc822_header(body, "From:", blen); |
---|
| 385 | |
---|
| 386 | if (from && fromname) { |
---|
[0864a52] | 387 | imcb_notify_email(ic, "Received an e-mail message from %s <%s>.", fromname, from); |
---|
[5ebff60] | 388 | } |
---|
| 389 | |
---|
| 390 | g_free(from); |
---|
| 391 | g_free(fromname); |
---|
[b7d3cc34] | 392 | } |
---|
[5ebff60] | 393 | } else if (g_strncasecmp(ct, "text/x-msmsgsactivemailnotification", 35) == 0) { |
---|
[d550358] | 394 | /* Notification that a message has been read... Ignore it */ |
---|
[b7d3cc34] | 395 | } |
---|
[5ebff60] | 396 | |
---|
| 397 | g_free(ct); |
---|
[b7d3cc34] | 398 | } |
---|
[5ebff60] | 399 | } else if (strcmp(cmd[0], "ADL") == 0) { |
---|
[e5854a8] | 400 | struct xt_node *adl, *d, *c; |
---|
[5ebff60] | 401 | |
---|
| 402 | if (!(adl = xt_from_string(msg, msglen))) { |
---|
[e5854a8] | 403 | return 1; |
---|
[5ebff60] | 404 | } |
---|
| 405 | |
---|
| 406 | for (d = adl->children; d; d = d->next) { |
---|
[e5854a8] | 407 | char *dn; |
---|
[5ebff60] | 408 | if (strcmp(d->name, "d") != 0 || |
---|
| 409 | (dn = xt_find_attr(d, "n")) == NULL) { |
---|
[e5854a8] | 410 | continue; |
---|
[5ebff60] | 411 | } |
---|
| 412 | for (c = d->children; c; c = c->next) { |
---|
[e5854a8] | 413 | bee_user_t *bu; |
---|
| 414 | struct msn_buddy_data *bd; |
---|
| 415 | char *cn, *handle, *f, *l; |
---|
| 416 | int flags; |
---|
[5ebff60] | 417 | |
---|
| 418 | if (strcmp(c->name, "c") != 0 || |
---|
| 419 | (l = xt_find_attr(c, "l")) == NULL || |
---|
| 420 | (cn = xt_find_attr(c, "n")) == NULL) { |
---|
[e5854a8] | 421 | continue; |
---|
[5ebff60] | 422 | } |
---|
| 423 | |
---|
[3901b5d] | 424 | /* FIXME: Use "t" here, guess I should just add it |
---|
| 425 | as a prefix like elsewhere in the protocol. */ |
---|
[5ebff60] | 426 | handle = g_strdup_printf("%s@%s", cn, dn); |
---|
| 427 | if (!((bu = bee_user_by_handle(ic->bee, ic, handle)) || |
---|
| 428 | (bu = bee_user_new(ic->bee, ic, handle, 0)))) { |
---|
| 429 | g_free(handle); |
---|
[e5854a8] | 430 | continue; |
---|
| 431 | } |
---|
[5ebff60] | 432 | g_free(handle); |
---|
[e5854a8] | 433 | bd = bu->data; |
---|
[5ebff60] | 434 | |
---|
| 435 | if ((f = xt_find_attr(c, "f"))) { |
---|
| 436 | http_decode(f); |
---|
| 437 | imcb_rename_buddy(ic, bu->handle, f); |
---|
[e5854a8] | 438 | } |
---|
[5ebff60] | 439 | |
---|
| 440 | flags = atoi(l) & 15; |
---|
| 441 | if (bd->flags != flags) { |
---|
[e5854a8] | 442 | bd->flags = flags; |
---|
[5ebff60] | 443 | msn_buddy_ask(bu); |
---|
[e5854a8] | 444 | } |
---|
| 445 | } |
---|
| 446 | } |
---|
[cd5fdcf] | 447 | } |
---|
| 448 | |
---|
| 449 | return 1; |
---|
| 450 | } |
---|
| 451 | |
---|
[5fbf815] | 452 | static void msn_ns_structured_message(struct msn_data *md, char *msg, int msglen, char **cmd) |
---|
[cd5fdcf] | 453 | { |
---|
| 454 | char **parts = NULL; |
---|
| 455 | char *semicolon = NULL; |
---|
| 456 | char *action = NULL; |
---|
| 457 | char *from = NULL; |
---|
| 458 | char *who = NULL; |
---|
| 459 | |
---|
| 460 | parts = g_strsplit(msg, "\r\n\r\n", 4); |
---|
| 461 | |
---|
| 462 | if (!(from = get_rfc822_header(parts[0], "From", 0))) { |
---|
| 463 | goto cleanup; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | /* either the semicolon or the end of the string */ |
---|
| 467 | semicolon = strchr(from, ';') ? : (from + strlen(from)); |
---|
| 468 | |
---|
| 469 | who = g_strndup(from + 2, semicolon - from - 2); |
---|
| 470 | |
---|
| 471 | if ((strcmp(cmd[0], "SDG") == 0) && (action = get_rfc822_header(parts[2], "Message-Type", 0))) { |
---|
[5fbf815] | 472 | msn_ns_sdg(md, who, parts, action); |
---|
[cd5fdcf] | 473 | |
---|
| 474 | } else if ((strcmp(cmd[0], "NFY") == 0) && (action = get_rfc822_header(parts[2], "Uri", 0))) { |
---|
| 475 | gboolean is_put = (strcmp(cmd[1], "PUT") == 0); |
---|
[5fbf815] | 476 | msn_ns_nfy(md, who, parts, action, is_put); |
---|
[cd5fdcf] | 477 | } |
---|
| 478 | |
---|
| 479 | cleanup: |
---|
| 480 | g_strfreev(parts); |
---|
| 481 | g_free(action); |
---|
| 482 | g_free(from); |
---|
| 483 | g_free(who); |
---|
| 484 | } |
---|
| 485 | |
---|
[5fbf815] | 486 | static void msn_ns_sdg(struct msn_data *md, char *who, char **parts, char *action) |
---|
[cd5fdcf] | 487 | { |
---|
[5fbf815] | 488 | struct im_connection *ic = md->ic; |
---|
[cd5fdcf] | 489 | |
---|
| 490 | if (strcmp(action, "Control/Typing") == 0) { |
---|
| 491 | imcb_buddy_typing(ic, who, OPT_TYPING); |
---|
| 492 | } else if (strcmp(action, "Text") == 0) { |
---|
| 493 | imcb_buddy_msg(ic, who, parts[3], 0, 0); |
---|
| 494 | } |
---|
| 495 | } |
---|
| 496 | |
---|
[5fbf815] | 497 | static void msn_ns_nfy(struct msn_data *md, char *who, char **parts, char *action, gboolean is_put) |
---|
[cd5fdcf] | 498 | { |
---|
[5fbf815] | 499 | struct im_connection *ic = md->ic; |
---|
[cd5fdcf] | 500 | struct xt_node *body = NULL; |
---|
| 501 | struct xt_node *s = NULL; |
---|
| 502 | const char *state = NULL; |
---|
| 503 | char *nick = NULL; |
---|
| 504 | char *psm = NULL; |
---|
| 505 | int flags = OPT_LOGGED_IN; |
---|
| 506 | |
---|
| 507 | if (strcmp(action, "/user") != 0) { |
---|
| 508 | return; |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | if (!(body = xt_from_string(parts[3], 0))) { |
---|
| 512 | goto cleanup; |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | s = body->children; |
---|
| 516 | while ((s = xt_find_node(s, "s"))) { |
---|
| 517 | struct xt_node *s2; |
---|
| 518 | char *n = xt_find_attr(s, "n"); /* service name: IM, PE, etc */ |
---|
| 519 | |
---|
| 520 | if (strcmp(n, "IM") == 0) { |
---|
| 521 | /* IM has basic presence information */ |
---|
| 522 | if (!is_put) { |
---|
| 523 | /* NFY DEL with a <s> usually means log out from the last endpoint */ |
---|
| 524 | flags &= ~OPT_LOGGED_IN; |
---|
| 525 | break; |
---|
| 526 | } |
---|
| 527 | |
---|
| 528 | s2 = xt_find_node(s->children, "Status"); |
---|
| 529 | if (s2 && s2->text_len) { |
---|
| 530 | const struct msn_away_state *msn_state = msn_away_state_by_code(s2->text); |
---|
| 531 | state = msn_state->name; |
---|
| 532 | if (msn_state != msn_away_state_list) { |
---|
| 533 | flags |= OPT_AWAY; |
---|
| 534 | } |
---|
| 535 | } |
---|
| 536 | } else if (strcmp(n, "PE") == 0) { |
---|
| 537 | if ((s2 = xt_find_node(s->children, "PSM")) && s2->text_len) { |
---|
| 538 | psm = s2->text; |
---|
| 539 | } |
---|
| 540 | if ((s2 = xt_find_node(s->children, "FriendlyName")) && s2->text_len) { |
---|
| 541 | nick = s2->text; |
---|
[11e42dc] | 542 | } |
---|
| 543 | } |
---|
[cd5fdcf] | 544 | s = s->next; |
---|
[3901b5d] | 545 | } |
---|
[5ebff60] | 546 | |
---|
[cd5fdcf] | 547 | imcb_buddy_status(ic, who, flags, state, psm); |
---|
| 548 | |
---|
| 549 | if (nick) { |
---|
| 550 | imcb_rename_buddy(ic, who, nick); |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | cleanup: |
---|
| 554 | xt_free_node(body); |
---|
[b7d3cc34] | 555 | } |
---|
| 556 | |
---|
[5ebff60] | 557 | void msn_auth_got_passport_token(struct im_connection *ic, const char *token, const char *error) |
---|
[b7d3cc34] | 558 | { |
---|
[d84e2a9] | 559 | struct msn_data *md; |
---|
[4107fea] | 560 | char *payload = NULL; |
---|
[5ebff60] | 561 | |
---|
[d84e2a9] | 562 | /* Dead connection? */ |
---|
[5ebff60] | 563 | if (g_slist_find(msn_connections, ic) == NULL) { |
---|
[d84e2a9] | 564 | return; |
---|
[b7d3cc34] | 565 | } |
---|
[5ebff60] | 566 | |
---|
| 567 | md = ic->proto_data; |
---|
| 568 | |
---|
[4107fea] | 569 | if (!token) { |
---|
[5ebff60] | 570 | imcb_error(ic, "Error during Passport authentication: %s", error); |
---|
| 571 | imc_logout(ic, TRUE); |
---|
[660cb00] | 572 | } |
---|
[4107fea] | 573 | |
---|
| 574 | // ATH |
---|
| 575 | payload = g_markup_printf_escaped( |
---|
| 576 | "<user><ssl-compact-ticket>%s</ssl-compact-ticket>" |
---|
| 577 | "<ssl-site-name>chatservice.live.com</ssl-site-name></user>", |
---|
| 578 | md->tokens[0]); |
---|
| 579 | |
---|
| 580 | msn_ns_write_cmd(ic, "ATH", "CON\\USER", payload); |
---|
| 581 | |
---|
| 582 | g_free(payload); |
---|
| 583 | |
---|
| 584 | /* continues in msn_ns_command */ |
---|
[b7d3cc34] | 585 | } |
---|
[e3413cc] | 586 | |
---|
[5ebff60] | 587 | void msn_auth_got_contact_list(struct im_connection *ic) |
---|
[ca7de3a] | 588 | { |
---|
| 589 | /* Dead connection? */ |
---|
[5ebff60] | 590 | if (g_slist_find(msn_connections, ic) == NULL) { |
---|
[ca7de3a] | 591 | return; |
---|
[5ebff60] | 592 | } |
---|
| 593 | |
---|
[254a4da] | 594 | msn_ns_send_adl_start(ic); |
---|
| 595 | msn_ns_finish_login(ic); |
---|
[ca7de3a] | 596 | } |
---|
| 597 | |
---|
[5ebff60] | 598 | static gboolean msn_ns_send_adl_1(gpointer key, gpointer value, gpointer data) |
---|
[ca7de3a] | 599 | { |
---|
[254a4da] | 600 | struct xt_node *adl = data, *d, *c, *s; |
---|
[ca7de3a] | 601 | struct bee_user *bu = value; |
---|
| 602 | struct msn_buddy_data *bd = bu->data; |
---|
[5a7af1b] | 603 | struct msn_data *md = bu->ic->proto_data; |
---|
[4f161e3] | 604 | char handle[strlen(bu->handle) + 1]; |
---|
[ca7de3a] | 605 | char *domain; |
---|
| 606 | char l[4]; |
---|
[5ebff60] | 607 | |
---|
[2730c79] | 608 | if ((bd->flags & (MSN_BUDDY_FL | MSN_BUDDY_AL)) == 0 || (bd->flags & MSN_BUDDY_ADL_SYNCED)) { |
---|
[5ebff60] | 609 | return FALSE; |
---|
| 610 | } |
---|
| 611 | |
---|
| 612 | strcpy(handle, bu->handle); |
---|
| 613 | if ((domain = strchr(handle, '@')) == NULL) { /* WTF */ |
---|
[e5854a8] | 614 | return FALSE; |
---|
[5ebff60] | 615 | } |
---|
[ca7de3a] | 616 | *domain = '\0'; |
---|
[5ebff60] | 617 | domain++; |
---|
| 618 | |
---|
| 619 | if ((d = adl->children) == NULL || |
---|
| 620 | g_strcasecmp(xt_find_attr(d, "n"), domain) != 0) { |
---|
| 621 | d = xt_new_node("d", NULL, NULL); |
---|
| 622 | xt_add_attr(d, "n", domain); |
---|
| 623 | xt_insert_child(adl, d); |
---|
[ca7de3a] | 624 | } |
---|
[5ebff60] | 625 | |
---|
[2730c79] | 626 | g_snprintf(l, sizeof(l), "%d", bd->flags & (MSN_BUDDY_FL | MSN_BUDDY_AL)); |
---|
[5ebff60] | 627 | c = xt_new_node("c", NULL, NULL); |
---|
| 628 | xt_add_attr(c, "n", handle); |
---|
| 629 | xt_add_attr(c, "t", "1"); /* FIXME: Network type, i.e. 32 for Y!MSG */ |
---|
[254a4da] | 630 | s = xt_new_node("s", NULL, NULL); |
---|
| 631 | xt_add_attr(s, "n", "IM"); |
---|
| 632 | xt_add_attr(s, "l", l); |
---|
| 633 | xt_insert_child(c, s); |
---|
[5ebff60] | 634 | xt_insert_child(d, c); |
---|
| 635 | |
---|
[5a7af1b] | 636 | /* Do this in batches of 100. */ |
---|
| 637 | bd->flags |= MSN_BUDDY_ADL_SYNCED; |
---|
| 638 | return (--md->adl_todo % 140) == 0; |
---|
[ca7de3a] | 639 | } |
---|
| 640 | |
---|
[5ebff60] | 641 | static void msn_ns_send_adl(struct im_connection *ic) |
---|
[ca7de3a] | 642 | { |
---|
| 643 | struct xt_node *adl; |
---|
[5a7af1b] | 644 | struct msn_data *md = ic->proto_data; |
---|
[64768d4] | 645 | char *adls; |
---|
[5ebff60] | 646 | |
---|
| 647 | adl = xt_new_node("ml", NULL, NULL); |
---|
| 648 | xt_add_attr(adl, "l", "1"); |
---|
| 649 | g_tree_foreach(md->domaintree, msn_ns_send_adl_1, adl); |
---|
| 650 | if (adl->children == NULL) { |
---|
[5a7af1b] | 651 | /* This tells the caller that we're done now. */ |
---|
| 652 | md->adl_todo = -1; |
---|
[5ebff60] | 653 | xt_free_node(adl); |
---|
[5a7af1b] | 654 | return; |
---|
| 655 | } |
---|
[5ebff60] | 656 | |
---|
| 657 | adls = xt_to_string(adl); |
---|
| 658 | xt_free_node(adl); |
---|
[074c9b6] | 659 | msn_ns_write(ic, "ADL %d %zd\r\n%s", ++md->trId, strlen(adls), adls); |
---|
[5ebff60] | 660 | g_free(adls); |
---|
[5a7af1b] | 661 | } |
---|
| 662 | |
---|
[5ebff60] | 663 | static void msn_ns_send_adl_start(struct im_connection *ic) |
---|
[5a7af1b] | 664 | { |
---|
| 665 | struct msn_data *md; |
---|
| 666 | GSList *l; |
---|
[5ebff60] | 667 | |
---|
[5a7af1b] | 668 | /* Dead connection? */ |
---|
[5ebff60] | 669 | if (g_slist_find(msn_connections, ic) == NULL) { |
---|
[5a7af1b] | 670 | return; |
---|
[5ebff60] | 671 | } |
---|
| 672 | |
---|
[5a7af1b] | 673 | md = ic->proto_data; |
---|
| 674 | md->adl_todo = 0; |
---|
[5ebff60] | 675 | for (l = ic->bee->users; l; l = l->next) { |
---|
[5a7af1b] | 676 | bee_user_t *bu = l->data; |
---|
| 677 | struct msn_buddy_data *bd = bu->data; |
---|
[5ebff60] | 678 | |
---|
[2730c79] | 679 | if (bu->ic != ic || (bd->flags & (MSN_BUDDY_FL | MSN_BUDDY_AL)) == 0) { |
---|
[5a7af1b] | 680 | continue; |
---|
[5ebff60] | 681 | } |
---|
| 682 | |
---|
[5a7af1b] | 683 | bd->flags &= ~MSN_BUDDY_ADL_SYNCED; |
---|
| 684 | md->adl_todo++; |
---|
| 685 | } |
---|
[5ebff60] | 686 | |
---|
| 687 | msn_ns_send_adl(ic); |
---|
[ca7de3a] | 688 | } |
---|
[80175a1] | 689 | |
---|
[5ebff60] | 690 | int msn_ns_finish_login(struct im_connection *ic) |
---|
[80175a1] | 691 | { |
---|
| 692 | struct msn_data *md = ic->proto_data; |
---|
[5ebff60] | 693 | |
---|
| 694 | if (ic->flags & OPT_LOGGED_IN) { |
---|
[80175a1] | 695 | return 1; |
---|
[5ebff60] | 696 | } |
---|
| 697 | |
---|
| 698 | if (md->adl_todo < 0) { |
---|
[80175a1] | 699 | md->flags |= MSN_DONE_ADL; |
---|
[ed0589c] | 700 | } |
---|
[5ebff60] | 701 | |
---|
| 702 | if ((md->flags & MSN_DONE_ADL) && (md->flags & MSN_GOT_PROFILE)) { |
---|
[d550358] | 703 | imcb_connected(ic); |
---|
[5ebff60] | 704 | } |
---|
| 705 | |
---|
[ed0589c] | 706 | return 1; |
---|
[80175a1] | 707 | } |
---|
[bc676ac] | 708 | |
---|
[99fe030] | 709 | static int msn_ns_send_sdg(struct im_connection *ic, bee_user_t *bu, const char *message_type, const char *text) |
---|
[bc676ac] | 710 | { |
---|
| 711 | struct msn_data *md = ic->proto_data; |
---|
[11e42dc] | 712 | int retval = 0; |
---|
| 713 | char *buf; |
---|
[5ebff60] | 714 | |
---|
[99fe030] | 715 | buf = g_strdup_printf(MSN_MESSAGE_HEADERS, bu->handle, ic->acc->user, md->uuid, message_type, strlen(text), text); |
---|
[074c9b6] | 716 | retval = msn_ns_write(ic, "SDG %d %zd\r\n%s", ++md->trId, strlen(buf), buf); |
---|
[11e42dc] | 717 | g_free(buf); |
---|
| 718 | return retval; |
---|
[bc676ac] | 719 | } |
---|
[99fe030] | 720 | |
---|
| 721 | int msn_ns_send_typing(struct im_connection *ic, bee_user_t *bu) |
---|
| 722 | { |
---|
| 723 | return msn_ns_send_sdg(ic, bu, "Control/Typing", ""); |
---|
| 724 | } |
---|
| 725 | |
---|
| 726 | int msn_ns_send_message(struct im_connection *ic, bee_user_t *bu, const char *text) |
---|
| 727 | { |
---|
| 728 | return msn_ns_send_sdg(ic, bu, "Text", text); |
---|
| 729 | } |
---|
| 730 | |
---|