source: protocols/msn/msn_util.c @ e5854a8

Last change on this file since e5854a8 was e5854a8, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-14T14:50:10Z

Show incoming auth. requests (although responding to them currently causes
a disconnect).

  • Property mode set to 100644
File size: 12.3 KB
RevLine 
[b7d3cc34]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[21029d0]4  * Copyright 2002-2010 Wilmer van der Gaast and others                *
[b7d3cc34]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"
[21029d0]28#include "md5.h"
[b7d3cc34]29#include <ctype.h>
30
[0da65d5]31int msn_write( struct im_connection *ic, char *s, int len )
[b7d3cc34]32{
[0da65d5]33        struct msn_data *md = ic->proto_data;
[b7d3cc34]34        int st;
35       
[523fb23]36        if( getenv( "BITLBEE_DEBUG" ) )
37        {
38                write( 2, "->NS:", 5 );
39                write( 2, s, len );
40        }
41       
[b7d3cc34]42        st = write( md->fd, s, len );
43        if( st != len )
44        {
[84b045d]45                imcb_error( ic, "Short write() to main server" );
[c2fb3809]46                imc_logout( ic, TRUE );
[e3413cc]47                return 0;
[b7d3cc34]48        }
49       
[e3413cc]50        return 1;
[b7d3cc34]51}
52
[0da65d5]53int msn_logged_in( struct im_connection *ic )
[b7d3cc34]54{
[84b045d]55        imcb_connected( ic );
[b7d3cc34]56       
57        return( 0 );
58}
59
[6acc033]60int msn_buddy_list_add( struct im_connection *ic, const char *list, const char *who, const char *realname_, const char *group )
[b7d3cc34]61{
[0da65d5]62        struct msn_data *md = ic->proto_data;
[4452e69]63        char buf[1024], realname[strlen(realname_)*3+1], groupid[8];
[b7d3cc34]64       
[6acc033]65        *groupid = '\0';
66        if( group )
67        {
68                int i;
69                for( i = 0; i < md->groupcount; i ++ )
70                        if( g_strcasecmp( md->grouplist[i], group ) == 0 )
71                        {
72                                g_snprintf( groupid, sizeof( groupid ), " %d", i );
73                                break;
74                        }
[b7d3cc34]75               
[70ac477]76                if( *groupid == '\0' )
77                {
78                        /* Have to create this group, it doesn't exist yet. */
79                        struct msn_groupadd *ga;
80                        GSList *l;
81                       
82                        for( l = md->grpq; l; l = l->next )
83                        {
84                                ga = l->data;
85                                if( g_strcasecmp( ga->group, group ) == 0 )
86                                        break;
87                        }
88                       
89                        ga = g_new0( struct msn_groupadd, 1 );
90                        ga->who = g_strdup( who );
91                        ga->group = g_strdup( group );
92                        md->grpq = g_slist_prepend( md->grpq, ga );
93                       
94                        if( l == NULL )
95                        {
[4452e69]96                                char groupname[strlen(group)+1];
97                                strcpy( groupname, group );
98                                http_encode( groupname );
[70ac477]99                                g_snprintf( buf, sizeof( buf ), "ADG %d %s %d\r\n", ++md->trId, groupname, 0 );
100                                return msn_write( ic, buf, strlen( buf ) );
101                        }
102                        else
103                        {
104                                /* This can happen if the user's doing lots of adds to a
105                                   new group at once; we're still waiting for the server
106                                   to confirm group creation. */
107                                return 1;
108                        }
109                }
[b7d3cc34]110        }
111       
[4452e69]112        strcpy( realname, realname_ );
113        http_encode( realname );
[70ac477]114        g_snprintf( buf, sizeof( buf ), "ADD %d %s %s %s%s\r\n", ++md->trId, list, who, realname, groupid );
[b7d3cc34]115       
[70ac477]116        return msn_write( ic, buf, strlen( buf ) );
[b7d3cc34]117}
118
[8b01217]119int msn_buddy_list_remove( struct im_connection *ic, char *list, const char *who, const char *group )
[b7d3cc34]120{
[0da65d5]121        struct msn_data *md = ic->proto_data;
[8b01217]122        char buf[1024], groupid[8];
[b7d3cc34]123       
[8b01217]124        *groupid = '\0';
125        if( group )
126        {
127                int i;
128                for( i = 0; i < md->groupcount; i ++ )
129                        if( g_strcasecmp( md->grouplist[i], group ) == 0 )
130                        {
131                                g_snprintf( groupid, sizeof( groupid ), " %d", i );
132                                break;
133                        }
134        }
135       
136        g_snprintf( buf, sizeof( buf ), "REM %d %s %s%s\r\n", ++md->trId, list, who, groupid );
[0da65d5]137        if( msn_write( ic, buf, strlen( buf ) ) )
[b7d3cc34]138                return( 1 );
139       
140        return( 0 );
141}
142
143struct msn_buddy_ask_data
144{
[0da65d5]145        struct im_connection *ic;
[b7d3cc34]146        char *handle;
147        char *realname;
148};
149
[9143aeb]150static void msn_buddy_ask_yes( void *data )
[b7d3cc34]151{
[9143aeb]152        struct msn_buddy_ask_data *bla = data;
153       
[6acc033]154        msn_buddy_list_add( bla->ic, "AL", bla->handle, bla->realname, NULL );
[b7d3cc34]155       
[17a6ee9]156        imcb_ask_add( bla->ic, bla->handle, NULL );
[e6d6047]157       
[b7d3cc34]158        g_free( bla->handle );
159        g_free( bla->realname );
160        g_free( bla );
161}
162
[9143aeb]163static void msn_buddy_ask_no( void *data )
[b7d3cc34]164{
[9143aeb]165        struct msn_buddy_ask_data *bla = data;
166       
[6acc033]167        msn_buddy_list_add( bla->ic, "BL", bla->handle, bla->realname, NULL );
[b7d3cc34]168       
169        g_free( bla->handle );
170        g_free( bla->realname );
171        g_free( bla );
172}
173
[e5854a8]174void msn_buddy_ask( bee_user_t *bu )
[b7d3cc34]175{
[e5854a8]176        struct msn_buddy_ask_data *bla;
177        struct msn_buddy_data *bd = bu->data;
[b7d3cc34]178        char buf[1024];
179       
[e5854a8]180        if( ( bd->flags & 30 ) != 8 && ( bd->flags & 30 ) != 16 )
181                return;
182       
183        bla = g_new0( struct msn_buddy_ask_data, 1 );
184        bla->ic = bu->ic;
185        bla->handle = g_strdup( bu->handle );
186        bla->realname = g_strdup( bu->fullname );
[b7d3cc34]187       
188        g_snprintf( buf, sizeof( buf ),
[5c09a59]189                    "The user %s (%s) wants to add you to his/her buddy list.",
[e5854a8]190                    bu->handle, bu->fullname );
191        imcb_ask( bu->ic, buf, bla, msn_buddy_ask_yes, msn_buddy_ask_no );
[b7d3cc34]192}
193
194char *msn_findheader( char *text, char *header, int len )
195{
196        int hlen = strlen( header ), i;
197        char *ret;
198       
199        if( len == 0 )
200                len = strlen( text );
201       
202        i = 0;
203        while( ( i + hlen ) < len )
204        {
205                /* Maybe this is a bit over-commented, but I just hate this part... */
206                if( g_strncasecmp( text + i, header, hlen ) == 0 )
207                {
208                        /* Skip to the (probable) end of the header */
209                        i += hlen;
210                       
211                        /* Find the first non-[: \t] character */
212                        while( i < len && ( text[i] == ':' || text[i] == ' ' || text[i] == '\t' ) ) i ++;
213                       
214                        /* Make sure we're still inside the string */
215                        if( i >= len ) return( NULL );
216                       
217                        /* Save the position */
218                        ret = text + i;
219                       
220                        /* Search for the end of this line */
221                        while( i < len && text[i] != '\r' && text[i] != '\n' ) i ++;
222                       
223                        /* Make sure we're still inside the string */
224                        if( i >= len ) return( NULL );
225                       
226                        /* Copy the found data */
227                        return( g_strndup( ret, text + i - ret ) );
228                }
229               
230                /* This wasn't the header we were looking for, skip to the next line. */
231                while( i < len && ( text[i] != '\r' && text[i] != '\n' ) ) i ++;
232                while( i < len && ( text[i] == '\r' || text[i] == '\n' ) ) i ++;
233               
234                /* End of headers? */
[75ec2c8]235                if( ( i >= 4 && strncmp( text + i - 4, "\r\n\r\n", 4 ) == 0 ) ||
236                    ( i >= 2 && ( strncmp( text + i - 2, "\n\n", 2 ) == 0 ||   
237                                  strncmp( text + i - 2, "\r\r", 2 ) == 0 ) ) )
[b7d3cc34]238                {
239                        break;
240                }
241        }
242       
243        return( NULL );
244}
245
246/* *NOT* thread-safe, but that's not a problem for now... */
247char **msn_linesplit( char *line )
248{
249        static char **ret = NULL;
250        static int size = 3;
251        int i, n = 0;
252       
253        if( ret == NULL )
254                ret = g_new0( char*, size );
255       
256        for( i = 0; line[i] && line[i] == ' '; i ++ );
257        if( line[i] )
258        {
259                ret[n++] = line + i;
260                for( i ++; line[i]; i ++ )
261                {
262                        if( line[i] == ' ' )
263                                line[i] = 0;
264                        else if( line[i] != ' ' && !line[i-1] )
265                                ret[n++] = line + i;
266                       
267                        if( n >= size )
268                                ret = g_renew( char*, ret, size += 2 );
269                }
270        }
271        ret[n] = NULL;
272       
273        return( ret );
274}
275
276/* This one handles input from a MSN Messenger server. Both the NS and SB servers usually give
277   commands, but sometimes they give additional data (payload). This function tries to handle
278   this all in a nice way and send all data to the right places. */
279
280/* Return values: -1: Read error, abort connection.
281                   0: Command reported error; Abort *immediately*. (The connection does not exist anymore)
282                   1: OK */
283
284int msn_handler( struct msn_handler_data *h )
285{
286        int st;
287       
288        h->rxq = g_renew( char, h->rxq, h->rxlen + 1024 );
289        st = read( h->fd, h->rxq + h->rxlen, 1024 );
290        h->rxlen += st;
291       
292        if( st <= 0 )
293                return( -1 );
294       
[523fb23]295        if( getenv( "BITLBEE_DEBUG" ) )
296        {
297                write( 2, "->C:", 4 );
298                write( 2, h->rxq + h->rxlen - st, st );
299        }
300       
[b7d3cc34]301        while( st )
302        {
303                int i;
304               
305                if( h->msglen == 0 )
306                {
307                        for( i = 0; i < h->rxlen; i ++ )
308                        {
309                                if( h->rxq[i] == '\r' || h->rxq[i] == '\n' )
310                                {
311                                        char *cmd_text, **cmd;
312                                        int count;
313                                       
314                                        cmd_text = g_strndup( h->rxq, i );
315                                        cmd = msn_linesplit( cmd_text );
316                                        for( count = 0; cmd[count]; count ++ );
317                                        st = h->exec_command( h->data, cmd, count );
318                                        g_free( cmd_text );
319                                       
320                                        /* If the connection broke, don't continue. We don't even exist anymore. */
321                                        if( !st )
322                                                return( 0 );
323                                       
324                                        if( h->msglen )
325                                                h->cmd_text = g_strndup( h->rxq, i );
326                                       
327                                        /* Skip to the next non-emptyline */
328                                        while( i < h->rxlen && ( h->rxq[i] == '\r' || h->rxq[i] == '\n' ) ) i ++;
329                                       
330                                        break;
331                                }
332                        }
333                       
334                        /* If we reached the end of the buffer, there's still an incomplete command there.
335                           Return and wait for more data. */
336                        if( i == h->rxlen && h->rxq[i-1] != '\r' && h->rxq[i-1] != '\n' )
337                                break;
338                }
339                else
340                {
341                        char *msg, **cmd;
342                        int count;
343                       
344                        /* Do we have the complete message already? */
345                        if( h->msglen > h->rxlen )
346                                break;
347                       
348                        msg = g_strndup( h->rxq, h->msglen );
349                        cmd = msn_linesplit( h->cmd_text );
350                        for( count = 0; cmd[count]; count ++ );
351                       
352                        st = h->exec_message( h->data, msg, h->msglen, cmd, count );
353                        g_free( msg );
354                        g_free( h->cmd_text );
355                        h->cmd_text = NULL;
356                       
357                        if( !st )
358                                return( 0 );
359                       
360                        i = h->msglen;
361                        h->msglen = 0;
362                }
363               
364                /* More data after this block? */
365                if( i < h->rxlen )
366                {
367                        char *tmp;
368                       
369                        tmp = g_memdup( h->rxq + i, h->rxlen - i );
370                        g_free( h->rxq );
371                        h->rxq = tmp;
372                        h->rxlen -= i;
373                        i = 0;
374                }
375                else
376                /* If not, reset the rx queue and get lost. */
377                {
378                        g_free( h->rxq );
379                        h->rxq = g_new0( char, 1 );
380                        h->rxlen = 0;
381                        return( 1 );
382                }
383        }
384       
385        return( 1 );
386}
[54794b8]387
[46dca11]388void msn_msgq_purge( struct im_connection *ic, GSList **list )
389{
390        struct msn_message *m;
391        GString *ret;
392        GSList *l;
[4255320]393        int n = 0;
[46dca11]394       
395        l = *list;
396        if( l == NULL )
397                return;
398       
399        m = l->data;
400        ret = g_string_sized_new( 1024 );
[43462708]401        g_string_printf( ret, "Warning: Cleaning up MSN (switchboard) connection with unsent "
[46dca11]402                              "messages to %s:", m->who ? m->who : "unknown recipient" );
403       
404        while( l )
405        {
406                m = l->data;
407               
[4255320]408                if( strncmp( m->text, "\r\r\r", 3 ) != 0 )
409                {
410                        g_string_append_printf( ret, "\n%s", m->text );
411                        n ++;
412                }
[46dca11]413               
414                g_free( m->who );
415                g_free( m->text );
416                g_free( m );
417               
418                l = l->next;
419        }
420        g_slist_free( *list );
421        *list = NULL;
422       
[4255320]423        if( n > 0 )
424                imcb_log( ic, "%s", ret->str );
[46dca11]425        g_string_free( ret, TRUE );
426}
[e3413cc]427
[21029d0]428/* Copied and heavily modified from http://tmsnc.sourceforge.net/chl.c */
429char *msn_p11_challenge( char *challenge )
430{
431        char *output, buf[256];
432        md5_state_t md5c;
433        unsigned char md5Hash[16], *newHash;
434        unsigned int *md5Parts, *chlStringParts, newHashParts[5];
435        long long nHigh = 0, nLow = 0;
436        int i, n;
437
438        /* Create the MD5 hash */
439        md5_init(&md5c);
440        md5_append(&md5c, (unsigned char*) challenge, strlen(challenge));
441        md5_append(&md5c, (unsigned char*) MSNP11_PROD_KEY, strlen(MSNP11_PROD_KEY));
442        md5_finish(&md5c, md5Hash);
443
444        /* Split it into four integers */
445        md5Parts = (unsigned int *)md5Hash;
446        for (i = 0; i < 4; i ++)
447        { 
[523fb23]448                md5Parts[i] = GUINT32_TO_LE(md5Parts[i]);
[21029d0]449               
450                /* & each integer with 0x7FFFFFFF */
451                /* and save one unmodified array for later */
452                newHashParts[i] = md5Parts[i];
453                md5Parts[i] &= 0x7FFFFFFF;
454        }
455       
456        /* make a new string and pad with '0' */
457        n = g_snprintf(buf, sizeof(buf)-5, "%s%s00000000", challenge, MSNP11_PROD_ID);
458        /* truncate at an 8-byte boundary */
459        buf[n&=~7] = '\0';
460       
461        /* split into integers */
462        chlStringParts = (unsigned int *)buf;
463       
464        /* this is magic */
465        for (i = 0; i < (n / 4) - 1; i += 2)
466        {
467                long long temp;
468
[523fb23]469                chlStringParts[i]   = GUINT32_TO_LE(chlStringParts[i]);
470                chlStringParts[i+1] = GUINT32_TO_LE(chlStringParts[i+1]);
[21029d0]471
472                temp  = (md5Parts[0] * (((0x0E79A9C1 * (long long)chlStringParts[i]) % 0x7FFFFFFF)+nHigh) + md5Parts[1])%0x7FFFFFFF;
473                nHigh = (md5Parts[2] * (((long long)chlStringParts[i+1]+temp) % 0x7FFFFFFF) + md5Parts[3]) % 0x7FFFFFFF;
474                nLow  = nLow + nHigh + temp;
475        }
476        nHigh = (nHigh+md5Parts[1]) % 0x7FFFFFFF;
477        nLow = (nLow+md5Parts[3]) % 0x7FFFFFFF;
478       
479        newHashParts[0] ^= nHigh;
480        newHashParts[1] ^= nLow;
481        newHashParts[2] ^= nHigh;
482        newHashParts[3] ^= nLow;
483       
484        /* swap more bytes if big endian */
485        for (i = 0; i < 4; i ++)
[523fb23]486                newHashParts[i] = GUINT32_TO_LE(newHashParts[i]); 
[21029d0]487       
488        /* make a string of the parts */
489        newHash = (unsigned char *)newHashParts;
490       
491        /* convert to hexadecimal */
492        output = g_new(char, 33);
493        for (i = 0; i < 16; i ++)
494                sprintf(output + i * 2, "%02x", newHash[i]);
495       
496        return output;
497}
[ca7de3a]498
499gint msn_domaintree_cmp( gconstpointer a_, gconstpointer b_ )
500{
501        const char *a = a_, *b = b_;
502        gint ret;
503       
504        if( !( a = strchr( a, '@' ) ) || !( b = strchr( b, '@' ) ) ||
505            ( ret = strcmp( a, b ) ) == 0 )
506                ret = strcmp( a_, b_ );
507       
508        return ret;
509}
Note: See TracBrowser for help on using the repository browser.