source: skype/skype.c @ f9c3e7b

Last change on this file since f9c3e7b was f9c3e7b, checked in by Miklos Vajna <vmiklos@…>, at 2007-12-16T02:55:56Z

fixed birthday output

  • Property mode set to 100644
File size: 26.8 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                                                        /* this is currently buggy, says gmt+12 when I set gmt+1
356                                                        g_string_append_printf(st, "Local Time: %s\n", sd->info_tz); */
357                                                }
358                                                g_free(sd->info_tz);
359                                        }
360                                        if(sd->info_seen)
361                                        {
362                                                if(strlen(sd->info_seen))
363                                                {
364                                                        char ib[256];
365                                                        time_t it = atoi(sd->info_seen);
366                                                        struct tm *tm = localtime(&it);
367                                                        strftime(ib, 256, ("%Y. %m. %d. %H:%M"), tm);
368                                                        g_string_append_printf(st, "Last Seen: %s\n", ib);
369                                                }
370                                                g_free(sd->info_seen);
371                                        }
372                                        if(sd->info_birthday)
373                                        {
374                                                if(strlen(sd->info_birthday))
375                                                {
376                                                        char ib[256];
377                                                        struct tm tm;
378                                                        strptime(sd->info_birthday, "%Y%m%d", &tm);
379                                                        strftime(ib, 256, "%B %d, %Y", &tm);
380                                                        g_string_append_printf(st, "Birthday: %s\n", ib);
381                                                        g_string_append_printf(st, "Age:\n");
382                                                }
383                                                g_free(sd->info_birthday);
384                                        }
385                                        if(sd->info_sex)
386                                        {
387                                                if(strlen(sd->info_sex))
388                                                {
389                                                        // FIXME: UNKNOWN -> Unknown
390                                                        g_string_append_printf(st, "Gender: %s\n", sd->info_sex);
391                                                }
392                                                g_free(sd->info_sex);
393                                        }
394                                        if(sd->info_language)
395                                        {
396                                                if(strlen(sd->info_language))
397                                                {
398                                                        char *iptr = strchr(sd->info_language, ' ');
399                                                        if(iptr)
400                                                                iptr++;
401                                                        else
402                                                                iptr = sd->info_language;
403                                                        g_string_append_printf(st, "Language: %s\n", iptr);
404                                                }
405                                                g_free(sd->info_language);
406                                        }
407                                        if(sd->info_country)
408                                        {
409                                                if(strlen(sd->info_country))
410                                                {
411                                                        char *iptr = strchr(sd->info_country, ' ');
412                                                        if(iptr)
413                                                                iptr++;
414                                                        else
415                                                                iptr = sd->info_country;
416                                                        g_string_append_printf(st, "Country: %s\n", iptr);
417                                                }
418                                                g_free(sd->info_country);
419                                        }
420                                        if(sd->info_province)
421                                        {
422                                                if(strlen(sd->info_province))
423                                                        g_string_append_printf(st, "Region: %s\n", sd->info_province);
424                                                g_free(sd->info_province);
425                                        }
426                                        if(sd->info_city)
427                                        {
428                                                if(strlen(sd->info_city))
429                                                        g_string_append_printf(st, "City: %s\n", sd->info_city);
430                                                g_free(sd->info_city);
431                                        }
432                                        if(sd->info_homepage)
433                                        {
434                                                if(strlen(sd->info_homepage))
435                                                        g_string_append_printf(st, "Homepage: %s\n", sd->info_homepage);
436                                                g_free(sd->info_homepage);
437                                        }
438                                        if(sd->info_about)
439                                        {
440                                                if(strlen(sd->info_about))
441                                                        g_string_append_printf(st, "%s\n", sd->info_about);
442                                                g_free(sd->info_about);
443                                        }
444                                        imcb_log(ic, "%s", st->str);
445                                        g_string_free(st, TRUE);
446                                }
447                        }
448                        else if(!strncmp(line, "CHATMESSAGE ", 12))
449                        {
450                                char *id = strchr(line, ' ');
451                                if(++id)
452                                {
453                                        char *info = strchr(id, ' ');
454                                        *info = '\0';
455                                        info++;
456                                        if(!strcmp(info, "STATUS RECEIVED"))
457                                        {
458                                                /* New message ID:
459                                                 * (1) Request its from field
460                                                 * (2) Request its body
461                                                 * (3) Request its type
462                                                 * (4) Query chatname
463                                                 */
464                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s FROM_HANDLE\n", id);
465                                                skype_write( ic, buf, strlen( buf ) );
466                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s BODY\n", id);
467                                                skype_write( ic, buf, strlen( buf ) );
468                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s TYPE\n", id);
469                                                skype_write( ic, buf, strlen( buf ) );
470                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s CHATNAME\n", id);
471                                                skype_write( ic, buf, strlen( buf ) );
472                                        }
473                                        else if(!strncmp(info, "FROM_HANDLE ", 12))
474                                        {
475                                                info += 12;
476                                                /* New from field value. Store
477                                                 * it, then we can later use it
478                                                 * when we got the message's
479                                                 * body. */
480                                                g_free(sd->handle);
481                                                sd->handle = g_strdup_printf("%s@skype.com", info);
482                                        }
483                                        else if(!strncmp(info, "EDITED_BY ", 10))
484                                        {
485                                                info += 10;
486                                                /* This is the same as
487                                                 * FROM_HANDLE, except that we
488                                                 * never request these lines
489                                                 * from Skype, we just get
490                                                 * them. */
491                                                g_free(sd->handle);
492                                                sd->handle = g_strdup_printf("%s@skype.com", info);
493                                        }
494                                        else if(!strncmp(info, "BODY ", 5))
495                                        {
496                                                info += 5;
497                                                sd->body = g_list_append(sd->body, g_strdup(info));
498                                        }
499                                        else if(!strncmp(info, "TYPE ", 5))
500                                        {
501                                                info += 5;
502                                                g_free(sd->type);
503                                                sd->type = g_strdup(info);
504                                        }
505                                        else if(!strncmp(info, "CHATNAME ", 9))
506                                        {
507                                                info += 9;
508                                                if(sd->handle && sd->body && sd->type)
509                                                {
510                                                        struct groupchat *gc = skype_chat_by_name(ic, info);
511                                                        int i;
512                                                        for(i=0;i<g_list_length(sd->body);i++)
513                                                        {
514                                                                char *body = g_list_nth_data(sd->body, i);
515                                                                if(!strcmp(sd->type, "SAID"))
516                                                                {
517                                                                        if(!gc)
518                                                                                /* Private message */
519                                                                                imcb_buddy_msg(ic, sd->handle, body, 0, 0);
520                                                                        else
521                                                                                /* Groupchat message */
522                                                                                imcb_chat_msg(gc, sd->handle, body, 0, 0);
523                                                                }
524                                                                else if(!strcmp(sd->type, "SETTOPIC"))
525                                                                {
526                                                                        if(gc)
527                                                                                imcb_chat_topic(gc, sd->handle, body, 0);
528                                                                }
529                                                                else if(!strcmp(sd->type, "LEFT"))
530                                                                {
531                                                                        if(gc)
532                                                                                imcb_chat_remove_buddy(gc, sd->handle, NULL);
533                                                                }
534                                                        }
535                                                        g_list_free(sd->body);
536                                                        sd->body = NULL;
537                                                }
538                                        }
539                                }
540                        }
541                        else if(!strncmp(line, "CALL ", 5))
542                        {
543                                char *id = strchr(line, ' ');
544                                if(++id)
545                                {
546                                        char *info = strchr(id, ' ');
547                                        *info = '\0';
548                                        info++;
549                                        if(!strcmp(info, "STATUS RINGING"))
550                                        {
551                                                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
552                                                skype_write( ic, buf, strlen( buf ) );
553                                                sd->call_status = SKYPE_CALL_RINGING;
554                                        }
555                                        else if(!strcmp(info, "STATUS MISSED"))
556                                        {
557                                                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
558                                                skype_write( ic, buf, strlen( buf ) );
559                                                sd->call_status = SKYPE_CALL_MISSED;
560                                        }
561                                        else if(!strncmp(info, "PARTNER_HANDLE ", 15))
562                                        {
563                                                info += 15;
564                                                if(sd->call_status) {
565                                                        switch(sd->call_status)
566                                                        {
567                                                                case SKYPE_CALL_RINGING:
568                                                                        imcb_log(ic, "The user %s is currently ringing you.", info);
569                                                                        break;
570                                                                case SKYPE_CALL_MISSED:
571                                                                        imcb_log(ic, "You have missed a call from user %s.", info);
572                                                                        break;
573                                                        }
574                                                        sd->call_status = 0;
575                                                }
576                                        }
577                                }
578                        }
579                        else if(!strncmp(line, "FILETRANSFER ", 13))
580                        {
581                                char *id = strchr(line, ' ');
582                                if(++id)
583                                {
584                                        char *info = strchr(id, ' ');
585                                        *info = '\0';
586                                        info++;
587                                        if(!strcmp(info, "STATUS NEW"))
588                                        {
589                                                g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id);
590                                                skype_write( ic, buf, strlen( buf ) );
591                                                sd->filetransfer_status = SKYPE_FILETRANSFER_NEW;
592                                        }
593                                        else if(!strcmp(info, "STATUS FAILED"))
594                                        {
595                                                g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id);
596                                                skype_write( ic, buf, strlen( buf ) );
597                                                sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED;
598                                        }
599                                        else if(!strncmp(info, "PARTNER_HANDLE ", 15))
600                                        {
601                                                info += 15;
602                                                if(sd->filetransfer_status) {
603                                                        switch(sd->filetransfer_status)
604                                                        {
605                                                                case SKYPE_FILETRANSFER_NEW:
606                                                                        imcb_log(ic, "The user %s offered a new file for you.", info);
607                                                                        break;
608                                                                case SKYPE_FILETRANSFER_FAILED:
609                                                                        imcb_log(ic, "Failed to transfer file from user %s.", info);
610                                                                        break;
611                                                        }
612                                                        sd->filetransfer_status = 0;
613                                                }
614                                        }
615                                }
616                        }
617                        else if(!strncmp(line, "CHAT ", 5))
618                        {
619                                char *id = strchr(line, ' ');
620                                if(++id)
621                                {
622                                        char *info = strchr(id, ' ');
623                                        if(info)
624                                                *info = '\0';
625                                        info++;
626                                        /* Remove fake chat if we created one in skype_chat_with() */
627                                        struct groupchat *gc = skype_chat_by_name(ic, "");
628                                        if(gc)
629                                                imcb_chat_free(gc);
630                                        if(!strcmp(info, "STATUS MULTI_SUBSCRIBED"))
631                                        {
632                                                imcb_chat_new( ic, id );
633                                                g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id);
634                                                skype_write(ic, buf, strlen(buf));
635                                                g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id);
636                                                skype_write(ic, buf, strlen(buf));
637                                        }
638                                        else if(!strcmp(info, "STATUS DIALOG") && sd->groupchat_with)
639                                        {
640                                                gc = imcb_chat_new( ic, id );
641                                                /* According to the docs this
642                                                 * is necessary. However it
643                                                 * does not seem the situation
644                                                 * and it would open an extra
645                                                 * window on our client, so
646                                                 * just leave it out. */
647                                                /*g_snprintf(buf, 1024, "OPEN CHAT %s\n", id);
648                                                skype_write(ic, buf, strlen(buf));*/
649                                                g_snprintf(buf, 1024, "%s@skype.com", sd->groupchat_with);
650                                                imcb_chat_add_buddy(gc, buf);
651                                                imcb_chat_add_buddy(gc, sd->username);
652                                                g_free(sd->groupchat_with);
653                                                sd->groupchat_with = NULL;
654                                                g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id);
655                                                skype_write(ic, buf, strlen(buf));
656                                                g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id);
657                                                skype_write(ic, buf, strlen(buf));
658                                        }
659                                        else if(!strcmp(info, "STATUS UNSUBSCRIBED"))
660                                        {
661                                                gc = skype_chat_by_name(ic, id);
662                                                if(gc)
663                                                        gc->data = (void*)FALSE;
664                                        }
665                                        else if(!strncmp(info, "ADDER ", 6))
666                                        {
667                                                info += 6;
668                                                g_free(sd->adder);
669                                                sd->adder = g_strdup_printf("%s@skype.com", info);
670                                        }
671                                        else if(!strncmp(info, "TOPIC ", 6))
672                                        {
673                                                info += 6;
674                                                gc = skype_chat_by_name(ic, id);
675                                                if(gc && (sd->adder || sd->topic_wait))
676                                                {
677                                                        if(sd->topic_wait)
678                                                        {
679                                                                sd->adder = g_strdup(sd->username);
680                                                                sd->topic_wait = 0;
681                                                        }
682                                                        imcb_chat_topic(gc, sd->adder, info, 0);
683                                                        g_free(sd->adder);
684                                                        sd->adder = NULL;
685                                                }
686                                        }
687                                        else if(!strncmp(info, "ACTIVEMEMBERS ", 14))
688                                        {
689                                                info += 14;
690                                                gc = skype_chat_by_name(ic, id);
691                                                /* Hack! We set ->data to TRUE
692                                                 * while we're on the channel
693                                                 * so that we won't rejoin
694                                                 * after a /part. */
695                                                if(gc && !gc->data)
696                                                {
697                                                        char **members = g_strsplit(info, " ", 0);
698                                                        int i;
699                                                        for(i=0;members[i];i++)
700                                                        {
701                                                                if(!strcmp(members[i], sd->username))
702                                                                        continue;
703                                                                g_snprintf(buf, 1024, "%s@skype.com", members[i]);
704                                                                if(!g_list_find_custom(gc->in_room, buf, (GCompareFunc)strcmp))
705                                                                        imcb_chat_add_buddy(gc, buf);
706                                                        }
707                                                        imcb_chat_add_buddy(gc, sd->username);
708                                                        g_strfreev(members);
709                                                }
710                                        }
711                                }
712                        }
713                        lineptr++;
714                }
715                g_strfreev(lines);
716        }
717        else if( st == 0 || ( st < 0 && !sockerr_again() ) )
718        {
719                closesocket( sd->fd );
720                sd->fd = -1;
721
722                imcb_error( ic, "Error while reading from server" );
723                imc_logout( ic, TRUE );
724                return FALSE;
725        }
726        return TRUE;
727}
728
729gboolean skype_start_stream( struct im_connection *ic )
730{
731        struct skype_data *sd = ic->proto_data;
732        char *buf;
733        int st;
734
735        if(!sd)
736                return FALSE;
737
738        if( sd->bfd <= 0 )
739                sd->bfd = b_input_add( sd->fd, GAIM_INPUT_READ, skype_read_callback, ic );
740
741        /* This will download all buddies. */
742        buf = g_strdup_printf("SEARCH FRIENDS\n");
743        st = skype_write( ic, buf, strlen( buf ) );
744        g_free(buf);
745        buf = g_strdup_printf("SET USERSTATUS ONLINE\n");
746        skype_write( ic, buf, strlen( buf ) );
747        g_free(buf);
748        return st;
749}
750
751gboolean skype_connected( gpointer data, gint source, b_input_condition cond )
752{
753        struct im_connection *ic = data;
754        imcb_connected(ic);
755        return skype_start_stream(ic);
756}
757
758static void skype_login( account_t *acc )
759{
760        struct im_connection *ic = imcb_new( acc );
761        struct skype_data *sd = g_new0( struct skype_data, 1 );
762
763        ic->proto_data = sd;
764
765        imcb_log( ic, "Connecting" );
766        sd->fd = proxy_connect(acc->server, set_getint( &acc->set, "port" ), skype_connected, ic );
767        sd->username = g_strdup( acc->user );
768
769        sd->ic = ic;
770}
771
772static void skype_logout( struct im_connection *ic )
773{
774        struct skype_data *sd = ic->proto_data;
775        char *buf;
776
777        buf = g_strdup_printf("SET USERSTATUS OFFLINE\n");
778        skype_write( ic, buf, strlen( buf ) );
779        g_free(buf);
780
781        g_free(sd->username);
782        g_free(sd->handle);
783        g_free(sd);
784        ic->proto_data = NULL;
785}
786
787static int skype_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
788{
789        char *buf, *ptr, *nick;
790        int st;
791
792        nick = g_strdup(who);
793        ptr = strchr(nick, '@');
794        if(ptr)
795                *ptr = '\0';
796
797        buf = g_strdup_printf("MESSAGE %s %s\n", nick, message);
798        g_free(nick);
799        st = skype_write( ic, buf, strlen( buf ) );
800        g_free(buf);
801
802        return st;
803}
804
805const struct skype_away_state *skype_away_state_by_name( char *name )
806{
807        int i;
808
809        for( i = 0; skype_away_state_list[i].full_name; i ++ )
810                if( g_strcasecmp( skype_away_state_list[i].full_name, name ) == 0 )
811                        return( skype_away_state_list + i );
812
813        return NULL;
814}
815
816static void skype_set_away( struct im_connection *ic, char *state_txt, char *message )
817{
818        const struct skype_away_state *state;
819        char *buf;
820
821        if( strcmp( state_txt, GAIM_AWAY_CUSTOM ) == 0 )
822                state = skype_away_state_by_name( "Away" );
823        else
824                state = skype_away_state_by_name( state_txt );
825        buf = g_strdup_printf("SET USERSTATUS %s\n", state->code);
826        skype_write( ic, buf, strlen( buf ) );
827        g_free(buf);
828}
829
830static GList *skype_away_states( struct im_connection *ic )
831{
832        static GList *l = NULL;
833        int i;
834       
835        if( l == NULL )
836                for( i = 0; skype_away_state_list[i].full_name; i ++ )
837                        l = g_list_append( l, (void*) skype_away_state_list[i].full_name );
838       
839        return l;
840}
841
842static void skype_add_buddy( struct im_connection *ic, char *who, char *group )
843{
844        char *buf, *nick, *ptr;
845
846        nick = g_strdup(who);
847        ptr = strchr(nick, '@');
848        if(ptr)
849                *ptr = '\0';
850        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n", nick);
851        skype_write( ic, buf, strlen( buf ) );
852        g_free(nick);
853}
854
855static void skype_remove_buddy( struct im_connection *ic, char *who, char *group )
856{
857        char *buf, *nick, *ptr;
858
859        nick = g_strdup(who);
860        ptr = strchr(nick, '@');
861        if(ptr)
862                *ptr = '\0';
863        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick);
864        skype_write( ic, buf, strlen( buf ) );
865        g_free(nick);
866}
867
868void skype_chat_msg( struct groupchat *gc, char *message, int flags )
869{
870        struct im_connection *ic = gc->ic;
871        char *buf;
872        buf = g_strdup_printf("CHATMESSAGE %s %s\n", gc->title, message);
873        skype_write( ic, buf, strlen( buf ) );
874        g_free(buf);
875}
876
877void skype_chat_leave( struct groupchat *gc )
878{
879        struct im_connection *ic = gc->ic;
880        char *buf;
881        buf = g_strdup_printf("ALTER CHAT %s LEAVE\n", gc->title);
882        skype_write( ic, buf, strlen( buf ) );
883        g_free(buf);
884        gc->data = (void*)TRUE;
885}
886
887void skype_chat_invite(struct groupchat *gc, char *who, char *message)
888{
889        struct im_connection *ic = gc->ic;
890        char *buf, *ptr, *nick;
891        nick = g_strdup(message);
892        ptr = strchr(nick, '@');
893        if(ptr)
894                *ptr = '\0';
895        buf = g_strdup_printf("ALTER CHAT %s ADDMEMBERS %s\n", gc->title, nick);
896        skype_write( ic, buf, strlen( buf ) );
897        g_free(buf);
898        g_free(nick);
899}
900
901void skype_chat_topic(struct groupchat *gc, char *message)
902{
903        struct im_connection *ic = gc->ic;
904        struct skype_data *sd = ic->proto_data;
905        char *buf;
906        buf = g_strdup_printf("ALTER CHAT %s SETTOPIC %s\n", gc->title, message);
907        skype_write( ic, buf, strlen( buf ) );
908        g_free(buf);
909        sd->topic_wait = 1;
910}
911
912struct groupchat *skype_chat_with(struct im_connection *ic, char *who)
913{
914        struct skype_data *sd = ic->proto_data;
915        char *ptr, *nick, *buf;
916        nick = g_strdup(who);
917        ptr = strchr(nick, '@');
918        if(ptr)
919                *ptr = '\0';
920        buf = g_strdup_printf("CHAT CREATE %s\n", nick);
921        skype_write(ic, buf, strlen(buf));
922        g_free(buf);
923        sd->groupchat_with = g_strdup(nick);
924        g_free(nick);
925        /* We create a fake chat for now. We will replace it with a real one in
926         * the real callback. */
927        return(imcb_chat_new( ic, "" ));
928}
929
930static void skype_get_info(struct im_connection *ic, char *who)
931{
932        char *ptr, *nick, *buf;
933        nick = g_strdup(who);
934        ptr = strchr(nick, '@');
935        if(ptr)
936                *ptr = '\0';
937        buf = g_strdup_printf("GET USER %s FULLNAME\n", nick);
938        skype_write(ic, buf, strlen(buf));
939        g_free(buf);
940        buf = g_strdup_printf("GET USER %s PHONE_HOME\n", nick);
941        skype_write(ic, buf, strlen(buf));
942        g_free(buf);
943        buf = g_strdup_printf("GET USER %s PHONE_OFFICE\n", nick);
944        skype_write(ic, buf, strlen(buf));
945        g_free(buf);
946        buf = g_strdup_printf("GET USER %s PHONE_MOBILE\n", nick);
947        skype_write(ic, buf, strlen(buf));
948        g_free(buf);
949        buf = g_strdup_printf("GET USER %s NROF_AUTHED_BUDDIES\n", nick);
950        skype_write(ic, buf, strlen(buf));
951        g_free(buf);
952        buf = g_strdup_printf("GET USER %s TIMEZONE\n", nick);
953        skype_write(ic, buf, strlen(buf));
954        g_free(buf);
955        buf = g_strdup_printf("GET USER %s LASTONLINETIMESTAMP\n", nick);
956        skype_write(ic, buf, strlen(buf));
957        g_free(buf);
958        buf = g_strdup_printf("GET USER %s BIRTHDAY\n", nick);
959        skype_write(ic, buf, strlen(buf));
960        g_free(buf);
961        buf = g_strdup_printf("GET USER %s SEX\n", nick);
962        skype_write(ic, buf, strlen(buf));
963        g_free(buf);
964        buf = g_strdup_printf("GET USER %s LANGUAGE\n", nick);
965        skype_write(ic, buf, strlen(buf));
966        g_free(buf);
967        buf = g_strdup_printf("GET USER %s COUNTRY\n", nick);
968        skype_write(ic, buf, strlen(buf));
969        g_free(buf);
970        buf = g_strdup_printf("GET USER %s PROVINCE\n", nick);
971        skype_write(ic, buf, strlen(buf));
972        g_free(buf);
973        buf = g_strdup_printf("GET USER %s CITY\n", nick);
974        skype_write(ic, buf, strlen(buf));
975        g_free(buf);
976        buf = g_strdup_printf("GET USER %s HOMEPAGE\n", nick);
977        skype_write(ic, buf, strlen(buf));
978        g_free(buf);
979        buf = g_strdup_printf("GET USER %s ABOUT\n", nick);
980        skype_write(ic, buf, strlen(buf));
981        g_free(buf);
982}
983
984void init_plugin(void)
985{
986        struct prpl *ret = g_new0( struct prpl, 1 );
987
988        ret->name = "skype";
989        ret->login = skype_login;
990        ret->init = skype_init;
991        ret->logout = skype_logout;
992        ret->buddy_msg = skype_buddy_msg;
993        ret->get_info = skype_get_info;
994        ret->away_states = skype_away_states;
995        ret->set_away = skype_set_away;
996        ret->add_buddy = skype_add_buddy;
997        ret->remove_buddy = skype_remove_buddy;
998        ret->chat_msg = skype_chat_msg;
999        ret->chat_leave = skype_chat_leave;
1000        ret->chat_invite = skype_chat_invite;
1001        ret->chat_with = skype_chat_with;
1002        ret->handle_cmp = g_strcasecmp;
1003        ret->chat_topic = skype_chat_topic;
1004        register_protocol( ret );
1005}
Note: See TracBrowser for help on using the repository browser.