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