source: skype/skype.c @ f4d37c6

Last change on this file since f4d37c6 was f4d37c6, checked in by Miklos Vajna <vmiklos@…>, at 2010-03-07T19:50:39Z

make the edit_prefix configurable

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