source: protocols/msn/msn.c @ 9c84617

Last change on this file since 9c84617 was 9c84617, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-12-13T00:42:58Z

Silently adding /CTCP NUDGE support for MSN contacts.

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