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