source: skype/skype.c @ 969f643

Last change on this file since 969f643 was 969f643, checked in by Miklos Vajna <vmiklos@…>, at 2007-12-16T21:06:51Z

handle the case when the birthday is unknown

  • Property mode set to 100644
File size: 27.2 KB
Line 
1/*
2 *  skype.c - Skype plugin for BitlBee
3 *
4 *  Copyright (c) 2007 by Miklos Vajna <vmiklos@frugalware.org>
5 *
6 *  Several ideas are used from the BitlBee Jabber plugin, which is
7 *
8 *  Copyright (c) 2006 by Wilmer van der Gaast <wilmer@gaast.net>
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
21 *  along with this program; if not, write to the Free Software
22 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
23 *  USA.
24 */
25
26#define _XOPEN_SOURCE
27#include <stdio.h>
28#include <poll.h>
29#include <bitlbee.h>
30#include <glib.h>
31
32#define SKYPE_PORT_DEFAULT "2727"
33
34/*
35 * Enumerations
36 */
37
38typedef enum
39{
40        SKYPE_CALL_RINGING = 1,
41        SKYPE_CALL_MISSED
42} skype_call_status;
43
44typedef enum
45{
46        SKYPE_FILETRANSFER_NEW = 1,
47        SKYPE_FILETRANSFER_FAILED
48} skype_filetransfer_status;
49
50/*
51 * Structures
52 */
53
54struct skype_data
55{
56        struct im_connection *ic;
57        char *username;
58        /* The effective file descriptor. We store it here so any function can
59         * write() to it. */
60        int fd;
61        /* File descriptor returned by bitlbee. we store it so we know when
62         * we're connected and when we aren't. */
63        int bfd;
64        /* When we receive a new message id, we query the properties, finally
65         * the chatname. Store the properties here so that we can use
66         * imcb_buddy_msg() when we got the chatname. */
67        char *handle;
68        /* List, because of multiline messages. */
69        GList *body;
70        char *type;
71        /* This is necessary because we send a notification when we get the
72         * handle. So we store the state here and then we can send a
73         * notification about the handle is in a given status. */
74        skype_call_status call_status;
75        /* Same for file transfers. */
76        skype_filetransfer_status filetransfer_status;
77        /* Using /j #nick we want to have a groupchat with two people. Usually
78         * not (default). */
79        char* groupchat_with;
80        /* The user who invited us to the chat. */
81        char* adder;
82        /* If we are waiting for a confirmation about we changed the topic. */
83        int topic_wait;
84        /* These are used by the info command. */
85        char *info_fullname;
86        char *info_phonehome;
87        char *info_phoneoffice;
88        char *info_phonemobile;
89        char *info_nrbuddies;
90        char *info_tz;
91        char *info_seen;
92        char *info_birthday;
93        char *info_sex;
94        char *info_language;
95        char *info_country;
96        char *info_province;
97        char *info_city;
98        char *info_homepage;
99        char *info_about;
100};
101
102struct skype_away_state
103{
104        char *code;
105        char *full_name;
106};
107
108struct skype_buddy_ask_data
109{
110        struct im_connection *ic;
111        char *handle;
112};
113
114/*
115 * Tables
116 */
117
118const struct skype_away_state skype_away_state_list[] =
119{
120        { "ONLINE",  "Online" },
121        { "SKYPEME",  "Skype Me" },
122        { "AWAY",   "Away" },
123        { "NA",    "Not available" },
124        { "DND",      "Do Not Disturb" },
125        { "INVISIBLE",      "Invisible" },
126        { "OFFLINE",      "Offline" },
127        { NULL, NULL}
128};
129
130/*
131 * Functions
132 */
133
134static void skype_init( account_t *acc )
135{
136        set_t *s;
137
138        s = set_add( &acc->set, "port", SKYPE_PORT_DEFAULT, set_eval_int, acc );
139        s->flags |= ACC_SET_OFFLINE_ONLY;
140
141        s = set_add( &acc->set, "server", NULL, set_eval_account, acc );
142        s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY;
143}
144
145int skype_write( struct im_connection *ic, char *buf, int len )
146{
147        struct skype_data *sd = ic->proto_data;
148        struct pollfd pfd[1];
149
150        pfd[0].fd = sd->fd;
151        pfd[0].events = POLLOUT;
152
153        /* This poll is necessary or we'll get a SIGPIPE when we write() to
154         * sd->fd. */
155        poll(pfd, 1, 1000);
156        if(pfd[0].revents & POLLHUP)
157        {
158                imcb_error( ic, "Could not connect to server" );
159                imc_logout( ic, TRUE );
160                return FALSE;
161        }
162        write( sd->fd, buf, len );
163
164        return TRUE;
165}
166
167static void skype_buddy_ask_yes( gpointer w, struct skype_buddy_ask_data *bla )
168{
169        char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED TRUE", bla->handle);
170        skype_write( bla->ic, buf, strlen( buf ) );
171        g_free(buf);
172        g_free(bla->handle);
173        g_free(bla);
174}
175
176static void skype_buddy_ask_no( gpointer w, struct skype_buddy_ask_data *bla )
177{
178        char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED FALSE", bla->handle);
179        skype_write( bla->ic, buf, strlen( buf ) );
180        g_free(buf);
181        g_free(bla->handle);
182        g_free(bla);
183}
184
185void skype_buddy_ask( struct im_connection *ic, char *handle, char *message)
186{
187        struct skype_buddy_ask_data *bla = g_new0( struct skype_buddy_ask_data, 1 );
188        char *buf;
189
190        bla->ic = ic;
191        bla->handle = g_strdup(handle);
192
193        buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list, saying: '%s'.", handle, message);
194        imcb_ask( ic, buf, bla, skype_buddy_ask_yes, skype_buddy_ask_no );
195        g_free( buf );
196}
197
198struct groupchat *skype_chat_by_name( struct im_connection *ic, char *name )
199{
200        struct groupchat *ret;
201
202        for( ret = ic->groupchats; ret; ret = ret->next )
203        {
204                if(strcmp(name, ret->title ) == 0 )
205                        break;
206        }
207
208        return ret;
209}
210
211static gboolean skype_read_callback( gpointer data, gint fd, b_input_condition cond )
212{
213        struct im_connection *ic = data;
214        struct skype_data *sd = ic->proto_data;
215        char buf[1024];
216        int st;
217        char **lines, **lineptr, *line, *ptr;
218
219        if( !sd || sd->fd == -1 )
220                return FALSE;
221        /* Read the whole data. */
222        st = read( sd->fd, buf, sizeof( buf ) );
223        if( st > 0 )
224        {
225                buf[st] = '\0';
226                /* Then split it up to lines. */
227                lines = g_strsplit(buf, "\n", 0);
228                lineptr = lines;
229                while((line = *lineptr))
230                {
231                        if(!strlen(line))
232                                break;
233                        if(!strncmp(line, "USERS ", 6))
234                        {
235                                char **i;
236                                char **nicks;
237
238                                nicks = g_strsplit(line + 6, ", ", 0);
239                                i = nicks;
240                                while(*i)
241                                {
242                                        g_snprintf(buf, 1024, "GET USER %s ONLINESTATUS\n", *i);
243                                        skype_write( ic, buf, strlen( buf ) );
244                                        i++;
245                                }
246                                g_strfreev(nicks);
247                        }
248                        else if(!strncmp(line, "USER ", 5))
249                        {
250                                int flags = 0;
251                                char *status = strrchr(line, ' ');
252                                char *user = strchr(line, ' ');
253                                status++;
254                                ptr = strchr(++user, ' ');
255                                *ptr = '\0';
256                                ptr++;
257                                if(!strncmp(ptr, "ONLINESTATUS ", 13) &&
258                                                strcmp(user, sd->username) != 0
259                                                && strcmp(user, "echo123") != 0)
260                                {
261                                        ptr = g_strdup_printf("%s@skype.com", user);
262                                        imcb_add_buddy(ic, ptr, NULL);
263                                        if(strcmp(status, "OFFLINE") != 0)
264                                                flags |= OPT_LOGGED_IN;
265                                        if(strcmp(status, "ONLINE") != 0 && strcmp(status, "SKYPEME") != 0)
266                                                flags |= OPT_AWAY;
267                                        imcb_buddy_status(ic, ptr, flags, NULL, NULL);
268                                        g_free(ptr);
269                                }
270                                else if(!strncmp(ptr, "RECEIVEDAUTHREQUEST ", 20))
271                                {
272                                        char *message = ptr + 20;
273                                        if(strlen(message))
274                                                skype_buddy_ask(ic, user, message);
275                                }
276                                else if(!strncmp(ptr, "BUDDYSTATUS ", 12))
277                                {
278                                        char *st = ptr + 12;
279                                        if(!strcmp(st, "3"))
280                                        {
281                                                char *buf = g_strdup_printf("%s@skype.com", user);
282                                                imcb_add_buddy(ic, buf, NULL);
283                                                g_free(buf);
284                                        }
285                                }
286                                else if(!strncmp(ptr, "FULLNAME ", 9))
287                                        sd->info_fullname = g_strdup_printf("%s", ptr + 9);
288                                else if(!strncmp(ptr, "PHONE_HOME ", 11))
289                                        sd->info_phonehome = g_strdup_printf("%s", ptr + 11);
290                                else if(!strncmp(ptr, "PHONE_OFFICE ", 13))
291                                        sd->info_phoneoffice = g_strdup_printf("%s", ptr + 13);
292                                else if(!strncmp(ptr, "PHONE_MOBILE ", 13))
293                                        sd->info_phonemobile = g_strdup_printf("%s", ptr + 13);
294                                else if(!strncmp(ptr, "NROF_AUTHED_BUDDIES ", 20))
295                                        sd->info_nrbuddies = g_strdup_printf("%s", ptr + 20);
296                                else if(!strncmp(ptr, "TIMEZONE ", 9))
297                                        sd->info_tz = g_strdup_printf("%s", ptr + 9);
298                                else if(!strncmp(ptr, "LASTONLINETIMESTAMP ", 20))
299                                        sd->info_seen = g_strdup_printf("%s", ptr + 20);
300                                else if(!strncmp(ptr, "BIRTHDAY ", 9))
301                                        sd->info_birthday = g_strdup_printf("%s", ptr + 9);
302                                else if(!strncmp(ptr, "SEX ", 4))
303                                        sd->info_sex = g_strdup_printf("%s", ptr + 4);
304                                else if(!strncmp(ptr, "LANGUAGE ", 9))
305                                        sd->info_language = g_strdup_printf("%s", ptr + 9);
306                                else if(!strncmp(ptr, "COUNTRY ", 8))
307                                        sd->info_country = g_strdup_printf("%s", ptr + 8);
308                                else if(!strncmp(ptr, "PROVINCE ", 9))
309                                        sd->info_province = g_strdup_printf("%s", ptr + 9);
310                                else if(!strncmp(ptr, "CITY ", 5))
311                                        sd->info_city = g_strdup_printf("%s", ptr + 5);
312                                else if(!strncmp(ptr, "HOMEPAGE ", 9))
313                                        sd->info_homepage = g_strdup_printf("%s", ptr + 9);
314                                else if(!strncmp(ptr, "ABOUT ", 6))
315                                {
316                                        sd->info_about = g_strdup_printf("%s", ptr + 6);
317
318                                        GString *st = g_string_new("Contact Information\n");
319                                        g_string_append_printf(st, "Skype Name: %s\n", user);
320                                        if(sd->info_fullname)
321                                        {
322                                                if(strlen(sd->info_fullname))
323                                                        g_string_append_printf(st, "Full Name: %s\n", sd->info_fullname);
324                                                g_free(sd->info_fullname);
325                                        }
326                                        if(sd->info_phonehome)
327                                        {
328                                                if(strlen(sd->info_phonehome))
329                                                        g_string_append_printf(st, "Home Phone: %s\n", sd->info_phonehome);
330                                                g_free(sd->info_phonehome);
331                                        }
332                                        if(sd->info_phoneoffice)
333                                        {
334                                                if(strlen(sd->info_phoneoffice))
335                                                        g_string_append_printf(st, "Office Phone: %s\n", sd->info_phoneoffice);
336                                                g_free(sd->info_phoneoffice);
337                                        }
338                                        if(sd->info_phonemobile)
339                                        {
340                                                if(strlen(sd->info_phonemobile))
341                                                        g_string_append_printf(st, "Mobile Phone: %s\n", sd->info_phonemobile);
342                                                g_free(sd->info_phonemobile);
343                                        }
344                                        g_string_append_printf(st, "Personal Information\n");
345                                        if(sd->info_nrbuddies)
346                                        {
347                                                if(strlen(sd->info_nrbuddies))
348                                                        g_string_append_printf(st, "Contacts: %s\n", sd->info_nrbuddies);
349                                                g_free(sd->info_nrbuddies);
350                                        }
351                                        if(sd->info_tz)
352                                        {
353                                                if(strlen(sd->info_tz))
354                                                {
355                                                        char ib[256];
356                                                        time_t t = time(NULL);
357                                                        t += atoi(sd->info_tz)-(60*60*24);
358                                                        struct tm *gt = gmtime(&t);
359                                                        strftime(ib, 256, "%H:%M:%S", gt);
360                                                        g_string_append_printf(st, "Local Time: %s\n", ib);
361                                                }
362                                                g_free(sd->info_tz);
363                                        }
364                                        if(sd->info_seen)
365                                        {
366                                                if(strlen(sd->info_seen))
367                                                {
368                                                        char ib[256];
369                                                        time_t it = atoi(sd->info_seen);
370                                                        struct tm *tm = localtime(&it);
371                                                        strftime(ib, 256, ("%Y. %m. %d. %H:%M"), tm);
372                                                        g_string_append_printf(st, "Last Seen: %s\n", ib);
373                                                }
374                                                g_free(sd->info_seen);
375                                        }
376                                        if(sd->info_birthday)
377                                        {
378                                                if(strlen(sd->info_birthday) && strcmp(sd->info_birthday, "0"))
379                                                {
380                                                        char ib[256];
381                                                        struct tm tm;
382                                                        strptime(sd->info_birthday, "%Y%m%d", &tm);
383                                                        strftime(ib, 256, "%B %d, %Y", &tm);
384                                                        g_string_append_printf(st, "Birthday: %s\n", ib);
385
386                                                        strftime(ib, 256, "%Y", &tm);
387                                                        int year = atoi(ib);
388                                                        time_t t = time(NULL);
389                                                        struct tm *lt = localtime(&t);
390                                                        g_string_append_printf(st, "Age: %d\n", lt->tm_year+1900-year);
391                                                }
392                                                g_free(sd->info_birthday);
393                                        }
394                                        if(sd->info_sex)
395                                        {
396                                                if(strlen(sd->info_sex))
397                                                {
398                                                        char *iptr = sd->info_sex;
399                                                        while(*iptr++)
400                                                                *iptr = tolower(*iptr);
401                                                        g_string_append_printf(st, "Gender: %s\n", sd->info_sex);
402                                                }
403                                                g_free(sd->info_sex);
404                                        }
405                                        if(sd->info_language)
406                                        {
407                                                if(strlen(sd->info_language))
408                                                {
409                                                        char *iptr = strchr(sd->info_language, ' ');
410                                                        if(iptr)
411                                                                iptr++;
412                                                        else
413                                                                iptr = sd->info_language;
414                                                        g_string_append_printf(st, "Language: %s\n", iptr);
415                                                }
416                                                g_free(sd->info_language);
417                                        }
418                                        if(sd->info_country)
419                                        {
420                                                if(strlen(sd->info_country))
421                                                {
422                                                        char *iptr = strchr(sd->info_country, ' ');
423                                                        if(iptr)
424                                                                iptr++;
425                                                        else
426                                                                iptr = sd->info_country;
427                                                        g_string_append_printf(st, "Country: %s\n", iptr);
428                                                }
429                                                g_free(sd->info_country);
430                                        }
431                                        if(sd->info_province)
432                                        {
433                                                if(strlen(sd->info_province))
434                                                        g_string_append_printf(st, "Region: %s\n", sd->info_province);
435                                                g_free(sd->info_province);
436                                        }
437                                        if(sd->info_city)
438                                        {
439                                                if(strlen(sd->info_city))
440                                                        g_string_append_printf(st, "City: %s\n", sd->info_city);
441                                                g_free(sd->info_city);
442                                        }
443                                        if(sd->info_homepage)
444                                        {
445                                                if(strlen(sd->info_homepage))
446                                                        g_string_append_printf(st, "Homepage: %s\n", sd->info_homepage);
447                                                g_free(sd->info_homepage);
448                                        }
449                                        if(sd->info_about)
450                                        {
451                                                if(strlen(sd->info_about))
452                                                        g_string_append_printf(st, "%s\n", sd->info_about);
453                                                g_free(sd->info_about);
454                                        }
455                                        imcb_log(ic, "%s", st->str);
456                                        g_string_free(st, TRUE);
457                                }
458                        }
459                        else if(!strncmp(line, "CHATMESSAGE ", 12))
460                        {
461                                char *id = strchr(line, ' ');
462                                if(++id)
463                                {
464                                        char *info = strchr(id, ' ');
465                                        *info = '\0';
466                                        info++;
467                                        if(!strcmp(info, "STATUS RECEIVED"))
468                                        {
469                                                /* New message ID:
470                                                 * (1) Request its from field
471                                                 * (2) Request its body
472                                                 * (3) Request its type
473                                                 * (4) Query chatname
474                                                 */
475                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s FROM_HANDLE\n", id);
476                                                skype_write( ic, buf, strlen( buf ) );
477                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s BODY\n", id);
478                                                skype_write( ic, buf, strlen( buf ) );
479                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s TYPE\n", id);
480                                                skype_write( ic, buf, strlen( buf ) );
481                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s CHATNAME\n", id);
482                                                skype_write( ic, buf, strlen( buf ) );
483                                        }
484                                        else if(!strncmp(info, "FROM_HANDLE ", 12))
485                                        {
486                                                info += 12;
487                                                /* New from field value. Store
488                                                 * it, then we can later use it
489                                                 * when we got the message's
490                                                 * body. */
491                                                g_free(sd->handle);
492                                                sd->handle = g_strdup_printf("%s@skype.com", info);
493                                        }
494                                        else if(!strncmp(info, "EDITED_BY ", 10))
495                                        {
496                                                info += 10;
497                                                /* This is the same as
498                                                 * FROM_HANDLE, except that we
499                                                 * never request these lines
500                                                 * from Skype, we just get
501                                                 * them. */
502                                                g_free(sd->handle);
503                                                sd->handle = g_strdup_printf("%s@skype.com", info);
504                                        }
505                                        else if(!strncmp(info, "BODY ", 5))
506                                        {
507                                                info += 5;
508                                                sd->body = g_list_append(sd->body, g_strdup(info));
509                                        }
510                                        else if(!strncmp(info, "TYPE ", 5))
511                                        {
512                                                info += 5;
513                                                g_free(sd->type);
514                                                sd->type = g_strdup(info);
515                                        }
516                                        else if(!strncmp(info, "CHATNAME ", 9))
517                                        {
518                                                info += 9;
519                                                if(sd->handle && sd->body && sd->type)
520                                                {
521                                                        struct groupchat *gc = skype_chat_by_name(ic, info);
522                                                        int i;
523                                                        for(i=0;i<g_list_length(sd->body);i++)
524                                                        {
525                                                                char *body = g_list_nth_data(sd->body, i);
526                                                                if(!strcmp(sd->type, "SAID"))
527                                                                {
528                                                                        if(!gc)
529                                                                                /* Private message */
530                                                                                imcb_buddy_msg(ic, sd->handle, body, 0, 0);
531                                                                        else
532                                                                                /* Groupchat message */
533                                                                                imcb_chat_msg(gc, sd->handle, body, 0, 0);
534                                                                }
535                                                                else if(!strcmp(sd->type, "SETTOPIC"))
536                                                                {
537                                                                        if(gc)
538                                                                                imcb_chat_topic(gc, sd->handle, body, 0);
539                                                                }
540                                                                else if(!strcmp(sd->type, "LEFT"))
541                                                                {
542                                                                        if(gc)
543                                                                                imcb_chat_remove_buddy(gc, sd->handle, NULL);
544                                                                }
545                                                        }
546                                                        g_list_free(sd->body);
547                                                        sd->body = NULL;
548                                                }
549                                        }
550                                }
551                        }
552                        else if(!strncmp(line, "CALL ", 5))
553                        {
554                                char *id = strchr(line, ' ');
555                                if(++id)
556                                {
557                                        char *info = strchr(id, ' ');
558                                        *info = '\0';
559                                        info++;
560                                        if(!strcmp(info, "STATUS RINGING"))
561                                        {
562                                                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
563                                                skype_write( ic, buf, strlen( buf ) );
564                                                sd->call_status = SKYPE_CALL_RINGING;
565                                        }
566                                        else if(!strcmp(info, "STATUS MISSED"))
567                                        {
568                                                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
569                                                skype_write( ic, buf, strlen( buf ) );
570                                                sd->call_status = SKYPE_CALL_MISSED;
571                                        }
572                                        else if(!strncmp(info, "PARTNER_HANDLE ", 15))
573                                        {
574                                                info += 15;
575                                                if(sd->call_status) {
576                                                        switch(sd->call_status)
577                                                        {
578                                                                case SKYPE_CALL_RINGING:
579                                                                        imcb_log(ic, "The user %s is currently ringing you.", info);
580                                                                        break;
581                                                                case SKYPE_CALL_MISSED:
582                                                                        imcb_log(ic, "You have missed a call from user %s.", info);
583                                                                        break;
584                                                        }
585                                                        sd->call_status = 0;
586                                                }
587                                        }
588                                }
589                        }
590                        else if(!strncmp(line, "FILETRANSFER ", 13))
591                        {
592                                char *id = strchr(line, ' ');
593                                if(++id)
594                                {
595                                        char *info = strchr(id, ' ');
596                                        *info = '\0';
597                                        info++;
598                                        if(!strcmp(info, "STATUS NEW"))
599                                        {
600                                                g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id);
601                                                skype_write( ic, buf, strlen( buf ) );
602                                                sd->filetransfer_status = SKYPE_FILETRANSFER_NEW;
603                                        }
604                                        else if(!strcmp(info, "STATUS FAILED"))
605                                        {
606                                                g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id);
607                                                skype_write( ic, buf, strlen( buf ) );
608                                                sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED;
609                                        }
610                                        else if(!strncmp(info, "PARTNER_HANDLE ", 15))
611                                        {
612                                                info += 15;
613                                                if(sd->filetransfer_status) {
614                                                        switch(sd->filetransfer_status)
615                                                        {
616                                                                case SKYPE_FILETRANSFER_NEW:
617                                                                        imcb_log(ic, "The user %s offered a new file for you.", info);
618                                                                        break;
619                                                                case SKYPE_FILETRANSFER_FAILED:
620                                                                        imcb_log(ic, "Failed to transfer file from user %s.", info);
621                                                                        break;
622                                                        }
623                                                        sd->filetransfer_status = 0;
624                                                }
625                                        }
626                                }
627                        }
628                        else if(!strncmp(line, "CHAT ", 5))
629                        {
630                                char *id = strchr(line, ' ');
631                                if(++id)
632                                {
633                                        char *info = strchr(id, ' ');
634                                        if(info)
635                                                *info = '\0';
636                                        info++;
637                                        /* Remove fake chat if we created one in skype_chat_with() */
638                                        struct groupchat *gc = skype_chat_by_name(ic, "");
639                                        if(gc)
640                                                imcb_chat_free(gc);
641                                        if(!strcmp(info, "STATUS MULTI_SUBSCRIBED"))
642                                        {
643                                                imcb_chat_new( ic, id );
644                                                g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id);
645                                                skype_write(ic, buf, strlen(buf));
646                                                g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id);
647                                                skype_write(ic, buf, strlen(buf));
648                                        }
649                                        else if(!strcmp(info, "STATUS DIALOG") && sd->groupchat_with)
650                                        {
651                                                gc = imcb_chat_new( ic, id );
652                                                /* According to the docs this
653                                                 * is necessary. However it
654                                                 * does not seem the situation
655                                                 * and it would open an extra
656                                                 * window on our client, so
657                                                 * just leave it out. */
658                                                /*g_snprintf(buf, 1024, "OPEN CHAT %s\n", id);
659                                                skype_write(ic, buf, strlen(buf));*/
660                                                g_snprintf(buf, 1024, "%s@skype.com", sd->groupchat_with);
661                                                imcb_chat_add_buddy(gc, buf);
662                                                imcb_chat_add_buddy(gc, sd->username);
663                                                g_free(sd->groupchat_with);
664                                                sd->groupchat_with = NULL;
665                                                g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id);
666                                                skype_write(ic, buf, strlen(buf));
667                                                g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id);
668                                                skype_write(ic, buf, strlen(buf));
669                                        }
670                                        else if(!strcmp(info, "STATUS UNSUBSCRIBED"))
671                                        {
672                                                gc = skype_chat_by_name(ic, id);
673                                                if(gc)
674                                                        gc->data = (void*)FALSE;
675                                        }
676                                        else if(!strncmp(info, "ADDER ", 6))
677                                        {
678                                                info += 6;
679                                                g_free(sd->adder);
680                                                sd->adder = g_strdup_printf("%s@skype.com", info);
681                                        }
682                                        else if(!strncmp(info, "TOPIC ", 6))
683                                        {
684                                                info += 6;
685                                                gc = skype_chat_by_name(ic, id);
686                                                if(gc && (sd->adder || sd->topic_wait))
687                                                {
688                                                        if(sd->topic_wait)
689                                                        {
690                                                                sd->adder = g_strdup(sd->username);
691                                                                sd->topic_wait = 0;
692                                                        }
693                                                        imcb_chat_topic(gc, sd->adder, info, 0);
694                                                        g_free(sd->adder);
695                                                        sd->adder = NULL;
696                                                }
697                                        }
698                                        else if(!strncmp(info, "ACTIVEMEMBERS ", 14))
699                                        {
700                                                info += 14;
701                                                gc = skype_chat_by_name(ic, id);
702                                                /* Hack! We set ->data to TRUE
703                                                 * while we're on the channel
704                                                 * so that we won't rejoin
705                                                 * after a /part. */
706                                                if(gc && !gc->data)
707                                                {
708                                                        char **members = g_strsplit(info, " ", 0);
709                                                        int i;
710                                                        for(i=0;members[i];i++)
711                                                        {
712                                                                if(!strcmp(members[i], sd->username))
713                                                                        continue;
714                                                                g_snprintf(buf, 1024, "%s@skype.com", members[i]);
715                                                                if(!g_list_find_custom(gc->in_room, buf, (GCompareFunc)strcmp))
716                                                                        imcb_chat_add_buddy(gc, buf);
717                                                        }
718                                                        imcb_chat_add_buddy(gc, sd->username);
719                                                        g_strfreev(members);
720                                                }
721                                        }
722                                }
723                        }
724                        lineptr++;
725                }
726                g_strfreev(lines);
727        }
728        else if( st == 0 || ( st < 0 && !sockerr_again() ) )
729        {
730                closesocket( sd->fd );
731                sd->fd = -1;
732
733                imcb_error( ic, "Error while reading from server" );
734                imc_logout( ic, TRUE );
735                return FALSE;
736        }
737        return TRUE;
738}
739
740gboolean skype_start_stream( struct im_connection *ic )
741{
742        struct skype_data *sd = ic->proto_data;
743        char *buf;
744        int st;
745
746        if(!sd)
747                return FALSE;
748
749        if( sd->bfd <= 0 )
750                sd->bfd = b_input_add( sd->fd, GAIM_INPUT_READ, skype_read_callback, ic );
751
752        /* This will download all buddies. */
753        buf = g_strdup_printf("SEARCH FRIENDS\n");
754        st = skype_write( ic, buf, strlen( buf ) );
755        g_free(buf);
756        buf = g_strdup_printf("SET USERSTATUS ONLINE\n");
757        skype_write( ic, buf, strlen( buf ) );
758        g_free(buf);
759        return st;
760}
761
762gboolean skype_connected( gpointer data, gint source, b_input_condition cond )
763{
764        struct im_connection *ic = data;
765        imcb_connected(ic);
766        return skype_start_stream(ic);
767}
768
769static void skype_login( account_t *acc )
770{
771        struct im_connection *ic = imcb_new( acc );
772        struct skype_data *sd = g_new0( struct skype_data, 1 );
773
774        ic->proto_data = sd;
775
776        imcb_log( ic, "Connecting" );
777        sd->fd = proxy_connect(acc->server, set_getint( &acc->set, "port" ), skype_connected, ic );
778        sd->username = g_strdup( acc->user );
779
780        sd->ic = ic;
781}
782
783static void skype_logout( struct im_connection *ic )
784{
785        struct skype_data *sd = ic->proto_data;
786        char *buf;
787
788        buf = g_strdup_printf("SET USERSTATUS OFFLINE\n");
789        skype_write( ic, buf, strlen( buf ) );
790        g_free(buf);
791
792        g_free(sd->username);
793        g_free(sd->handle);
794        g_free(sd);
795        ic->proto_data = NULL;
796}
797
798static int skype_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
799{
800        char *buf, *ptr, *nick;
801        int st;
802
803        nick = g_strdup(who);
804        ptr = strchr(nick, '@');
805        if(ptr)
806                *ptr = '\0';
807
808        buf = g_strdup_printf("MESSAGE %s %s\n", nick, message);
809        g_free(nick);
810        st = skype_write( ic, buf, strlen( buf ) );
811        g_free(buf);
812
813        return st;
814}
815
816const struct skype_away_state *skype_away_state_by_name( char *name )
817{
818        int i;
819
820        for( i = 0; skype_away_state_list[i].full_name; i ++ )
821                if( g_strcasecmp( skype_away_state_list[i].full_name, name ) == 0 )
822                        return( skype_away_state_list + i );
823
824        return NULL;
825}
826
827static void skype_set_away( struct im_connection *ic, char *state_txt, char *message )
828{
829        const struct skype_away_state *state;
830        char *buf;
831
832        if( strcmp( state_txt, GAIM_AWAY_CUSTOM ) == 0 )
833                state = skype_away_state_by_name( "Away" );
834        else
835                state = skype_away_state_by_name( state_txt );
836        buf = g_strdup_printf("SET USERSTATUS %s\n", state->code);
837        skype_write( ic, buf, strlen( buf ) );
838        g_free(buf);
839}
840
841static GList *skype_away_states( struct im_connection *ic )
842{
843        static GList *l = NULL;
844        int i;
845       
846        if( l == NULL )
847                for( i = 0; skype_away_state_list[i].full_name; i ++ )
848                        l = g_list_append( l, (void*) skype_away_state_list[i].full_name );
849       
850        return l;
851}
852
853static void skype_add_buddy( struct im_connection *ic, char *who, char *group )
854{
855        char *buf, *nick, *ptr;
856
857        nick = g_strdup(who);
858        ptr = strchr(nick, '@');
859        if(ptr)
860                *ptr = '\0';
861        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n", nick);
862        skype_write( ic, buf, strlen( buf ) );
863        g_free(nick);
864}
865
866static void skype_remove_buddy( struct im_connection *ic, char *who, char *group )
867{
868        char *buf, *nick, *ptr;
869
870        nick = g_strdup(who);
871        ptr = strchr(nick, '@');
872        if(ptr)
873                *ptr = '\0';
874        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick);
875        skype_write( ic, buf, strlen( buf ) );
876        g_free(nick);
877}
878
879void skype_chat_msg( struct groupchat *gc, char *message, int flags )
880{
881        struct im_connection *ic = gc->ic;
882        char *buf;
883        buf = g_strdup_printf("CHATMESSAGE %s %s\n", gc->title, message);
884        skype_write( ic, buf, strlen( buf ) );
885        g_free(buf);
886}
887
888void skype_chat_leave( struct groupchat *gc )
889{
890        struct im_connection *ic = gc->ic;
891        char *buf;
892        buf = g_strdup_printf("ALTER CHAT %s LEAVE\n", gc->title);
893        skype_write( ic, buf, strlen( buf ) );
894        g_free(buf);
895        gc->data = (void*)TRUE;
896}
897
898void skype_chat_invite(struct groupchat *gc, char *who, char *message)
899{
900        struct im_connection *ic = gc->ic;
901        char *buf, *ptr, *nick;
902        nick = g_strdup(message);
903        ptr = strchr(nick, '@');
904        if(ptr)
905                *ptr = '\0';
906        buf = g_strdup_printf("ALTER CHAT %s ADDMEMBERS %s\n", gc->title, nick);
907        skype_write( ic, buf, strlen( buf ) );
908        g_free(buf);
909        g_free(nick);
910}
911
912void skype_chat_topic(struct groupchat *gc, char *message)
913{
914        struct im_connection *ic = gc->ic;
915        struct skype_data *sd = ic->proto_data;
916        char *buf;
917        buf = g_strdup_printf("ALTER CHAT %s SETTOPIC %s\n", gc->title, message);
918        skype_write( ic, buf, strlen( buf ) );
919        g_free(buf);
920        sd->topic_wait = 1;
921}
922
923struct groupchat *skype_chat_with(struct im_connection *ic, char *who)
924{
925        struct skype_data *sd = ic->proto_data;
926        char *ptr, *nick, *buf;
927        nick = g_strdup(who);
928        ptr = strchr(nick, '@');
929        if(ptr)
930                *ptr = '\0';
931        buf = g_strdup_printf("CHAT CREATE %s\n", nick);
932        skype_write(ic, buf, strlen(buf));
933        g_free(buf);
934        sd->groupchat_with = g_strdup(nick);
935        g_free(nick);
936        /* We create a fake chat for now. We will replace it with a real one in
937         * the real callback. */
938        return(imcb_chat_new( ic, "" ));
939}
940
941static void skype_get_info(struct im_connection *ic, char *who)
942{
943        char *ptr, *nick, *buf;
944        nick = g_strdup(who);
945        ptr = strchr(nick, '@');
946        if(ptr)
947                *ptr = '\0';
948        buf = g_strdup_printf("GET USER %s FULLNAME\n", nick);
949        skype_write(ic, buf, strlen(buf));
950        g_free(buf);
951        buf = g_strdup_printf("GET USER %s PHONE_HOME\n", nick);
952        skype_write(ic, buf, strlen(buf));
953        g_free(buf);
954        buf = g_strdup_printf("GET USER %s PHONE_OFFICE\n", nick);
955        skype_write(ic, buf, strlen(buf));
956        g_free(buf);
957        buf = g_strdup_printf("GET USER %s PHONE_MOBILE\n", nick);
958        skype_write(ic, buf, strlen(buf));
959        g_free(buf);
960        buf = g_strdup_printf("GET USER %s NROF_AUTHED_BUDDIES\n", nick);
961        skype_write(ic, buf, strlen(buf));
962        g_free(buf);
963        buf = g_strdup_printf("GET USER %s TIMEZONE\n", nick);
964        skype_write(ic, buf, strlen(buf));
965        g_free(buf);
966        buf = g_strdup_printf("GET USER %s LASTONLINETIMESTAMP\n", nick);
967        skype_write(ic, buf, strlen(buf));
968        g_free(buf);
969        buf = g_strdup_printf("GET USER %s BIRTHDAY\n", nick);
970        skype_write(ic, buf, strlen(buf));
971        g_free(buf);
972        buf = g_strdup_printf("GET USER %s SEX\n", nick);
973        skype_write(ic, buf, strlen(buf));
974        g_free(buf);
975        buf = g_strdup_printf("GET USER %s LANGUAGE\n", nick);
976        skype_write(ic, buf, strlen(buf));
977        g_free(buf);
978        buf = g_strdup_printf("GET USER %s COUNTRY\n", nick);
979        skype_write(ic, buf, strlen(buf));
980        g_free(buf);
981        buf = g_strdup_printf("GET USER %s PROVINCE\n", nick);
982        skype_write(ic, buf, strlen(buf));
983        g_free(buf);
984        buf = g_strdup_printf("GET USER %s CITY\n", nick);
985        skype_write(ic, buf, strlen(buf));
986        g_free(buf);
987        buf = g_strdup_printf("GET USER %s HOMEPAGE\n", nick);
988        skype_write(ic, buf, strlen(buf));
989        g_free(buf);
990        buf = g_strdup_printf("GET USER %s ABOUT\n", nick);
991        skype_write(ic, buf, strlen(buf));
992        g_free(buf);
993}
994
995void init_plugin(void)
996{
997        struct prpl *ret = g_new0( struct prpl, 1 );
998
999        ret->name = "skype";
1000        ret->login = skype_login;
1001        ret->init = skype_init;
1002        ret->logout = skype_logout;
1003        ret->buddy_msg = skype_buddy_msg;
1004        ret->get_info = skype_get_info;
1005        ret->away_states = skype_away_states;
1006        ret->set_away = skype_set_away;
1007        ret->add_buddy = skype_add_buddy;
1008        ret->remove_buddy = skype_remove_buddy;
1009        ret->chat_msg = skype_chat_msg;
1010        ret->chat_leave = skype_chat_leave;
1011        ret->chat_invite = skype_chat_invite;
1012        ret->chat_with = skype_chat_with;
1013        ret->handle_cmp = g_strcasecmp;
1014        ret->chat_topic = skype_chat_topic;
1015        register_protocol( ret );
1016}
Note: See TracBrowser for help on using the repository browser.