source: skype/skype.c @ d7938f9

Last change on this file since d7938f9 was d7938f9, checked in by Miklos Vajna <vmiklos@…>, at 2009-02-13T22:36:27Z

g_strdup_printf("%s", foo) -> g_strdup(foo)

  • Property mode set to 100644
File size: 33.8 KB
RevLine 
[7daec06]1/*
2 *  skype.c - Skype plugin for BitlBee
[5adcc65]3 *
[5258dcc9]4 *  Copyright (c) 2007, 2008, 2009 by Miklos Vajna <vmiklos@frugalware.org>
[f06e3ac]5 *
[7daec06]6 *  This program is free software; you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation; either version 2 of the License, or
9 *  (at your option) any later version.
10 *
11 *  This program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with this program; if not, write to the Free Software
[5adcc65]18 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
[7daec06]19 *  USA.
[f06e3ac]20 */
[7daec06]21
[f9c3e7b]22#define _XOPEN_SOURCE
[ed2e37f]23#include <poll.h>
[f06e3ac]24#include <bitlbee.h>
[c7304b2]25#include <bitlbee/ssl_client.h>
[f06e3ac]26
[f5aedd91]27#define SKYPE_DEFAULT_SERVER "localhost"
[4bbd9db]28#define SKYPE_DEFAULT_PORT "2727"
[ff436ba]29#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
[f06e3ac]30
[bbba374]31/*
32 * Enumerations
33 */
34
[359f4d9]35enum {
[df9255d]36        SKYPE_CALL_RINGING = 1,
[9db0234]37        SKYPE_CALL_MISSED,
[2eb4b1f]38        SKYPE_CALL_CANCELLED,
[acd9478]39        SKYPE_CALL_FINISHED,
[d87daf3]40        SKYPE_CALL_REFUSED
[5365f84]41};
[bbba374]42
[359f4d9]43enum {
[df9255d]44        SKYPE_FILETRANSFER_NEW = 1,
45        SKYPE_FILETRANSFER_FAILED
[5365f84]46};
[df9255d]47
[7daec06]48/*
49 * Structures
50 */
51
[5adcc65]52struct skype_data {
[f06e3ac]53        struct im_connection *ic;
[a3d6427]54        char *username;
[368861e]55        /* The effective file descriptor. We store it here so any function can
56         * write() to it. */
[f06e3ac]57        int fd;
[368861e]58        /* File descriptor returned by bitlbee. we store it so we know when
59         * we're connected and when we aren't. */
60        int bfd;
[c7304b2]61        /* ssl_getfd() uses this to get the file desciptor. */
62        void *ssl;
[2a0f99c]63        /* When we receive a new message id, we query the properties, finally
64         * the chatname. Store the properties here so that we can use
[c81d0ef]65         * imcb_buddy_msg() when we got the chatname. */
[77c1abe]66        char *handle;
[a7af5f0]67        /* List, because of multiline messages. */
68        GList *body;
[2a0f99c]69        char *type;
[bbba374]70        /* This is necessary because we send a notification when we get the
71         * handle. So we store the state here and then we can send a
72         * notification about the handle is in a given status. */
[359f4d9]73        int call_status;
[2eb4b1f]74        char *call_id;
[48181f0]75        char *call_duration;
[d87daf3]76        /* If the call is outgoing or not */
77        int call_out;
[df9255d]78        /* Same for file transfers. */
[359f4d9]79        int filetransfer_status;
[86278cd]80        /* Using /j #nick we want to have a groupchat with two people. Usually
81         * not (default). */
[5adcc65]82        char *groupchat_with;
[f8674db]83        /* The user who invited us to the chat. */
[5adcc65]84        char *adder;
[a5f76a2]85        /* If we are waiting for a confirmation about we changed the topic. */
86        int topic_wait;
[67454bd]87        /* These are used by the info command. */
88        char *info_fullname;
89        char *info_phonehome;
90        char *info_phoneoffice;
91        char *info_phonemobile;
92        char *info_nrbuddies;
93        char *info_tz;
94        char *info_seen;
95        char *info_birthday;
96        char *info_sex;
97        char *info_language;
98        char *info_country;
99        char *info_province;
100        char *info_city;
101        char *info_homepage;
102        char *info_about;
[b054fad]103        /* When a call fails, we get the reason and later we get the failure
104         * event, so store the failure code here till then */
105        int failurereason;
[f06e3ac]106};
107
[5adcc65]108struct skype_away_state {
[adce2de]109        char *code;
110        char *full_name;
111};
112
[5adcc65]113struct skype_buddy_ask_data {
[7daec06]114        struct im_connection *ic;
[e0074cb]115        /* This is also used for call IDs for simplicity */
[7daec06]116        char *handle;
117};
118
119/*
120 * Tables
121 */
122
[5adcc65]123const struct skype_away_state skype_away_state_list[] = {
[bc744df]124        { "ONLINE", "Online" },
125        { "SKYPEME", "Skype Me" },
126        { "AWAY", "Away" },
127        { "NA", "Not available" },
128        { "DND", "Do Not Disturb" },
129        { "INVISIBLE", "Invisible" },
130        { "OFFLINE", "Offline" },
[23411c6]131        { NULL, NULL}
[adce2de]132};
133
[7daec06]134/*
135 * Functions
136 */
[d3cbd17]137
[5adcc65]138int skype_write(struct im_connection *ic, char *buf)
[f06e3ac]139{
140        struct skype_data *sd = ic->proto_data;
[1fb89e3]141        struct pollfd pfd[1];
[a5f4040]142        int len = strlen(buf);
[1fb89e3]143
144        pfd[0].fd = sd->fd;
145        pfd[0].events = POLLOUT;
[f06e3ac]146
[7daec06]147        /* This poll is necessary or we'll get a SIGPIPE when we write() to
148         * sd->fd. */
[1fb89e3]149        poll(pfd, 1, 1000);
[5adcc65]150        if (pfd[0].revents & POLLHUP) {
151                imc_logout(ic, TRUE);
[1fb89e3]152                return FALSE;
153        }
[5adcc65]154        ssl_write(sd->ssl, buf, len);
[f06e3ac]155
[9fd4241]156        return TRUE;
[f06e3ac]157}
158
[5adcc65]159static void skype_buddy_ask_yes(void *data)
[d3cbd17]160{
[039116a]161        struct skype_buddy_ask_data *bla = data;
[8c09bb3]162        char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED TRUE",
163                bla->handle);
[5adcc65]164        skype_write(bla->ic, buf);
[d3cbd17]165        g_free(buf);
166        g_free(bla->handle);
167        g_free(bla);
168}
169
[5adcc65]170static void skype_buddy_ask_no(void *data)
[d3cbd17]171{
[039116a]172        struct skype_buddy_ask_data *bla = data;
[8c09bb3]173        char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED FALSE",
174                bla->handle);
[5adcc65]175        skype_write(bla->ic, buf);
[d3cbd17]176        g_free(buf);
177        g_free(bla->handle);
178        g_free(bla);
179}
180
[5adcc65]181void skype_buddy_ask(struct im_connection *ic, char *handle, char *message)
[d3cbd17]182{
[8c09bb3]183        struct skype_buddy_ask_data *bla = g_new0(struct skype_buddy_ask_data,
184                1);
[d3cbd17]185        char *buf;
186
187        bla->ic = ic;
188        bla->handle = g_strdup(handle);
189
[8c09bb3]190        buf = g_strdup_printf("The user %s wants to add you to "
191                "his/her buddy list, saying: '%s'.", handle, message);
[5adcc65]192        imcb_ask(ic, buf, bla, skype_buddy_ask_yes, skype_buddy_ask_no);
193        g_free(buf);
[d3cbd17]194}
195
[5adcc65]196static void skype_call_ask_yes(void *data)
[e0074cb]197{
[039116a]198        struct skype_buddy_ask_data *bla = data;
[8c09bb3]199        char *buf = g_strdup_printf("SET CALL %s STATUS INPROGRESS",
200                bla->handle);
[5adcc65]201        skype_write(bla->ic, buf);
[e0074cb]202        g_free(buf);
203        g_free(bla->handle);
204        g_free(bla);
205}
206
[5adcc65]207static void skype_call_ask_no(void *data)
[e0074cb]208{
[039116a]209        struct skype_buddy_ask_data *bla = data;
[8c09bb3]210        char *buf = g_strdup_printf("SET CALL %s STATUS FINISHED",
211                bla->handle);
[5adcc65]212        skype_write(bla->ic, buf);
[e0074cb]213        g_free(buf);
214        g_free(bla->handle);
215        g_free(bla);
216}
217
[5adcc65]218void skype_call_ask(struct im_connection *ic, char *call_id, char *message)
[e0074cb]219{
[8c09bb3]220        struct skype_buddy_ask_data *bla = g_new0(struct skype_buddy_ask_data,
221                1);
[e0074cb]222
223        bla->ic = ic;
224        bla->handle = g_strdup(call_id);
225
[5adcc65]226        imcb_ask(ic, message, bla, skype_call_ask_yes, skype_call_ask_no);
[e0074cb]227}
[5adcc65]228struct groupchat *skype_chat_by_name(struct im_connection *ic, char *name)
[72aa7f0]229{
230        struct groupchat *ret;
231
[5adcc65]232        for (ret = ic->groupchats; ret; ret = ret->next)
233                if (strcmp(name, ret->title) == 0)
[72aa7f0]234                        break;
235
236        return ret;
237}
238
[b054fad]239static char *skype_call_strerror(int err)
240{
[5adcc65]241        switch (err) {
242        case 1:
243                return "Miscellaneous error";
244        case 2:
245                return "User or phone number does not exist.";
246        case 3:
247                return "User is offline";
248        case 4:
249                return "No proxy found";
250        case 5:
251                return "Session terminated.";
252        case 6:
253                return "No common codec found.";
254        case 7:
255                return "Sound I/O error.";
256        case 8:
257                return "Problem with remote sound device.";
258        case 9:
259                return "Call blocked by recipient.";
260        case 10:
261                return "Recipient not a friend.";
262        case 11:
263                return "Current user not authorized by recipient.";
264        case 12:
265                return "Sound recording error.";
266        default:
267                return "Unknown error";
[b054fad]268        }
269}
270
[078b0b9]271static void skype_parse_users(struct im_connection *ic, char *line)
272{
273        char **i, **nicks, *ptr;
274
275        nicks = g_strsplit(line + 6, ", ", 0);
[8bbe52a]276        for (i = nicks; *i; i++) {
[078b0b9]277                ptr = g_strdup_printf("GET USER %s ONLINESTATUS\n", *i);
278                skype_write(ic, ptr);
279                g_free(ptr);
280        }
281        g_strfreev(nicks);
282}
283
[6e14204]284static void skype_parse_user(struct im_connection *ic, char *line)
285{
286        int flags = 0;
287        char *ptr;
288        struct skype_data *sd = ic->proto_data;
289        char *user = strchr(line, ' ');
290        char *status = strrchr(line, ' ');
291
292        status++;
293        ptr = strchr(++user, ' ');
294        if (!ptr)
295                return;
296        *ptr = '\0';
297        ptr++;
298        if (!strncmp(ptr, "ONLINESTATUS ", 13) &&
299                        strcmp(user, sd->username) != 0
300                        && strcmp(user, "echo123") != 0) {
301                ptr = g_strdup_printf("%s@skype.com", user);
302                imcb_add_buddy(ic, ptr, NULL);
[62f51ee9]303                if (strcmp(status, "OFFLINE") && (strcmp(status, "SKYPEOUT") ||
304                        !set_getbool(&ic->acc->set, "skypeout_offline")))
[6e14204]305                        flags |= OPT_LOGGED_IN;
[62f51ee9]306                if (strcmp(status, "ONLINE") && strcmp(status, "SKYPEME"))
[6e14204]307                        flags |= OPT_AWAY;
308                imcb_buddy_status(ic, ptr, flags, NULL, NULL);
309                g_free(ptr);
310        } else if (!strncmp(ptr, "RECEIVEDAUTHREQUEST ", 20)) {
311                char *message = ptr + 20;
312                if (strlen(message))
313                        skype_buddy_ask(ic, user, message);
314        } else if (!strncmp(ptr, "BUDDYSTATUS ", 12)) {
315                char *st = ptr + 12;
316                if (!strcmp(st, "3")) {
317                        char *buf = g_strdup_printf("%s@skype.com", user);
318                        imcb_add_buddy(ic, buf, NULL);
319                        g_free(buf);
320                }
321        } else if (!strncmp(ptr, "FULLNAME ", 9))
[d7938f9]322                sd->info_fullname = g_strdup(ptr + 9);
[6e14204]323        else if (!strncmp(ptr, "PHONE_HOME ", 11))
[d7938f9]324                sd->info_phonehome = g_strdup(ptr + 11);
[6e14204]325        else if (!strncmp(ptr, "PHONE_OFFICE ", 13))
[d7938f9]326                sd->info_phoneoffice = g_strdup(ptr + 13);
[6e14204]327        else if (!strncmp(ptr, "PHONE_MOBILE ", 13))
[d7938f9]328                sd->info_phonemobile = g_strdup(ptr + 13);
[6e14204]329        else if (!strncmp(ptr, "NROF_AUTHED_BUDDIES ", 20))
[d7938f9]330                sd->info_nrbuddies = g_strdup(ptr + 20);
[6e14204]331        else if (!strncmp(ptr, "TIMEZONE ", 9))
[d7938f9]332                sd->info_tz = g_strdup(ptr + 9);
[6e14204]333        else if (!strncmp(ptr, "LASTONLINETIMESTAMP ", 20))
[d7938f9]334                sd->info_seen = g_strdup(ptr + 20);
[6e14204]335        else if (!strncmp(ptr, "BIRTHDAY ", 9))
[d7938f9]336                sd->info_birthday = g_strdup(ptr + 9);
[6e14204]337        else if (!strncmp(ptr, "SEX ", 4))
[d7938f9]338                sd->info_sex = g_strdup(ptr + 4);
[6e14204]339        else if (!strncmp(ptr, "LANGUAGE ", 9))
[d7938f9]340                sd->info_language = g_strdup(ptr + 9);
[6e14204]341        else if (!strncmp(ptr, "COUNTRY ", 8))
[d7938f9]342                sd->info_country = g_strdup(ptr + 8);
[6e14204]343        else if (!strncmp(ptr, "PROVINCE ", 9))
[d7938f9]344                sd->info_province = g_strdup(ptr + 9);
[6e14204]345        else if (!strncmp(ptr, "CITY ", 5))
[d7938f9]346                sd->info_city = g_strdup(ptr + 5);
[6e14204]347        else if (!strncmp(ptr, "HOMEPAGE ", 9))
[d7938f9]348                sd->info_homepage = g_strdup(ptr + 9);
[6e14204]349        else if (!strncmp(ptr, "ABOUT ", 6)) {
[d7938f9]350                sd->info_about = g_strdup(ptr + 6);
[6e14204]351
352                GString *st = g_string_new("Contact Information\n");
353                g_string_append_printf(st, "Skype Name: %s\n", user);
354                if (sd->info_fullname) {
355                        if (strlen(sd->info_fullname))
[62f51ee9]356                                g_string_append_printf(st, "Full Name: %s\n",
357                                        sd->info_fullname);
[6e14204]358                        g_free(sd->info_fullname);
359                }
360                if (sd->info_phonehome) {
361                        if (strlen(sd->info_phonehome))
[62f51ee9]362                                g_string_append_printf(st, "Home Phone: %s\n",
363                                        sd->info_phonehome);
[6e14204]364                        g_free(sd->info_phonehome);
365                }
366                if (sd->info_phoneoffice) {
367                        if (strlen(sd->info_phoneoffice))
[62f51ee9]368                                g_string_append_printf(st, "Office Phone: %s\n",
369                                        sd->info_phoneoffice);
[6e14204]370                        g_free(sd->info_phoneoffice);
371                }
372                if (sd->info_phonemobile) {
373                        if (strlen(sd->info_phonemobile))
[62f51ee9]374                                g_string_append_printf(st, "Mobile Phone: %s\n",
375                                        sd->info_phonemobile);
[6e14204]376                        g_free(sd->info_phonemobile);
377                }
378                g_string_append_printf(st, "Personal Information\n");
379                if (sd->info_nrbuddies) {
380                        if (strlen(sd->info_nrbuddies))
[62f51ee9]381                                g_string_append_printf(st,
382                                        "Contacts: %s\n", sd->info_nrbuddies);
[6e14204]383                        g_free(sd->info_nrbuddies);
384                }
385                if (sd->info_tz) {
386                        if (strlen(sd->info_tz)) {
387                                char ib[256];
388                                time_t t = time(NULL);
389                                t += atoi(sd->info_tz)-(60*60*24);
390                                struct tm *gt = gmtime(&t);
391                                strftime(ib, 256, "%H:%M:%S", gt);
[62f51ee9]392                                g_string_append_printf(st,
393                                        "Local Time: %s\n", ib);
[6e14204]394                        }
395                        g_free(sd->info_tz);
396                }
397                if (sd->info_seen) {
398                        if (strlen(sd->info_seen)) {
399                                char ib[256];
400                                time_t it = atoi(sd->info_seen);
401                                struct tm *tm = localtime(&it);
402                                strftime(ib, 256, ("%Y. %m. %d. %H:%M"), tm);
[62f51ee9]403                                g_string_append_printf(st,
404                                        "Last Seen: %s\n", ib);
[6e14204]405                        }
406                        g_free(sd->info_seen);
407                }
408                if (sd->info_birthday) {
[62f51ee9]409                        if (strlen(sd->info_birthday) &&
410                                strcmp(sd->info_birthday, "0")) {
[6e14204]411                                char ib[256];
412                                struct tm tm;
413                                strptime(sd->info_birthday, "%Y%m%d", &tm);
414                                strftime(ib, 256, "%B %d, %Y", &tm);
[62f51ee9]415                                g_string_append_printf(st,
416                                        "Birthday: %s\n", ib);
[6e14204]417
418                                strftime(ib, 256, "%Y", &tm);
419                                int year = atoi(ib);
420                                time_t t = time(NULL);
421                                struct tm *lt = localtime(&t);
[62f51ee9]422                                g_string_append_printf(st,
423                                        "Age: %d\n", lt->tm_year+1900-year);
[6e14204]424                        }
425                        g_free(sd->info_birthday);
426                }
427                if (sd->info_sex) {
428                        if (strlen(sd->info_sex)) {
429                                char *iptr = sd->info_sex;
430                                while (*iptr++)
431                                        *iptr = tolower(*iptr);
[62f51ee9]432                                g_string_append_printf(st,
433                                        "Gender: %s\n", sd->info_sex);
[6e14204]434                        }
435                        g_free(sd->info_sex);
436                }
437                if (sd->info_language) {
438                        if (strlen(sd->info_language)) {
439                                char *iptr = strchr(sd->info_language, ' ');
440                                if (iptr)
441                                        iptr++;
442                                else
443                                        iptr = sd->info_language;
[62f51ee9]444                                g_string_append_printf(st,
445                                        "Language: %s\n", iptr);
[6e14204]446                        }
447                        g_free(sd->info_language);
448                }
449                if (sd->info_country) {
450                        if (strlen(sd->info_country)) {
451                                char *iptr = strchr(sd->info_country, ' ');
452                                if (iptr)
453                                        iptr++;
454                                else
455                                        iptr = sd->info_country;
[62f51ee9]456                                g_string_append_printf(st,
457                                        "Country: %s\n", iptr);
[6e14204]458                        }
459                        g_free(sd->info_country);
460                }
461                if (sd->info_province) {
462                        if (strlen(sd->info_province))
[62f51ee9]463                                g_string_append_printf(st,
464                                        "Region: %s\n", sd->info_province);
[6e14204]465                        g_free(sd->info_province);
466                }
467                if (sd->info_city) {
468                        if (strlen(sd->info_city))
[62f51ee9]469                                g_string_append_printf(st,
470                                        "City: %s\n", sd->info_city);
[6e14204]471                        g_free(sd->info_city);
472                }
473                if (sd->info_homepage) {
474                        if (strlen(sd->info_homepage))
[62f51ee9]475                                g_string_append_printf(st,
476                                        "Homepage: %s\n", sd->info_homepage);
[6e14204]477                        g_free(sd->info_homepage);
478                }
479                if (sd->info_about) {
480                        if (strlen(sd->info_about))
[62f51ee9]481                                g_string_append_printf(st, "%s\n",
482                                        sd->info_about);
[6e14204]483                        g_free(sd->info_about);
484                }
485                imcb_log(ic, "%s", st->str);
486                g_string_free(st, TRUE);
487        }
488}
489
490static void skype_parse_chatmessage(struct im_connection *ic, char *line)
491{
492        struct skype_data *sd = ic->proto_data;
493        char buf[1024];
494        char *id = strchr(line, ' ');
495
[6b9d22a]496        if (!++id)
497                return;
498        char *info = strchr(id, ' ');
499
500        if (!info)
501                return;
502        *info = '\0';
503        info++;
504        if (!strcmp(info, "STATUS RECEIVED")) {
505                /* New message ID:
506                 * (1) Request its from field
507                 * (2) Request its body
508                 * (3) Request its type
509                 * (4) Query chatname
510                 */
511                g_snprintf(buf, 1024, "GET CHATMESSAGE %s FROM_HANDLE\n", id);
512                skype_write(ic, buf);
513                g_snprintf(buf, 1024, "GET CHATMESSAGE %s BODY\n", id);
514                skype_write(ic, buf);
515                g_snprintf(buf, 1024, "GET CHATMESSAGE %s TYPE\n", id);
516                skype_write(ic, buf);
517                g_snprintf(buf, 1024, "GET CHATMESSAGE %s CHATNAME\n", id);
518                skype_write(ic, buf);
519        } else if (!strncmp(info, "FROM_HANDLE ", 12)) {
520                info += 12;
521                /* New from field value. Store
522                 * it, then we can later use it
523                 * when we got the message's
524                 * body. */
525                g_free(sd->handle);
526                sd->handle = g_strdup_printf("%s@skype.com", info);
527        } else if (!strncmp(info, "EDITED_BY ", 10)) {
528                info += 10;
529                /* This is the same as
530                 * FROM_HANDLE, except that we
531                 * never request these lines
532                 * from Skype, we just get
533                 * them. */
534                g_free(sd->handle);
535                sd->handle = g_strdup_printf("%s@skype.com", info);
536        } else if (!strncmp(info, "BODY ", 5)) {
537                info += 5;
538                sd->body = g_list_append(sd->body, g_strdup(info));
539        }       else if (!strncmp(info, "TYPE ", 5)) {
540                info += 5;
541                g_free(sd->type);
542                sd->type = g_strdup(info);
543        } else if (!strncmp(info, "CHATNAME ", 9)) {
544                info += 9;
545                if (sd->handle && sd->body && sd->type) {
546                        struct groupchat *gc = skype_chat_by_name(ic, info);
547                        int i;
548                        for (i = 0; i < g_list_length(sd->body); i++) {
549                                char *body = g_list_nth_data(sd->body, i);
[62f51ee9]550                                if (!strcmp(sd->type, "SAID") ||
551                                        !strcmp(sd->type, "EMOTED")) {
[6b9d22a]552                                        if (!strcmp(sd->type, "SAID"))
[62f51ee9]553                                                g_snprintf(buf, 1024, "%s",
554                                                        body);
[6b9d22a]555                                        else
[62f51ee9]556                                                g_snprintf(buf, 1024, "/me %s",
557                                                        body);
[6b9d22a]558                                        if (!gc)
559                                                /* Private message */
[62f51ee9]560                                                imcb_buddy_msg(ic,
561                                                        sd->handle, buf, 0, 0);
[6b9d22a]562                                        else
563                                                /* Groupchat message */
[62f51ee9]564                                                imcb_chat_msg(gc,
565                                                        sd->handle, buf, 0, 0);
566                                } else if (!strcmp(sd->type, "SETTOPIC") && gc)
567                                        imcb_chat_topic(gc,
568                                                sd->handle, body, 0);
569                                else if (!strcmp(sd->type, "LEFT") && gc)
570                                        imcb_chat_remove_buddy(gc,
571                                                sd->handle, NULL);
[6e14204]572                        }
[6b9d22a]573                        g_list_free(sd->body);
574                        sd->body = NULL;
[6e14204]575                }
576        }
577}
578
[9f2f25f]579static void skype_parse_call(struct im_connection *ic, char *line)
580{
581        struct skype_data *sd = ic->proto_data;
582        char *id = strchr(line, ' ');
583        char buf[1024];
584
[6b9d22a]585        if (!++id)
586                return;
587        char *info = strchr(id, ' ');
588
589        if (!info)
590                return;
591        *info = '\0';
592        info++;
593        if (!strncmp(info, "FAILUREREASON ", 14))
594                sd->failurereason = atoi(strchr(info, ' '));
595        else if (!strcmp(info, "STATUS RINGING")) {
596                if (sd->call_id)
597                        g_free(sd->call_id);
598                sd->call_id = g_strdup(id);
599                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
600                skype_write(ic, buf);
601                sd->call_status = SKYPE_CALL_RINGING;
602        } else if (!strcmp(info, "STATUS MISSED")) {
603                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
604                skype_write(ic, buf);
605                sd->call_status = SKYPE_CALL_MISSED;
606        } else if (!strcmp(info, "STATUS CANCELLED")) {
607                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
608                skype_write(ic, buf);
609                sd->call_status = SKYPE_CALL_CANCELLED;
610        } else if (!strcmp(info, "STATUS FINISHED")) {
611                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
612                skype_write(ic, buf);
613                sd->call_status = SKYPE_CALL_FINISHED;
614        } else if (!strcmp(info, "STATUS REFUSED")) {
615                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
616                skype_write(ic, buf);
617                sd->call_status = SKYPE_CALL_REFUSED;
618        } else if (!strcmp(info, "STATUS UNPLACED")) {
619                if (sd->call_id)
620                        g_free(sd->call_id);
621                /* Save the ID for later usage (Cancel/Finish). */
622                sd->call_id = g_strdup(id);
623                sd->call_out = TRUE;
624        } else if (!strcmp(info, "STATUS FAILED")) {
[62f51ee9]625                imcb_error(ic, "Call failed: %s",
626                        skype_call_strerror(sd->failurereason));
[6b9d22a]627                sd->call_id = NULL;
628        } else if (!strncmp(info, "DURATION ", 9)) {
629                if (sd->call_duration)
630                        g_free(sd->call_duration);
631                sd->call_duration = g_strdup(info+9);
632        } else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
633                info += 15;
[62f51ee9]634                if (!sd->call_status)
635                        return;
636                switch (sd->call_status) {
637                case SKYPE_CALL_RINGING:
638                        if (sd->call_out)
639                                imcb_log(ic, "You are currently ringing "
640                                        "the user %s.", info);
641                        else {
642                                g_snprintf(buf, 1024,
643                                        "The user %s is currently ringing you.",
644                                        info);
645                                skype_call_ask(ic, sd->call_id, buf);
[9f2f25f]646                        }
[62f51ee9]647                        break;
648                case SKYPE_CALL_MISSED:
649                        imcb_log(ic, "You have missed a call from user %s.",
650                                info);
651                        break;
652                case SKYPE_CALL_CANCELLED:
653                        imcb_log(ic, "You cancelled the call to the user %s.",
654                                info);
[6b9d22a]655                        sd->call_status = 0;
[62f51ee9]656                        sd->call_out = FALSE;
657                        break;
658                case SKYPE_CALL_REFUSED:
659                        if (sd->call_out)
660                                imcb_log(ic, "The user %s refused the call.",
661                                        info);
662                        else
663                                imcb_log(ic,
664                                        "You refused the call from user %s.",
665                                        info);
666                        sd->call_out = FALSE;
667                        break;
668                case SKYPE_CALL_FINISHED:
669                        if (sd->call_duration)
670                                imcb_log(ic,
671                                        "You finished the call to the user %s "
672                                        "(duration: %s seconds).",
673                                        info, sd->call_duration);
674                        else
675                                imcb_log(ic,
676                                        "You finished the call to the user %s.",
677                                        info);
678                        sd->call_out = FALSE;
679                        break;
680                default:
681                        /* Don't be noisy, ignore other statuses for now. */
682                        break;
[9f2f25f]683                }
[62f51ee9]684                sd->call_status = 0;
[9f2f25f]685        }
686}
687
[e200daf]688static void skype_parse_filetransfer(struct im_connection *ic, char *line)
689{
690        struct skype_data *sd = ic->proto_data;
691        char buf[1024];
692        char *id = strchr(line, ' ');
693
[6b9d22a]694        if (!++id)
695                return;
696        char *info = strchr(id, ' ');
697
698        if (!info)
699                return;
700        *info = '\0';
701        info++;
702        if (!strcmp(info, "STATUS NEW")) {
[62f51ee9]703                g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n",
704                        id);
[6b9d22a]705                skype_write(ic, buf);
706                sd->filetransfer_status = SKYPE_FILETRANSFER_NEW;
707        } else if (!strcmp(info, "STATUS FAILED")) {
[62f51ee9]708                g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n",
709                        id);
[6b9d22a]710                skype_write(ic, buf);
711                sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED;
712        } else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
713                info += 15;
[62f51ee9]714                if (!sd->filetransfer_status)
715                        return;
716                switch (sd->filetransfer_status) {
717                case SKYPE_FILETRANSFER_NEW:
718                        imcb_log(ic, "The user %s offered a new file for you.",
719                                info);
720                        break;
721                case SKYPE_FILETRANSFER_FAILED:
722                        imcb_log(ic, "Failed to transfer file from user %s.",
723                                info);
724                        break;
[e200daf]725                }
[62f51ee9]726                sd->filetransfer_status = 0;
[e200daf]727        }
728}
729
[c35bf7a]730static void skype_parse_chat(struct im_connection *ic, char *line)
731{
732        struct skype_data *sd = ic->proto_data;
733        char buf[1024];
734        char *id = strchr(line, ' ');
735
[6b9d22a]736        if (!++id)
737                return;
738        struct groupchat *gc;
739        char *info = strchr(id, ' ');
[c35bf7a]740
[6b9d22a]741        if (!info)
742                return;
743        *info = '\0';
744        info++;
745        /* Remove fake chat if we created one in skype_chat_with() */
746        gc = skype_chat_by_name(ic, "");
747        if (gc)
748                imcb_chat_free(gc);
749        if (!strcmp(info, "STATUS MULTI_SUBSCRIBED")) {
750                imcb_chat_new(ic, id);
751                g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id);
752                skype_write(ic, buf);
753                g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id);
754                skype_write(ic, buf);
755        } else if (!strcmp(info, "STATUS DIALOG") && sd->groupchat_with) {
756                gc = imcb_chat_new(ic, id);
757                /* According to the docs this
758                 * is necessary. However it
759                 * does not seem the situation
760                 * and it would open an extra
761                 * window on our client, so
762                 * just leave it out. */
763                /*g_snprintf(buf, 1024, "OPEN CHAT %s\n", id);
764                  skype_write(ic, buf);*/
765                g_snprintf(buf, 1024, "%s@skype.com", sd->groupchat_with);
766                imcb_chat_add_buddy(gc, buf);
767                imcb_chat_add_buddy(gc, sd->username);
768                g_free(sd->groupchat_with);
769                sd->groupchat_with = NULL;
770                g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id);
771                skype_write(ic, buf);
772                g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id);
773                skype_write(ic, buf);
774        } else if (!strcmp(info, "STATUS UNSUBSCRIBED")) {
775                gc = skype_chat_by_name(ic, id);
[c35bf7a]776                if (gc)
[6b9d22a]777                        gc->data = (void *)FALSE;
778        } else if (!strncmp(info, "ADDER ", 6)) {
779                info += 6;
780                g_free(sd->adder);
781                sd->adder = g_strdup_printf("%s@skype.com", info);
782        } else if (!strncmp(info, "TOPIC ", 6)) {
783                info += 6;
784                gc = skype_chat_by_name(ic, id);
785                if (gc && (sd->adder || sd->topic_wait)) {
786                        if (sd->topic_wait) {
787                                sd->adder = g_strdup(sd->username);
788                                sd->topic_wait = 0;
[c35bf7a]789                        }
[6b9d22a]790                        imcb_chat_topic(gc, sd->adder, info, 0);
791                        g_free(sd->adder);
792                        sd->adder = NULL;
793                }
794        } else if (!strncmp(info, "ACTIVEMEMBERS ", 14)) {
795                info += 14;
796                gc = skype_chat_by_name(ic, id);
797                /* Hack! We set ->data to TRUE
798                 * while we're on the channel
799                 * so that we won't rejoin
800                 * after a /part. */
[62f51ee9]801                if (!gc || gc->data)
802                        return;
803                char **members = g_strsplit(info, " ", 0);
804                int i;
805                for (i = 0; members[i]; i++) {
806                        if (!strcmp(members[i], sd->username))
807                                continue;
808                        g_snprintf(buf, 1024, "%s@skype.com", members[i]);
809                        if (!g_list_find_custom(gc->in_room, buf,
810                                (GCompareFunc)strcmp))
811                                imcb_chat_add_buddy(gc, buf);
[c35bf7a]812                }
[62f51ee9]813                imcb_chat_add_buddy(gc, sd->username);
814                g_strfreev(members);
[c35bf7a]815        }
816}
817
[2709f4c]818static void skype_parse_password(struct im_connection *ic, char *line)
819{
820        if (!strncmp(line+9, "OK", 2))
821                imcb_connected(ic);
822        else {
823                imcb_error(ic, "Authentication Failed");
824                imc_logout(ic, TRUE);
825        }
826}
827
[607f5e3]828static void skype_parse_profile(struct im_connection *ic, char *line)
829{
830        imcb_log(ic, "SkypeOut balance value is '%s'.", line+21);
831}
832
833static void skype_parse_ping(struct im_connection *ic, char *line)
834{
835        skype_write(ic, "PONG\n");
836}
837
838static void skype_parse_chats(struct im_connection *ic, char *line)
839{
840        char buf[1024];
841        char **i;
842        char **chats = g_strsplit(line + 6, ", ", 0);
843
844        i = chats;
845        while (*i) {
846                g_snprintf(buf, 1024, "GET CHAT %s STATUS\n", *i);
847                skype_write(ic, buf);
848                g_snprintf(buf, 1024, "GET CHAT %s ACTIVEMEMBERS\n", *i);
849                skype_write(ic, buf);
850                i++;
851        }
852        g_strfreev(chats);
853}
854
[ff436ba]855typedef void (*skype_parser)(struct im_connection *ic, char *line);
856
[8c09bb3]857static gboolean skype_read_callback(gpointer data, gint fd,
858                                    b_input_condition cond)
[1323e36]859{
860        struct im_connection *ic = data;
861        struct skype_data *sd = ic->proto_data;
862        char buf[1024];
[ff436ba]863        int st, i;
[6e14204]864        char **lines, **lineptr, *line;
[ff436ba]865        static struct parse_map {
866                char *k;
867                skype_parser v;
868        } parsers[] = {
869                { "USERS ", skype_parse_users },
870                { "USER ", skype_parse_user },
871                { "CHATMESSAGE ", skype_parse_chatmessage },
872                { "CALL ", skype_parse_call },
873                { "FILETRANSFER ", skype_parse_filetransfer },
874                { "CHAT ", skype_parse_chat },
875                { "PASSWORD ", skype_parse_password },
876                { "PROFILE PSTN_BALANCE ", skype_parse_profile },
877                { "PING", skype_parse_ping },
878                { "CHATS ", skype_parse_chats },
879        };
[1323e36]880
[5adcc65]881        if (!sd || sd->fd == -1)
[1323e36]882                return FALSE;
[7daec06]883        /* Read the whole data. */
[5adcc65]884        st = ssl_read(sd->ssl, buf, sizeof(buf));
885        if (st > 0) {
[1323e36]886                buf[st] = '\0';
[7daec06]887                /* Then split it up to lines. */
[9fd4241]888                lines = g_strsplit(buf, "\n", 0);
889                lineptr = lines;
[5adcc65]890                while ((line = *lineptr)) {
891                        if (!strlen(line))
[9fd4241]892                                break;
[b820226]893                        if (set_getbool(&ic->acc->set, "skypeconsole_receive"))
894                                imcb_buddy_msg(ic, "skypeconsole", line, 0, 0);
[6b9d22a]895                        for (i = 0; i < ARRAY_SIZE(parsers); i++)
896                                if (!strncmp(line, parsers[i].k,
897                                        strlen(parsers[i].k))) {
[ff436ba]898                                        parsers[i].v(ic, line);
899                                        break;
900                                }
[9fd4241]901                        lineptr++;
902                }
903                g_strfreev(lines);
[5adcc65]904        } else if (st == 0 || (st < 0 && !sockerr_again())) {
905                closesocket(sd->fd);
[1323e36]906                sd->fd = -1;
907
[5adcc65]908                imcb_error(ic, "Error while reading from server");
909                imc_logout(ic, TRUE);
[1323e36]910                return FALSE;
911        }
912        return TRUE;
913}
914
[5adcc65]915gboolean skype_start_stream(struct im_connection *ic)
[f06e3ac]916{
[1323e36]917        struct skype_data *sd = ic->proto_data;
[f06e3ac]918        char *buf;
919        int st;
920
[5adcc65]921        if (!sd)
[1fb89e3]922                return FALSE;
923
[5adcc65]924        if (sd->bfd <= 0)
[8c09bb3]925                sd->bfd = b_input_add(sd->fd, GAIM_INPUT_READ,
926                        skype_read_callback, ic);
[1323e36]927
[a0b206b]928        /* Log in */
929        buf = g_strdup_printf("USERNAME %s\n", ic->acc->user);
[5adcc65]930        st = skype_write(ic, buf);
[a0b206b]931        g_free(buf);
932        buf = g_strdup_printf("PASSWORD %s\n", ic->acc->pass);
[5adcc65]933        st = skype_write(ic, buf);
[a0b206b]934        g_free(buf);
935
[7daec06]936        /* This will download all buddies. */
[9fd4241]937        buf = g_strdup_printf("SEARCH FRIENDS\n");
[5adcc65]938        st = skype_write(ic, buf);
[f06e3ac]939        g_free(buf);
[348a3a2]940        buf = g_strdup_printf("SET USERSTATUS ONLINE\n");
[5adcc65]941        skype_write(ic, buf);
[348a3a2]942        g_free(buf);
[5acf9ab]943
944        /* Auto join to bookmarked chats if requested.*/
945        if (set_getbool(&ic->acc->set, "auto_join")) {
946                buf = g_strdup_printf("SEARCH BOOKMARKEDCHATS\n");
[5adcc65]947                skype_write(ic, buf);
[5acf9ab]948                g_free(buf);
949        }
[f06e3ac]950        return st;
951}
952
[5adcc65]953gboolean skype_connected(gpointer data, void *source, b_input_condition cond)
[f06e3ac]954{
955        struct im_connection *ic = data;
[c7304b2]956        struct skype_data *sd = ic->proto_data;
[5adcc65]957        if (!source) {
[c7304b2]958                sd->ssl = NULL;
[5adcc65]959                imcb_error(ic, "Could not connect to server");
960                imc_logout(ic, TRUE);
[c7304b2]961                return FALSE;
962        }
[5adcc65]963        imcb_log(ic, "Connected to server, logging in");
[f06e3ac]964        return skype_start_stream(ic);
965}
966
[5adcc65]967static void skype_login(account_t *acc)
[f06e3ac]968{
[5adcc65]969        struct im_connection *ic = imcb_new(acc);
970        struct skype_data *sd = g_new0(struct skype_data, 1);
[f06e3ac]971
972        ic->proto_data = sd;
973
[5adcc65]974        imcb_log(ic, "Connecting");
[8c09bb3]975        sd->ssl = ssl_connect(set_getstr(&acc->set, "server"),
976                set_getint(&acc->set, "port"), skype_connected, ic);
[5adcc65]977        sd->fd = sd->ssl ? ssl_getfd(sd->ssl) : -1;
978        sd->username = g_strdup(acc->user);
[f06e3ac]979
980        sd->ic = ic;
[08a355b]981
982        if (set_getbool(&acc->set, "skypeconsole"))
983                imcb_add_buddy(ic, "skypeconsole", NULL);
[f06e3ac]984}
985
[5adcc65]986static void skype_logout(struct im_connection *ic)
[f06e3ac]987{
988        struct skype_data *sd = ic->proto_data;
[98bca36]989        char *buf;
990
991        buf = g_strdup_printf("SET USERSTATUS OFFLINE\n");
[5adcc65]992        skype_write(ic, buf);
[98bca36]993        g_free(buf);
994
[a3d6427]995        g_free(sd->username);
[7613670]996        g_free(sd->handle);
[f06e3ac]997        g_free(sd);
[98bca36]998        ic->proto_data = NULL;
[f06e3ac]999}
1000
[8c09bb3]1001static int skype_buddy_msg(struct im_connection *ic, char *who, char *message,
1002                           int flags)
[93ece66]1003{
[77c1abe]1004        char *buf, *ptr, *nick;
[93ece66]1005        int st;
1006
[cbec0d6]1007        nick = g_strdup(who);
[77c1abe]1008        ptr = strchr(nick, '@');
[5adcc65]1009        if (ptr)
[0bb1b7f]1010                *ptr = '\0';
[93ece66]1011
[08a355b]1012        if (!strncmp(who, "skypeconsole", 12))
1013                buf = g_strdup_printf("%s\n", message);
1014        else
1015                buf = g_strdup_printf("MESSAGE %s %s\n", nick, message);
[77c1abe]1016        g_free(nick);
[5adcc65]1017        st = skype_write(ic, buf);
[93ece66]1018        g_free(buf);
1019
1020        return st;
1021}
1022
[5adcc65]1023const struct skype_away_state *skype_away_state_by_name(char *name)
[23411c6]1024{
1025        int i;
1026
[5adcc65]1027        for (i = 0; skype_away_state_list[i].full_name; i++)
1028                if (g_strcasecmp(skype_away_state_list[i].full_name, name) == 0)
1029                        return skype_away_state_list + i;
[23411c6]1030
1031        return NULL;
1032}
1033
[8c09bb3]1034static void skype_set_away(struct im_connection *ic, char *state_txt,
1035                           char *message)
[f06e3ac]1036{
[23411c6]1037        const struct skype_away_state *state;
1038        char *buf;
1039
[5adcc65]1040        if (strcmp(state_txt, GAIM_AWAY_CUSTOM) == 0)
1041                state = skype_away_state_by_name("Away");
[23411c6]1042        else
[5adcc65]1043                state = skype_away_state_by_name(state_txt);
[23411c6]1044        buf = g_strdup_printf("SET USERSTATUS %s\n", state->code);
[5adcc65]1045        skype_write(ic, buf);
[23411c6]1046        g_free(buf);
[f06e3ac]1047}
1048
[5adcc65]1049static GList *skype_away_states(struct im_connection *ic)
[f06e3ac]1050{
[5adcc65]1051        static GList *l;
[adce2de]1052        int i;
[5adcc65]1053
1054        if (l == NULL)
1055                for (i = 0; skype_away_state_list[i].full_name; i++)
[8c09bb3]1056                        l = g_list_append(l,
1057                                (void *)skype_away_state_list[i].full_name);
[5adcc65]1058
[f06e3ac]1059        return l;
1060}
1061
[5adcc65]1062static char *skype_set_display_name(set_t *set, char *value)
[93dffea]1063{
1064        account_t *acc = set->data;
1065        struct im_connection *ic = acc->ic;
1066        char *buf;
1067
[fcef6e6]1068        buf = g_strdup_printf("SET PROFILE FULLNAME %s", value);
[5adcc65]1069        skype_write(ic, buf);
[93dffea]1070        g_free(buf);
[5adcc65]1071        return value;
[93dffea]1072}
1073
[5adcc65]1074static char *skype_set_balance(set_t *set, char *value)
[2af671a]1075{
1076        account_t *acc = set->data;
1077        struct im_connection *ic = acc->ic;
1078        char *buf;
1079
1080        buf = g_strdup_printf("GET PROFILE PSTN_BALANCE");
[5adcc65]1081        skype_write(ic, buf);
[2af671a]1082        g_free(buf);
[5adcc65]1083        return value;
[2af671a]1084}
1085
[5adcc65]1086static char *skype_set_call(set_t *set, char *value)
[b68b023]1087{
1088        account_t *acc = set->data;
1089        struct im_connection *ic = acc->ic;
[2eb4b1f]1090        struct skype_data *sd = ic->proto_data;
[b68b023]1091        char *nick, *ptr, *buf;
1092
[5adcc65]1093        if (value) {
[2eb4b1f]1094                user_t *u = user_find(acc->irc, value);
1095                /* We are starting a call */
[5adcc65]1096                if (!u)
[51dc72d]1097                        nick = g_strdup(value);
[a317ba6]1098                else
1099                        nick = g_strdup(u->handle);
[2eb4b1f]1100                ptr = strchr(nick, '@');
[5adcc65]1101                if (ptr)
[2eb4b1f]1102                        *ptr = '\0';
1103
1104                buf = g_strdup_printf("CALL %s", nick);
[5adcc65]1105                skype_write(ic, buf);
[2eb4b1f]1106                g_free(buf);
1107                g_free(nick);
[5adcc65]1108        } else {
[2eb4b1f]1109                /* We are ending a call */
[5adcc65]1110                if (sd->call_id) {
[8c09bb3]1111                        buf = g_strdup_printf("SET CALL %s STATUS FINISHED",
1112                                sd->call_id);
[5adcc65]1113                        skype_write(ic, buf);
[2eb4b1f]1114                        g_free(buf);
[9fd7202]1115                        g_free(sd->call_id);
1116                        sd->call_id = NULL;
[5adcc65]1117                } else
[2eb4b1f]1118                        imcb_error(ic, "There are no active calls currently.");
[b68b023]1119        }
[5adcc65]1120        return value;
[b68b023]1121}
1122
[5adcc65]1123static void skype_add_buddy(struct im_connection *ic, char *who, char *group)
[f06e3ac]1124{
[6627d92]1125        char *buf, *nick, *ptr;
1126
[cbec0d6]1127        nick = g_strdup(who);
[6627d92]1128        ptr = strchr(nick, '@');
[5adcc65]1129        if (ptr)
[6627d92]1130                *ptr = '\0';
[8c09bb3]1131        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n",
1132                nick);
[5adcc65]1133        skype_write(ic, buf);
[6627d92]1134        g_free(nick);
[f06e3ac]1135}
1136
[5adcc65]1137static void skype_remove_buddy(struct im_connection *ic, char *who, char *group)
[f06e3ac]1138{
[6627d92]1139        char *buf, *nick, *ptr;
1140
[cbec0d6]1141        nick = g_strdup(who);
[6627d92]1142        ptr = strchr(nick, '@');
[5adcc65]1143        if (ptr)
[6627d92]1144                *ptr = '\0';
1145        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick);
[5adcc65]1146        skype_write(ic, buf);
[6627d92]1147        g_free(nick);
[f06e3ac]1148}
1149
[5adcc65]1150void skype_chat_msg(struct groupchat *gc, char *message, int flags)
[66c9558]1151{
[79e20f9]1152        struct im_connection *ic = gc->ic;
1153        char *buf;
1154        buf = g_strdup_printf("CHATMESSAGE %s %s\n", gc->title, message);
[5adcc65]1155        skype_write(ic, buf);
[79e20f9]1156        g_free(buf);
[66c9558]1157}
1158
[5adcc65]1159void skype_chat_leave(struct groupchat *gc)
[b01dc6c]1160{
1161        struct im_connection *ic = gc->ic;
1162        char *buf;
1163        buf = g_strdup_printf("ALTER CHAT %s LEAVE\n", gc->title);
[5adcc65]1164        skype_write(ic, buf);
[b01dc6c]1165        g_free(buf);
[5adcc65]1166        gc->data = (void *)TRUE;
[760319d]1167}
1168
1169void skype_chat_invite(struct groupchat *gc, char *who, char *message)
1170{
1171        struct im_connection *ic = gc->ic;
1172        char *buf, *ptr, *nick;
1173        nick = g_strdup(message);
1174        ptr = strchr(nick, '@');
[5adcc65]1175        if (ptr)
[760319d]1176                *ptr = '\0';
1177        buf = g_strdup_printf("ALTER CHAT %s ADDMEMBERS %s\n", gc->title, nick);
[5adcc65]1178        skype_write(ic, buf);
[760319d]1179        g_free(buf);
1180        g_free(nick);
[b01dc6c]1181}
1182
[09e2a69]1183void skype_chat_topic(struct groupchat *gc, char *message)
1184{
1185        struct im_connection *ic = gc->ic;
[a5f76a2]1186        struct skype_data *sd = ic->proto_data;
[09e2a69]1187        char *buf;
[8c09bb3]1188        buf = g_strdup_printf("ALTER CHAT %s SETTOPIC %s\n",
1189                gc->title, message);
[5adcc65]1190        skype_write(ic, buf);
[09e2a69]1191        g_free(buf);
[a5f76a2]1192        sd->topic_wait = 1;
[09e2a69]1193}
1194
[86278cd]1195struct groupchat *skype_chat_with(struct im_connection *ic, char *who)
1196{
1197        struct skype_data *sd = ic->proto_data;
1198        char *ptr, *nick, *buf;
1199        nick = g_strdup(who);
1200        ptr = strchr(nick, '@');
[5adcc65]1201        if (ptr)
[86278cd]1202                *ptr = '\0';
1203        buf = g_strdup_printf("CHAT CREATE %s\n", nick);
[a5f4040]1204        skype_write(ic, buf);
[86278cd]1205        g_free(buf);
1206        sd->groupchat_with = g_strdup(nick);
1207        g_free(nick);
[5652d43]1208        /* We create a fake chat for now. We will replace it with a real one in
1209         * the real callback. */
[5adcc65]1210        return imcb_chat_new(ic, "");
[86278cd]1211}
1212
[67454bd]1213static void skype_get_info(struct im_connection *ic, char *who)
1214{
1215        char *ptr, *nick, *buf;
1216        nick = g_strdup(who);
1217        ptr = strchr(nick, '@');
[5adcc65]1218        if (ptr)
[67454bd]1219                *ptr = '\0';
1220        buf = g_strdup_printf("GET USER %s FULLNAME\n", nick);
[a5f4040]1221        skype_write(ic, buf);
[67454bd]1222        g_free(buf);
1223        buf = g_strdup_printf("GET USER %s PHONE_HOME\n", nick);
[a5f4040]1224        skype_write(ic, buf);
[67454bd]1225        g_free(buf);
1226        buf = g_strdup_printf("GET USER %s PHONE_OFFICE\n", nick);
[a5f4040]1227        skype_write(ic, buf);
[67454bd]1228        g_free(buf);
1229        buf = g_strdup_printf("GET USER %s PHONE_MOBILE\n", nick);
[a5f4040]1230        skype_write(ic, buf);
[67454bd]1231        g_free(buf);
1232        buf = g_strdup_printf("GET USER %s NROF_AUTHED_BUDDIES\n", nick);
[a5f4040]1233        skype_write(ic, buf);
[67454bd]1234        g_free(buf);
1235        buf = g_strdup_printf("GET USER %s TIMEZONE\n", nick);
[a5f4040]1236        skype_write(ic, buf);
[67454bd]1237        g_free(buf);
1238        buf = g_strdup_printf("GET USER %s LASTONLINETIMESTAMP\n", nick);
[a5f4040]1239        skype_write(ic, buf);
[67454bd]1240        g_free(buf);
1241        buf = g_strdup_printf("GET USER %s BIRTHDAY\n", nick);
[a5f4040]1242        skype_write(ic, buf);
[67454bd]1243        g_free(buf);
1244        buf = g_strdup_printf("GET USER %s SEX\n", nick);
[a5f4040]1245        skype_write(ic, buf);
[67454bd]1246        g_free(buf);
1247        buf = g_strdup_printf("GET USER %s LANGUAGE\n", nick);
[a5f4040]1248        skype_write(ic, buf);
[67454bd]1249        g_free(buf);
1250        buf = g_strdup_printf("GET USER %s COUNTRY\n", nick);
[a5f4040]1251        skype_write(ic, buf);
[67454bd]1252        g_free(buf);
1253        buf = g_strdup_printf("GET USER %s PROVINCE\n", nick);
[a5f4040]1254        skype_write(ic, buf);
[67454bd]1255        g_free(buf);
1256        buf = g_strdup_printf("GET USER %s CITY\n", nick);
[a5f4040]1257        skype_write(ic, buf);
[67454bd]1258        g_free(buf);
1259        buf = g_strdup_printf("GET USER %s HOMEPAGE\n", nick);
[a5f4040]1260        skype_write(ic, buf);
[67454bd]1261        g_free(buf);
1262        buf = g_strdup_printf("GET USER %s ABOUT\n", nick);
[a5f4040]1263        skype_write(ic, buf);
[67454bd]1264        g_free(buf);
1265}
1266
[5adcc65]1267static void skype_set_my_name(struct im_connection *ic, char *info)
[93dffea]1268{
[5adcc65]1269        skype_set_display_name(set_find(&ic->acc->set, "display_name"), info);
[93dffea]1270}
1271
[5adcc65]1272static void skype_init(account_t *acc)
[93dffea]1273{
1274        set_t *s;
1275
[8c09bb3]1276        s = set_add(&acc->set, "server", SKYPE_DEFAULT_SERVER, set_eval_account,
1277                acc);
[93dffea]1278        s->flags |= ACC_SET_OFFLINE_ONLY;
1279
[5adcc65]1280        s = set_add(&acc->set, "port", SKYPE_DEFAULT_PORT, set_eval_int, acc);
[93dffea]1281        s->flags |= ACC_SET_OFFLINE_ONLY;
1282
[8c09bb3]1283        s = set_add(&acc->set, "display_name", NULL, skype_set_display_name,
1284                acc);
[93dffea]1285        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[b68b023]1286
[5adcc65]1287        s = set_add(&acc->set, "call", NULL, skype_set_call, acc);
[b68b023]1288        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[2af671a]1289
[5adcc65]1290        s = set_add(&acc->set, "balance", NULL, skype_set_balance, acc);
[2af671a]1291        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[bd417a1]1292
[5adcc65]1293        s = set_add(&acc->set, "skypeout_offline", "true", set_eval_bool, acc);
[08a355b]1294
[5adcc65]1295        s = set_add(&acc->set, "skypeconsole", "false", set_eval_bool, acc);
[08a355b]1296        s->flags |= ACC_SET_OFFLINE_ONLY;
[5acf9ab]1297
[8c09bb3]1298        s = set_add(&acc->set, "skypeconsole_receive", "false", set_eval_bool,
1299                acc);
[b820226]1300        s->flags |= ACC_SET_OFFLINE_ONLY;
1301
[5adcc65]1302        s = set_add(&acc->set, "auto_join", "false", set_eval_bool, acc);
[5acf9ab]1303        s->flags |= ACC_SET_OFFLINE_ONLY;
[93dffea]1304}
1305
[f06e3ac]1306void init_plugin(void)
1307{
[5adcc65]1308        struct prpl *ret = g_new0(struct prpl, 1);
[f06e3ac]1309
1310        ret->name = "skype";
1311        ret->login = skype_login;
1312        ret->init = skype_init;
1313        ret->logout = skype_logout;
[93ece66]1314        ret->buddy_msg = skype_buddy_msg;
[67454bd]1315        ret->get_info = skype_get_info;
[93dffea]1316        ret->set_my_name = skype_set_my_name;
[f06e3ac]1317        ret->away_states = skype_away_states;
[7daec06]1318        ret->set_away = skype_set_away;
[f06e3ac]1319        ret->add_buddy = skype_add_buddy;
1320        ret->remove_buddy = skype_remove_buddy;
[66c9558]1321        ret->chat_msg = skype_chat_msg;
[b01dc6c]1322        ret->chat_leave = skype_chat_leave;
[760319d]1323        ret->chat_invite = skype_chat_invite;
[86278cd]1324        ret->chat_with = skype_chat_with;
[f06e3ac]1325        ret->handle_cmp = g_strcasecmp;
[09e2a69]1326        ret->chat_topic = skype_chat_topic;
[5adcc65]1327        register_protocol(ret);
[f06e3ac]1328}
Note: See TracBrowser for help on using the repository browser.