source: skype/skype.c @ 93dffea

Last change on this file since 93dffea was 93dffea, checked in by Miklos Vajna <vmiklos@…>, at 2008-01-10T16:10:10Z

new skype_set_display_name() function

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