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