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 | server = "geo.gateway.messenger.live.com"; |
---|
65 | } |
---|
66 | |
---|
67 | if (strchr(acc->user, '@') == NULL) { |
---|
68 | imcb_error(ic, "Invalid account name"); |
---|
69 | imc_logout(ic, FALSE); |
---|
70 | return; |
---|
71 | } |
---|
72 | |
---|
73 | md->ic = ic; |
---|
74 | md->away_state = msn_away_state_list; |
---|
75 | md->domaintree = g_tree_new(msn_domaintree_cmp); |
---|
76 | md->fd = -1; |
---|
77 | md->is_http = TRUE; |
---|
78 | |
---|
79 | msn_connections = g_slist_prepend(msn_connections, ic); |
---|
80 | |
---|
81 | imcb_log(ic, "Connecting"); |
---|
82 | msn_ns_connect(ic, server, |
---|
83 | set_getint(&ic->acc->set, "port")); |
---|
84 | } |
---|
85 | |
---|
86 | static void msn_logout(struct im_connection *ic) |
---|
87 | { |
---|
88 | struct msn_data *md = ic->proto_data; |
---|
89 | GSList *l; |
---|
90 | int i; |
---|
91 | |
---|
92 | if (md) { |
---|
93 | msn_ns_close(md); |
---|
94 | |
---|
95 | msn_soapq_flush(ic, FALSE); |
---|
96 | |
---|
97 | for (i = 0; i < sizeof(md->tokens) / sizeof(md->tokens[0]); i++) { |
---|
98 | g_free(md->tokens[i]); |
---|
99 | } |
---|
100 | g_free(md->lock_key); |
---|
101 | g_free(md->pp_policy); |
---|
102 | g_free(md->uuid); |
---|
103 | |
---|
104 | while (md->groups) { |
---|
105 | struct msn_group *mg = md->groups->data; |
---|
106 | md->groups = g_slist_remove(md->groups, mg); |
---|
107 | g_free(mg->id); |
---|
108 | g_free(mg->name); |
---|
109 | g_free(mg); |
---|
110 | } |
---|
111 | |
---|
112 | g_free(md->profile_rid); |
---|
113 | |
---|
114 | if (md->domaintree) { |
---|
115 | g_tree_destroy(md->domaintree); |
---|
116 | } |
---|
117 | md->domaintree = NULL; |
---|
118 | |
---|
119 | while (md->grpq) { |
---|
120 | struct msn_groupadd *ga = md->grpq->data; |
---|
121 | md->grpq = g_slist_remove(md->grpq, ga); |
---|
122 | g_free(ga->group); |
---|
123 | g_free(ga->who); |
---|
124 | g_free(ga); |
---|
125 | } |
---|
126 | |
---|
127 | g_free(md); |
---|
128 | } |
---|
129 | |
---|
130 | for (l = ic->permit; l; l = l->next) { |
---|
131 | g_free(l->data); |
---|
132 | } |
---|
133 | g_slist_free(ic->permit); |
---|
134 | |
---|
135 | for (l = ic->deny; l; l = l->next) { |
---|
136 | g_free(l->data); |
---|
137 | } |
---|
138 | g_slist_free(ic->deny); |
---|
139 | |
---|
140 | msn_connections = g_slist_remove(msn_connections, ic); |
---|
141 | } |
---|
142 | |
---|
143 | static int msn_buddy_msg(struct im_connection *ic, char *who, char *message, int away) |
---|
144 | { |
---|
145 | struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who); |
---|
146 | |
---|
147 | #ifdef DEBUG |
---|
148 | if (strcmp(who, "raw") == 0) { |
---|
149 | msn_ns_write(ic, -1, "%s\r\n", message); |
---|
150 | return 0; |
---|
151 | } |
---|
152 | #endif |
---|
153 | |
---|
154 | msn_ns_sendmessage(ic, bu, message); |
---|
155 | return(0); |
---|
156 | } |
---|
157 | |
---|
158 | static GList *msn_away_states(struct im_connection *ic) |
---|
159 | { |
---|
160 | static GList *l = NULL; |
---|
161 | int i; |
---|
162 | |
---|
163 | if (l == NULL) { |
---|
164 | for (i = 0; *msn_away_state_list[i].code; i++) { |
---|
165 | if (*msn_away_state_list[i].name) { |
---|
166 | l = g_list_append(l, (void *) msn_away_state_list[i].name); |
---|
167 | } |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | return l; |
---|
172 | } |
---|
173 | |
---|
174 | static void msn_set_away(struct im_connection *ic, char *state, char *message) |
---|
175 | { |
---|
176 | struct msn_data *md = ic->proto_data; |
---|
177 | char *nick, *psm, *idle, *statecode, *body, *buf; |
---|
178 | |
---|
179 | if (state == NULL) { |
---|
180 | md->away_state = msn_away_state_list; |
---|
181 | } else if ((md->away_state = msn_away_state_by_name(state)) == NULL) { |
---|
182 | md->away_state = msn_away_state_list + 1; |
---|
183 | } |
---|
184 | |
---|
185 | statecode = (char *) md->away_state->code; |
---|
186 | nick = set_getstr(&ic->acc->set, "display_name"); |
---|
187 | psm = message ? message : ""; |
---|
188 | idle = (strcmp(statecode, "IDL") == 0) ? "false" : "true"; |
---|
189 | |
---|
190 | body = g_markup_printf_escaped(MSN_PUT_USER_BODY, |
---|
191 | nick, psm, psm, md->uuid, statecode, md->uuid, idle, statecode, |
---|
192 | MSN_CAP1, MSN_CAP2, MSN_CAP1, MSN_CAP2 |
---|
193 | ); |
---|
194 | |
---|
195 | buf = g_strdup_printf(MSN_PUT_HEADERS, ic->acc->user, ic->acc->user, md->uuid, |
---|
196 | "/user", "application/user+xml", |
---|
197 | strlen(body), body); |
---|
198 | msn_ns_write(ic, -1, "PUT %d %zd\r\n%s", ++md->trId, strlen(buf), buf); |
---|
199 | |
---|
200 | g_free(buf); |
---|
201 | g_free(body); |
---|
202 | } |
---|
203 | |
---|
204 | static void msn_get_info(struct im_connection *ic, char *who) |
---|
205 | { |
---|
206 | /* Just make an URL and let the user fetch the info */ |
---|
207 | imcb_log(ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who); |
---|
208 | } |
---|
209 | |
---|
210 | static void msn_add_buddy(struct im_connection *ic, char *who, char *group) |
---|
211 | { |
---|
212 | struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who); |
---|
213 | |
---|
214 | msn_buddy_list_add(ic, MSN_BUDDY_FL, who, who, group); |
---|
215 | if (bu && bu->group) { |
---|
216 | msn_buddy_list_remove(ic, MSN_BUDDY_FL, who, bu->group->name); |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | static void msn_remove_buddy(struct im_connection *ic, char *who, char *group) |
---|
221 | { |
---|
222 | msn_buddy_list_remove(ic, MSN_BUDDY_FL, who, NULL); |
---|
223 | } |
---|
224 | |
---|
225 | static void msn_chat_msg(struct groupchat *c, char *message, int flags) |
---|
226 | { |
---|
227 | /* TODO: groupchats*/ |
---|
228 | } |
---|
229 | |
---|
230 | static void msn_chat_invite(struct groupchat *c, char *who, char *message) |
---|
231 | { |
---|
232 | /* TODO: groupchats*/ |
---|
233 | } |
---|
234 | |
---|
235 | static void msn_chat_leave(struct groupchat *c) |
---|
236 | { |
---|
237 | /* TODO: groupchats*/ |
---|
238 | } |
---|
239 | |
---|
240 | static struct groupchat *msn_chat_with(struct im_connection *ic, char *who) |
---|
241 | { |
---|
242 | /* TODO: groupchats*/ |
---|
243 | struct groupchat *c = imcb_chat_new(ic, who); |
---|
244 | return c; |
---|
245 | } |
---|
246 | |
---|
247 | static void msn_keepalive(struct im_connection *ic) |
---|
248 | { |
---|
249 | msn_ns_write(ic, -1, "PNG\r\n"); |
---|
250 | } |
---|
251 | |
---|
252 | static void msn_add_permit(struct im_connection *ic, char *who) |
---|
253 | { |
---|
254 | msn_buddy_list_add(ic, MSN_BUDDY_AL, who, who, NULL); |
---|
255 | } |
---|
256 | |
---|
257 | static void msn_rem_permit(struct im_connection *ic, char *who) |
---|
258 | { |
---|
259 | msn_buddy_list_remove(ic, MSN_BUDDY_AL, who, NULL); |
---|
260 | } |
---|
261 | |
---|
262 | static void msn_add_deny(struct im_connection *ic, char *who) |
---|
263 | { |
---|
264 | msn_buddy_list_add(ic, MSN_BUDDY_BL, who, who, NULL); |
---|
265 | } |
---|
266 | |
---|
267 | static void msn_rem_deny(struct im_connection *ic, char *who) |
---|
268 | { |
---|
269 | msn_buddy_list_remove(ic, MSN_BUDDY_BL, who, NULL); |
---|
270 | } |
---|
271 | |
---|
272 | static int msn_send_typing(struct im_connection *ic, char *who, int typing) |
---|
273 | { |
---|
274 | struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who); |
---|
275 | |
---|
276 | if (!(bu->flags & BEE_USER_ONLINE)) { |
---|
277 | return 0; |
---|
278 | } else if (typing & OPT_TYPING) { |
---|
279 | return(msn_buddy_msg(ic, who, TYPING_NOTIFICATION_MESSAGE, 0)); |
---|
280 | } else { |
---|
281 | return 1; |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | static char *set_eval_display_name(set_t *set, char *value) |
---|
286 | { |
---|
287 | account_t *acc = set->data; |
---|
288 | struct im_connection *ic = acc->ic; |
---|
289 | struct msn_data *md = ic->proto_data; |
---|
290 | |
---|
291 | if (md->flags & MSN_EMAIL_UNVERIFIED) { |
---|
292 | imcb_log(ic, "Warning: Your e-mail address is unverified. MSN doesn't allow " |
---|
293 | "changing your display name until your e-mail address is verified."); |
---|
294 | } |
---|
295 | |
---|
296 | if (md->flags & MSN_GOT_PROFILE_DN) { |
---|
297 | msn_soap_profile_set_dn(ic, value); |
---|
298 | } else { |
---|
299 | msn_soap_addressbook_set_display_name(ic, value); |
---|
300 | } |
---|
301 | |
---|
302 | return msn_ns_set_display_name(ic, value) ? value : NULL; |
---|
303 | } |
---|
304 | |
---|
305 | static void msn_buddy_data_add(bee_user_t *bu) |
---|
306 | { |
---|
307 | struct msn_data *md = bu->ic->proto_data; |
---|
308 | struct msn_buddy_data *bd; |
---|
309 | char *handle; |
---|
310 | |
---|
311 | bd = bu->data = g_new0(struct msn_buddy_data, 1); |
---|
312 | g_tree_insert(md->domaintree, bu->handle, bu); |
---|
313 | |
---|
314 | for (handle = bu->handle; g_ascii_isdigit(*handle); handle++) { |
---|
315 | ; |
---|
316 | } |
---|
317 | if (*handle == ':') { |
---|
318 | /* Pass a nick hint so hopefully the stupid numeric prefix |
---|
319 | won't show up to the user. */ |
---|
320 | char *s = strchr(++handle, '@'); |
---|
321 | if (s) { |
---|
322 | handle = g_strndup(handle, s - handle); |
---|
323 | imcb_buddy_nick_hint(bu->ic, bu->handle, handle); |
---|
324 | g_free(handle); |
---|
325 | } |
---|
326 | |
---|
327 | bd->flags |= MSN_BUDDY_FED; |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | static void msn_buddy_data_free(bee_user_t *bu) |
---|
332 | { |
---|
333 | struct msn_data *md = bu->ic->proto_data; |
---|
334 | struct msn_buddy_data *bd = bu->data; |
---|
335 | |
---|
336 | g_free(bd->cid); |
---|
337 | g_free(bd); |
---|
338 | |
---|
339 | g_tree_remove(md->domaintree, bu->handle); |
---|
340 | } |
---|
341 | |
---|
342 | GList *msn_buddy_action_list(bee_user_t *bu) |
---|
343 | { |
---|
344 | static GList *ret = NULL; |
---|
345 | |
---|
346 | if (ret == NULL) { |
---|
347 | static const struct buddy_action ba[2] = { |
---|
348 | { "NUDGE", "Draw attention" }, |
---|
349 | }; |
---|
350 | |
---|
351 | ret = g_list_prepend(ret, (void *) ba + 0); |
---|
352 | } |
---|
353 | |
---|
354 | return ret; |
---|
355 | } |
---|
356 | |
---|
357 | void *msn_buddy_action(struct bee_user *bu, const char *action, char * const args[], void *data) |
---|
358 | { |
---|
359 | if (g_strcasecmp(action, "NUDGE") == 0) { |
---|
360 | msn_buddy_msg(bu->ic, bu->handle, NUDGE_MESSAGE, 0); |
---|
361 | } |
---|
362 | |
---|
363 | return NULL; |
---|
364 | } |
---|
365 | |
---|
366 | void msn_initmodule() |
---|
367 | { |
---|
368 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
369 | |
---|
370 | ret->name = "msn"; |
---|
371 | ret->mms = 1409; /* this guess taken from libotr UPGRADING file */ |
---|
372 | ret->login = msn_login; |
---|
373 | ret->init = msn_init; |
---|
374 | ret->logout = msn_logout; |
---|
375 | ret->buddy_msg = msn_buddy_msg; |
---|
376 | ret->away_states = msn_away_states; |
---|
377 | ret->set_away = msn_set_away; |
---|
378 | ret->get_info = msn_get_info; |
---|
379 | ret->add_buddy = msn_add_buddy; |
---|
380 | ret->remove_buddy = msn_remove_buddy; |
---|
381 | ret->chat_msg = msn_chat_msg; |
---|
382 | ret->chat_invite = msn_chat_invite; |
---|
383 | ret->chat_leave = msn_chat_leave; |
---|
384 | ret->chat_with = msn_chat_with; |
---|
385 | ret->keepalive = msn_keepalive; |
---|
386 | ret->add_permit = msn_add_permit; |
---|
387 | ret->rem_permit = msn_rem_permit; |
---|
388 | ret->add_deny = msn_add_deny; |
---|
389 | ret->rem_deny = msn_rem_deny; |
---|
390 | ret->send_typing = msn_send_typing; |
---|
391 | ret->handle_cmp = g_strcasecmp; |
---|
392 | ret->buddy_data_add = msn_buddy_data_add; |
---|
393 | ret->buddy_data_free = msn_buddy_data_free; |
---|
394 | ret->buddy_action_list = msn_buddy_action_list; |
---|
395 | ret->buddy_action = msn_buddy_action; |
---|
396 | |
---|
397 | register_protocol(ret); |
---|
398 | } |
---|