source: protocols/msn/msn.c @ 0e788f5

Last change on this file since 0e788f5 was 0e788f5, checked in by Wilmer van der Gaast <wilmer@…>, at 2013-02-21T19:15:59Z

I'm still bored on a long flight. Wrote a script to automatically update
my copyright mentions since some were getting pretty stale. Left files not
touched since before 2012 alone so that this change doesn't touch almost
EVERY source file.

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