source: protocols/msn/msn.c @ a6a2d81

Last change on this file since a6a2d81 was e132b60, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-11-11T23:32:47Z

Extend keepalive code to time out connections when pings don't get
acknowledged, using this for Twitter streams and MSN so far.

  • Property mode set to 100644
File size: 12.2 KB
Line 
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 - Main file; functions to be called from BitlBee          */
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 "nogaim.h"
27#include "soap.h"
28#include "msn.h"
29
30int msn_chat_id;
31GSList *msn_connections;
32GSList *msn_switchboards;
33
34static char *set_eval_display_name( set_t *set, char *value );
35
36static void msn_init( account_t *acc )
37{
38        set_t *s;
39       
40        s = set_add( &acc->set, "display_name", NULL, set_eval_display_name, acc );
41        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
42       
43        set_add( &acc->set, "mail_notifications", "false", set_eval_bool, acc );
44        set_add( &acc->set, "switchboard_keepalives", "false", set_eval_bool, acc );
45       
46        acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE;
47}
48
49static void msn_login( account_t *acc )
50{
51        struct im_connection *ic = imcb_new( acc );
52        struct msn_data *md = g_new0( struct msn_data, 1 );
53       
54        ic->proto_data = md;
55        ic->flags |= OPT_PONGS | OPT_PONGED;
56       
57        if( strchr( acc->user, '@' ) == NULL )
58        {
59                imcb_error( ic, "Invalid account name" );
60                imc_logout( ic, FALSE );
61                return;
62        }
63       
64        md->ic = ic;
65        md->away_state = msn_away_state_list;
66        md->domaintree = g_tree_new( msn_domaintree_cmp );
67        md->ns->fd = -1;
68       
69        msn_connections = g_slist_prepend( msn_connections, ic );
70       
71        imcb_log( ic, "Connecting" );
72        msn_ns_connect( ic, md->ns, MSN_NS_HOST, MSN_NS_PORT );
73}
74
75static void msn_logout( struct im_connection *ic )
76{
77        struct msn_data *md = ic->proto_data;
78        GSList *l;
79        int i;
80       
81        if( md )
82        {
83                /** Disabling MSN ft support for now.
84                while( md->filetransfers ) {
85                        imcb_file_canceled( md->filetransfers->data, "Closing connection" );
86                }
87                */
88               
89                msn_ns_close( md->ns );
90               
91                while( md->switchboards )
92                        msn_sb_destroy( md->switchboards->data );
93               
94                msn_msgq_purge( ic, &md->msgq );
95                msn_soapq_flush( ic, FALSE );
96               
97                for( i = 0; i < sizeof( md->tokens ) / sizeof( md->tokens[0] ); i ++ )
98                        g_free( md->tokens[i] );
99                g_free( md->lock_key );
100                g_free( md->pp_policy );
101                g_free( md->uuid );
102               
103                while( md->groups )
104                {
105                        struct msn_group *mg = md->groups->data;
106                        g_free( mg->id );
107                        g_free( mg->name );
108                        g_free( mg );
109                        md->groups = g_slist_remove( md->groups, mg );
110                }
111               
112                g_free( md->profile_rid );
113               
114                if( md->domaintree )
115                        g_tree_destroy( md->domaintree );
116                md->domaintree = NULL;
117               
118                while( md->grpq )
119                {
120                        struct msn_groupadd *ga = md->grpq->data;
121                        g_free( ga->group );
122                        g_free( ga->who );
123                        g_free( ga );
124                        md->grpq = g_slist_remove( md->grpq, ga );
125                }
126               
127                g_free( md );
128        }
129       
130        for( l = ic->permit; l; l = l->next )
131                g_free( l->data );
132        g_slist_free( ic->permit );
133       
134        for( l = ic->deny; l; l = l->next )
135                g_free( l->data );
136        g_slist_free( ic->deny );
137       
138        msn_connections = g_slist_remove( msn_connections, ic );
139}
140
141static int msn_buddy_msg( struct im_connection *ic, char *who, char *message, int away )
142{
143        struct bee_user *bu = bee_user_by_handle( ic->bee, ic, who );
144        struct msn_buddy_data *bd = bu ? bu->data : NULL;
145        struct msn_switchboard *sb;
146       
147#ifdef DEBUG
148        if( strcmp( who, "raw" ) == 0 )
149        {
150                msn_ns_write( ic, -1, "%s\r\n", message );
151        }
152        else
153#endif
154        if( bd && bd->flags & MSN_BUDDY_FED )
155        {
156                msn_ns_sendmessage( ic, bu, message );
157        }
158        else if( ( sb = msn_sb_by_handle( ic, who ) ) )
159        {
160                return( msn_sb_sendmessage( sb, message ) );
161        }
162        else
163        {
164                struct msn_message *m;
165               
166                /* Create a message. We have to arrange a usable switchboard, and send the message later. */
167                m = g_new0( struct msn_message, 1 );
168                m->who = g_strdup( who );
169                m->text = g_strdup( message );
170               
171                return msn_sb_write_msg( ic, m );
172        }
173       
174        return( 0 );
175}
176
177static GList *msn_away_states( struct im_connection *ic )
178{
179        static GList *l = NULL;
180        int i;
181       
182        if( l == NULL )
183                for( i = 0; *msn_away_state_list[i].code; i ++ )
184                        if( *msn_away_state_list[i].name )
185                                l = g_list_append( l, (void*) msn_away_state_list[i].name );
186       
187        return l;
188}
189
190static void msn_set_away( struct im_connection *ic, char *state, char *message )
191{
192        char *uux;
193        struct msn_data *md = ic->proto_data;
194       
195        if( state == NULL )
196                md->away_state = msn_away_state_list;
197        else if( ( md->away_state = msn_away_state_by_name( state ) ) == NULL )
198                md->away_state = msn_away_state_list + 1;
199       
200        if( !msn_ns_write( ic, -1, "CHG %d %s %d:%02d\r\n", ++md->trId, md->away_state->code, MSN_CAP1, MSN_CAP2 ) )
201                return;
202       
203        uux = g_markup_printf_escaped( "<EndpointData><Capabilities>%d:%02d"
204                                       "</Capabilities></EndpointData>",
205                                       MSN_CAP1, MSN_CAP2 );
206        msn_ns_write( ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen( uux ), uux );
207        g_free( uux );
208       
209        uux = g_markup_printf_escaped( "<PrivateEndpointData><EpName>%s</EpName>"
210                                       "<Idle>%s</Idle><ClientType>%d</ClientType>"
211                                       "<State>%s</State></PrivateEndpointData>",
212                                       md->uuid,
213                                       strcmp( md->away_state->code, "IDL" ) ? "false" : "true",
214                                       1, /* ? */
215                                       md->away_state->code );
216        msn_ns_write( ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen( uux ), uux );
217        g_free( uux );
218       
219        uux = g_markup_printf_escaped( "<Data><DDP></DDP><PSM>%s</PSM>"
220                                       "<CurrentMedia></CurrentMedia>"
221                                       "<MachineGuid>%s</MachineGuid></Data>",
222                                       message ? message : "", md->uuid );
223        msn_ns_write( ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen( uux ), uux );
224        g_free( uux );
225}
226
227static void msn_get_info(struct im_connection *ic, char *who) 
228{
229        /* Just make an URL and let the user fetch the info */
230        imcb_log( ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who );
231}
232
233static void msn_add_buddy( struct im_connection *ic, char *who, char *group )
234{
235        struct bee_user *bu = bee_user_by_handle( ic->bee, ic, who );
236       
237        msn_buddy_list_add( ic, MSN_BUDDY_FL, who, who, group );
238        if( bu && bu->group )
239                msn_buddy_list_remove( ic, MSN_BUDDY_FL, who, bu->group->name );
240}
241
242static void msn_remove_buddy( struct im_connection *ic, char *who, char *group )
243{
244        msn_buddy_list_remove( ic, MSN_BUDDY_FL, who, NULL );
245}
246
247static void msn_chat_msg( struct groupchat *c, char *message, int flags )
248{
249        struct msn_switchboard *sb = msn_sb_by_chat( c );
250       
251        if( sb )
252                msn_sb_sendmessage( sb, message );
253        /* FIXME: Error handling (although this can't happen unless something's
254           already severely broken) disappeared here! */
255}
256
257static void msn_chat_invite( struct groupchat *c, char *who, char *message )
258{
259        struct msn_switchboard *sb = msn_sb_by_chat( c );
260       
261        if( sb )
262                msn_sb_write( sb, "CAL %d %s\r\n", ++sb->trId, who );
263}
264
265static void msn_chat_leave( struct groupchat *c )
266{
267        struct msn_switchboard *sb = msn_sb_by_chat( c );
268       
269        if( sb )
270                msn_sb_write( sb, "OUT\r\n" );
271}
272
273static struct groupchat *msn_chat_with( struct im_connection *ic, char *who )
274{
275        struct msn_switchboard *sb;
276        struct groupchat *c = imcb_chat_new( ic, who );
277       
278        if( ( sb = msn_sb_by_handle( ic, who ) ) )
279        {
280                debug( "Converting existing switchboard to %s to a groupchat", who );
281                return msn_sb_to_chat( sb );
282        }
283        else
284        {
285                struct msn_message *m;
286               
287                /* Create a magic message. This is quite hackish, but who cares? :-P */
288                m = g_new0( struct msn_message, 1 );
289                m->who = g_strdup( who );
290                m->text = g_strdup( GROUPCHAT_SWITCHBOARD_MESSAGE );
291               
292                msn_sb_write_msg( ic, m );
293
294                return c;
295        }
296}
297
298static void msn_keepalive( struct im_connection *ic )
299{
300        msn_ns_write( ic, -1, "PNG\r\n" );
301}
302
303static void msn_add_permit( struct im_connection *ic, char *who )
304{
305        msn_buddy_list_add( ic, MSN_BUDDY_AL, who, who, NULL );
306}
307
308static void msn_rem_permit( struct im_connection *ic, char *who )
309{
310        msn_buddy_list_remove( ic, MSN_BUDDY_AL, who, NULL );
311}
312
313static void msn_add_deny( struct im_connection *ic, char *who )
314{
315        struct msn_switchboard *sb;
316       
317        msn_buddy_list_add( ic, MSN_BUDDY_BL, who, who, NULL );
318       
319        /* If there's still a conversation with this person, close it. */
320        if( ( sb = msn_sb_by_handle( ic, who ) ) )
321        {
322                msn_sb_destroy( sb );
323        }
324}
325
326static void msn_rem_deny( struct im_connection *ic, char *who )
327{
328        msn_buddy_list_remove( ic, MSN_BUDDY_BL, who, NULL );
329}
330
331static int msn_send_typing( struct im_connection *ic, char *who, int typing )
332{
333        struct bee_user *bu = bee_user_by_handle( ic->bee, ic, who );
334       
335        if( !( bu->flags & BEE_USER_ONLINE ) )
336                return 0;
337        else if( typing & OPT_TYPING )
338                return( msn_buddy_msg( ic, who, TYPING_NOTIFICATION_MESSAGE, 0 ) );
339        else
340                return 1;
341}
342
343static char *set_eval_display_name( set_t *set, char *value )
344{
345        account_t *acc = set->data;
346        struct im_connection *ic = acc->ic;
347        struct msn_data *md = ic->proto_data;
348       
349        if( md->flags & MSN_EMAIL_UNVERIFIED )
350                imcb_log( ic, "Warning: Your e-mail address is unverified. MSN doesn't allow "
351                              "changing your display name until your e-mail address is verified." );
352       
353        if( md->flags & MSN_GOT_PROFILE_DN )
354                msn_soap_profile_set_dn( ic, value );
355        else
356                msn_soap_addressbook_set_display_name( ic, value );
357       
358        return msn_ns_set_display_name( ic, value ) ? value : NULL;
359}
360
361static void msn_buddy_data_add( bee_user_t *bu )
362{
363        struct msn_data *md = bu->ic->proto_data;
364        struct msn_buddy_data *bd;
365        char *handle;
366       
367        bd = bu->data = g_new0( struct msn_buddy_data, 1 );
368        g_tree_insert( md->domaintree, bu->handle, bu );
369       
370        for( handle = bu->handle; isdigit( *handle ); handle ++ );
371        if( *handle == ':' )
372        {
373                /* Pass a nick hint so hopefully the stupid numeric prefix
374                   won't show up to the user.  */
375                char *s = strchr( ++handle, '@' );
376                if( s )
377                {
378                        handle = g_strndup( handle, s - handle );
379                        imcb_buddy_nick_hint( bu->ic, bu->handle, handle );
380                        g_free( handle );
381                }
382               
383                bd->flags |= MSN_BUDDY_FED;
384        }
385}
386
387static void msn_buddy_data_free( bee_user_t *bu )
388{
389        struct msn_data *md = bu->ic->proto_data;
390        struct msn_buddy_data *bd = bu->data;
391       
392        g_free( bd->cid );
393        g_free( bd );
394       
395        g_tree_remove( md->domaintree, bu->handle );
396}
397
398GList *msn_buddy_action_list( bee_user_t *bu )
399{
400        static GList *ret = NULL;
401       
402        if( ret == NULL )
403        {
404                static const struct buddy_action ba[2] = {
405                        { "NUDGE", "Draw attention" },
406                };
407               
408                ret = g_list_prepend( ret, (void*) ba + 0 );
409        }
410       
411        return ret;
412}
413
414void *msn_buddy_action( struct bee_user *bu, const char *action, char * const args[], void *data )
415{
416        if( g_strcasecmp( action, "NUDGE" ) == 0 )
417                msn_buddy_msg( bu->ic, bu->handle, NUDGE_MESSAGE, 0 );
418       
419        return NULL;
420}
421
422void msn_initmodule()
423{
424        struct prpl *ret = g_new0(struct prpl, 1);
425       
426        ret->name = "msn";
427        ret->mms = 1409;         /* this guess taken from libotr UPGRADING file */
428        ret->login = msn_login;
429        ret->init = msn_init;
430        ret->logout = msn_logout;
431        ret->buddy_msg = msn_buddy_msg;
432        ret->away_states = msn_away_states;
433        ret->set_away = msn_set_away;
434        ret->get_info = msn_get_info;
435        ret->add_buddy = msn_add_buddy;
436        ret->remove_buddy = msn_remove_buddy;
437        ret->chat_msg = msn_chat_msg;
438        ret->chat_invite = msn_chat_invite;
439        ret->chat_leave = msn_chat_leave;
440        ret->chat_with = msn_chat_with;
441        ret->keepalive = msn_keepalive;
442        ret->add_permit = msn_add_permit;
443        ret->rem_permit = msn_rem_permit;
444        ret->add_deny = msn_add_deny;
445        ret->rem_deny = msn_rem_deny;
446        ret->send_typing = msn_send_typing;
447        ret->handle_cmp = g_strcasecmp;
448        ret->buddy_data_add = msn_buddy_data_add;
449        ret->buddy_data_free = msn_buddy_data_free;
450        ret->buddy_action_list = msn_buddy_action_list;
451        ret->buddy_action = msn_buddy_action;
452       
453        //ret->transfer_request = msn_ftp_transfer_request;
454
455        register_protocol(ret);
456}
Note: See TracBrowser for help on using the repository browser.