[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")) { |
---|
[074c9b6] | 58 | fprintf(stderr, "\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 | |
---|
[074c9b6] | 150 | if (msn_ns_write(ic, "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER)) { |
---|
[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; |
---|
[5ebff60] | 217 | |
---|
| 218 | if (num_parts == 0) { |
---|
[b7d3cc34] | 219 | /* Hrrm... Empty command...? Ignore? */ |
---|
[5ebff60] | 220 | return(1); |
---|
[b7d3cc34] | 221 | } |
---|
[5ebff60] | 222 | |
---|
| 223 | if (strcmp(cmd[0], "VER") == 0) { |
---|
| 224 | if (cmd[2] && strncmp(cmd[2], MSNP_VER, 5) != 0) { |
---|
| 225 | imcb_error(ic, "Unsupported protocol"); |
---|
| 226 | imc_logout(ic, FALSE); |
---|
| 227 | return(0); |
---|
[b7d3cc34] | 228 | } |
---|
[5ebff60] | 229 | |
---|
[074c9b6] | 230 | return(msn_ns_write(ic, "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s VmVyc2lvbjogMQ0KWGZyQ291bnQ6IDINClhmclNlbnRVVENUaW1lOiA2MzU2MTQ3OTU5NzgzOTAwMDANCklzR2VvWGZyOiB0cnVlDQo=\r\n", |
---|
[5ebff60] | 231 | ++md->trId, ic->acc->user)); |
---|
| 232 | } else if (strcmp(cmd[0], "CVR") == 0) { |
---|
[b7d3cc34] | 233 | /* We don't give a damn about the information we just received */ |
---|
[074c9b6] | 234 | return msn_ns_write(ic, "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user); |
---|
[5ebff60] | 235 | } else if (strcmp(cmd[0], "XFR") == 0) { |
---|
[b7d3cc34] | 236 | char *server; |
---|
| 237 | int port; |
---|
[5ebff60] | 238 | |
---|
| 239 | if (num_parts >= 6 && strcmp(cmd[2], "NS") == 0) { |
---|
[5fbf815] | 240 | b_event_remove(md->inpa); |
---|
| 241 | md->inpa = -1; |
---|
[5ebff60] | 242 | |
---|
| 243 | server = strchr(cmd[3], ':'); |
---|
| 244 | if (!server) { |
---|
| 245 | imcb_error(ic, "Syntax error"); |
---|
| 246 | imc_logout(ic, TRUE); |
---|
| 247 | return(0); |
---|
[b7d3cc34] | 248 | } |
---|
| 249 | *server = 0; |
---|
[5ebff60] | 250 | port = atoi(server + 1); |
---|
[b7d3cc34] | 251 | server = cmd[3]; |
---|
[5ebff60] | 252 | |
---|
| 253 | imcb_log(ic, "Transferring to other server"); |
---|
[11e42dc] | 254 | return msn_ns_connect(ic, server, port); |
---|
[5ebff60] | 255 | } else { |
---|
| 256 | imcb_error(ic, "Syntax error"); |
---|
| 257 | imc_logout(ic, TRUE); |
---|
| 258 | return(0); |
---|
| 259 | } |
---|
| 260 | } else if (strcmp(cmd[0], "USR") == 0) { |
---|
| 261 | if (num_parts >= 6 && strcmp(cmd[2], "SSO") == 0 && |
---|
| 262 | strcmp(cmd[3], "S") == 0) { |
---|
| 263 | g_free(md->pp_policy); |
---|
| 264 | md->pp_policy = g_strdup(cmd[4]); |
---|
| 265 | msn_soap_passport_sso_request(ic, cmd[5]); |
---|
| 266 | } else if (strcmp(cmd[2], "OK") == 0) { |
---|
[ed0589c] | 267 | /* If the number after the handle is 0, the e-mail |
---|
| 268 | address is unverified, which means we can't change |
---|
| 269 | the display name. */ |
---|
[5ebff60] | 270 | if (cmd[4][0] == '0') { |
---|
[ed0589c] | 271 | md->flags |= MSN_EMAIL_UNVERIFIED; |
---|
[5ebff60] | 272 | } |
---|
| 273 | |
---|
| 274 | imcb_log(ic, "Authenticated, getting buddy list"); |
---|
| 275 | msn_soap_memlist_request(ic); |
---|
| 276 | } else { |
---|
| 277 | imcb_error(ic, "Unknown authentication type"); |
---|
| 278 | imc_logout(ic, FALSE); |
---|
| 279 | return(0); |
---|
| 280 | } |
---|
| 281 | } else if (strcmp(cmd[0], "ADL") == 0) { |
---|
| 282 | if (num_parts >= 3 && strcmp(cmd[2], "OK") == 0) { |
---|
| 283 | msn_ns_send_adl(ic); |
---|
| 284 | return msn_ns_finish_login(ic); |
---|
| 285 | } |
---|
| 286 | } else if (strcmp(cmd[0], "CHL") == 0) { |
---|
[be7a180] | 287 | char *resp; |
---|
[64768d4] | 288 | int st; |
---|
[5ebff60] | 289 | |
---|
| 290 | if (num_parts < 3) { |
---|
| 291 | imcb_error(ic, "Syntax error"); |
---|
| 292 | imc_logout(ic, TRUE); |
---|
| 293 | return(0); |
---|
[b7d3cc34] | 294 | } |
---|
[5ebff60] | 295 | |
---|
| 296 | resp = msn_p11_challenge(cmd[2]); |
---|
| 297 | |
---|
[074c9b6] | 298 | st = msn_ns_write(ic, "QRY %d %s %zd\r\n%s", |
---|
[5ebff60] | 299 | ++md->trId, MSNP11_PROD_ID, |
---|
| 300 | strlen(resp), resp); |
---|
| 301 | g_free(resp); |
---|
[64768d4] | 302 | return st; |
---|
[73b1a8e] | 303 | } else if (strcmp(cmd[0], "QRY") == 0) { |
---|
| 304 | /* CONGRATULATIONS */ |
---|
[5ebff60] | 305 | } else if (strcmp(cmd[0], "OUT") == 0) { |
---|
[2fe8297] | 306 | imcb_error(ic, "Session terminated by remote server (%s)", cmd[1] ? cmd[1] : "reason unknown"); |
---|
| 307 | imc_logout(ic, TRUE); |
---|
[5ebff60] | 308 | return(0); |
---|
| 309 | } else if (strcmp(cmd[0], "QNG") == 0) { |
---|
[e132b60] | 310 | ic->flags |= OPT_PONGED; |
---|
[5ebff60] | 311 | } else if (g_ascii_isdigit(cmd[0][0])) { |
---|
| 312 | int num = atoi(cmd[0]); |
---|
| 313 | const struct msn_status_code *err = msn_status_by_number(num); |
---|
| 314 | |
---|
| 315 | imcb_error(ic, "Error reported by MSN server: %s", err->text); |
---|
| 316 | |
---|
| 317 | if (err->flags & STATUS_FATAL) { |
---|
| 318 | imc_logout(ic, TRUE); |
---|
| 319 | return(0); |
---|
[b7d3cc34] | 320 | } |
---|
[95fdf22] | 321 | } else if ((strcmp(cmd[0], "SDG") == 0) || (strcmp(cmd[0], "NFY") == 0)) { |
---|
| 322 | msn_ns_structured_message(md, msg, msglen, cmd); |
---|
[5ebff60] | 323 | } else { |
---|
[d550358] | 324 | imcb_error(ic, "Received unknown command from main server: %s", cmd[0]); |
---|
[b7d3cc34] | 325 | } |
---|
[5ebff60] | 326 | |
---|
| 327 | return(1); |
---|
[b7d3cc34] | 328 | } |
---|
| 329 | |
---|
[5fbf815] | 330 | int msn_ns_message(struct msn_data *md, char *msg, int msglen, char **cmd, int num_parts) |
---|
[b7d3cc34] | 331 | { |
---|
[5fbf815] | 332 | struct im_connection *ic = md->ic; |
---|
[b7d3cc34] | 333 | char *body; |
---|
| 334 | int blen = 0; |
---|
[5ebff60] | 335 | |
---|
| 336 | if ((body = strstr(msg, "\r\n\r\n"))) { |
---|
[b7d3cc34] | 337 | body += 4; |
---|
[5ebff60] | 338 | blen = msglen - (body - msg); |
---|
[b7d3cc34] | 339 | } |
---|
[5ebff60] | 340 | |
---|
| 341 | if (strcmp(cmd[0], "MSG") == 0) { |
---|
| 342 | if (g_strcasecmp(cmd[1], "Hotmail") == 0) { |
---|
| 343 | char *ct = get_rfc822_header(msg, "Content-Type:", msglen); |
---|
| 344 | |
---|
| 345 | if (!ct) { |
---|
| 346 | return(1); |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | if (g_strncasecmp(ct, "application/x-msmsgssystemmessage", 33) == 0) { |
---|
[b7d3cc34] | 350 | char *mtype; |
---|
| 351 | char *arg1; |
---|
[5ebff60] | 352 | |
---|
| 353 | if (!body) { |
---|
| 354 | return(1); |
---|
[b7d3cc34] | 355 | } |
---|
[5ebff60] | 356 | |
---|
| 357 | mtype = get_rfc822_header(body, "Type:", blen); |
---|
| 358 | arg1 = get_rfc822_header(body, "Arg1:", blen); |
---|
| 359 | |
---|
| 360 | if (mtype && strcmp(mtype, "1") == 0) { |
---|
| 361 | if (arg1) { |
---|
| 362 | imcb_log(ic, "The server is going down for maintenance in %s minutes.", |
---|
| 363 | arg1); |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | g_free(arg1); |
---|
| 368 | g_free(mtype); |
---|
| 369 | } else if (g_strncasecmp(ct, "text/x-msmsgsprofile", 20) == 0) { |
---|
[b7d3cc34] | 370 | /* We don't care about this profile for now... */ |
---|
[5ebff60] | 371 | } else if (g_strncasecmp(ct, "text/x-msmsgsinitialemailnotification", 37) == 0) { |
---|
| 372 | if (set_getbool(&ic->acc->set, "mail_notifications")) { |
---|
| 373 | char *inbox = get_rfc822_header(body, "Inbox-Unread:", blen); |
---|
| 374 | char *folders = get_rfc822_header(body, "Folders-Unread:", blen); |
---|
| 375 | |
---|
| 376 | if (inbox && folders) { |
---|
[0864a52] | 377 | imcb_notify_email(ic, |
---|
[dd43c62] | 378 | "INBOX contains %s new messages, plus %s messages in other folders.", inbox, |
---|
| 379 | folders); |
---|
[5ebff60] | 380 | } |
---|
| 381 | |
---|
| 382 | g_free(inbox); |
---|
| 383 | g_free(folders); |
---|
[b7d3cc34] | 384 | } |
---|
[5ebff60] | 385 | } else if (g_strncasecmp(ct, "text/x-msmsgsemailnotification", 30) == 0) { |
---|
| 386 | if (set_getbool(&ic->acc->set, "mail_notifications")) { |
---|
| 387 | char *from = get_rfc822_header(body, "From-Addr:", blen); |
---|
| 388 | char *fromname = get_rfc822_header(body, "From:", blen); |
---|
| 389 | |
---|
| 390 | if (from && fromname) { |
---|
[0864a52] | 391 | imcb_notify_email(ic, "Received an e-mail message from %s <%s>.", fromname, from); |
---|
[5ebff60] | 392 | } |
---|
| 393 | |
---|
| 394 | g_free(from); |
---|
| 395 | g_free(fromname); |
---|
[b7d3cc34] | 396 | } |
---|
[5ebff60] | 397 | } else if (g_strncasecmp(ct, "text/x-msmsgsactivemailnotification", 35) == 0) { |
---|
[d550358] | 398 | /* Notification that a message has been read... Ignore it */ |
---|
[b7d3cc34] | 399 | } |
---|
[5ebff60] | 400 | |
---|
| 401 | g_free(ct); |
---|
[b7d3cc34] | 402 | } |
---|
[5ebff60] | 403 | } else if (strcmp(cmd[0], "ADL") == 0) { |
---|
[e5854a8] | 404 | struct xt_node *adl, *d, *c; |
---|
[5ebff60] | 405 | |
---|
| 406 | if (!(adl = xt_from_string(msg, msglen))) { |
---|
[e5854a8] | 407 | return 1; |
---|
[5ebff60] | 408 | } |
---|
| 409 | |
---|
| 410 | for (d = adl->children; d; d = d->next) { |
---|
[e5854a8] | 411 | char *dn; |
---|
[5ebff60] | 412 | if (strcmp(d->name, "d") != 0 || |
---|
| 413 | (dn = xt_find_attr(d, "n")) == NULL) { |
---|
[e5854a8] | 414 | continue; |
---|
[5ebff60] | 415 | } |
---|
| 416 | for (c = d->children; c; c = c->next) { |
---|
[e5854a8] | 417 | bee_user_t *bu; |
---|
| 418 | struct msn_buddy_data *bd; |
---|
| 419 | char *cn, *handle, *f, *l; |
---|
| 420 | int flags; |
---|
[5ebff60] | 421 | |
---|
| 422 | if (strcmp(c->name, "c") != 0 || |
---|
| 423 | (l = xt_find_attr(c, "l")) == NULL || |
---|
| 424 | (cn = xt_find_attr(c, "n")) == NULL) { |
---|
[e5854a8] | 425 | continue; |
---|
[5ebff60] | 426 | } |
---|
| 427 | |
---|
[3901b5d] | 428 | /* FIXME: Use "t" here, guess I should just add it |
---|
| 429 | as a prefix like elsewhere in the protocol. */ |
---|
[5ebff60] | 430 | handle = g_strdup_printf("%s@%s", cn, dn); |
---|
| 431 | if (!((bu = bee_user_by_handle(ic->bee, ic, handle)) || |
---|
| 432 | (bu = bee_user_new(ic->bee, ic, handle, 0)))) { |
---|
| 433 | g_free(handle); |
---|
[e5854a8] | 434 | continue; |
---|
| 435 | } |
---|
[5ebff60] | 436 | g_free(handle); |
---|
[e5854a8] | 437 | bd = bu->data; |
---|
[5ebff60] | 438 | |
---|
| 439 | if ((f = xt_find_attr(c, "f"))) { |
---|
| 440 | http_decode(f); |
---|
| 441 | imcb_rename_buddy(ic, bu->handle, f); |
---|
[e5854a8] | 442 | } |
---|
[5ebff60] | 443 | |
---|
| 444 | flags = atoi(l) & 15; |
---|
| 445 | if (bd->flags != flags) { |
---|
[e5854a8] | 446 | bd->flags = flags; |
---|
[5ebff60] | 447 | msn_buddy_ask(bu); |
---|
[e5854a8] | 448 | } |
---|
| 449 | } |
---|
| 450 | } |
---|
[cd5fdcf] | 451 | } |
---|
| 452 | |
---|
| 453 | return 1; |
---|
| 454 | } |
---|
| 455 | |
---|
[5fbf815] | 456 | static void msn_ns_structured_message(struct msn_data *md, char *msg, int msglen, char **cmd) |
---|
[cd5fdcf] | 457 | { |
---|
| 458 | char **parts = NULL; |
---|
| 459 | char *semicolon = NULL; |
---|
| 460 | char *action = NULL; |
---|
| 461 | char *from = NULL; |
---|
| 462 | char *who = NULL; |
---|
| 463 | |
---|
| 464 | parts = g_strsplit(msg, "\r\n\r\n", 4); |
---|
| 465 | |
---|
| 466 | if (!(from = get_rfc822_header(parts[0], "From", 0))) { |
---|
| 467 | goto cleanup; |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | /* either the semicolon or the end of the string */ |
---|
| 471 | semicolon = strchr(from, ';') ? : (from + strlen(from)); |
---|
| 472 | |
---|
| 473 | who = g_strndup(from + 2, semicolon - from - 2); |
---|
| 474 | |
---|
| 475 | if ((strcmp(cmd[0], "SDG") == 0) && (action = get_rfc822_header(parts[2], "Message-Type", 0))) { |
---|
[5fbf815] | 476 | msn_ns_sdg(md, who, parts, action); |
---|
[cd5fdcf] | 477 | |
---|
| 478 | } else if ((strcmp(cmd[0], "NFY") == 0) && (action = get_rfc822_header(parts[2], "Uri", 0))) { |
---|
| 479 | gboolean is_put = (strcmp(cmd[1], "PUT") == 0); |
---|
[5fbf815] | 480 | msn_ns_nfy(md, who, parts, action, is_put); |
---|
[cd5fdcf] | 481 | } |
---|
| 482 | |
---|
| 483 | cleanup: |
---|
| 484 | g_strfreev(parts); |
---|
| 485 | g_free(action); |
---|
| 486 | g_free(from); |
---|
| 487 | g_free(who); |
---|
| 488 | } |
---|
| 489 | |
---|
[5fbf815] | 490 | static void msn_ns_sdg(struct msn_data *md, char *who, char **parts, char *action) |
---|
[cd5fdcf] | 491 | { |
---|
[5fbf815] | 492 | struct im_connection *ic = md->ic; |
---|
[cd5fdcf] | 493 | |
---|
| 494 | if (strcmp(action, "Control/Typing") == 0) { |
---|
| 495 | imcb_buddy_typing(ic, who, OPT_TYPING); |
---|
| 496 | } else if (strcmp(action, "Text") == 0) { |
---|
| 497 | imcb_buddy_msg(ic, who, parts[3], 0, 0); |
---|
| 498 | } |
---|
| 499 | } |
---|
| 500 | |
---|
[5fbf815] | 501 | static void msn_ns_nfy(struct msn_data *md, char *who, char **parts, char *action, gboolean is_put) |
---|
[cd5fdcf] | 502 | { |
---|
[5fbf815] | 503 | struct im_connection *ic = md->ic; |
---|
[cd5fdcf] | 504 | struct xt_node *body = NULL; |
---|
| 505 | struct xt_node *s = NULL; |
---|
| 506 | const char *state = NULL; |
---|
| 507 | char *nick = NULL; |
---|
| 508 | char *psm = NULL; |
---|
| 509 | int flags = OPT_LOGGED_IN; |
---|
| 510 | |
---|
| 511 | if (strcmp(action, "/user") != 0) { |
---|
| 512 | return; |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | if (!(body = xt_from_string(parts[3], 0))) { |
---|
| 516 | goto cleanup; |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | s = body->children; |
---|
| 520 | while ((s = xt_find_node(s, "s"))) { |
---|
| 521 | struct xt_node *s2; |
---|
| 522 | char *n = xt_find_attr(s, "n"); /* service name: IM, PE, etc */ |
---|
| 523 | |
---|
| 524 | if (strcmp(n, "IM") == 0) { |
---|
| 525 | /* IM has basic presence information */ |
---|
| 526 | if (!is_put) { |
---|
| 527 | /* NFY DEL with a <s> usually means log out from the last endpoint */ |
---|
| 528 | flags &= ~OPT_LOGGED_IN; |
---|
| 529 | break; |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | s2 = xt_find_node(s->children, "Status"); |
---|
| 533 | if (s2 && s2->text_len) { |
---|
| 534 | const struct msn_away_state *msn_state = msn_away_state_by_code(s2->text); |
---|
| 535 | state = msn_state->name; |
---|
| 536 | if (msn_state != msn_away_state_list) { |
---|
| 537 | flags |= OPT_AWAY; |
---|
| 538 | } |
---|
| 539 | } |
---|
| 540 | } else if (strcmp(n, "PE") == 0) { |
---|
| 541 | if ((s2 = xt_find_node(s->children, "PSM")) && s2->text_len) { |
---|
| 542 | psm = s2->text; |
---|
| 543 | } |
---|
| 544 | if ((s2 = xt_find_node(s->children, "FriendlyName")) && s2->text_len) { |
---|
| 545 | nick = s2->text; |
---|
[11e42dc] | 546 | } |
---|
| 547 | } |
---|
[cd5fdcf] | 548 | s = s->next; |
---|
[3901b5d] | 549 | } |
---|
[5ebff60] | 550 | |
---|
[cd5fdcf] | 551 | imcb_buddy_status(ic, who, flags, state, psm); |
---|
| 552 | |
---|
| 553 | if (nick) { |
---|
| 554 | imcb_rename_buddy(ic, who, nick); |
---|
| 555 | } |
---|
| 556 | |
---|
| 557 | cleanup: |
---|
| 558 | xt_free_node(body); |
---|
[b7d3cc34] | 559 | } |
---|
| 560 | |
---|
[5ebff60] | 561 | void msn_auth_got_passport_token(struct im_connection *ic, const char *token, const char *error) |
---|
[b7d3cc34] | 562 | { |
---|
[d84e2a9] | 563 | struct msn_data *md; |
---|
[5ebff60] | 564 | |
---|
[d84e2a9] | 565 | /* Dead connection? */ |
---|
[5ebff60] | 566 | if (g_slist_find(msn_connections, ic) == NULL) { |
---|
[d84e2a9] | 567 | return; |
---|
[b7d3cc34] | 568 | } |
---|
[5ebff60] | 569 | |
---|
| 570 | md = ic->proto_data; |
---|
| 571 | |
---|
| 572 | if (token) { |
---|
[074c9b6] | 573 | msn_ns_write(ic, "USR %d SSO S %s %s {%s}\r\n", ++md->trId, md->tokens[0], token, md->uuid); |
---|
[5ebff60] | 574 | } else { |
---|
| 575 | imcb_error(ic, "Error during Passport authentication: %s", error); |
---|
| 576 | imc_logout(ic, TRUE); |
---|
[660cb00] | 577 | } |
---|
[b7d3cc34] | 578 | } |
---|
[e3413cc] | 579 | |
---|
[5ebff60] | 580 | void msn_auth_got_contact_list(struct im_connection *ic) |
---|
[ca7de3a] | 581 | { |
---|
| 582 | /* Dead connection? */ |
---|
[5ebff60] | 583 | if (g_slist_find(msn_connections, ic) == NULL) { |
---|
[ca7de3a] | 584 | return; |
---|
[5ebff60] | 585 | } |
---|
| 586 | |
---|
[254a4da] | 587 | msn_ns_send_adl_start(ic); |
---|
| 588 | msn_ns_finish_login(ic); |
---|
[ca7de3a] | 589 | } |
---|
| 590 | |
---|
[5ebff60] | 591 | static gboolean msn_ns_send_adl_1(gpointer key, gpointer value, gpointer data) |
---|
[ca7de3a] | 592 | { |
---|
[254a4da] | 593 | struct xt_node *adl = data, *d, *c, *s; |
---|
[ca7de3a] | 594 | struct bee_user *bu = value; |
---|
| 595 | struct msn_buddy_data *bd = bu->data; |
---|
[5a7af1b] | 596 | struct msn_data *md = bu->ic->proto_data; |
---|
[4f161e3] | 597 | char handle[strlen(bu->handle) + 1]; |
---|
[ca7de3a] | 598 | char *domain; |
---|
| 599 | char l[4]; |
---|
[5ebff60] | 600 | |
---|
[2730c79] | 601 | if ((bd->flags & (MSN_BUDDY_FL | MSN_BUDDY_AL)) == 0 || (bd->flags & MSN_BUDDY_ADL_SYNCED)) { |
---|
[5ebff60] | 602 | return FALSE; |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | strcpy(handle, bu->handle); |
---|
| 606 | if ((domain = strchr(handle, '@')) == NULL) { /* WTF */ |
---|
[e5854a8] | 607 | return FALSE; |
---|
[5ebff60] | 608 | } |
---|
[ca7de3a] | 609 | *domain = '\0'; |
---|
[5ebff60] | 610 | domain++; |
---|
| 611 | |
---|
| 612 | if ((d = adl->children) == NULL || |
---|
| 613 | g_strcasecmp(xt_find_attr(d, "n"), domain) != 0) { |
---|
| 614 | d = xt_new_node("d", NULL, NULL); |
---|
| 615 | xt_add_attr(d, "n", domain); |
---|
| 616 | xt_insert_child(adl, d); |
---|
[ca7de3a] | 617 | } |
---|
[5ebff60] | 618 | |
---|
[2730c79] | 619 | g_snprintf(l, sizeof(l), "%d", bd->flags & (MSN_BUDDY_FL | MSN_BUDDY_AL)); |
---|
[5ebff60] | 620 | c = xt_new_node("c", NULL, NULL); |
---|
| 621 | xt_add_attr(c, "n", handle); |
---|
| 622 | xt_add_attr(c, "t", "1"); /* FIXME: Network type, i.e. 32 for Y!MSG */ |
---|
[254a4da] | 623 | s = xt_new_node("s", NULL, NULL); |
---|
| 624 | xt_add_attr(s, "n", "IM"); |
---|
| 625 | xt_add_attr(s, "l", l); |
---|
| 626 | xt_insert_child(c, s); |
---|
[5ebff60] | 627 | xt_insert_child(d, c); |
---|
| 628 | |
---|
[5a7af1b] | 629 | /* Do this in batches of 100. */ |
---|
| 630 | bd->flags |= MSN_BUDDY_ADL_SYNCED; |
---|
| 631 | return (--md->adl_todo % 140) == 0; |
---|
[ca7de3a] | 632 | } |
---|
| 633 | |
---|
[5ebff60] | 634 | static void msn_ns_send_adl(struct im_connection *ic) |
---|
[ca7de3a] | 635 | { |
---|
| 636 | struct xt_node *adl; |
---|
[5a7af1b] | 637 | struct msn_data *md = ic->proto_data; |
---|
[64768d4] | 638 | char *adls; |
---|
[5ebff60] | 639 | |
---|
| 640 | adl = xt_new_node("ml", NULL, NULL); |
---|
| 641 | xt_add_attr(adl, "l", "1"); |
---|
| 642 | g_tree_foreach(md->domaintree, msn_ns_send_adl_1, adl); |
---|
| 643 | if (adl->children == NULL) { |
---|
[5a7af1b] | 644 | /* This tells the caller that we're done now. */ |
---|
| 645 | md->adl_todo = -1; |
---|
[5ebff60] | 646 | xt_free_node(adl); |
---|
[5a7af1b] | 647 | return; |
---|
| 648 | } |
---|
[5ebff60] | 649 | |
---|
| 650 | adls = xt_to_string(adl); |
---|
| 651 | xt_free_node(adl); |
---|
[074c9b6] | 652 | msn_ns_write(ic, "ADL %d %zd\r\n%s", ++md->trId, strlen(adls), adls); |
---|
[5ebff60] | 653 | g_free(adls); |
---|
[5a7af1b] | 654 | } |
---|
| 655 | |
---|
[5ebff60] | 656 | static void msn_ns_send_adl_start(struct im_connection *ic) |
---|
[5a7af1b] | 657 | { |
---|
| 658 | struct msn_data *md; |
---|
| 659 | GSList *l; |
---|
[5ebff60] | 660 | |
---|
[5a7af1b] | 661 | /* Dead connection? */ |
---|
[5ebff60] | 662 | if (g_slist_find(msn_connections, ic) == NULL) { |
---|
[5a7af1b] | 663 | return; |
---|
[5ebff60] | 664 | } |
---|
| 665 | |
---|
[5a7af1b] | 666 | md = ic->proto_data; |
---|
| 667 | md->adl_todo = 0; |
---|
[5ebff60] | 668 | for (l = ic->bee->users; l; l = l->next) { |
---|
[5a7af1b] | 669 | bee_user_t *bu = l->data; |
---|
| 670 | struct msn_buddy_data *bd = bu->data; |
---|
[5ebff60] | 671 | |
---|
[2730c79] | 672 | if (bu->ic != ic || (bd->flags & (MSN_BUDDY_FL | MSN_BUDDY_AL)) == 0) { |
---|
[5a7af1b] | 673 | continue; |
---|
[5ebff60] | 674 | } |
---|
| 675 | |
---|
[5a7af1b] | 676 | bd->flags &= ~MSN_BUDDY_ADL_SYNCED; |
---|
| 677 | md->adl_todo++; |
---|
| 678 | } |
---|
[5ebff60] | 679 | |
---|
| 680 | msn_ns_send_adl(ic); |
---|
[ca7de3a] | 681 | } |
---|
[80175a1] | 682 | |
---|
[5ebff60] | 683 | int msn_ns_finish_login(struct im_connection *ic) |
---|
[80175a1] | 684 | { |
---|
| 685 | struct msn_data *md = ic->proto_data; |
---|
[5ebff60] | 686 | |
---|
| 687 | if (ic->flags & OPT_LOGGED_IN) { |
---|
[80175a1] | 688 | return 1; |
---|
[5ebff60] | 689 | } |
---|
| 690 | |
---|
| 691 | if (md->adl_todo < 0) { |
---|
[80175a1] | 692 | md->flags |= MSN_DONE_ADL; |
---|
[ed0589c] | 693 | } |
---|
[5ebff60] | 694 | |
---|
| 695 | if ((md->flags & MSN_DONE_ADL) && (md->flags & MSN_GOT_PROFILE)) { |
---|
[d550358] | 696 | imcb_connected(ic); |
---|
[5ebff60] | 697 | } |
---|
| 698 | |
---|
[ed0589c] | 699 | return 1; |
---|
[80175a1] | 700 | } |
---|
[bc676ac] | 701 | |
---|
[99fe030] | 702 | static int msn_ns_send_sdg(struct im_connection *ic, bee_user_t *bu, const char *message_type, const char *text) |
---|
[bc676ac] | 703 | { |
---|
| 704 | struct msn_data *md = ic->proto_data; |
---|
[11e42dc] | 705 | int retval = 0; |
---|
| 706 | char *buf; |
---|
[5ebff60] | 707 | |
---|
[99fe030] | 708 | buf = g_strdup_printf(MSN_MESSAGE_HEADERS, bu->handle, ic->acc->user, md->uuid, message_type, strlen(text), text); |
---|
[074c9b6] | 709 | retval = msn_ns_write(ic, "SDG %d %zd\r\n%s", ++md->trId, strlen(buf), buf); |
---|
[11e42dc] | 710 | g_free(buf); |
---|
| 711 | return retval; |
---|
[bc676ac] | 712 | } |
---|
[99fe030] | 713 | |
---|
| 714 | int msn_ns_send_typing(struct im_connection *ic, bee_user_t *bu) |
---|
| 715 | { |
---|
| 716 | return msn_ns_send_sdg(ic, bu, "Control/Typing", ""); |
---|
| 717 | } |
---|
| 718 | |
---|
| 719 | int msn_ns_send_message(struct im_connection *ic, bee_user_t *bu, const char *text) |
---|
| 720 | { |
---|
| 721 | return msn_ns_send_sdg(ic, bu, "Text", text); |
---|
| 722 | } |
---|
| 723 | |
---|