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