source: protocols/msn/msn.c @ ffa5b70

Last change on this file since ffa5b70 was ffa5b70, checked in by dequis <dx@…>, at 2015-03-15T14:41:47Z

msn: removed switchboards, implemented SDG message

  • Property mode set to 100644
File size: 10.9 KB
RevLine 
[5ebff60]1/********************************************************************\
[b7d3cc34]2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[0e788f5]4  * Copyright 2002-2013 Wilmer van der Gaast and others                *
[b7d3cc34]5  \********************************************************************/
6
7/* MSN module - Main file; functions to be called from BitlBee          */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
[6f10697]22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
[b7d3cc34]24*/
25
26#include "nogaim.h"
[4452e69]27#include "soap.h"
[b7d3cc34]28#include "msn.h"
29
[c6ca3ee]30int msn_chat_id;
31GSList *msn_connections;
32
[5ebff60]33static char *set_eval_display_name(set_t *set, char *value);
[911f2eb]34
[5ebff60]35static void msn_init(account_t *acc)
[911f2eb]36{
[4452e69]37        set_t *s;
[5ebff60]38
39        s = set_add(&acc->set, "display_name", NULL, set_eval_display_name, acc);
[bb5ce568]40        s->flags |= SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[5ebff60]41
[a6b00fc]42        s = set_add(&acc->set, "server", NULL, set_eval_account, acc);
[bc7a0d4]43        s->flags |= SET_NOSAVE | ACC_SET_OFFLINE_ONLY;
44
[5ebff60]45        s = set_add(&acc->set, "port", MSN_NS_PORT, set_eval_int, acc);
[bc7a0d4]46        s->flags |= ACC_SET_OFFLINE_ONLY;
47
[5ebff60]48        set_add(&acc->set, "mail_notifications", "false", set_eval_bool, acc);
49
[06eef80]50        acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE |
51                      ACC_FLAG_HANDLE_DOMAINS;
[911f2eb]52}
53
[5ebff60]54static void msn_login(account_t *acc)
[b7d3cc34]55{
[5ebff60]56        struct im_connection *ic = imcb_new(acc);
57        struct msn_data *md = g_new0(struct msn_data, 1);
[a6b00fc]58        char *server = set_getstr(&ic->acc->set, "server");
[5ebff60]59
[0da65d5]60        ic->proto_data = md;
[e132b60]61        ic->flags |= OPT_PONGS | OPT_PONGED;
[5ebff60]62
[a6b00fc]63        if (!server) {
64                imcb_error(ic, "The msn protocol is disabled in this version because most servers disabled MSNP18 over port 1863.");
65                imcb_error(ic, "If you find a working server, you can change the 'server' setting of this account. Good luck!");
66                imcb_error(ic, "See also: http://ismsndeadyet.com/"); // shameless plug
67                imc_logout(ic, FALSE);
68                return;
69        }
70
[5ebff60]71        if (strchr(acc->user, '@') == NULL) {
72                imcb_error(ic, "Invalid account name");
73                imc_logout(ic, FALSE);
[b7d3cc34]74                return;
75        }
[5ebff60]76
[0da65d5]77        md->ic = ic;
[2a29eac]78        md->away_state = msn_away_state_list;
[5ebff60]79        md->domaintree = g_tree_new(msn_domaintree_cmp);
[ffa5b70]80        md->fd = -1;
[5ebff60]81
82        msn_connections = g_slist_prepend(msn_connections, ic);
83
84        imcb_log(ic, "Connecting");
[ffa5b70]85        msn_ns_connect(ic, server,
[5ebff60]86                       set_getint(&ic->acc->set, "port"));
[b7d3cc34]87}
88
[5ebff60]89static void msn_logout(struct im_connection *ic)
[b7d3cc34]90{
[0da65d5]91        struct msn_data *md = ic->proto_data;
[b7d3cc34]92        GSList *l;
[80175a1]93        int i;
[5ebff60]94
95        if (md) {
[ffa5b70]96                msn_ns_close(md);
[5ebff60]97
98                msn_msgq_purge(ic, &md->msgq);
99                msn_soapq_flush(ic, FALSE);
100
101                for (i = 0; i < sizeof(md->tokens) / sizeof(md->tokens[0]); i++) {
102                        g_free(md->tokens[i]);
103                }
104                g_free(md->lock_key);
105                g_free(md->pp_policy);
106                g_free(md->uuid);
107
108                while (md->groups) {
[ff27648]109                        struct msn_group *mg = md->groups->data;
[05816dd]110                        md->groups = g_slist_remove(md->groups, mg);
[5ebff60]111                        g_free(mg->id);
112                        g_free(mg->name);
113                        g_free(mg);
114                }
115
116                g_free(md->profile_rid);
117
118                if (md->domaintree) {
119                        g_tree_destroy(md->domaintree);
[ff27648]120                }
[ca7de3a]121                md->domaintree = NULL;
[5ebff60]122
123                while (md->grpq) {
[70ac477]124                        struct msn_groupadd *ga = md->grpq->data;
[05816dd]125                        md->grpq = g_slist_remove(md->grpq, ga);
[5ebff60]126                        g_free(ga->group);
127                        g_free(ga->who);
128                        g_free(ga);
[70ac477]129                }
[5ebff60]130
131                g_free(md);
132        }
133
134        for (l = ic->permit; l; l = l->next) {
135                g_free(l->data);
136        }
137        g_slist_free(ic->permit);
138
139        for (l = ic->deny; l; l = l->next) {
140                g_free(l->data);
[b7d3cc34]141        }
[5ebff60]142        g_slist_free(ic->deny);
143
144        msn_connections = g_slist_remove(msn_connections, ic);
[b7d3cc34]145}
146
[5ebff60]147static int msn_buddy_msg(struct im_connection *ic, char *who, char *message, int away)
[b7d3cc34]148{
[5ebff60]149        struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who);
150
[70ac477]151#ifdef DEBUG
[5ebff60]152        if (strcmp(who, "raw") == 0) {
153                msn_ns_write(ic, -1, "%s\r\n", message);
[ffa5b70]154                return 0;
[b7d3cc34]155        }
[ffa5b70]156#endif
[5ebff60]157
[ffa5b70]158        msn_ns_sendmessage(ic, bu, message);
[5ebff60]159        return(0);
[b7d3cc34]160}
161
[5ebff60]162static GList *msn_away_states(struct im_connection *ic)
[b7d3cc34]163{
[5e202b0]164        static GList *l = NULL;
[b7d3cc34]165        int i;
[5ebff60]166
167        if (l == NULL) {
168                for (i = 0; *msn_away_state_list[i].code; i++) {
169                        if (*msn_away_state_list[i].name) {
170                                l = g_list_append(l, (void *) msn_away_state_list[i].name);
171                        }
172                }
173        }
174
[5e202b0]175        return l;
[b7d3cc34]176}
177
[5ebff60]178static void msn_set_away(struct im_connection *ic, char *state, char *message)
[b7d3cc34]179{
[ffa5b70]180        //char *uux;
[0da65d5]181        struct msn_data *md = ic->proto_data;
[5ebff60]182
183        if (state == NULL) {
[b051d39]184                md->away_state = msn_away_state_list;
[5ebff60]185        } else if ((md->away_state = msn_away_state_by_name(state)) == NULL) {
[daae10f]186                md->away_state = msn_away_state_list + 1;
[5ebff60]187        }
188
189        if (!msn_ns_write(ic, -1, "CHG %d %s %d:%02d\r\n", ++md->trId, md->away_state->code, MSN_CAP1, MSN_CAP2)) {
[12767e3]190                return;
[5ebff60]191        }
192
193        uux = g_markup_printf_escaped("<EndpointData><Capabilities>%d:%02d"
194                                      "</Capabilities></EndpointData>",
195                                      MSN_CAP1, MSN_CAP2);
196        msn_ns_write(ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen(uux), uux);
197        g_free(uux);
198
199        uux = g_markup_printf_escaped("<PrivateEndpointData><EpName>%s</EpName>"
200                                      "<Idle>%s</Idle><ClientType>%d</ClientType>"
201                                      "<State>%s</State></PrivateEndpointData>",
202                                      md->uuid,
203                                      strcmp(md->away_state->code, "IDL") ? "false" : "true",
204                                      1,  /* ? */
205                                      md->away_state->code);
206        msn_ns_write(ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen(uux), uux);
207        g_free(uux);
208
209        uux = g_markup_printf_escaped("<Data><DDP></DDP><PSM>%s</PSM>"
210                                      "<CurrentMedia></CurrentMedia>"
211                                      "<MachineGuid>%s</MachineGuid></Data>",
212                                      message ? message : "", md->uuid);
213        msn_ns_write(ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen(uux), uux);
214        g_free(uux);
[b7d3cc34]215}
216
[5ebff60]217static void msn_get_info(struct im_connection *ic, char *who)
[b7d3cc34]218{
219        /* Just make an URL and let the user fetch the info */
[5ebff60]220        imcb_log(ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who);
[b7d3cc34]221}
222
[5ebff60]223static void msn_add_buddy(struct im_connection *ic, char *who, char *group)
[b7d3cc34]224{
[5ebff60]225        struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who);
226
227        msn_buddy_list_add(ic, MSN_BUDDY_FL, who, who, group);
228        if (bu && bu->group) {
229                msn_buddy_list_remove(ic, MSN_BUDDY_FL, who, bu->group->name);
230        }
[b7d3cc34]231}
232
[5ebff60]233static void msn_remove_buddy(struct im_connection *ic, char *who, char *group)
[b7d3cc34]234{
[5ebff60]235        msn_buddy_list_remove(ic, MSN_BUDDY_FL, who, NULL);
[b7d3cc34]236}
237
[5ebff60]238static void msn_chat_msg(struct groupchat *c, char *message, int flags)
[b7d3cc34]239{
[ffa5b70]240        /* TODO: groupchats*/
[b7d3cc34]241}
242
[5ebff60]243static void msn_chat_invite(struct groupchat *c, char *who, char *message)
[b7d3cc34]244{
[ffa5b70]245        /* TODO: groupchats*/
[b7d3cc34]246}
247
[5ebff60]248static void msn_chat_leave(struct groupchat *c)
[b7d3cc34]249{
[ffa5b70]250        /* TODO: groupchats*/
[b7d3cc34]251}
252
[5ebff60]253static struct groupchat *msn_chat_with(struct im_connection *ic, char *who)
[b7d3cc34]254{
[ffa5b70]255        /* TODO: groupchats*/
[5ebff60]256        struct groupchat *c = imcb_chat_new(ic, who);
[ffa5b70]257        return c;
[b7d3cc34]258}
259
[5ebff60]260static void msn_keepalive(struct im_connection *ic)
[b7d3cc34]261{
[5ebff60]262        msn_ns_write(ic, -1, "PNG\r\n");
[b7d3cc34]263}
264
[5ebff60]265static void msn_add_permit(struct im_connection *ic, char *who)
[b7d3cc34]266{
[5ebff60]267        msn_buddy_list_add(ic, MSN_BUDDY_AL, who, who, NULL);
[b7d3cc34]268}
269
[5ebff60]270static void msn_rem_permit(struct im_connection *ic, char *who)
[b7d3cc34]271{
[5ebff60]272        msn_buddy_list_remove(ic, MSN_BUDDY_AL, who, NULL);
[b7d3cc34]273}
274
[5ebff60]275static void msn_add_deny(struct im_connection *ic, char *who)
[b7d3cc34]276{
[5ebff60]277        msn_buddy_list_add(ic, MSN_BUDDY_BL, who, who, NULL);
[b7d3cc34]278}
279
[5ebff60]280static void msn_rem_deny(struct im_connection *ic, char *who)
[b7d3cc34]281{
[5ebff60]282        msn_buddy_list_remove(ic, MSN_BUDDY_BL, who, NULL);
[b7d3cc34]283}
284
[5ebff60]285static int msn_send_typing(struct im_connection *ic, char *who, int typing)
[b7d3cc34]286{
[5ebff60]287        struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who);
288
289        if (!(bu->flags & BEE_USER_ONLINE)) {
[4255320]290                return 0;
[5ebff60]291        } else if (typing & OPT_TYPING) {
292                return(msn_buddy_msg(ic, who, TYPING_NOTIFICATION_MESSAGE, 0));
293        } else {
[4255320]294                return 1;
[5ebff60]295        }
[b7d3cc34]296}
297
[5ebff60]298static char *set_eval_display_name(set_t *set, char *value)
[911f2eb]299{
300        account_t *acc = set->data;
[0da65d5]301        struct im_connection *ic = acc->ic;
[80175a1]302        struct msn_data *md = ic->proto_data;
[5ebff60]303
304        if (md->flags & MSN_EMAIL_UNVERIFIED) {
305                imcb_log(ic, "Warning: Your e-mail address is unverified. MSN doesn't allow "
306                         "changing your display name until your e-mail address is verified.");
307        }
308
309        if (md->flags & MSN_GOT_PROFILE_DN) {
310                msn_soap_profile_set_dn(ic, value);
311        } else {
312                msn_soap_addressbook_set_display_name(ic, value);
313        }
314
315        return msn_ns_set_display_name(ic, value) ? value : NULL;
[911f2eb]316}
317
[5ebff60]318static void msn_buddy_data_add(bee_user_t *bu)
[7f34ce2]319{
[ca7de3a]320        struct msn_data *md = bu->ic->proto_data;
[208db4b]321        struct msn_buddy_data *bd;
322        char *handle;
[5ebff60]323
324        bd = bu->data = g_new0(struct msn_buddy_data, 1);
325        g_tree_insert(md->domaintree, bu->handle, bu);
326
327        for (handle = bu->handle; g_ascii_isdigit(*handle); handle++) {
328                ;
329        }
330        if (*handle == ':') {
[208db4b]331                /* Pass a nick hint so hopefully the stupid numeric prefix
332                   won't show up to the user.  */
[5ebff60]333                char *s = strchr(++handle, '@');
334                if (s) {
335                        handle = g_strndup(handle, s - handle);
336                        imcb_buddy_nick_hint(bu->ic, bu->handle, handle);
337                        g_free(handle);
[208db4b]338                }
[5ebff60]339
[208db4b]340                bd->flags |= MSN_BUDDY_FED;
341        }
[7f34ce2]342}
343
[5ebff60]344static void msn_buddy_data_free(bee_user_t *bu)
[7f34ce2]345{
[ca7de3a]346        struct msn_data *md = bu->ic->proto_data;
[c1d40e7]347        struct msn_buddy_data *bd = bu->data;
[5ebff60]348
349        g_free(bd->cid);
350        g_free(bd);
351
352        g_tree_remove(md->domaintree, bu->handle);
[7f34ce2]353}
354
[5ebff60]355GList *msn_buddy_action_list(bee_user_t *bu)
[9c84617]356{
357        static GList *ret = NULL;
[5ebff60]358
359        if (ret == NULL) {
[9c84617]360                static const struct buddy_action ba[2] = {
361                        { "NUDGE", "Draw attention" },
362                };
[5ebff60]363
364                ret = g_list_prepend(ret, (void *) ba + 0);
[9c84617]365        }
[5ebff60]366
[9c84617]367        return ret;
368}
369
[5ebff60]370void *msn_buddy_action(struct bee_user *bu, const char *action, char * const args[], void *data)
[9c84617]371{
[5ebff60]372        if (g_strcasecmp(action, "NUDGE") == 0) {
373                msn_buddy_msg(bu->ic, bu->handle, NUDGE_MESSAGE, 0);
374        }
375
[9c84617]376        return NULL;
377}
378
[0da65d5]379void msn_initmodule()
[b7d3cc34]380{
[7b23afd]381        struct prpl *ret = g_new0(struct prpl, 1);
[5ebff60]382
[7b23afd]383        ret->name = "msn";
[76c89dc7]384        ret->mms = 1409;         /* this guess taken from libotr UPGRADING file */
[b7d3cc34]385        ret->login = msn_login;
[0da65d5]386        ret->init = msn_init;
387        ret->logout = msn_logout;
[f6c963b]388        ret->buddy_msg = msn_buddy_msg;
[b7d3cc34]389        ret->away_states = msn_away_states;
390        ret->set_away = msn_set_away;
391        ret->get_info = msn_get_info;
392        ret->add_buddy = msn_add_buddy;
393        ret->remove_buddy = msn_remove_buddy;
[f6c963b]394        ret->chat_msg = msn_chat_msg;
[b7d3cc34]395        ret->chat_invite = msn_chat_invite;
396        ret->chat_leave = msn_chat_leave;
[0da65d5]397        ret->chat_with = msn_chat_with;
[b7d3cc34]398        ret->keepalive = msn_keepalive;
399        ret->add_permit = msn_add_permit;
400        ret->rem_permit = msn_rem_permit;
401        ret->add_deny = msn_add_deny;
402        ret->rem_deny = msn_rem_deny;
403        ret->send_typing = msn_send_typing;
[5b52a48]404        ret->handle_cmp = g_strcasecmp;
[7f34ce2]405        ret->buddy_data_add = msn_buddy_data_add;
406        ret->buddy_data_free = msn_buddy_data_free;
[9c84617]407        ret->buddy_action_list = msn_buddy_action_list;
408        ret->buddy_action = msn_buddy_action;
[5ebff60]409
[7b23afd]410        register_protocol(ret);
[b7d3cc34]411}
Note: See TracBrowser for help on using the repository browser.