source: protocols/msn/msn.c @ 7815a2b

Last change on this file since 7815a2b was e3413cc, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-30T01:30:19Z

Added local_display_name setting for MSN accounts and some hopefully clever
enough handling for it. Should solve problems with MSN servers forgetting/
overriding display names set by the user.

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