1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
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; |
---|
22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
23 | Suite 330, Boston, MA 02111-1307 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #include <ctype.h> |
---|
27 | #include "nogaim.h" |
---|
28 | #include "msn.h" |
---|
29 | #include "md5.h" |
---|
30 | #include "soap.h" |
---|
31 | #include "xmltree.h" |
---|
32 | |
---|
33 | static gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond ); |
---|
34 | static gboolean msn_ns_callback( gpointer data, gint source, b_input_condition cond ); |
---|
35 | static int msn_ns_command( struct msn_handler_data *handler, char **cmd, int num_parts ); |
---|
36 | static int msn_ns_message( struct msn_handler_data *handler, char *msg, int msglen, char **cmd, int num_parts ); |
---|
37 | |
---|
38 | static void msn_ns_send_adl_start( struct im_connection *ic ); |
---|
39 | static void msn_ns_send_adl( struct im_connection *ic ); |
---|
40 | |
---|
41 | int msn_ns_write( struct im_connection *ic, int fd, const char *fmt, ... ) |
---|
42 | { |
---|
43 | struct msn_data *md = ic->proto_data; |
---|
44 | va_list params; |
---|
45 | char *out; |
---|
46 | size_t len; |
---|
47 | int st; |
---|
48 | |
---|
49 | va_start( params, fmt ); |
---|
50 | out = g_strdup_vprintf( fmt, params ); |
---|
51 | va_end( params ); |
---|
52 | |
---|
53 | if( fd < 0 ) |
---|
54 | fd = md->ns->fd; |
---|
55 | |
---|
56 | if( getenv( "BITLBEE_DEBUG" ) ) |
---|
57 | fprintf( stderr, "->NS%d:%s", fd, out ); |
---|
58 | |
---|
59 | len = strlen( out ); |
---|
60 | st = write( fd, out, len ); |
---|
61 | g_free( out ); |
---|
62 | if( st != len ) |
---|
63 | { |
---|
64 | imcb_error( ic, "Short write() to main server" ); |
---|
65 | imc_logout( ic, TRUE ); |
---|
66 | return 0; |
---|
67 | } |
---|
68 | |
---|
69 | return 1; |
---|
70 | } |
---|
71 | |
---|
72 | gboolean msn_ns_connect( struct im_connection *ic, struct msn_handler_data *handler, const char *host, int port ) |
---|
73 | { |
---|
74 | if( handler->fd >= 0 ) |
---|
75 | closesocket( handler->fd ); |
---|
76 | |
---|
77 | handler->exec_command = msn_ns_command; |
---|
78 | handler->exec_message = msn_ns_message; |
---|
79 | handler->data = ic; |
---|
80 | handler->fd = proxy_connect( host, port, msn_ns_connected, handler ); |
---|
81 | if( handler->fd < 0 ) |
---|
82 | { |
---|
83 | imcb_error( ic, "Could not connect to server" ); |
---|
84 | imc_logout( ic, TRUE ); |
---|
85 | return FALSE; |
---|
86 | } |
---|
87 | |
---|
88 | return TRUE; |
---|
89 | } |
---|
90 | |
---|
91 | static gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond ) |
---|
92 | { |
---|
93 | struct msn_handler_data *handler = data; |
---|
94 | struct im_connection *ic = handler->data; |
---|
95 | struct msn_data *md; |
---|
96 | |
---|
97 | if( !g_slist_find( msn_connections, ic ) ) |
---|
98 | return FALSE; |
---|
99 | |
---|
100 | md = ic->proto_data; |
---|
101 | |
---|
102 | if( source == -1 ) |
---|
103 | { |
---|
104 | imcb_error( ic, "Could not connect to server" ); |
---|
105 | imc_logout( ic, TRUE ); |
---|
106 | return FALSE; |
---|
107 | } |
---|
108 | |
---|
109 | g_free( handler->rxq ); |
---|
110 | handler->rxlen = 0; |
---|
111 | handler->rxq = g_new0( char, 1 ); |
---|
112 | |
---|
113 | if( msn_ns_write( ic, source, "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER ) ) |
---|
114 | { |
---|
115 | handler->inpa = b_input_add( handler->fd, B_EV_IO_READ, msn_ns_callback, handler ); |
---|
116 | imcb_log( ic, "Connected to server, waiting for reply" ); |
---|
117 | } |
---|
118 | |
---|
119 | return FALSE; |
---|
120 | } |
---|
121 | |
---|
122 | void msn_ns_close( struct msn_handler_data *handler ) |
---|
123 | { |
---|
124 | if( handler->fd >= 0 ) |
---|
125 | { |
---|
126 | closesocket( handler->fd ); |
---|
127 | b_event_remove( handler->inpa ); |
---|
128 | } |
---|
129 | |
---|
130 | handler->fd = handler->inpa = -1; |
---|
131 | g_free( handler->rxq ); |
---|
132 | g_free( handler->cmd_text ); |
---|
133 | |
---|
134 | handler->rxlen = 0; |
---|
135 | handler->rxq = NULL; |
---|
136 | handler->cmd_text = NULL; |
---|
137 | } |
---|
138 | |
---|
139 | static gboolean msn_ns_callback( gpointer data, gint source, b_input_condition cond ) |
---|
140 | { |
---|
141 | struct msn_handler_data *handler = data; |
---|
142 | struct im_connection *ic = handler->data; |
---|
143 | |
---|
144 | if( msn_handler( handler ) == -1 ) /* Don't do this on ret == 0, it's already done then. */ |
---|
145 | { |
---|
146 | imcb_error( ic, "Error while reading from server" ); |
---|
147 | imc_logout( ic, TRUE ); |
---|
148 | |
---|
149 | return FALSE; |
---|
150 | } |
---|
151 | else |
---|
152 | return TRUE; |
---|
153 | } |
---|
154 | |
---|
155 | static int msn_ns_command( struct msn_handler_data *handler, char **cmd, int num_parts ) |
---|
156 | { |
---|
157 | struct im_connection *ic = handler->data; |
---|
158 | struct msn_data *md = ic->proto_data; |
---|
159 | |
---|
160 | if( num_parts == 0 ) |
---|
161 | { |
---|
162 | /* Hrrm... Empty command...? Ignore? */ |
---|
163 | return( 1 ); |
---|
164 | } |
---|
165 | |
---|
166 | if( strcmp( cmd[0], "VER" ) == 0 ) |
---|
167 | { |
---|
168 | if( cmd[2] && strncmp( cmd[2], MSNP_VER, 5 ) != 0 ) |
---|
169 | { |
---|
170 | imcb_error( ic, "Unsupported protocol" ); |
---|
171 | imc_logout( ic, FALSE ); |
---|
172 | return( 0 ); |
---|
173 | } |
---|
174 | |
---|
175 | return( msn_ns_write( ic, handler->fd, "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s\r\n", |
---|
176 | ++md->trId, ic->acc->user ) ); |
---|
177 | } |
---|
178 | else if( strcmp( cmd[0], "CVR" ) == 0 ) |
---|
179 | { |
---|
180 | /* We don't give a damn about the information we just received */ |
---|
181 | return msn_ns_write( ic, handler->fd, "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user ); |
---|
182 | } |
---|
183 | else if( strcmp( cmd[0], "XFR" ) == 0 ) |
---|
184 | { |
---|
185 | char *server; |
---|
186 | int port; |
---|
187 | |
---|
188 | if( num_parts >= 6 && strcmp( cmd[2], "NS" ) == 0 ) |
---|
189 | { |
---|
190 | b_event_remove( handler->inpa ); |
---|
191 | handler->inpa = -1; |
---|
192 | |
---|
193 | server = strchr( cmd[3], ':' ); |
---|
194 | if( !server ) |
---|
195 | { |
---|
196 | imcb_error( ic, "Syntax error" ); |
---|
197 | imc_logout( ic, TRUE ); |
---|
198 | return( 0 ); |
---|
199 | } |
---|
200 | *server = 0; |
---|
201 | port = atoi( server + 1 ); |
---|
202 | server = cmd[3]; |
---|
203 | |
---|
204 | imcb_log( ic, "Transferring to other server" ); |
---|
205 | return msn_ns_connect( ic, handler, server, port ); |
---|
206 | } |
---|
207 | else if( num_parts >= 6 && strcmp( cmd[2], "SB" ) == 0 ) |
---|
208 | { |
---|
209 | struct msn_switchboard *sb; |
---|
210 | |
---|
211 | server = strchr( cmd[3], ':' ); |
---|
212 | if( !server ) |
---|
213 | { |
---|
214 | imcb_error( ic, "Syntax error" ); |
---|
215 | imc_logout( ic, TRUE ); |
---|
216 | return( 0 ); |
---|
217 | } |
---|
218 | *server = 0; |
---|
219 | port = atoi( server + 1 ); |
---|
220 | server = cmd[3]; |
---|
221 | |
---|
222 | if( strcmp( cmd[4], "CKI" ) != 0 ) |
---|
223 | { |
---|
224 | imcb_error( ic, "Unknown authentication method for switchboard" ); |
---|
225 | imc_logout( ic, TRUE ); |
---|
226 | return( 0 ); |
---|
227 | } |
---|
228 | |
---|
229 | debug( "Connecting to a new switchboard with key %s", cmd[5] ); |
---|
230 | |
---|
231 | if( ( sb = msn_sb_create( ic, server, port, cmd[5], MSN_SB_NEW ) ) == NULL ) |
---|
232 | { |
---|
233 | /* Although this isn't strictly fatal for the NS connection, it's |
---|
234 | definitely something serious (we ran out of file descriptors?). */ |
---|
235 | imcb_error( ic, "Could not create new switchboard" ); |
---|
236 | imc_logout( ic, TRUE ); |
---|
237 | return( 0 ); |
---|
238 | } |
---|
239 | |
---|
240 | if( md->msgq ) |
---|
241 | { |
---|
242 | struct msn_message *m = md->msgq->data; |
---|
243 | GSList *l; |
---|
244 | |
---|
245 | sb->who = g_strdup( m->who ); |
---|
246 | |
---|
247 | /* Move all the messages to the first user in the message |
---|
248 | queue to the switchboard message queue. */ |
---|
249 | l = md->msgq; |
---|
250 | while( l ) |
---|
251 | { |
---|
252 | m = l->data; |
---|
253 | l = l->next; |
---|
254 | if( strcmp( m->who, sb->who ) == 0 ) |
---|
255 | { |
---|
256 | sb->msgq = g_slist_append( sb->msgq, m ); |
---|
257 | md->msgq = g_slist_remove( md->msgq, m ); |
---|
258 | } |
---|
259 | } |
---|
260 | } |
---|
261 | } |
---|
262 | else |
---|
263 | { |
---|
264 | imcb_error( ic, "Syntax error" ); |
---|
265 | imc_logout( ic, TRUE ); |
---|
266 | return( 0 ); |
---|
267 | } |
---|
268 | } |
---|
269 | else if( strcmp( cmd[0], "USR" ) == 0 ) |
---|
270 | { |
---|
271 | if( num_parts >= 6 && strcmp( cmd[2], "SSO" ) == 0 && |
---|
272 | strcmp( cmd[3], "S" ) == 0 ) |
---|
273 | { |
---|
274 | g_free( md->pp_policy ); |
---|
275 | md->pp_policy = g_strdup( cmd[4] ); |
---|
276 | msn_soap_passport_sso_request( ic, cmd[5] ); |
---|
277 | } |
---|
278 | else if( strcmp( cmd[2], "OK" ) == 0 ) |
---|
279 | { |
---|
280 | imcb_log( ic, "Authenticated, getting buddy list" ); |
---|
281 | msn_soap_memlist_request( ic ); |
---|
282 | } |
---|
283 | else |
---|
284 | { |
---|
285 | imcb_error( ic, "Unknown authentication type" ); |
---|
286 | imc_logout( ic, FALSE ); |
---|
287 | return( 0 ); |
---|
288 | } |
---|
289 | } |
---|
290 | else if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
291 | { |
---|
292 | if( num_parts < 4 ) |
---|
293 | { |
---|
294 | imcb_error( ic, "Syntax error" ); |
---|
295 | imc_logout( ic, TRUE ); |
---|
296 | return( 0 ); |
---|
297 | } |
---|
298 | |
---|
299 | handler->msglen = atoi( cmd[3] ); |
---|
300 | |
---|
301 | if( handler->msglen <= 0 ) |
---|
302 | { |
---|
303 | imcb_error( ic, "Syntax error" ); |
---|
304 | imc_logout( ic, TRUE ); |
---|
305 | return( 0 ); |
---|
306 | } |
---|
307 | } |
---|
308 | else if( strcmp( cmd[0], "BLP" ) == 0 ) |
---|
309 | { |
---|
310 | msn_ns_send_adl_start( ic ); |
---|
311 | return msn_ns_finish_login( ic ); |
---|
312 | } |
---|
313 | else if( strcmp( cmd[0], "ADL" ) == 0 ) |
---|
314 | { |
---|
315 | if( num_parts >= 3 && strcmp( cmd[2], "OK" ) == 0 ) |
---|
316 | { |
---|
317 | msn_ns_send_adl( ic ); |
---|
318 | return msn_ns_finish_login( ic ); |
---|
319 | } |
---|
320 | else if( num_parts >= 3 ) |
---|
321 | { |
---|
322 | handler->msglen = atoi( cmd[2] ); |
---|
323 | } |
---|
324 | } |
---|
325 | else if( strcmp( cmd[0], "PRP" ) == 0 ) |
---|
326 | { |
---|
327 | imcb_connected( ic ); |
---|
328 | } |
---|
329 | else if( strcmp( cmd[0], "CHL" ) == 0 ) |
---|
330 | { |
---|
331 | char *resp; |
---|
332 | int st; |
---|
333 | |
---|
334 | if( num_parts < 3 ) |
---|
335 | { |
---|
336 | imcb_error( ic, "Syntax error" ); |
---|
337 | imc_logout( ic, TRUE ); |
---|
338 | return( 0 ); |
---|
339 | } |
---|
340 | |
---|
341 | resp = msn_p11_challenge( cmd[2] ); |
---|
342 | |
---|
343 | st = msn_ns_write( ic, -1, "QRY %d %s %zd\r\n%s", |
---|
344 | ++md->trId, MSNP11_PROD_ID, |
---|
345 | strlen( resp ), resp ); |
---|
346 | g_free( resp ); |
---|
347 | return st; |
---|
348 | } |
---|
349 | else if( strcmp( cmd[0], "ILN" ) == 0 ) |
---|
350 | { |
---|
351 | const struct msn_away_state *st; |
---|
352 | |
---|
353 | if( num_parts < 6 ) |
---|
354 | { |
---|
355 | imcb_error( ic, "Syntax error" ); |
---|
356 | imc_logout( ic, TRUE ); |
---|
357 | return( 0 ); |
---|
358 | } |
---|
359 | |
---|
360 | http_decode( cmd[5] ); |
---|
361 | imcb_rename_buddy( ic, cmd[3], cmd[5] ); |
---|
362 | |
---|
363 | st = msn_away_state_by_code( cmd[2] ); |
---|
364 | if( !st ) |
---|
365 | { |
---|
366 | /* FIXME: Warn/Bomb about unknown away state? */ |
---|
367 | st = msn_away_state_list + 1; |
---|
368 | } |
---|
369 | |
---|
370 | imcb_buddy_status( ic, cmd[3], OPT_LOGGED_IN | |
---|
371 | ( st != msn_away_state_list ? OPT_AWAY : 0 ), |
---|
372 | st->name, NULL ); |
---|
373 | } |
---|
374 | else if( strcmp( cmd[0], "FLN" ) == 0 ) |
---|
375 | { |
---|
376 | if( cmd[1] == NULL ) |
---|
377 | return 1; |
---|
378 | |
---|
379 | imcb_buddy_status( ic, cmd[1], 0, NULL, NULL ); |
---|
380 | |
---|
381 | msn_sb_start_keepalives( msn_sb_by_handle( ic, cmd[1] ), TRUE ); |
---|
382 | } |
---|
383 | else if( strcmp( cmd[0], "NLN" ) == 0 ) |
---|
384 | { |
---|
385 | const struct msn_away_state *st; |
---|
386 | int cap; |
---|
387 | |
---|
388 | if( num_parts < 6 ) |
---|
389 | { |
---|
390 | imcb_error( ic, "Syntax error" ); |
---|
391 | imc_logout( ic, TRUE ); |
---|
392 | return( 0 ); |
---|
393 | } |
---|
394 | |
---|
395 | http_decode( cmd[4] ); |
---|
396 | cap = atoi( cmd[5] ); |
---|
397 | imcb_rename_buddy( ic, cmd[2], cmd[4] ); |
---|
398 | |
---|
399 | st = msn_away_state_by_code( cmd[1] ); |
---|
400 | if( !st ) |
---|
401 | { |
---|
402 | /* FIXME: Warn/Bomb about unknown away state? */ |
---|
403 | st = msn_away_state_list + 1; |
---|
404 | } |
---|
405 | |
---|
406 | imcb_buddy_status( ic, cmd[2], OPT_LOGGED_IN | |
---|
407 | ( st != msn_away_state_list ? OPT_AWAY : 0 ) | |
---|
408 | ( cap & 1 ? OPT_MOBILE : 0 ), |
---|
409 | st->name, NULL ); |
---|
410 | |
---|
411 | msn_sb_stop_keepalives( msn_sb_by_handle( ic, cmd[2] ) ); |
---|
412 | } |
---|
413 | else if( strcmp( cmd[0], "RNG" ) == 0 ) |
---|
414 | { |
---|
415 | struct msn_switchboard *sb; |
---|
416 | char *server; |
---|
417 | int session, port; |
---|
418 | |
---|
419 | if( num_parts < 7 ) |
---|
420 | { |
---|
421 | imcb_error( ic, "Syntax error" ); |
---|
422 | imc_logout( ic, TRUE ); |
---|
423 | return( 0 ); |
---|
424 | } |
---|
425 | |
---|
426 | session = atoi( cmd[1] ); |
---|
427 | |
---|
428 | server = strchr( cmd[2], ':' ); |
---|
429 | if( !server ) |
---|
430 | { |
---|
431 | imcb_error( ic, "Syntax error" ); |
---|
432 | imc_logout( ic, TRUE ); |
---|
433 | return( 0 ); |
---|
434 | } |
---|
435 | *server = 0; |
---|
436 | port = atoi( server + 1 ); |
---|
437 | server = cmd[2]; |
---|
438 | |
---|
439 | if( strcmp( cmd[3], "CKI" ) != 0 ) |
---|
440 | { |
---|
441 | imcb_error( ic, "Unknown authentication method for switchboard" ); |
---|
442 | imc_logout( ic, TRUE ); |
---|
443 | return( 0 ); |
---|
444 | } |
---|
445 | |
---|
446 | debug( "Got a call from %s (session %d). Key = %s", cmd[5], session, cmd[4] ); |
---|
447 | |
---|
448 | if( ( sb = msn_sb_create( ic, server, port, cmd[4], session ) ) == NULL ) |
---|
449 | { |
---|
450 | /* Although this isn't strictly fatal for the NS connection, it's |
---|
451 | definitely something serious (we ran out of file descriptors?). */ |
---|
452 | imcb_error( ic, "Could not create new switchboard" ); |
---|
453 | imc_logout( ic, TRUE ); |
---|
454 | return( 0 ); |
---|
455 | } |
---|
456 | else |
---|
457 | { |
---|
458 | sb->who = g_strdup( cmd[5] ); |
---|
459 | } |
---|
460 | } |
---|
461 | else if( strcmp( cmd[0], "OUT" ) == 0 ) |
---|
462 | { |
---|
463 | int allow_reconnect = TRUE; |
---|
464 | |
---|
465 | if( cmd[1] && strcmp( cmd[1], "OTH" ) == 0 ) |
---|
466 | { |
---|
467 | imcb_error( ic, "Someone else logged in with your account" ); |
---|
468 | allow_reconnect = FALSE; |
---|
469 | } |
---|
470 | else if( cmd[1] && strcmp( cmd[1], "SSD" ) == 0 ) |
---|
471 | { |
---|
472 | imcb_error( ic, "Terminating session because of server shutdown" ); |
---|
473 | } |
---|
474 | else |
---|
475 | { |
---|
476 | imcb_error( ic, "Session terminated by remote server (reason unknown)" ); |
---|
477 | } |
---|
478 | |
---|
479 | imc_logout( ic, allow_reconnect ); |
---|
480 | return( 0 ); |
---|
481 | } |
---|
482 | else if( strcmp( cmd[0], "IPG" ) == 0 ) |
---|
483 | { |
---|
484 | imcb_error( ic, "Received IPG command, we don't handle them yet." ); |
---|
485 | |
---|
486 | handler->msglen = atoi( cmd[1] ); |
---|
487 | |
---|
488 | if( handler->msglen <= 0 ) |
---|
489 | { |
---|
490 | imcb_error( ic, "Syntax error" ); |
---|
491 | imc_logout( ic, TRUE ); |
---|
492 | return( 0 ); |
---|
493 | } |
---|
494 | } |
---|
495 | #if 0 |
---|
496 | else if( strcmp( cmd[0], "ADG" ) == 0 ) |
---|
497 | { |
---|
498 | char *group = g_strdup( cmd[3] ); |
---|
499 | int groupnum, i; |
---|
500 | GSList *l, *next; |
---|
501 | |
---|
502 | http_decode( group ); |
---|
503 | if( sscanf( cmd[4], "%d", &groupnum ) == 1 ) |
---|
504 | { |
---|
505 | if( groupnum >= md->groupcount ) |
---|
506 | { |
---|
507 | md->grouplist = g_renew( char *, md->grouplist, groupnum + 1 ); |
---|
508 | for( i = md->groupcount; i <= groupnum; i ++ ) |
---|
509 | md->grouplist[i] = NULL; |
---|
510 | md->groupcount = groupnum + 1; |
---|
511 | } |
---|
512 | g_free( md->grouplist[groupnum] ); |
---|
513 | md->grouplist[groupnum] = group; |
---|
514 | } |
---|
515 | else |
---|
516 | { |
---|
517 | /* Shouldn't happen, but if it does, give up on the group. */ |
---|
518 | g_free( group ); |
---|
519 | imcb_error( ic, "Syntax error" ); |
---|
520 | imc_logout( ic, TRUE ); |
---|
521 | return 0; |
---|
522 | } |
---|
523 | |
---|
524 | for( l = md->grpq; l; l = next ) |
---|
525 | { |
---|
526 | struct msn_groupadd *ga = l->data; |
---|
527 | next = l->next; |
---|
528 | if( g_strcasecmp( ga->group, group ) == 0 ) |
---|
529 | { |
---|
530 | if( !msn_buddy_list_add( ic, "FL", ga->who, ga->who, group ) ) |
---|
531 | return 0; |
---|
532 | |
---|
533 | g_free( ga->group ); |
---|
534 | g_free( ga->who ); |
---|
535 | g_free( ga ); |
---|
536 | md->grpq = g_slist_remove( md->grpq, ga ); |
---|
537 | } |
---|
538 | } |
---|
539 | } |
---|
540 | #endif |
---|
541 | else if( strcmp( cmd[0], "GCF" ) == 0 ) |
---|
542 | { |
---|
543 | /* Coming up is cmd[2] bytes of stuff we're supposed to |
---|
544 | censore. Meh. */ |
---|
545 | handler->msglen = atoi( cmd[2] ); |
---|
546 | } |
---|
547 | else if( strcmp( cmd[0], "UBX" ) == 0 ) |
---|
548 | { |
---|
549 | /* Status message. */ |
---|
550 | if( num_parts >= 4 ) |
---|
551 | handler->msglen = atoi( cmd[3] ); |
---|
552 | } |
---|
553 | else if( strcmp( cmd[0], "NOT" ) == 0 ) |
---|
554 | { |
---|
555 | /* Some kind of notification, poorly documented but |
---|
556 | apparently used to announce address book changes. */ |
---|
557 | if( num_parts >= 2 ) |
---|
558 | handler->msglen = atoi( cmd[1] ); |
---|
559 | } |
---|
560 | else if( isdigit( cmd[0][0] ) ) |
---|
561 | { |
---|
562 | int num = atoi( cmd[0] ); |
---|
563 | const struct msn_status_code *err = msn_status_by_number( num ); |
---|
564 | |
---|
565 | imcb_error( ic, "Error reported by MSN server: %s", err->text ); |
---|
566 | |
---|
567 | if( err->flags & STATUS_FATAL ) |
---|
568 | { |
---|
569 | imc_logout( ic, TRUE ); |
---|
570 | return( 0 ); |
---|
571 | } |
---|
572 | |
---|
573 | /* Oh yes, errors can have payloads too now. Discard them for now. */ |
---|
574 | if( num_parts >= 3 ) |
---|
575 | handler->msglen = atoi( cmd[2] ); |
---|
576 | } |
---|
577 | else |
---|
578 | { |
---|
579 | /* debug( "Received unknown command from main server: %s", cmd[0] ); */ |
---|
580 | } |
---|
581 | |
---|
582 | return( 1 ); |
---|
583 | } |
---|
584 | |
---|
585 | static int msn_ns_message( struct msn_handler_data *handler, char *msg, int msglen, char **cmd, int num_parts ) |
---|
586 | { |
---|
587 | struct im_connection *ic = handler->data; |
---|
588 | char *body; |
---|
589 | int blen = 0; |
---|
590 | |
---|
591 | if( !num_parts ) |
---|
592 | return( 1 ); |
---|
593 | |
---|
594 | if( ( body = strstr( msg, "\r\n\r\n" ) ) ) |
---|
595 | { |
---|
596 | body += 4; |
---|
597 | blen = msglen - ( body - msg ); |
---|
598 | } |
---|
599 | |
---|
600 | if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
601 | { |
---|
602 | if( g_strcasecmp( cmd[1], "Hotmail" ) == 0 ) |
---|
603 | { |
---|
604 | char *ct = msn_findheader( msg, "Content-Type:", msglen ); |
---|
605 | |
---|
606 | if( !ct ) |
---|
607 | return( 1 ); |
---|
608 | |
---|
609 | if( g_strncasecmp( ct, "application/x-msmsgssystemmessage", 33 ) == 0 ) |
---|
610 | { |
---|
611 | char *mtype; |
---|
612 | char *arg1; |
---|
613 | |
---|
614 | if( !body ) |
---|
615 | return( 1 ); |
---|
616 | |
---|
617 | mtype = msn_findheader( body, "Type:", blen ); |
---|
618 | arg1 = msn_findheader( body, "Arg1:", blen ); |
---|
619 | |
---|
620 | if( mtype && strcmp( mtype, "1" ) == 0 ) |
---|
621 | { |
---|
622 | if( arg1 ) |
---|
623 | imcb_log( ic, "The server is going down for maintenance in %s minutes.", arg1 ); |
---|
624 | } |
---|
625 | |
---|
626 | g_free( arg1 ); |
---|
627 | g_free( mtype ); |
---|
628 | } |
---|
629 | else if( g_strncasecmp( ct, "text/x-msmsgsprofile", 20 ) == 0 ) |
---|
630 | { |
---|
631 | /* We don't care about this profile for now... */ |
---|
632 | } |
---|
633 | else if( g_strncasecmp( ct, "text/x-msmsgsinitialemailnotification", 37 ) == 0 ) |
---|
634 | { |
---|
635 | if( set_getbool( &ic->acc->set, "mail_notifications" ) ) |
---|
636 | { |
---|
637 | char *inbox = msn_findheader( body, "Inbox-Unread:", blen ); |
---|
638 | char *folders = msn_findheader( body, "Folders-Unread:", blen ); |
---|
639 | |
---|
640 | if( inbox && folders ) |
---|
641 | imcb_log( ic, "INBOX contains %s new messages, plus %s messages in other folders.", inbox, folders ); |
---|
642 | |
---|
643 | g_free( inbox ); |
---|
644 | g_free( folders ); |
---|
645 | } |
---|
646 | } |
---|
647 | else if( g_strncasecmp( ct, "text/x-msmsgsemailnotification", 30 ) == 0 ) |
---|
648 | { |
---|
649 | if( set_getbool( &ic->acc->set, "mail_notifications" ) ) |
---|
650 | { |
---|
651 | char *from = msn_findheader( body, "From-Addr:", blen ); |
---|
652 | char *fromname = msn_findheader( body, "From:", blen ); |
---|
653 | |
---|
654 | if( from && fromname ) |
---|
655 | imcb_log( ic, "Received an e-mail message from %s <%s>.", fromname, from ); |
---|
656 | |
---|
657 | g_free( from ); |
---|
658 | g_free( fromname ); |
---|
659 | } |
---|
660 | } |
---|
661 | else if( g_strncasecmp( ct, "text/x-msmsgsactivemailnotification", 35 ) == 0 ) |
---|
662 | { |
---|
663 | /* Sorry, but this one really is *USELESS* */ |
---|
664 | } |
---|
665 | else |
---|
666 | { |
---|
667 | debug( "Can't handle %s packet from notification server", ct ); |
---|
668 | } |
---|
669 | |
---|
670 | g_free( ct ); |
---|
671 | } |
---|
672 | } |
---|
673 | else if( strcmp( cmd[0], "UBX" ) == 0 ) |
---|
674 | { |
---|
675 | struct xt_node *psm; |
---|
676 | char *psm_text = NULL; |
---|
677 | |
---|
678 | psm = xt_from_string( msg ); |
---|
679 | if( psm && strcmp( psm->name, "Data" ) == 0 && |
---|
680 | ( psm = xt_find_node( psm->children, "PSM" ) ) ) |
---|
681 | psm_text = psm->text; |
---|
682 | |
---|
683 | imcb_buddy_status_msg( ic, cmd[1], psm_text ); |
---|
684 | xt_free_node( psm ); |
---|
685 | } |
---|
686 | else if( strcmp( cmd[0], "ADL" ) == 0 ) |
---|
687 | { |
---|
688 | struct xt_node *adl, *d, *c; |
---|
689 | |
---|
690 | if( !( adl = xt_from_string( msg ) ) ) |
---|
691 | return 1; |
---|
692 | |
---|
693 | for( d = adl->children; d; d = d->next ) |
---|
694 | { |
---|
695 | char *dn; |
---|
696 | if( strcmp( d->name, "d" ) != 0 || |
---|
697 | ( dn = xt_find_attr( d, "n" ) ) == NULL ) |
---|
698 | continue; |
---|
699 | for( c = d->children; c; c = c->next ) |
---|
700 | { |
---|
701 | bee_user_t *bu; |
---|
702 | struct msn_buddy_data *bd; |
---|
703 | char *cn, *handle, *f, *l; |
---|
704 | int flags; |
---|
705 | |
---|
706 | if( strcmp( c->name, "c" ) != 0 || |
---|
707 | ( l = xt_find_attr( c, "l" ) ) == NULL || |
---|
708 | ( cn = xt_find_attr( c, "n" ) ) == NULL ) |
---|
709 | continue; |
---|
710 | |
---|
711 | handle = g_strdup_printf( "%s@%s", cn, dn ); |
---|
712 | if( !( ( bu = bee_user_by_handle( ic->bee, ic, handle ) ) || |
---|
713 | ( bu = bee_user_new( ic->bee, ic, handle, 0 ) ) ) ) |
---|
714 | { |
---|
715 | g_free( handle ); |
---|
716 | continue; |
---|
717 | } |
---|
718 | g_free( handle ); |
---|
719 | bd = bu->data; |
---|
720 | |
---|
721 | if( ( f = xt_find_attr( c, "f" ) ) ) |
---|
722 | { |
---|
723 | http_decode( f ); |
---|
724 | imcb_rename_buddy( ic, bu->handle, f ); |
---|
725 | } |
---|
726 | |
---|
727 | flags = atoi( l ) & 15; |
---|
728 | if( bd->flags != flags ) |
---|
729 | { |
---|
730 | bd->flags = flags; |
---|
731 | msn_buddy_ask( bu ); |
---|
732 | } |
---|
733 | } |
---|
734 | } |
---|
735 | } |
---|
736 | |
---|
737 | return( 1 ); |
---|
738 | } |
---|
739 | |
---|
740 | void msn_auth_got_passport_token( struct im_connection *ic, const char *token, const char *error ) |
---|
741 | { |
---|
742 | struct msn_data *md; |
---|
743 | |
---|
744 | /* Dead connection? */ |
---|
745 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
746 | return; |
---|
747 | |
---|
748 | md = ic->proto_data; |
---|
749 | |
---|
750 | if( token ) |
---|
751 | { |
---|
752 | msn_ns_write( ic, -1, "USR %d SSO S %s %s\r\n", ++md->trId, md->tokens[0], token ); |
---|
753 | } |
---|
754 | else |
---|
755 | { |
---|
756 | imcb_error( ic, "Error during Passport authentication: %s", error ); |
---|
757 | imc_logout( ic, TRUE ); |
---|
758 | } |
---|
759 | } |
---|
760 | |
---|
761 | void msn_auth_got_contact_list( struct im_connection *ic ) |
---|
762 | { |
---|
763 | struct msn_data *md; |
---|
764 | |
---|
765 | /* Dead connection? */ |
---|
766 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
767 | return; |
---|
768 | |
---|
769 | md = ic->proto_data; |
---|
770 | msn_ns_write( ic, -1, "BLP %d %s\r\n", ++md->trId, "BL" ); |
---|
771 | } |
---|
772 | |
---|
773 | static gboolean msn_ns_send_adl_1( gpointer key, gpointer value, gpointer data ) |
---|
774 | { |
---|
775 | struct xt_node *adl = data, *d, *c; |
---|
776 | struct bee_user *bu = value; |
---|
777 | struct msn_buddy_data *bd = bu->data; |
---|
778 | struct msn_data *md = bu->ic->proto_data; |
---|
779 | char handle[strlen(bu->handle)]; |
---|
780 | char *domain; |
---|
781 | char l[4]; |
---|
782 | |
---|
783 | if( ( bd->flags & 7 ) == 0 || ( bd->flags & MSN_BUDDY_ADL_SYNCED ) ) |
---|
784 | return FALSE; |
---|
785 | |
---|
786 | strcpy( handle, bu->handle ); |
---|
787 | if( ( domain = strchr( handle, '@' ) ) == NULL ) /* WTF */ |
---|
788 | return FALSE; |
---|
789 | *domain = '\0'; |
---|
790 | domain ++; |
---|
791 | |
---|
792 | if( ( d = adl->children ) == NULL || |
---|
793 | g_strcasecmp( xt_find_attr( d, "n" ), domain ) != 0 ) |
---|
794 | { |
---|
795 | d = xt_new_node( "d", NULL, NULL ); |
---|
796 | xt_add_attr( d, "n", domain ); |
---|
797 | xt_insert_child( adl, d ); |
---|
798 | } |
---|
799 | |
---|
800 | g_snprintf( l, sizeof( l ), "%d", bd->flags & 7 ); |
---|
801 | c = xt_new_node( "c", NULL, NULL ); |
---|
802 | xt_add_attr( c, "n", handle ); |
---|
803 | xt_add_attr( c, "l", l ); |
---|
804 | xt_add_attr( c, "t", "1" ); /* 1 means normal, 4 means mobile? */ |
---|
805 | xt_insert_child( d, c ); |
---|
806 | |
---|
807 | /* Do this in batches of 100. */ |
---|
808 | bd->flags |= MSN_BUDDY_ADL_SYNCED; |
---|
809 | return (--md->adl_todo % 140) == 0; |
---|
810 | } |
---|
811 | |
---|
812 | static void msn_ns_send_adl( struct im_connection *ic ) |
---|
813 | { |
---|
814 | struct xt_node *adl; |
---|
815 | struct msn_data *md = ic->proto_data; |
---|
816 | char *adls; |
---|
817 | |
---|
818 | adl = xt_new_node( "ml", NULL, NULL ); |
---|
819 | xt_add_attr( adl, "l", "1" ); |
---|
820 | g_tree_foreach( md->domaintree, msn_ns_send_adl_1, adl ); |
---|
821 | if( adl->children == NULL ) |
---|
822 | { |
---|
823 | /* This tells the caller that we're done now. */ |
---|
824 | md->adl_todo = -1; |
---|
825 | xt_free_node( adl ); |
---|
826 | return; |
---|
827 | } |
---|
828 | |
---|
829 | adls = xt_to_string( adl ); |
---|
830 | xt_free_node( adl ); |
---|
831 | msn_ns_write( ic, -1, "ADL %d %zd\r\n%s", ++md->trId, strlen( adls ), adls ); |
---|
832 | g_free( adls ); |
---|
833 | } |
---|
834 | |
---|
835 | static void msn_ns_send_adl_start( struct im_connection *ic ) |
---|
836 | { |
---|
837 | struct msn_data *md; |
---|
838 | GSList *l; |
---|
839 | |
---|
840 | /* Dead connection? */ |
---|
841 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
842 | return; |
---|
843 | |
---|
844 | md = ic->proto_data; |
---|
845 | md->adl_todo = 0; |
---|
846 | for( l = ic->bee->users; l; l = l->next ) |
---|
847 | { |
---|
848 | bee_user_t *bu = l->data; |
---|
849 | struct msn_buddy_data *bd = bu->data; |
---|
850 | |
---|
851 | if( bu->ic != ic || ( bd->flags & 7 ) == 0 ) |
---|
852 | continue; |
---|
853 | |
---|
854 | bd->flags &= ~MSN_BUDDY_ADL_SYNCED; |
---|
855 | md->adl_todo++; |
---|
856 | } |
---|
857 | |
---|
858 | msn_ns_send_adl( ic ); |
---|
859 | } |
---|
860 | |
---|
861 | int msn_ns_finish_login( struct im_connection *ic ) |
---|
862 | { |
---|
863 | struct msn_data *md = ic->proto_data; |
---|
864 | |
---|
865 | if( ic->flags & OPT_LOGGED_IN ) |
---|
866 | return 1; |
---|
867 | |
---|
868 | if( md->adl_todo < 0 ) |
---|
869 | md->flags |= MSN_DONE_ADL; |
---|
870 | |
---|
871 | if( ( md->flags & MSN_DONE_ADL ) && ( md->flags & MSN_GOT_PROFILE ) ) |
---|
872 | return msn_ns_set_display_name( ic, set_getstr( &ic->acc->set, "display_name" ) ); |
---|
873 | else |
---|
874 | return 1; |
---|
875 | } |
---|