source: protocols/msn/msn.c @ 64768d4

Last change on this file since 64768d4 was 64768d4, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-09-02T09:15:44Z

Replace msn*write functions with saner versions that accept format strings.
Also preparing for additional temporary NS connections (auth token renewal).

  • Property mode set to 100644
File size: 10.2 KB
RevLine 
[b7d3cc34]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[4e4af1b]4  * Copyright 2002-2010 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;
[b7d3cc34]55        md->fd = -1;
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       
[84b045d]64        imcb_log( ic, "Connecting" );
[2a29eac]65       
[0da65d5]66        md->fd = proxy_connect( "messenger.hotmail.com", 1863, msn_ns_connected, ic );
[b7d3cc34]67        if( md->fd < 0 )
68        {
[84b045d]69                imcb_error( ic, "Could not connect to server" );
[c2fb3809]70                imc_logout( ic, TRUE );
[2a29eac]71                return;
[b7d3cc34]72        }
[2a29eac]73       
[0da65d5]74        md->ic = ic;
[2a29eac]75        md->away_state = msn_away_state_list;
[ca7de3a]76        md->domaintree = g_tree_new( msn_domaintree_cmp );
[2a29eac]77       
[0da65d5]78        msn_connections = g_slist_append( msn_connections, ic );
[b7d3cc34]79}
80
[0da65d5]81static void msn_logout( struct im_connection *ic )
[b7d3cc34]82{
[0da65d5]83        struct msn_data *md = ic->proto_data;
[b7d3cc34]84        GSList *l;
[80175a1]85        int i;
[b7d3cc34]86       
[5a5c926]87        if( md )
[b7d3cc34]88        {
[17a6ee9]89                /** Disabling MSN ft support for now.
[a2b99ec]90                while( md->filetransfers ) {
[1c3008a]91                        imcb_file_canceled( md->filetransfers->data, "Closing connection" );
[a2b99ec]92                }
[17a6ee9]93                */
[a2b99ec]94               
[5a5c926]95                if( md->fd >= 0 )
96                        closesocket( md->fd );
[b7d3cc34]97               
[5a5c926]98                if( md->handler )
[b7d3cc34]99                {
[5a5c926]100                        if( md->handler->rxq ) g_free( md->handler->rxq );
101                        if( md->handler->cmd_text ) g_free( md->handler->cmd_text );
102                        g_free( md->handler );
103                }
104               
105                while( md->switchboards )
106                        msn_sb_destroy( md->switchboards->data );
[1053836]107               
[46dca11]108                msn_msgq_purge( ic, &md->msgq );
[5a5c926]109               
[80175a1]110                for( i = 0; i < sizeof( md->tokens ) / sizeof( md->tokens[0] ); i ++ )
111                        g_free( md->tokens[i] );
[ffb6dea]112                g_free( md->lock_key );
[59f5c42a]113               
[ff27648]114                while( md->groups )
115                {
116                        struct msn_group *mg = md->groups->data;
117                        g_free( mg->id );
118                        g_free( mg->name );
119                        g_free( mg );
120                        md->groups = g_slist_remove( md->groups, mg );
121                }
122               
[ca7de3a]123                g_tree_destroy( md->domaintree );
124                md->domaintree = NULL;
125               
[70ac477]126                while( md->grpq )
127                {
128                        struct msn_groupadd *ga = md->grpq->data;
129                        g_free( ga->group );
130                        g_free( ga->who );
131                        g_free( ga );
132                        md->grpq = g_slist_remove( md->grpq, ga );
133                }
134               
[5a5c926]135                g_free( md );
[b7d3cc34]136        }
137       
[0da65d5]138        for( l = ic->permit; l; l = l->next )
[b7d3cc34]139                g_free( l->data );
[0da65d5]140        g_slist_free( ic->permit );
[b7d3cc34]141       
[0da65d5]142        for( l = ic->deny; l; l = l->next )
[b7d3cc34]143                g_free( l->data );
[0da65d5]144        g_slist_free( ic->deny );
[b7d3cc34]145       
[0da65d5]146        msn_connections = g_slist_remove( msn_connections, ic );
[b7d3cc34]147}
148
[f6c963b]149static int msn_buddy_msg( struct im_connection *ic, char *who, char *message, int away )
[b7d3cc34]150{
151        struct msn_switchboard *sb;
152       
[70ac477]153#ifdef DEBUG
154        if( strcmp( who, "raw" ) == 0 )
155        {
[64768d4]156                msn_ns_write( ic, -1, "%s\r\n", message );
[70ac477]157        }
158        else
159#endif
[0da65d5]160        if( ( sb = msn_sb_by_handle( ic, who ) ) )
[b7d3cc34]161        {
162                return( msn_sb_sendmessage( sb, message ) );
163        }
164        else
165        {
166                struct msn_message *m;
167               
168                /* Create a message. We have to arrange a usable switchboard, and send the message later. */
169                m = g_new0( struct msn_message, 1 );
170                m->who = g_strdup( who );
171                m->text = g_strdup( message );
172               
[a830512]173                return msn_sb_write_msg( ic, m );
[b7d3cc34]174        }
175       
176        return( 0 );
177}
178
[0da65d5]179static GList *msn_away_states( struct im_connection *ic )
[b7d3cc34]180{
[5e202b0]181        static GList *l = NULL;
[b7d3cc34]182        int i;
183       
[5e202b0]184        if( l == NULL )
[b051d39]185                for( i = 0; *msn_away_state_list[i].code; i ++ )
186                        if( *msn_away_state_list[i].name )
187                                l = g_list_append( l, (void*) msn_away_state_list[i].name );
[b7d3cc34]188       
[5e202b0]189        return l;
[b7d3cc34]190}
191
[0da65d5]192static void msn_set_away( struct im_connection *ic, char *state, char *message )
[b7d3cc34]193{
[12767e3]194        char *uux;
[0da65d5]195        struct msn_data *md = ic->proto_data;
[b7d3cc34]196       
[daae10f]197        if( state == NULL )
[b051d39]198                md->away_state = msn_away_state_list;
[daae10f]199        else if( ( md->away_state = msn_away_state_by_name( state ) ) == NULL )
200                md->away_state = msn_away_state_list + 1;
[b7d3cc34]201       
[64768d4]202        if( !msn_ns_write( ic, -1, "CHG %d %s\r\n", ++md->trId, md->away_state->code ) )
[12767e3]203                return;
204       
205        uux = g_markup_printf_escaped( "<Data><PSM>%s</PSM><CurrentMedia></CurrentMedia>"
206                                       "</Data>", message ? message : "" );
[64768d4]207        msn_ns_write( ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen( uux ), uux );
208        g_free( uux );
[b7d3cc34]209}
210
[0da65d5]211static void msn_get_info(struct im_connection *ic, char *who) 
[b7d3cc34]212{
213        /* Just make an URL and let the user fetch the info */
[84b045d]214        imcb_log( ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who );
[b7d3cc34]215}
216
[0da65d5]217static void msn_add_buddy( struct im_connection *ic, char *who, char *group )
[b7d3cc34]218{
[8b01217]219        struct bee_user *bu = bee_user_by_handle( ic->bee, ic, who );
220       
[193dc74]221        msn_buddy_list_add( ic, MSN_BUDDY_FL, who, who, group );
[8b01217]222        if( bu && bu->group )
[193dc74]223                msn_buddy_list_remove( ic, MSN_BUDDY_FL, who, bu->group->name );
[b7d3cc34]224}
225
[0da65d5]226static void msn_remove_buddy( struct im_connection *ic, char *who, char *group )
[b7d3cc34]227{
[193dc74]228        msn_buddy_list_remove( ic, MSN_BUDDY_FL, who, NULL );
[b7d3cc34]229}
230
[f6c963b]231static void msn_chat_msg( struct groupchat *c, char *message, int flags )
[b7d3cc34]232{
[fa29d093]233        struct msn_switchboard *sb = msn_sb_by_chat( c );
[b7d3cc34]234       
235        if( sb )
[0da65d5]236                msn_sb_sendmessage( sb, message );
237        /* FIXME: Error handling (although this can't happen unless something's
238           already severely broken) disappeared here! */
[b7d3cc34]239}
240
[c058ff9]241static void msn_chat_invite( struct groupchat *c, char *who, char *message )
[b7d3cc34]242{
[fa29d093]243        struct msn_switchboard *sb = msn_sb_by_chat( c );
[b7d3cc34]244        char buf[1024];
245       
246        if( sb )
247        {
248                g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who );
249                msn_sb_write( sb, buf, strlen( buf ) );
250        }
251}
252
[0da65d5]253static void msn_chat_leave( struct groupchat *c )
[b7d3cc34]254{
[fa29d093]255        struct msn_switchboard *sb = msn_sb_by_chat( c );
[b7d3cc34]256       
257        if( sb )
258                msn_sb_write( sb, "OUT\r\n", 5 );
259}
260
[0da65d5]261static struct groupchat *msn_chat_with( struct im_connection *ic, char *who )
[b7d3cc34]262{
263        struct msn_switchboard *sb;
[36577aa]264        struct groupchat *c = imcb_chat_new( ic, who );
[b7d3cc34]265       
[0da65d5]266        if( ( sb = msn_sb_by_handle( ic, who ) ) )
[b7d3cc34]267        {
268                debug( "Converting existing switchboard to %s to a groupchat", who );
[fa29d093]269                return msn_sb_to_chat( sb );
[b7d3cc34]270        }
271        else
272        {
273                struct msn_message *m;
274               
275                /* Create a magic message. This is quite hackish, but who cares? :-P */
276                m = g_new0( struct msn_message, 1 );
277                m->who = g_strdup( who );
278                m->text = g_strdup( GROUPCHAT_SWITCHBOARD_MESSAGE );
279               
[a830512]280                msn_sb_write_msg( ic, m );
281
[36577aa]282                return c;
[b7d3cc34]283        }
284}
285
[0da65d5]286static void msn_keepalive( struct im_connection *ic )
[b7d3cc34]287{
[64768d4]288        msn_ns_write( ic, -1, "PNG\r\n" );
[b7d3cc34]289}
290
[0da65d5]291static void msn_add_permit( struct im_connection *ic, char *who )
[b7d3cc34]292{
[193dc74]293        msn_buddy_list_add( ic, MSN_BUDDY_AL, who, who, NULL );
[b7d3cc34]294}
295
[0da65d5]296static void msn_rem_permit( struct im_connection *ic, char *who )
[b7d3cc34]297{
[193dc74]298        msn_buddy_list_remove( ic, MSN_BUDDY_AL, who, NULL );
[b7d3cc34]299}
300
[0da65d5]301static void msn_add_deny( struct im_connection *ic, char *who )
[b7d3cc34]302{
303        struct msn_switchboard *sb;
304       
[193dc74]305        msn_buddy_list_add( ic, MSN_BUDDY_BL, who, who, NULL );
[b7d3cc34]306       
307        /* If there's still a conversation with this person, close it. */
[0da65d5]308        if( ( sb = msn_sb_by_handle( ic, who ) ) )
[b7d3cc34]309        {
310                msn_sb_destroy( sb );
311        }
312}
313
[0da65d5]314static void msn_rem_deny( struct im_connection *ic, char *who )
[b7d3cc34]315{
[193dc74]316        msn_buddy_list_remove( ic, MSN_BUDDY_BL, who, NULL );
[b7d3cc34]317}
318
[0da65d5]319static int msn_send_typing( struct im_connection *ic, char *who, int typing )
[b7d3cc34]320{
[4255320]321        struct bee_user *bu = bee_user_by_handle( ic->bee, ic, who );
322       
323        if( !( bu->flags & BEE_USER_ONLINE ) )
324                return 0;
325        else if( typing & OPT_TYPING )
[f6c963b]326                return( msn_buddy_msg( ic, who, TYPING_NOTIFICATION_MESSAGE, 0 ) );
[b7d3cc34]327        else
[4255320]328                return 1;
[b7d3cc34]329}
330
[e3413cc]331static char *set_eval_display_name( set_t *set, char *value )
[911f2eb]332{
333        account_t *acc = set->data;
[0da65d5]334        struct im_connection *ic = acc->ic;
[80175a1]335        struct msn_data *md = ic->proto_data;
[911f2eb]336       
337        if( strlen( value ) > 129 )
338        {
[84b045d]339                imcb_log( ic, "Maximum name length exceeded" );
[911f2eb]340                return NULL;
341        }
342       
[80175a1]343        if( md->flags & MSN_GOT_PROFILE_DN )
344                imcb_log( ic, "Warning: Persistent name changes for this account have to be done "
345                              "in the profile. BitlBee doesn't currently support this." );
346       
[4452e69]347        msn_soap_addressbook_set_display_name( ic, value );
[e0e1546]348        return msn_ns_set_display_name( ic, value ) ? value : NULL;
[911f2eb]349}
350
[7f34ce2]351static void msn_buddy_data_add( bee_user_t *bu )
352{
[ca7de3a]353        struct msn_data *md = bu->ic->proto_data;
[7f34ce2]354        bu->data = g_new0( struct msn_buddy_data, 1 );
[ca7de3a]355        g_tree_insert( md->domaintree, bu->handle, bu );
[7f34ce2]356}
357
358static void msn_buddy_data_free( bee_user_t *bu )
359{
[ca7de3a]360        struct msn_data *md = bu->ic->proto_data;
361        g_tree_remove( md->domaintree, bu->handle );
[7f34ce2]362        g_free( bu->data );
363}
364
[0da65d5]365void msn_initmodule()
[b7d3cc34]366{
[7b23afd]367        struct prpl *ret = g_new0(struct prpl, 1);
[911f2eb]368       
[7b23afd]369        ret->name = "msn";
[b7d3cc34]370        ret->login = msn_login;
[0da65d5]371        ret->init = msn_init;
372        ret->logout = msn_logout;
[f6c963b]373        ret->buddy_msg = msn_buddy_msg;
[b7d3cc34]374        ret->away_states = msn_away_states;
375        ret->set_away = msn_set_away;
376        ret->get_info = msn_get_info;
377        ret->add_buddy = msn_add_buddy;
378        ret->remove_buddy = msn_remove_buddy;
[f6c963b]379        ret->chat_msg = msn_chat_msg;
[b7d3cc34]380        ret->chat_invite = msn_chat_invite;
381        ret->chat_leave = msn_chat_leave;
[0da65d5]382        ret->chat_with = msn_chat_with;
[b7d3cc34]383        ret->keepalive = msn_keepalive;
384        ret->add_permit = msn_add_permit;
385        ret->rem_permit = msn_rem_permit;
386        ret->add_deny = msn_add_deny;
387        ret->rem_deny = msn_rem_deny;
388        ret->send_typing = msn_send_typing;
[5b52a48]389        ret->handle_cmp = g_strcasecmp;
[7f34ce2]390        ret->buddy_data_add = msn_buddy_data_add;
391        ret->buddy_data_free = msn_buddy_data_free;
392       
[17a6ee9]393        //ret->transfer_request = msn_ftp_transfer_request;
[b7d3cc34]394
[7b23afd]395        register_protocol(ret);
[b7d3cc34]396}
Note: See TracBrowser for help on using the repository browser.