source: protocols/msn/msn.c @ 3bf9f71

Last change on this file since 3bf9f71 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
RevLine 
[b7d3cc34]1  /********************************************************************\
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 - 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"
[4452e69]27#include "soap.h"
[b7d3cc34]28#include "msn.h"
29
[c6ca3ee]30int msn_chat_id;
31GSList *msn_connections;
32GSList *msn_switchboards;
33
[e3413cc]34static char *set_eval_display_name( set_t *set, char *value );
[911f2eb]35
[0da65d5]36static void msn_init( account_t *acc )
[911f2eb]37{
[4452e69]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       
[e3413cc]43        set_add( &acc->set, "mail_notifications", "false", set_eval_bool, acc );
[efbc154]44        set_add( &acc->set, "switchboard_keepalives", "false", set_eval_bool, acc );
[12767e3]45       
46        acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE;
[911f2eb]47}
48
[0a3c243]49static void msn_login( account_t *acc )
[b7d3cc34]50{
[84b045d]51        struct im_connection *ic = imcb_new( acc );
[b7d3cc34]52        struct msn_data *md = g_new0( struct msn_data, 1 );
53       
[0da65d5]54        ic->proto_data = md;
[e132b60]55        ic->flags |= OPT_PONGS | OPT_PONGED;
[b7d3cc34]56       
[0a3c243]57        if( strchr( acc->user, '@' ) == NULL )
[b7d3cc34]58        {
[84b045d]59                imcb_error( ic, "Invalid account name" );
[c2fb3809]60                imc_logout( ic, FALSE );
[b7d3cc34]61                return;
62        }
63       
[0da65d5]64        md->ic = ic;
[2a29eac]65        md->away_state = msn_away_state_list;
[ca7de3a]66        md->domaintree = g_tree_new( msn_domaintree_cmp );
[27053b5]67        md->ns->fd = -1;
[2a29eac]68       
[bae0617]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 );
[b7d3cc34]73}
74
[0da65d5]75static void msn_logout( struct im_connection *ic )
[b7d3cc34]76{
[0da65d5]77        struct msn_data *md = ic->proto_data;
[b7d3cc34]78        GSList *l;
[80175a1]79        int i;
[b7d3cc34]80       
[5a5c926]81        if( md )
[b7d3cc34]82        {
[17a6ee9]83                /** Disabling MSN ft support for now.
[a2b99ec]84                while( md->filetransfers ) {
[1c3008a]85                        imcb_file_canceled( md->filetransfers->data, "Closing connection" );
[a2b99ec]86                }
[17a6ee9]87                */
[a2b99ec]88               
[bae0617]89                msn_ns_close( md->ns );
[5a5c926]90               
91                while( md->switchboards )
92                        msn_sb_destroy( md->switchboards->data );
[1053836]93               
[46dca11]94                msn_msgq_purge( ic, &md->msgq );
[4e1be76]95                msn_soapq_flush( ic, FALSE );
[5a5c926]96               
[80175a1]97                for( i = 0; i < sizeof( md->tokens ) / sizeof( md->tokens[0] ); i ++ )
98                        g_free( md->tokens[i] );
[ffb6dea]99                g_free( md->lock_key );
[27053b5]100                g_free( md->pp_policy );
[f9258ae]101                g_free( md->uuid );
[59f5c42a]102               
[ff27648]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               
[76c89dc7]112                g_free( md->profile_rid );
113               
[9e9140b]114                if( md->domaintree )
115                        g_tree_destroy( md->domaintree );
[ca7de3a]116                md->domaintree = NULL;
117               
[70ac477]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               
[5a5c926]127                g_free( md );
[b7d3cc34]128        }
129       
[0da65d5]130        for( l = ic->permit; l; l = l->next )
[b7d3cc34]131                g_free( l->data );
[0da65d5]132        g_slist_free( ic->permit );
[b7d3cc34]133       
[0da65d5]134        for( l = ic->deny; l; l = l->next )
[b7d3cc34]135                g_free( l->data );
[0da65d5]136        g_slist_free( ic->deny );
[b7d3cc34]137       
[0da65d5]138        msn_connections = g_slist_remove( msn_connections, ic );
[b7d3cc34]139}
140
[f6c963b]141static int msn_buddy_msg( struct im_connection *ic, char *who, char *message, int away )
[b7d3cc34]142{
[208db4b]143        struct bee_user *bu = bee_user_by_handle( ic->bee, ic, who );
144        struct msn_buddy_data *bd = bu ? bu->data : NULL;
[b7d3cc34]145        struct msn_switchboard *sb;
146       
[70ac477]147#ifdef DEBUG
148        if( strcmp( who, "raw" ) == 0 )
149        {
[64768d4]150                msn_ns_write( ic, -1, "%s\r\n", message );
[70ac477]151        }
152        else
153#endif
[208db4b]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 ) ) )
[b7d3cc34]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               
[a830512]171                return msn_sb_write_msg( ic, m );
[b7d3cc34]172        }
173       
174        return( 0 );
175}
176
[0da65d5]177static GList *msn_away_states( struct im_connection *ic )
[b7d3cc34]178{
[5e202b0]179        static GList *l = NULL;
[b7d3cc34]180        int i;
181       
[5e202b0]182        if( l == NULL )
[b051d39]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 );
[b7d3cc34]186       
[5e202b0]187        return l;
[b7d3cc34]188}
189
[0da65d5]190static void msn_set_away( struct im_connection *ic, char *state, char *message )
[b7d3cc34]191{
[12767e3]192        char *uux;
[0da65d5]193        struct msn_data *md = ic->proto_data;
[b7d3cc34]194       
[daae10f]195        if( state == NULL )
[b051d39]196                md->away_state = msn_away_state_list;
[daae10f]197        else if( ( md->away_state = msn_away_state_by_name( state ) ) == NULL )
198                md->away_state = msn_away_state_list + 1;
[b7d3cc34]199       
[080c43a]200        if( !msn_ns_write( ic, -1, "CHG %d %s %d:%02d\r\n", ++md->trId, md->away_state->code, MSN_CAP1, MSN_CAP2 ) )
[12767e3]201                return;
202       
[080c43a]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 );
[64768d4]223        msn_ns_write( ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen( uux ), uux );
224        g_free( uux );
[b7d3cc34]225}
226
[0da65d5]227static void msn_get_info(struct im_connection *ic, char *who) 
[b7d3cc34]228{
229        /* Just make an URL and let the user fetch the info */
[84b045d]230        imcb_log( ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who );
[b7d3cc34]231}
232
[0da65d5]233static void msn_add_buddy( struct im_connection *ic, char *who, char *group )
[b7d3cc34]234{
[8b01217]235        struct bee_user *bu = bee_user_by_handle( ic->bee, ic, who );
236       
[193dc74]237        msn_buddy_list_add( ic, MSN_BUDDY_FL, who, who, group );
[8b01217]238        if( bu && bu->group )
[193dc74]239                msn_buddy_list_remove( ic, MSN_BUDDY_FL, who, bu->group->name );
[b7d3cc34]240}
241
[0da65d5]242static void msn_remove_buddy( struct im_connection *ic, char *who, char *group )
[b7d3cc34]243{
[193dc74]244        msn_buddy_list_remove( ic, MSN_BUDDY_FL, who, NULL );
[b7d3cc34]245}
246
[f6c963b]247static void msn_chat_msg( struct groupchat *c, char *message, int flags )
[b7d3cc34]248{
[fa29d093]249        struct msn_switchboard *sb = msn_sb_by_chat( c );
[b7d3cc34]250       
251        if( sb )
[0da65d5]252                msn_sb_sendmessage( sb, message );
253        /* FIXME: Error handling (although this can't happen unless something's
254           already severely broken) disappeared here! */
[b7d3cc34]255}
256
[c058ff9]257static void msn_chat_invite( struct groupchat *c, char *who, char *message )
[b7d3cc34]258{
[fa29d093]259        struct msn_switchboard *sb = msn_sb_by_chat( c );
[b7d3cc34]260       
261        if( sb )
[bc676ac]262                msn_sb_write( sb, "CAL %d %s\r\n", ++sb->trId, who );
[b7d3cc34]263}
264
[0da65d5]265static void msn_chat_leave( struct groupchat *c )
[b7d3cc34]266{
[fa29d093]267        struct msn_switchboard *sb = msn_sb_by_chat( c );
[b7d3cc34]268       
269        if( sb )
[bc676ac]270                msn_sb_write( sb, "OUT\r\n" );
[b7d3cc34]271}
272
[0da65d5]273static struct groupchat *msn_chat_with( struct im_connection *ic, char *who )
[b7d3cc34]274{
275        struct msn_switchboard *sb;
[36577aa]276        struct groupchat *c = imcb_chat_new( ic, who );
[b7d3cc34]277       
[0da65d5]278        if( ( sb = msn_sb_by_handle( ic, who ) ) )
[b7d3cc34]279        {
280                debug( "Converting existing switchboard to %s to a groupchat", who );
[fa29d093]281                return msn_sb_to_chat( sb );
[b7d3cc34]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               
[a830512]292                msn_sb_write_msg( ic, m );
293
[36577aa]294                return c;
[b7d3cc34]295        }
296}
297
[0da65d5]298static void msn_keepalive( struct im_connection *ic )
[b7d3cc34]299{
[64768d4]300        msn_ns_write( ic, -1, "PNG\r\n" );
[b7d3cc34]301}
302
[0da65d5]303static void msn_add_permit( struct im_connection *ic, char *who )
[b7d3cc34]304{
[193dc74]305        msn_buddy_list_add( ic, MSN_BUDDY_AL, who, who, NULL );
[b7d3cc34]306}
307
[0da65d5]308static void msn_rem_permit( struct im_connection *ic, char *who )
[b7d3cc34]309{
[27053b5]310        msn_buddy_list_remove( ic, MSN_BUDDY_AL, who, NULL );
[b7d3cc34]311}
312
[0da65d5]313static void msn_add_deny( struct im_connection *ic, char *who )
[b7d3cc34]314{
315        struct msn_switchboard *sb;
316       
[193dc74]317        msn_buddy_list_add( ic, MSN_BUDDY_BL, who, who, NULL );
[b7d3cc34]318       
319        /* If there's still a conversation with this person, close it. */
[0da65d5]320        if( ( sb = msn_sb_by_handle( ic, who ) ) )
[b7d3cc34]321        {
322                msn_sb_destroy( sb );
323        }
324}
325
[0da65d5]326static void msn_rem_deny( struct im_connection *ic, char *who )
[b7d3cc34]327{
[27053b5]328        msn_buddy_list_remove( ic, MSN_BUDDY_BL, who, NULL );
[b7d3cc34]329}
330
[0da65d5]331static int msn_send_typing( struct im_connection *ic, char *who, int typing )
[b7d3cc34]332{
[4255320]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 )
[f6c963b]338                return( msn_buddy_msg( ic, who, TYPING_NOTIFICATION_MESSAGE, 0 ) );
[b7d3cc34]339        else
[4255320]340                return 1;
[b7d3cc34]341}
342
[e3413cc]343static char *set_eval_display_name( set_t *set, char *value )
[911f2eb]344{
345        account_t *acc = set->data;
[0da65d5]346        struct im_connection *ic = acc->ic;
[80175a1]347        struct msn_data *md = ic->proto_data;
[911f2eb]348       
[ed0589c]349        if( md->flags & MSN_EMAIL_UNVERIFIED )
350                imcb_log( ic, "Warning: Your e-mail address is unverified. MSN doesn't allow "
[10685d3]351                              "changing your display name until your e-mail address is verified." );
[ed0589c]352       
[76c89dc7]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       
[e0e1546]358        return msn_ns_set_display_name( ic, value ) ? value : NULL;
[911f2eb]359}
360
[7f34ce2]361static void msn_buddy_data_add( bee_user_t *bu )
362{
[ca7de3a]363        struct msn_data *md = bu->ic->proto_data;
[208db4b]364        struct msn_buddy_data *bd;
365        char *handle;
366       
367        bd = bu->data = g_new0( struct msn_buddy_data, 1 );
[ca7de3a]368        g_tree_insert( md->domaintree, bu->handle, bu );
[208db4b]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        }
[7f34ce2]385}
386
387static void msn_buddy_data_free( bee_user_t *bu )
388{
[ca7de3a]389        struct msn_data *md = bu->ic->proto_data;
[c1d40e7]390        struct msn_buddy_data *bd = bu->data;
[23b29c6]391       
[c1d40e7]392        g_free( bd->cid );
[23b29c6]393        g_free( bd );
394       
395        g_tree_remove( md->domaintree, bu->handle );
[7f34ce2]396}
397
[9c84617]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
[0da65d5]422void msn_initmodule()
[b7d3cc34]423{
[7b23afd]424        struct prpl *ret = g_new0(struct prpl, 1);
[911f2eb]425       
[7b23afd]426        ret->name = "msn";
[76c89dc7]427        ret->mms = 1409;         /* this guess taken from libotr UPGRADING file */
[b7d3cc34]428        ret->login = msn_login;
[0da65d5]429        ret->init = msn_init;
430        ret->logout = msn_logout;
[f6c963b]431        ret->buddy_msg = msn_buddy_msg;
[b7d3cc34]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;
[f6c963b]437        ret->chat_msg = msn_chat_msg;
[b7d3cc34]438        ret->chat_invite = msn_chat_invite;
439        ret->chat_leave = msn_chat_leave;
[0da65d5]440        ret->chat_with = msn_chat_with;
[b7d3cc34]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;
[5b52a48]447        ret->handle_cmp = g_strcasecmp;
[7f34ce2]448        ret->buddy_data_add = msn_buddy_data_add;
449        ret->buddy_data_free = msn_buddy_data_free;
[9c84617]450        ret->buddy_action_list = msn_buddy_action_list;
451        ret->buddy_action = msn_buddy_action;
[7f34ce2]452       
[17a6ee9]453        //ret->transfer_request = msn_ftp_transfer_request;
[b7d3cc34]454
[7b23afd]455        register_protocol(ret);
[b7d3cc34]456}
Note: See TracBrowser for help on using the repository browser.