source: protocols/msn/msn_util.c @ fb98634

Last change on this file since fb98634 was 9143aeb, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-04-05T12:26:04Z

query.h now defines a callback function type, not using void* for it anymore.
Got rid of the bogus window handler pointer as the first argument to the
callback.

  • Property mode set to 100644
File size: 8.9 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 - Miscellaneous utilities                                 */
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#include <ctype.h>
29
30int msn_write( struct im_connection *ic, char *s, int len )
31{
32        struct msn_data *md = ic->proto_data;
33        int st;
34       
35        st = write( md->fd, s, len );
36        if( st != len )
37        {
38                imcb_error( ic, "Short write() to main server" );
39                imc_logout( ic, TRUE );
40                return( 0 );
41        }
42       
43        return( 1 );
44}
45
46int msn_logged_in( struct im_connection *ic )
47{
48        imcb_connected( ic );
49       
50        return( 0 );
51}
52
53int msn_buddy_list_add( struct im_connection *ic, char *list, char *who, char *realname_ )
54{
55        struct msn_data *md = ic->proto_data;
56        char buf[1024], *realname;
57       
58        realname = msn_http_encode( realname_ );
59       
60        g_snprintf( buf, sizeof( buf ), "ADD %d %s %s %s\r\n", ++md->trId, list, who, realname );
61        if( msn_write( ic, buf, strlen( buf ) ) )
62        {
63                g_free( realname );
64               
65                return( 1 );
66        }
67       
68        g_free( realname );
69       
70        return( 0 );
71}
72
73int msn_buddy_list_remove( struct im_connection *ic, char *list, char *who )
74{
75        struct msn_data *md = ic->proto_data;
76        char buf[1024];
77       
78        g_snprintf( buf, sizeof( buf ), "REM %d %s %s\r\n", ++md->trId, list, who );
79        if( msn_write( ic, buf, strlen( buf ) ) )
80                return( 1 );
81       
82        return( 0 );
83}
84
85struct msn_buddy_ask_data
86{
87        struct im_connection *ic;
88        char *handle;
89        char *realname;
90};
91
92static void msn_buddy_ask_yes( void *data )
93{
94        struct msn_buddy_ask_data *bla = data;
95       
96        msn_buddy_list_add( bla->ic, "AL", bla->handle, bla->realname );
97       
98        if( imcb_find_buddy( bla->ic, bla->handle ) == NULL )
99                imcb_ask_add( bla->ic, bla->handle, NULL );
100       
101        g_free( bla->handle );
102        g_free( bla->realname );
103        g_free( bla );
104}
105
106static void msn_buddy_ask_no( void *data )
107{
108        struct msn_buddy_ask_data *bla = data;
109       
110        msn_buddy_list_add( bla->ic, "BL", bla->handle, bla->realname );
111       
112        g_free( bla->handle );
113        g_free( bla->realname );
114        g_free( bla );
115}
116
117void msn_buddy_ask( struct im_connection *ic, char *handle, char *realname )
118{
119        struct msn_buddy_ask_data *bla = g_new0( struct msn_buddy_ask_data, 1 );
120        char buf[1024];
121       
122        bla->ic = ic;
123        bla->handle = g_strdup( handle );
124        bla->realname = g_strdup( realname );
125       
126        g_snprintf( buf, sizeof( buf ),
127                    "The user %s (%s) wants to add you to his/her buddy list.",
128                    handle, realname );
129        imcb_ask( ic, buf, bla, msn_buddy_ask_yes, msn_buddy_ask_no );
130}
131
132char *msn_findheader( char *text, char *header, int len )
133{
134        int hlen = strlen( header ), i;
135        char *ret;
136       
137        if( len == 0 )
138                len = strlen( text );
139       
140        i = 0;
141        while( ( i + hlen ) < len )
142        {
143                /* Maybe this is a bit over-commented, but I just hate this part... */
144                if( g_strncasecmp( text + i, header, hlen ) == 0 )
145                {
146                        /* Skip to the (probable) end of the header */
147                        i += hlen;
148                       
149                        /* Find the first non-[: \t] character */
150                        while( i < len && ( text[i] == ':' || text[i] == ' ' || text[i] == '\t' ) ) i ++;
151                       
152                        /* Make sure we're still inside the string */
153                        if( i >= len ) return( NULL );
154                       
155                        /* Save the position */
156                        ret = text + i;
157                       
158                        /* Search for the end of this line */
159                        while( i < len && text[i] != '\r' && text[i] != '\n' ) i ++;
160                       
161                        /* Make sure we're still inside the string */
162                        if( i >= len ) return( NULL );
163                       
164                        /* Copy the found data */
165                        return( g_strndup( ret, text + i - ret ) );
166                }
167               
168                /* This wasn't the header we were looking for, skip to the next line. */
169                while( i < len && ( text[i] != '\r' && text[i] != '\n' ) ) i ++;
170                while( i < len && ( text[i] == '\r' || text[i] == '\n' ) ) i ++;
171               
172                /* End of headers? */
173                if( strncmp( text + i - 2, "\n\n", 2 ) == 0 ||
174                    strncmp( text + i - 4, "\r\n\r\n", 4 ) == 0 ||
175                    strncmp( text + i - 2, "\r\r", 2 ) == 0 )
176                {
177                        break;
178                }
179        }
180       
181        return( NULL );
182}
183
184/* *NOT* thread-safe, but that's not a problem for now... */
185char **msn_linesplit( char *line )
186{
187        static char **ret = NULL;
188        static int size = 3;
189        int i, n = 0;
190       
191        if( ret == NULL )
192                ret = g_new0( char*, size );
193       
194        for( i = 0; line[i] && line[i] == ' '; i ++ );
195        if( line[i] )
196        {
197                ret[n++] = line + i;
198                for( i ++; line[i]; i ++ )
199                {
200                        if( line[i] == ' ' )
201                                line[i] = 0;
202                        else if( line[i] != ' ' && !line[i-1] )
203                                ret[n++] = line + i;
204                       
205                        if( n >= size )
206                                ret = g_renew( char*, ret, size += 2 );
207                }
208        }
209        ret[n] = NULL;
210       
211        return( ret );
212}
213
214/* This one handles input from a MSN Messenger server. Both the NS and SB servers usually give
215   commands, but sometimes they give additional data (payload). This function tries to handle
216   this all in a nice way and send all data to the right places. */
217
218/* Return values: -1: Read error, abort connection.
219                   0: Command reported error; Abort *immediately*. (The connection does not exist anymore)
220                   1: OK */
221
222int msn_handler( struct msn_handler_data *h )
223{
224        int st;
225       
226        h->rxq = g_renew( char, h->rxq, h->rxlen + 1024 );
227        st = read( h->fd, h->rxq + h->rxlen, 1024 );
228        h->rxlen += st;
229       
230        if( st <= 0 )
231                return( -1 );
232       
233        while( st )
234        {
235                int i;
236               
237                if( h->msglen == 0 )
238                {
239                        for( i = 0; i < h->rxlen; i ++ )
240                        {
241                                if( h->rxq[i] == '\r' || h->rxq[i] == '\n' )
242                                {
243                                        char *cmd_text, **cmd;
244                                        int count;
245                                       
246                                        cmd_text = g_strndup( h->rxq, i );
247                                        cmd = msn_linesplit( cmd_text );
248                                        for( count = 0; cmd[count]; count ++ );
249                                        st = h->exec_command( h->data, cmd, count );
250                                        g_free( cmd_text );
251                                       
252                                        /* If the connection broke, don't continue. We don't even exist anymore. */
253                                        if( !st )
254                                                return( 0 );
255                                       
256                                        if( h->msglen )
257                                                h->cmd_text = g_strndup( h->rxq, i );
258                                       
259                                        /* Skip to the next non-emptyline */
260                                        while( i < h->rxlen && ( h->rxq[i] == '\r' || h->rxq[i] == '\n' ) ) i ++;
261                                       
262                                        break;
263                                }
264                        }
265                       
266                        /* If we reached the end of the buffer, there's still an incomplete command there.
267                           Return and wait for more data. */
268                        if( i == h->rxlen && h->rxq[i-1] != '\r' && h->rxq[i-1] != '\n' )
269                                break;
270                }
271                else
272                {
273                        char *msg, **cmd;
274                        int count;
275                       
276                        /* Do we have the complete message already? */
277                        if( h->msglen > h->rxlen )
278                                break;
279                       
280                        msg = g_strndup( h->rxq, h->msglen );
281                        cmd = msn_linesplit( h->cmd_text );
282                        for( count = 0; cmd[count]; count ++ );
283                       
284                        st = h->exec_message( h->data, msg, h->msglen, cmd, count );
285                        g_free( msg );
286                        g_free( h->cmd_text );
287                        h->cmd_text = NULL;
288                       
289                        if( !st )
290                                return( 0 );
291                       
292                        i = h->msglen;
293                        h->msglen = 0;
294                }
295               
296                /* More data after this block? */
297                if( i < h->rxlen )
298                {
299                        char *tmp;
300                       
301                        tmp = g_memdup( h->rxq + i, h->rxlen - i );
302                        g_free( h->rxq );
303                        h->rxq = tmp;
304                        h->rxlen -= i;
305                        i = 0;
306                }
307                else
308                /* If not, reset the rx queue and get lost. */
309                {
310                        g_free( h->rxq );
311                        h->rxq = g_new0( char, 1 );
312                        h->rxlen = 0;
313                        return( 1 );
314                }
315        }
316       
317        return( 1 );
318}
319
320/* The difference between this function and the normal http_encode() function
321   is that this one escapes every 7-bit ASCII character because this is said
322   to avoid some lame server-side checks when setting a real-name. Also,
323   non-ASCII characters are not escaped because MSN servers don't seem to
324   appreciate that! */
325char *msn_http_encode( const char *input )
326{
327        char *ret, *s;
328        int i;
329       
330        ret = s = g_new0( char, strlen( input ) * 3 + 1 );
331        for( i = 0; input[i]; i ++ )
332                if( input[i] & 128 )
333                {
334                        *s = input[i];
335                        s ++;
336                }
337                else
338                {
339                        g_snprintf( s, 4, "%%%02X", input[i] );
340                        s += 3;
341                }
342       
343        return ret;
344}
345
346void msn_msgq_purge( struct im_connection *ic, GSList **list )
347{
348        struct msn_message *m;
349        GString *ret;
350        GSList *l;
351       
352        l = *list;
353        if( l == NULL )
354                return;
355       
356        m = l->data;
357        ret = g_string_sized_new( 1024 );
358        g_string_printf( ret, "Warning: Cleaning up MSN (switchboard) connection with unsent "
359                              "messages to %s:", m->who ? m->who : "unknown recipient" );
360       
361        while( l )
362        {
363                m = l->data;
364               
365                g_string_append_printf( ret, "\n%s", m->text );
366               
367                g_free( m->who );
368                g_free( m->text );
369                g_free( m );
370               
371                l = l->next;
372        }
373        g_slist_free( *list );
374        *list = NULL;
375       
376        imcb_log( ic, ret->str );
377        g_string_free( ret, TRUE );
378}
Note: See TracBrowser for help on using the repository browser.