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
Line 
1/********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2013 Wilmer van der Gaast and others                *
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;
22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
24*/
25
26#include "nogaim.h"
27#include "soap.h"
28#include "msn.h"
29
30int msn_chat_id;
31GSList *msn_connections;
32
33static char *set_eval_display_name(set_t *set, char *value);
34
35static void msn_init(account_t *acc)
36{
37        set_t *s;
38
39        s = set_add(&acc->set, "display_name", NULL, set_eval_display_name, acc);
40        s->flags |= SET_NOSAVE | ACC_SET_ONLINE_ONLY;
41
42        s = set_add(&acc->set, "server", NULL, set_eval_account, acc);
43        s->flags |= SET_NOSAVE | ACC_SET_OFFLINE_ONLY;
44
45        s = set_add(&acc->set, "port", MSN_NS_PORT, set_eval_int, acc);
46        s->flags |= ACC_SET_OFFLINE_ONLY;
47
48        set_add(&acc->set, "mail_notifications", "false", set_eval_bool, acc);
49
50        acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE |
51                      ACC_FLAG_HANDLE_DOMAINS;
52}
53
54static void msn_login(account_t *acc)
55{
56        struct im_connection *ic = imcb_new(acc);
57        struct msn_data *md = g_new0(struct msn_data, 1);
58        char *server = set_getstr(&ic->acc->set, "server");
59
60        ic->proto_data = md;
61        ic->flags |= OPT_PONGS | OPT_PONGED;
62
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
71        if (strchr(acc->user, '@') == NULL) {
72                imcb_error(ic, "Invalid account name");
73                imc_logout(ic, FALSE);
74                return;
75        }
76
77        md->ic = ic;
78        md->away_state = msn_away_state_list;
79        md->domaintree = g_tree_new(msn_domaintree_cmp);
80        md->fd = -1;
81
82        msn_connections = g_slist_prepend(msn_connections, ic);
83
84        imcb_log(ic, "Connecting");
85        msn_ns_connect(ic, server,
86                       set_getint(&ic->acc->set, "port"));
87}
88
89static void msn_logout(struct im_connection *ic)
90{
91        struct msn_data *md = ic->proto_data;
92        GSList *l;
93        int i;
94
95        if (md) {
96                msn_ns_close(md);
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) {
109                        struct msn_group *mg = md->groups->data;
110                        md->groups = g_slist_remove(md->groups, mg);
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);
120                }
121                md->domaintree = NULL;
122
123                while (md->grpq) {
124                        struct msn_groupadd *ga = md->grpq->data;
125                        md->grpq = g_slist_remove(md->grpq, ga);
126                        g_free(ga->group);
127                        g_free(ga->who);
128                        g_free(ga);
129                }
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);
141        }
142        g_slist_free(ic->deny);
143
144        msn_connections = g_slist_remove(msn_connections, ic);
145}
146
147static int msn_buddy_msg(struct im_connection *ic, char *who, char *message, int away)
148{
149        struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who);
150
151#ifdef DEBUG
152        if (strcmp(who, "raw") == 0) {
153                msn_ns_write(ic, -1, "%s\r\n", message);
154                return 0;
155        }
156#endif
157
158        msn_ns_sendmessage(ic, bu, message);
159        return(0);
160}
161
162static GList *msn_away_states(struct im_connection *ic)
163{
164        static GList *l = NULL;
165        int i;
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
175        return l;
176}
177
178static void msn_set_away(struct im_connection *ic, char *state, char *message)
179{
180        //char *uux;
181        struct msn_data *md = ic->proto_data;
182
183        if (state == NULL) {
184                md->away_state = msn_away_state_list;
185        } else if ((md->away_state = msn_away_state_by_name(state)) == NULL) {
186                md->away_state = msn_away_state_list + 1;
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)) {
190                return;
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);
215}
216
217static void msn_get_info(struct im_connection *ic, char *who)
218{
219        /* Just make an URL and let the user fetch the info */
220        imcb_log(ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who);
221}
222
223static void msn_add_buddy(struct im_connection *ic, char *who, char *group)
224{
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        }
231}
232
233static void msn_remove_buddy(struct im_connection *ic, char *who, char *group)
234{
235        msn_buddy_list_remove(ic, MSN_BUDDY_FL, who, NULL);
236}
237
238static void msn_chat_msg(struct groupchat *c, char *message, int flags)
239{
240        /* TODO: groupchats*/
241}
242
243static void msn_chat_invite(struct groupchat *c, char *who, char *message)
244{
245        /* TODO: groupchats*/
246}
247
248static void msn_chat_leave(struct groupchat *c)
249{
250        /* TODO: groupchats*/
251}
252
253static struct groupchat *msn_chat_with(struct im_connection *ic, char *who)
254{
255        /* TODO: groupchats*/
256        struct groupchat *c = imcb_chat_new(ic, who);
257        return c;
258}
259
260static void msn_keepalive(struct im_connection *ic)
261{
262        msn_ns_write(ic, -1, "PNG\r\n");
263}
264
265static void msn_add_permit(struct im_connection *ic, char *who)
266{
267        msn_buddy_list_add(ic, MSN_BUDDY_AL, who, who, NULL);
268}
269
270static void msn_rem_permit(struct im_connection *ic, char *who)
271{
272        msn_buddy_list_remove(ic, MSN_BUDDY_AL, who, NULL);
273}
274
275static void msn_add_deny(struct im_connection *ic, char *who)
276{
277        msn_buddy_list_add(ic, MSN_BUDDY_BL, who, who, NULL);
278}
279
280static void msn_rem_deny(struct im_connection *ic, char *who)
281{
282        msn_buddy_list_remove(ic, MSN_BUDDY_BL, who, NULL);
283}
284
285static int msn_send_typing(struct im_connection *ic, char *who, int typing)
286{
287        struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who);
288
289        if (!(bu->flags & BEE_USER_ONLINE)) {
290                return 0;
291        } else if (typing & OPT_TYPING) {
292                return(msn_buddy_msg(ic, who, TYPING_NOTIFICATION_MESSAGE, 0));
293        } else {
294                return 1;
295        }
296}
297
298static char *set_eval_display_name(set_t *set, char *value)
299{
300        account_t *acc = set->data;
301        struct im_connection *ic = acc->ic;
302        struct msn_data *md = ic->proto_data;
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;
316}
317
318static void msn_buddy_data_add(bee_user_t *bu)
319{
320        struct msn_data *md = bu->ic->proto_data;
321        struct msn_buddy_data *bd;
322        char *handle;
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 == ':') {
331                /* Pass a nick hint so hopefully the stupid numeric prefix
332                   won't show up to the user.  */
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);
338                }
339
340                bd->flags |= MSN_BUDDY_FED;
341        }
342}
343
344static void msn_buddy_data_free(bee_user_t *bu)
345{
346        struct msn_data *md = bu->ic->proto_data;
347        struct msn_buddy_data *bd = bu->data;
348
349        g_free(bd->cid);
350        g_free(bd);
351
352        g_tree_remove(md->domaintree, bu->handle);
353}
354
355GList *msn_buddy_action_list(bee_user_t *bu)
356{
357        static GList *ret = NULL;
358
359        if (ret == NULL) {
360                static const struct buddy_action ba[2] = {
361                        { "NUDGE", "Draw attention" },
362                };
363
364                ret = g_list_prepend(ret, (void *) ba + 0);
365        }
366
367        return ret;
368}
369
370void *msn_buddy_action(struct bee_user *bu, const char *action, char * const args[], void *data)
371{
372        if (g_strcasecmp(action, "NUDGE") == 0) {
373                msn_buddy_msg(bu->ic, bu->handle, NUDGE_MESSAGE, 0);
374        }
375
376        return NULL;
377}
378
379void msn_initmodule()
380{
381        struct prpl *ret = g_new0(struct prpl, 1);
382
383        ret->name = "msn";
384        ret->mms = 1409;         /* this guess taken from libotr UPGRADING file */
385        ret->login = msn_login;
386        ret->init = msn_init;
387        ret->logout = msn_logout;
388        ret->buddy_msg = msn_buddy_msg;
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;
394        ret->chat_msg = msn_chat_msg;
395        ret->chat_invite = msn_chat_invite;
396        ret->chat_leave = msn_chat_leave;
397        ret->chat_with = msn_chat_with;
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;
404        ret->handle_cmp = g_strcasecmp;
405        ret->buddy_data_add = msn_buddy_data_add;
406        ret->buddy_data_free = msn_buddy_data_free;
407        ret->buddy_action_list = msn_buddy_action_list;
408        ret->buddy_action = msn_buddy_action;
409
410        register_protocol(ret);
411}
Note: See TracBrowser for help on using the repository browser.