source: protocols/msn/msn.c @ 80175a1

Last change on this file since 80175a1 was 80175a1, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-18T19:21:44Z

Fetch the user's profile to see if there's a display name set there. If
there is, the one in the address book should be ignored. No support for
changing the profile yet though.

  • Property mode set to 100644
File size: 10.4 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        {
156                msn_write( ic, message, strlen( message ) );
157                msn_write( ic, "\r\n", 2 );
158        }
159        else
160#endif
[0da65d5]161        if( ( sb = msn_sb_by_handle( ic, who ) ) )
[b7d3cc34]162        {
163                return( msn_sb_sendmessage( sb, message ) );
164        }
165        else
166        {
167                struct msn_message *m;
168               
169                /* Create a message. We have to arrange a usable switchboard, and send the message later. */
170                m = g_new0( struct msn_message, 1 );
171                m->who = g_strdup( who );
172                m->text = g_strdup( message );
173               
[a830512]174                return msn_sb_write_msg( ic, m );
[b7d3cc34]175        }
176       
177        return( 0 );
178}
179
[0da65d5]180static GList *msn_away_states( struct im_connection *ic )
[b7d3cc34]181{
[5e202b0]182        static GList *l = NULL;
[b7d3cc34]183        int i;
184       
[5e202b0]185        if( l == NULL )
[b051d39]186                for( i = 0; *msn_away_state_list[i].code; i ++ )
187                        if( *msn_away_state_list[i].name )
188                                l = g_list_append( l, (void*) msn_away_state_list[i].name );
[b7d3cc34]189       
[5e202b0]190        return l;
[b7d3cc34]191}
192
[0da65d5]193static void msn_set_away( struct im_connection *ic, char *state, char *message )
[b7d3cc34]194{
195        char buf[1024];
[12767e3]196        char *uux;
[0da65d5]197        struct msn_data *md = ic->proto_data;
[b7d3cc34]198       
[daae10f]199        if( state == NULL )
[b051d39]200                md->away_state = msn_away_state_list;
[daae10f]201        else if( ( md->away_state = msn_away_state_by_name( state ) ) == NULL )
202                md->away_state = msn_away_state_list + 1;
[b7d3cc34]203       
[b051d39]204        g_snprintf( buf, sizeof( buf ), "CHG %d %s\r\n", ++md->trId, md->away_state->code );
[12767e3]205        if( !msn_write( ic, buf, strlen( buf ) ) )
206                return;
207       
208        uux = g_markup_printf_escaped( "<Data><PSM>%s</PSM><CurrentMedia></CurrentMedia>"
209                                       "</Data>", message ? message : "" );
210        g_snprintf( buf, sizeof( buf ), "UUX %d %zd\r\n%s", ++md->trId, strlen( uux ), uux );
211        if( !msn_write( ic, buf, strlen( buf ) ) )
212                return;
[b7d3cc34]213}
214
[0da65d5]215static void msn_get_info(struct im_connection *ic, char *who) 
[b7d3cc34]216{
217        /* Just make an URL and let the user fetch the info */
[84b045d]218        imcb_log( ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who );
[b7d3cc34]219}
220
[0da65d5]221static void msn_add_buddy( struct im_connection *ic, char *who, char *group )
[b7d3cc34]222{
[8b01217]223        struct bee_user *bu = bee_user_by_handle( ic->bee, ic, who );
224       
[193dc74]225        msn_buddy_list_add( ic, MSN_BUDDY_FL, who, who, group );
[8b01217]226        if( bu && bu->group )
[193dc74]227                msn_buddy_list_remove( ic, MSN_BUDDY_FL, who, bu->group->name );
[b7d3cc34]228}
229
[0da65d5]230static void msn_remove_buddy( struct im_connection *ic, char *who, char *group )
[b7d3cc34]231{
[193dc74]232        msn_buddy_list_remove( ic, MSN_BUDDY_FL, who, NULL );
[b7d3cc34]233}
234
[f6c963b]235static void msn_chat_msg( struct groupchat *c, char *message, int flags )
[b7d3cc34]236{
[fa29d093]237        struct msn_switchboard *sb = msn_sb_by_chat( c );
[b7d3cc34]238       
239        if( sb )
[0da65d5]240                msn_sb_sendmessage( sb, message );
241        /* FIXME: Error handling (although this can't happen unless something's
242           already severely broken) disappeared here! */
[b7d3cc34]243}
244
[c058ff9]245static void msn_chat_invite( struct groupchat *c, char *who, char *message )
[b7d3cc34]246{
[fa29d093]247        struct msn_switchboard *sb = msn_sb_by_chat( c );
[b7d3cc34]248        char buf[1024];
249       
250        if( sb )
251        {
252                g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who );
253                msn_sb_write( sb, buf, strlen( buf ) );
254        }
255}
256
[0da65d5]257static void msn_chat_leave( struct groupchat *c )
[b7d3cc34]258{
[fa29d093]259        struct msn_switchboard *sb = msn_sb_by_chat( c );
[b7d3cc34]260       
261        if( sb )
262                msn_sb_write( sb, "OUT\r\n", 5 );
263}
264
[0da65d5]265static struct groupchat *msn_chat_with( struct im_connection *ic, char *who )
[b7d3cc34]266{
267        struct msn_switchboard *sb;
[36577aa]268        struct groupchat *c = imcb_chat_new( ic, who );
[b7d3cc34]269       
[0da65d5]270        if( ( sb = msn_sb_by_handle( ic, who ) ) )
[b7d3cc34]271        {
272                debug( "Converting existing switchboard to %s to a groupchat", who );
[fa29d093]273                return msn_sb_to_chat( sb );
[b7d3cc34]274        }
275        else
276        {
277                struct msn_message *m;
278               
279                /* Create a magic message. This is quite hackish, but who cares? :-P */
280                m = g_new0( struct msn_message, 1 );
281                m->who = g_strdup( who );
282                m->text = g_strdup( GROUPCHAT_SWITCHBOARD_MESSAGE );
283               
[a830512]284                msn_sb_write_msg( ic, m );
285
[36577aa]286                return c;
[b7d3cc34]287        }
288}
289
[0da65d5]290static void msn_keepalive( struct im_connection *ic )
[b7d3cc34]291{
[0da65d5]292        msn_write( ic, "PNG\r\n", strlen( "PNG\r\n" ) );
[b7d3cc34]293}
294
[0da65d5]295static void msn_add_permit( struct im_connection *ic, char *who )
[b7d3cc34]296{
[193dc74]297        msn_buddy_list_add( ic, MSN_BUDDY_AL, who, who, NULL );
[b7d3cc34]298}
299
[0da65d5]300static void msn_rem_permit( struct im_connection *ic, char *who )
[b7d3cc34]301{
[193dc74]302        msn_buddy_list_remove( ic, MSN_BUDDY_AL, who, NULL );
[b7d3cc34]303}
304
[0da65d5]305static void msn_add_deny( struct im_connection *ic, char *who )
[b7d3cc34]306{
307        struct msn_switchboard *sb;
308       
[193dc74]309        msn_buddy_list_add( ic, MSN_BUDDY_BL, who, who, NULL );
[b7d3cc34]310       
311        /* If there's still a conversation with this person, close it. */
[0da65d5]312        if( ( sb = msn_sb_by_handle( ic, who ) ) )
[b7d3cc34]313        {
314                msn_sb_destroy( sb );
315        }
316}
317
[0da65d5]318static void msn_rem_deny( struct im_connection *ic, char *who )
[b7d3cc34]319{
[193dc74]320        msn_buddy_list_remove( ic, MSN_BUDDY_BL, who, NULL );
[b7d3cc34]321}
322
[0da65d5]323static int msn_send_typing( struct im_connection *ic, char *who, int typing )
[b7d3cc34]324{
[4255320]325        struct bee_user *bu = bee_user_by_handle( ic->bee, ic, who );
326       
327        if( !( bu->flags & BEE_USER_ONLINE ) )
328                return 0;
329        else if( typing & OPT_TYPING )
[f6c963b]330                return( msn_buddy_msg( ic, who, TYPING_NOTIFICATION_MESSAGE, 0 ) );
[b7d3cc34]331        else
[4255320]332                return 1;
[b7d3cc34]333}
334
[e3413cc]335static char *set_eval_display_name( set_t *set, char *value )
[911f2eb]336{
337        account_t *acc = set->data;
[0da65d5]338        struct im_connection *ic = acc->ic;
[80175a1]339        struct msn_data *md = ic->proto_data;
[911f2eb]340       
341        if( strlen( value ) > 129 )
342        {
[84b045d]343                imcb_log( ic, "Maximum name length exceeded" );
[911f2eb]344                return NULL;
345        }
346       
[80175a1]347        if( md->flags & MSN_GOT_PROFILE_DN )
348                imcb_log( ic, "Warning: Persistent name changes for this account have to be done "
349                              "in the profile. BitlBee doesn't currently support this." );
350       
[4452e69]351        msn_soap_addressbook_set_display_name( ic, value );
[e0e1546]352        return msn_ns_set_display_name( ic, value ) ? value : NULL;
[911f2eb]353}
354
[7f34ce2]355static void msn_buddy_data_add( bee_user_t *bu )
356{
[ca7de3a]357        struct msn_data *md = bu->ic->proto_data;
[7f34ce2]358        bu->data = g_new0( struct msn_buddy_data, 1 );
[ca7de3a]359        g_tree_insert( md->domaintree, bu->handle, bu );
[7f34ce2]360}
361
362static void msn_buddy_data_free( bee_user_t *bu )
363{
[ca7de3a]364        struct msn_data *md = bu->ic->proto_data;
365        g_tree_remove( md->domaintree, bu->handle );
[7f34ce2]366        g_free( bu->data );
367}
368
[0da65d5]369void msn_initmodule()
[b7d3cc34]370{
[7b23afd]371        struct prpl *ret = g_new0(struct prpl, 1);
[911f2eb]372       
[7b23afd]373        ret->name = "msn";
[b7d3cc34]374        ret->login = msn_login;
[0da65d5]375        ret->init = msn_init;
376        ret->logout = msn_logout;
[f6c963b]377        ret->buddy_msg = msn_buddy_msg;
[b7d3cc34]378        ret->away_states = msn_away_states;
379        ret->set_away = msn_set_away;
380        ret->get_info = msn_get_info;
381        ret->add_buddy = msn_add_buddy;
382        ret->remove_buddy = msn_remove_buddy;
[f6c963b]383        ret->chat_msg = msn_chat_msg;
[b7d3cc34]384        ret->chat_invite = msn_chat_invite;
385        ret->chat_leave = msn_chat_leave;
[0da65d5]386        ret->chat_with = msn_chat_with;
[b7d3cc34]387        ret->keepalive = msn_keepalive;
388        ret->add_permit = msn_add_permit;
389        ret->rem_permit = msn_rem_permit;
390        ret->add_deny = msn_add_deny;
391        ret->rem_deny = msn_rem_deny;
392        ret->send_typing = msn_send_typing;
[5b52a48]393        ret->handle_cmp = g_strcasecmp;
[7f34ce2]394        ret->buddy_data_add = msn_buddy_data_add;
395        ret->buddy_data_free = msn_buddy_data_free;
396       
[17a6ee9]397        //ret->transfer_request = msn_ftp_transfer_request;
[b7d3cc34]398
[7b23afd]399        register_protocol(ret);
[b7d3cc34]400}
Note: See TracBrowser for help on using the repository browser.