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 | |
---|
30 | int msn_chat_id; |
---|
31 | GSList *msn_connections; |
---|
32 | |
---|
33 | static char *set_eval_display_name(set_t *set, char *value); |
---|
34 | |
---|
35 | static 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 | |
---|
54 | static 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 | |
---|
89 | static 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 | |
---|
147 | static 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 | |
---|
162 | static 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 | |
---|
178 | static void msn_set_away(struct im_connection *ic, char *state, char *message) |
---|
179 | { |
---|
180 | struct msn_data *md = ic->proto_data; |
---|
181 | char *nick, *psm, *idle, *statecode, *body, *buf; |
---|
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 | statecode = (char *) md->away_state->code; |
---|
190 | nick = set_getstr(&ic->acc->set, "display_name"); |
---|
191 | psm = message ? message : ""; |
---|
192 | idle = strcmp(statecode, "IDL") ? "false" : "true"; |
---|
193 | |
---|
194 | body = g_markup_printf_escaped(MSN_PUT_USER_BODY, |
---|
195 | nick, psm, psm, md->uuid, statecode, md->uuid, idle, statecode, |
---|
196 | MSN_CAP1, MSN_CAP2, MSN_CAP1, MSN_CAP2 |
---|
197 | ); |
---|
198 | |
---|
199 | buf = g_strdup_printf(MSN_PUT_HEADERS, ic->acc->user, ic->acc->user, md->uuid, |
---|
200 | "/user", "application/user+xml", |
---|
201 | strlen(body), body); |
---|
202 | msn_ns_write(ic, -1, "PUT %d %zd\r\n%s", ++md->trId, strlen(buf), buf); |
---|
203 | |
---|
204 | g_free(buf); |
---|
205 | g_free(body); |
---|
206 | } |
---|
207 | |
---|
208 | static void msn_get_info(struct im_connection *ic, char *who) |
---|
209 | { |
---|
210 | /* Just make an URL and let the user fetch the info */ |
---|
211 | imcb_log(ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who); |
---|
212 | } |
---|
213 | |
---|
214 | static void msn_add_buddy(struct im_connection *ic, char *who, char *group) |
---|
215 | { |
---|
216 | struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who); |
---|
217 | |
---|
218 | msn_buddy_list_add(ic, MSN_BUDDY_FL, who, who, group); |
---|
219 | if (bu && bu->group) { |
---|
220 | msn_buddy_list_remove(ic, MSN_BUDDY_FL, who, bu->group->name); |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | static void msn_remove_buddy(struct im_connection *ic, char *who, char *group) |
---|
225 | { |
---|
226 | msn_buddy_list_remove(ic, MSN_BUDDY_FL, who, NULL); |
---|
227 | } |
---|
228 | |
---|
229 | static void msn_chat_msg(struct groupchat *c, char *message, int flags) |
---|
230 | { |
---|
231 | /* TODO: groupchats*/ |
---|
232 | } |
---|
233 | |
---|
234 | static void msn_chat_invite(struct groupchat *c, char *who, char *message) |
---|
235 | { |
---|
236 | /* TODO: groupchats*/ |
---|
237 | } |
---|
238 | |
---|
239 | static void msn_chat_leave(struct groupchat *c) |
---|
240 | { |
---|
241 | /* TODO: groupchats*/ |
---|
242 | } |
---|
243 | |
---|
244 | static struct groupchat *msn_chat_with(struct im_connection *ic, char *who) |
---|
245 | { |
---|
246 | /* TODO: groupchats*/ |
---|
247 | struct groupchat *c = imcb_chat_new(ic, who); |
---|
248 | return c; |
---|
249 | } |
---|
250 | |
---|
251 | static void msn_keepalive(struct im_connection *ic) |
---|
252 | { |
---|
253 | msn_ns_write(ic, -1, "PNG\r\n"); |
---|
254 | } |
---|
255 | |
---|
256 | static void msn_add_permit(struct im_connection *ic, char *who) |
---|
257 | { |
---|
258 | msn_buddy_list_add(ic, MSN_BUDDY_AL, who, who, NULL); |
---|
259 | } |
---|
260 | |
---|
261 | static void msn_rem_permit(struct im_connection *ic, char *who) |
---|
262 | { |
---|
263 | msn_buddy_list_remove(ic, MSN_BUDDY_AL, who, NULL); |
---|
264 | } |
---|
265 | |
---|
266 | static void msn_add_deny(struct im_connection *ic, char *who) |
---|
267 | { |
---|
268 | msn_buddy_list_add(ic, MSN_BUDDY_BL, who, who, NULL); |
---|
269 | } |
---|
270 | |
---|
271 | static void msn_rem_deny(struct im_connection *ic, char *who) |
---|
272 | { |
---|
273 | msn_buddy_list_remove(ic, MSN_BUDDY_BL, who, NULL); |
---|
274 | } |
---|
275 | |
---|
276 | static int msn_send_typing(struct im_connection *ic, char *who, int typing) |
---|
277 | { |
---|
278 | struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who); |
---|
279 | |
---|
280 | if (!(bu->flags & BEE_USER_ONLINE)) { |
---|
281 | return 0; |
---|
282 | } else if (typing & OPT_TYPING) { |
---|
283 | return(msn_buddy_msg(ic, who, TYPING_NOTIFICATION_MESSAGE, 0)); |
---|
284 | } else { |
---|
285 | return 1; |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | static char *set_eval_display_name(set_t *set, char *value) |
---|
290 | { |
---|
291 | account_t *acc = set->data; |
---|
292 | struct im_connection *ic = acc->ic; |
---|
293 | struct msn_data *md = ic->proto_data; |
---|
294 | |
---|
295 | if (md->flags & MSN_EMAIL_UNVERIFIED) { |
---|
296 | imcb_log(ic, "Warning: Your e-mail address is unverified. MSN doesn't allow " |
---|
297 | "changing your display name until your e-mail address is verified."); |
---|
298 | } |
---|
299 | |
---|
300 | if (md->flags & MSN_GOT_PROFILE_DN) { |
---|
301 | msn_soap_profile_set_dn(ic, value); |
---|
302 | } else { |
---|
303 | msn_soap_addressbook_set_display_name(ic, value); |
---|
304 | } |
---|
305 | |
---|
306 | return msn_ns_set_display_name(ic, value) ? value : NULL; |
---|
307 | } |
---|
308 | |
---|
309 | static void msn_buddy_data_add(bee_user_t *bu) |
---|
310 | { |
---|
311 | struct msn_data *md = bu->ic->proto_data; |
---|
312 | struct msn_buddy_data *bd; |
---|
313 | char *handle; |
---|
314 | |
---|
315 | bd = bu->data = g_new0(struct msn_buddy_data, 1); |
---|
316 | g_tree_insert(md->domaintree, bu->handle, bu); |
---|
317 | |
---|
318 | for (handle = bu->handle; g_ascii_isdigit(*handle); handle++) { |
---|
319 | ; |
---|
320 | } |
---|
321 | if (*handle == ':') { |
---|
322 | /* Pass a nick hint so hopefully the stupid numeric prefix |
---|
323 | won't show up to the user. */ |
---|
324 | char *s = strchr(++handle, '@'); |
---|
325 | if (s) { |
---|
326 | handle = g_strndup(handle, s - handle); |
---|
327 | imcb_buddy_nick_hint(bu->ic, bu->handle, handle); |
---|
328 | g_free(handle); |
---|
329 | } |
---|
330 | |
---|
331 | bd->flags |= MSN_BUDDY_FED; |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | static void msn_buddy_data_free(bee_user_t *bu) |
---|
336 | { |
---|
337 | struct msn_data *md = bu->ic->proto_data; |
---|
338 | struct msn_buddy_data *bd = bu->data; |
---|
339 | |
---|
340 | g_free(bd->cid); |
---|
341 | g_free(bd); |
---|
342 | |
---|
343 | g_tree_remove(md->domaintree, bu->handle); |
---|
344 | } |
---|
345 | |
---|
346 | GList *msn_buddy_action_list(bee_user_t *bu) |
---|
347 | { |
---|
348 | static GList *ret = NULL; |
---|
349 | |
---|
350 | if (ret == NULL) { |
---|
351 | static const struct buddy_action ba[2] = { |
---|
352 | { "NUDGE", "Draw attention" }, |
---|
353 | }; |
---|
354 | |
---|
355 | ret = g_list_prepend(ret, (void *) ba + 0); |
---|
356 | } |
---|
357 | |
---|
358 | return ret; |
---|
359 | } |
---|
360 | |
---|
361 | void *msn_buddy_action(struct bee_user *bu, const char *action, char * const args[], void *data) |
---|
362 | { |
---|
363 | if (g_strcasecmp(action, "NUDGE") == 0) { |
---|
364 | msn_buddy_msg(bu->ic, bu->handle, NUDGE_MESSAGE, 0); |
---|
365 | } |
---|
366 | |
---|
367 | return NULL; |
---|
368 | } |
---|
369 | |
---|
370 | void msn_initmodule() |
---|
371 | { |
---|
372 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
373 | |
---|
374 | ret->name = "msn"; |
---|
375 | ret->mms = 1409; /* this guess taken from libotr UPGRADING file */ |
---|
376 | ret->login = msn_login; |
---|
377 | ret->init = msn_init; |
---|
378 | ret->logout = msn_logout; |
---|
379 | ret->buddy_msg = msn_buddy_msg; |
---|
380 | ret->away_states = msn_away_states; |
---|
381 | ret->set_away = msn_set_away; |
---|
382 | ret->get_info = msn_get_info; |
---|
383 | ret->add_buddy = msn_add_buddy; |
---|
384 | ret->remove_buddy = msn_remove_buddy; |
---|
385 | ret->chat_msg = msn_chat_msg; |
---|
386 | ret->chat_invite = msn_chat_invite; |
---|
387 | ret->chat_leave = msn_chat_leave; |
---|
388 | ret->chat_with = msn_chat_with; |
---|
389 | ret->keepalive = msn_keepalive; |
---|
390 | ret->add_permit = msn_add_permit; |
---|
391 | ret->rem_permit = msn_rem_permit; |
---|
392 | ret->add_deny = msn_add_deny; |
---|
393 | ret->rem_deny = msn_rem_deny; |
---|
394 | ret->send_typing = msn_send_typing; |
---|
395 | ret->handle_cmp = g_strcasecmp; |
---|
396 | ret->buddy_data_add = msn_buddy_data_add; |
---|
397 | ret->buddy_data_free = msn_buddy_data_free; |
---|
398 | ret->buddy_action_list = msn_buddy_action_list; |
---|
399 | ret->buddy_action = msn_buddy_action; |
---|
400 | |
---|
401 | register_protocol(ret); |
---|
402 | } |
---|