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