source: protocols/msn/msn.c @ b051d39

Last change on this file since b051d39 was b051d39, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-06T15:44:46Z

MSN module updated to deal with the new API. Not many changes since it
doesn't currently support away messages anyway. This is more a bit of a
cleanup.

  • Property mode set to 100644
File size: 8.4 KB
RevLine 
[b7d3cc34]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2004 Wilmer van der Gaast and others                *
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"
27#include "msn.h"
28
[911f2eb]29static char *msn_set_display_name( set_t *set, char *value );
30
[0da65d5]31static void msn_init( account_t *acc )
[911f2eb]32{
33        set_t *s;
34       
35        s = set_add( &acc->set, "display_name", NULL, msn_set_display_name, acc );
36        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[1febf5c]37
38        s = set_add( &acc->set, "mail_notifications", "false", set_eval_bool, acc );
[911f2eb]39}
40
[0a3c243]41static void msn_login( account_t *acc )
[b7d3cc34]42{
[84b045d]43        struct im_connection *ic = imcb_new( acc );
[b7d3cc34]44        struct msn_data *md = g_new0( struct msn_data, 1 );
45       
[0da65d5]46        ic->proto_data = md;
[b7d3cc34]47        md->fd = -1;
48       
[0a3c243]49        if( strchr( acc->user, '@' ) == NULL )
[b7d3cc34]50        {
[84b045d]51                imcb_error( ic, "Invalid account name" );
[c2fb3809]52                imc_logout( ic, FALSE );
[b7d3cc34]53                return;
54        }
55       
[84b045d]56        imcb_log( ic, "Connecting" );
[2a29eac]57       
[0da65d5]58        md->fd = proxy_connect( "messenger.hotmail.com", 1863, msn_ns_connected, ic );
[b7d3cc34]59        if( md->fd < 0 )
60        {
[84b045d]61                imcb_error( ic, "Could not connect to server" );
[c2fb3809]62                imc_logout( ic, TRUE );
[2a29eac]63                return;
[b7d3cc34]64        }
[2a29eac]65       
[0da65d5]66        md->ic = ic;
[2a29eac]67        md->away_state = msn_away_state_list;
68       
[0da65d5]69        msn_connections = g_slist_append( msn_connections, ic );
[b7d3cc34]70}
71
[0da65d5]72static void msn_logout( struct im_connection *ic )
[b7d3cc34]73{
[0da65d5]74        struct msn_data *md = ic->proto_data;
[b7d3cc34]75        GSList *l;
76       
[5a5c926]77        if( md )
[b7d3cc34]78        {
[5a5c926]79                if( md->fd >= 0 )
80                        closesocket( md->fd );
[b7d3cc34]81               
[5a5c926]82                if( md->handler )
[b7d3cc34]83                {
[5a5c926]84                        if( md->handler->rxq ) g_free( md->handler->rxq );
85                        if( md->handler->cmd_text ) g_free( md->handler->cmd_text );
86                        g_free( md->handler );
87                }
88               
89                while( md->switchboards )
90                        msn_sb_destroy( md->switchboards->data );
[1053836]91               
[46dca11]92                msn_msgq_purge( ic, &md->msgq );
[5a5c926]93               
[bf25fa3]94                while( md->groupcount > 0 )
95                        g_free( md->grouplist[--md->groupcount] );
[59f5c42a]96                g_free( md->grouplist );
97               
[5a5c926]98                g_free( md );
[b7d3cc34]99        }
100       
[0da65d5]101        for( l = ic->permit; l; l = l->next )
[b7d3cc34]102                g_free( l->data );
[0da65d5]103        g_slist_free( ic->permit );
[b7d3cc34]104       
[0da65d5]105        for( l = ic->deny; l; l = l->next )
[b7d3cc34]106                g_free( l->data );
[0da65d5]107        g_slist_free( ic->deny );
[b7d3cc34]108       
[0da65d5]109        msn_connections = g_slist_remove( msn_connections, ic );
[b7d3cc34]110}
111
[f6c963b]112static int msn_buddy_msg( struct im_connection *ic, char *who, char *message, int away )
[b7d3cc34]113{
114        struct msn_switchboard *sb;
115       
[0da65d5]116        if( ( sb = msn_sb_by_handle( ic, who ) ) )
[b7d3cc34]117        {
118                return( msn_sb_sendmessage( sb, message ) );
119        }
120        else
121        {
122                struct msn_message *m;
123               
124                /* Create a message. We have to arrange a usable switchboard, and send the message later. */
125                m = g_new0( struct msn_message, 1 );
126                m->who = g_strdup( who );
127                m->text = g_strdup( message );
128               
[a830512]129                return msn_sb_write_msg( ic, m );
[b7d3cc34]130        }
131       
132        return( 0 );
133}
134
[0da65d5]135static GList *msn_away_states( struct im_connection *ic )
[b7d3cc34]136{
[5e202b0]137        static GList *l = NULL;
[b7d3cc34]138        int i;
139       
[5e202b0]140        if( l == NULL )
[b051d39]141                for( i = 0; *msn_away_state_list[i].code; i ++ )
142                        if( *msn_away_state_list[i].name )
143                                l = g_list_append( l, (void*) msn_away_state_list[i].name );
[b7d3cc34]144       
[5e202b0]145        return l;
[b7d3cc34]146}
147
[0da65d5]148static void msn_set_away( struct im_connection *ic, char *state, char *message )
[b7d3cc34]149{
150        char buf[1024];
[0da65d5]151        struct msn_data *md = ic->proto_data;
[b7d3cc34]152       
[b051d39]153        if( state )
154                md->away_state = msn_away_state_by_name( state ) ? :
155                                 msn_away_state_list + 1;
[b7d3cc34]156        else
[b051d39]157                md->away_state = msn_away_state_list;
[b7d3cc34]158       
[b051d39]159        g_snprintf( buf, sizeof( buf ), "CHG %d %s\r\n", ++md->trId, md->away_state->code );
[0da65d5]160        msn_write( ic, buf, strlen( buf ) );
[b7d3cc34]161}
162
[0da65d5]163static void msn_set_my_name( struct im_connection *ic, char *info )
[b7d3cc34]164{
[0da65d5]165        msn_set_display_name( set_find( &ic->acc->set, "display_name" ), info );
[b7d3cc34]166}
167
[0da65d5]168static void msn_get_info(struct im_connection *ic, char *who) 
[b7d3cc34]169{
170        /* Just make an URL and let the user fetch the info */
[84b045d]171        imcb_log( ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who );
[b7d3cc34]172}
173
[0da65d5]174static void msn_add_buddy( struct im_connection *ic, char *who, char *group )
[b7d3cc34]175{
[0da65d5]176        msn_buddy_list_add( ic, "FL", who, who );
[b7d3cc34]177}
178
[0da65d5]179static void msn_remove_buddy( struct im_connection *ic, char *who, char *group )
[b7d3cc34]180{
[0da65d5]181        msn_buddy_list_remove( ic, "FL", who );
[b7d3cc34]182}
183
[f6c963b]184static void msn_chat_msg( struct groupchat *c, char *message, int flags )
[b7d3cc34]185{
[fa29d093]186        struct msn_switchboard *sb = msn_sb_by_chat( c );
[b7d3cc34]187       
188        if( sb )
[0da65d5]189                msn_sb_sendmessage( sb, message );
190        /* FIXME: Error handling (although this can't happen unless something's
191           already severely broken) disappeared here! */
[b7d3cc34]192}
193
[c058ff9]194static void msn_chat_invite( struct groupchat *c, char *who, char *message )
[b7d3cc34]195{
[fa29d093]196        struct msn_switchboard *sb = msn_sb_by_chat( c );
[b7d3cc34]197        char buf[1024];
198       
199        if( sb )
200        {
201                g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who );
202                msn_sb_write( sb, buf, strlen( buf ) );
203        }
204}
205
[0da65d5]206static void msn_chat_leave( struct groupchat *c )
[b7d3cc34]207{
[fa29d093]208        struct msn_switchboard *sb = msn_sb_by_chat( c );
[b7d3cc34]209       
210        if( sb )
211                msn_sb_write( sb, "OUT\r\n", 5 );
212}
213
[0da65d5]214static struct groupchat *msn_chat_with( struct im_connection *ic, char *who )
[b7d3cc34]215{
216        struct msn_switchboard *sb;
217       
[0da65d5]218        if( ( sb = msn_sb_by_handle( ic, who ) ) )
[b7d3cc34]219        {
220                debug( "Converting existing switchboard to %s to a groupchat", who );
[fa29d093]221                return msn_sb_to_chat( sb );
[b7d3cc34]222        }
223        else
224        {
225                struct msn_message *m;
226               
227                /* Create a magic message. This is quite hackish, but who cares? :-P */
228                m = g_new0( struct msn_message, 1 );
229                m->who = g_strdup( who );
230                m->text = g_strdup( GROUPCHAT_SWITCHBOARD_MESSAGE );
231               
[a830512]232                msn_sb_write_msg( ic, m );
233
[fa29d093]234                return NULL;
[b7d3cc34]235        }
236       
[fa29d093]237        return NULL;
[b7d3cc34]238}
239
[0da65d5]240static void msn_keepalive( struct im_connection *ic )
[b7d3cc34]241{
[0da65d5]242        msn_write( ic, "PNG\r\n", strlen( "PNG\r\n" ) );
[b7d3cc34]243}
244
[0da65d5]245static void msn_add_permit( struct im_connection *ic, char *who )
[b7d3cc34]246{
[0da65d5]247        msn_buddy_list_add( ic, "AL", who, who );
[b7d3cc34]248}
249
[0da65d5]250static void msn_rem_permit( struct im_connection *ic, char *who )
[b7d3cc34]251{
[0da65d5]252        msn_buddy_list_remove( ic, "AL", who );
[b7d3cc34]253}
254
[0da65d5]255static void msn_add_deny( struct im_connection *ic, char *who )
[b7d3cc34]256{
257        struct msn_switchboard *sb;
258       
[0da65d5]259        msn_buddy_list_add( ic, "BL", who, who );
[b7d3cc34]260       
261        /* If there's still a conversation with this person, close it. */
[0da65d5]262        if( ( sb = msn_sb_by_handle( ic, who ) ) )
[b7d3cc34]263        {
264                msn_sb_destroy( sb );
265        }
266}
267
[0da65d5]268static void msn_rem_deny( struct im_connection *ic, char *who )
[b7d3cc34]269{
[0da65d5]270        msn_buddy_list_remove( ic, "BL", who );
[b7d3cc34]271}
272
[0da65d5]273static int msn_send_typing( struct im_connection *ic, char *who, int typing )
[b7d3cc34]274{
[df1fb67]275        if( typing & OPT_TYPING )
[f6c963b]276                return( msn_buddy_msg( ic, who, TYPING_NOTIFICATION_MESSAGE, 0 ) );
[b7d3cc34]277        else
278                return( 1 );
279}
280
[911f2eb]281static char *msn_set_display_name( set_t *set, char *value )
282{
283        account_t *acc = set->data;
[0da65d5]284        struct im_connection *ic = acc->ic;
[911f2eb]285        struct msn_data *md;
[e97827b]286        char buf[1024], *fn;
[911f2eb]287       
288        /* Double-check. */
[0da65d5]289        if( ic == NULL )
[911f2eb]290                return NULL;
291       
[0da65d5]292        md = ic->proto_data;
[911f2eb]293       
294        if( strlen( value ) > 129 )
295        {
[84b045d]296                imcb_log( ic, "Maximum name length exceeded" );
[911f2eb]297                return NULL;
298        }
299       
[e97827b]300        fn = msn_http_encode( value );
[911f2eb]301       
[c2fb3809]302        g_snprintf( buf, sizeof( buf ), "REA %d %s %s\r\n", ++md->trId, ic->acc->user, fn );
[0da65d5]303        msn_write( ic, buf, strlen( buf ) );
[911f2eb]304        g_free( fn );
305       
306        /* Returning NULL would be better, because the server still has to
307           confirm the name change. However, it looks a bit confusing to the
308           user. */
309        return value;
310}
311
[0da65d5]312void msn_initmodule()
[b7d3cc34]313{
[7b23afd]314        struct prpl *ret = g_new0(struct prpl, 1);
[911f2eb]315       
[7b23afd]316        ret->name = "msn";
[b7d3cc34]317        ret->login = msn_login;
[0da65d5]318        ret->init = msn_init;
319        ret->logout = msn_logout;
[f6c963b]320        ret->buddy_msg = msn_buddy_msg;
[b7d3cc34]321        ret->away_states = msn_away_states;
322        ret->set_away = msn_set_away;
323        ret->get_info = msn_get_info;
[0da65d5]324        ret->set_my_name = msn_set_my_name;
[b7d3cc34]325        ret->add_buddy = msn_add_buddy;
326        ret->remove_buddy = msn_remove_buddy;
[f6c963b]327        ret->chat_msg = msn_chat_msg;
[b7d3cc34]328        ret->chat_invite = msn_chat_invite;
329        ret->chat_leave = msn_chat_leave;
[0da65d5]330        ret->chat_with = msn_chat_with;
[b7d3cc34]331        ret->keepalive = msn_keepalive;
332        ret->add_permit = msn_add_permit;
333        ret->rem_permit = msn_rem_permit;
334        ret->add_deny = msn_add_deny;
335        ret->rem_deny = msn_rem_deny;
336        ret->send_typing = msn_send_typing;
[5b52a48]337        ret->handle_cmp = g_strcasecmp;
[b7d3cc34]338
[7b23afd]339        register_protocol(ret);
[b7d3cc34]340}
Note: See TracBrowser for help on using the repository browser.