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