forked from felipec/msn-pecan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync.c
362 lines (297 loc) · 9.04 KB
/
sync.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
* Copyright (C) 2007-2009 Felipe Contreras
* Copyright (C) 1998-2006 Pidgin (see pidgin-copyright)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "session.h"
#include "sync.h"
#include "pn_log.h"
#include "session_private.h"
#include "cmd/command_private.h"
#include "cmd/cmdproc_private.h"
#include "ab/pn_group.h"
#include "ab/pn_contact_priv.h"
#include <string.h>
#include "pn_util.h"
/* libpurple stuff. */
#include <account.h>
#include <privacy.h>
static MsnTable *cbs_table;
static void
blp_cmd (MsnCmdProc *cmdproc,
MsnCommand *cmd)
{
MsnSession *session;
PurpleAccount *account;
const char *list_name;
list_name = cmd->params[0];
session = cmdproc->session;
account = msn_session_get_user_data (session);
if (g_ascii_strcasecmp (list_name, "AL") == 0)
{
/*
* If the current setting is AL, messages from users who
* are not in BL will be delivered.
*
* In other words, deny some.
*/
account->perm_deny = PURPLE_PRIVACY_DENY_USERS;
session->default_permission = PN_PERM_ALLOW;
}
else
{
/* If the current setting is BL, only messages from people
* who are in the AL will be delivered.
*
* In other words, permit some.
*/
account->perm_deny = PURPLE_PRIVACY_ALLOW_USERS;
session->default_permission = PN_PERM_DENY;
}
}
static void
prp_cmd (MsnCmdProc *cmdproc,
MsnCommand *cmd)
{
MsnSession *session = cmdproc->session;
const gchar *type, *value;
struct pn_contact *user;
type = cmd->params[0];
value = cmd->params[1];
user = msn_session_get_contact (session);
if (cmd->param_count == 2)
{
gchar *tmp;
tmp = pn_url_decode (value);
if (strcmp (type, "PHH") == 0)
pn_contact_set_home_phone (user, tmp);
else if (strcmp (type, "PHW") == 0)
pn_contact_set_work_phone (user, tmp);
else if (strcmp (type, "PHM") == 0)
pn_contact_set_mobile_phone (user, tmp);
else if (strcmp (type, "MFN") == 0)
{
PurpleAccount *account;
PurpleConnection *connection;
const gchar* friendly_name;
account = msn_session_get_user_data (session);
connection = purple_account_get_connection (account);
friendly_name = purple_account_get_string (account, "friendly_name", NULL);
/*
* The server doesn't seem to store the friendly name anymore,
* store it in account opts.
*/
if (friendly_name)
msn_session_set_public_alias (session, friendly_name);
else
purple_account_set_string (account, "friendly_name", tmp);
purple_connection_set_display_name (connection, friendly_name);
}
g_free (tmp);
}
else
{
if (strcmp (type, "PHH") == 0)
pn_contact_set_home_phone (user, NULL);
else if (strcmp (type, "PHW") == 0)
pn_contact_set_work_phone (user, NULL);
else if (strcmp (type, "PHM") == 0)
pn_contact_set_mobile_phone (user, NULL);
}
}
static void
lsg_cmd (MsnCmdProc *cmdproc,
MsnCommand *cmd)
{
MsnSession *session = cmdproc->session;
char *name;
const gchar *group_guid;
name = pn_url_decode (cmd->params[0]);
group_guid = cmd->params[1];
if (strcmp(name, MSN_NULL_GROUP_NAME) == 0) {
pn_error("Invalid group name, ignoring");
goto leave;
}
pn_group_new (session->contactlist, name, group_guid);
if (!purple_find_group (name))
{
PurpleGroup *g = purple_group_new (name);
purple_blist_add_group (g, NULL);
}
/* Group of ungroupped buddies */
if (!group_guid)
{
if (session->sync->total_users == 0)
{
cmdproc->cbs_table = session->sync->old_cbs_table;
msn_session_finish_login (session);
msn_sync_destroy (session->sync);
session->sync = NULL;
}
}
leave:
g_free (name);
}
static void
lst_cmd (MsnCmdProc *cmdproc,
MsnCommand *cmd)
{
MsnSession *session = cmdproc->session;
const gchar *passport = NULL;
struct pn_contact *user;
gchar *friendly = NULL;
const gchar *user_guid = NULL;
int list_op = -1;
gint type;
guint i;
for (i = 0; i < cmd->param_count; i++)
{
const char *chopped_str;
chopped_str = cmd->params[i] + 2;
/* Check for Name/email. */
if (strncmp (cmd->params[i], "N=", 2) == 0)
passport = chopped_str;
/* Check for Friendlyname. */
else if (strncmp (cmd->params[i], "F=", 2) == 0)
friendly = pn_url_decode (chopped_str);
/* Check for Contact GUID. */
else if (strncmp (cmd->params[i], "C=", 2) == 0)
user_guid = chopped_str;
else
break;
}
list_op = g_ascii_strtod (cmd->params[i++], NULL);
type = g_ascii_strtod (cmd->params[i++], NULL);
user = pn_contact_new (session->contactlist);
pn_contact_set_passport (user, passport);
pn_contact_set_guid (user, user_guid);
session->sync->last_user = user;
/* TODO: This can be improved */
if (list_op & MSN_LIST_FL_OP)
{
if (cmd->params[i])
{
gchar **c;
gchar **tokens;
const gchar *group_guids;
GSList *group_ids;
group_guids = cmd->params[i];
group_ids = NULL;
tokens = g_strsplit (group_guids, ",", -1);
for (c = tokens; *c; c++)
{
group_ids = g_slist_append (group_ids, g_strdup (*c));
}
g_strfreev (tokens);
msn_got_lst_contact (session, user, friendly, list_op, group_ids);
g_slist_foreach (group_ids, (GFunc) g_free, NULL);
g_slist_free (group_ids);
}
else
{
msn_got_lst_contact (session, user, friendly, list_op, NULL);
}
}
else
{
msn_got_lst_contact (session, user, friendly, list_op, NULL);
}
g_free (friendly);
session->sync->num_users++;
if (session->sync->num_users == session->sync->total_users)
{
cmdproc->cbs_table = session->sync->old_cbs_table;
msn_session_finish_login (session);
msn_sync_destroy (session->sync);
session->sync = NULL;
}
}
static void
bpr_cmd (MsnCmdProc *cmdproc,
MsnCommand *cmd)
{
MsnSync *sync = cmdproc->session->sync;
const char *type, *value;
struct pn_contact *user;
user = sync->last_user;
g_return_if_fail (user);
type = cmd->params[0];
value = cmd->params[1];
if (value)
{
if (strcmp(type, "MOB") == 0)
{
if (strcmp (value, "Y") == 0)
user->mobile = TRUE;
}
else
{
gchar *tmp;
tmp = pn_url_decode (value);
if (strcmp(type, "PHH") == 0)
pn_contact_set_home_phone (user, tmp);
else if (strcmp(type, "PHW") == 0)
pn_contact_set_work_phone (user, tmp);
else if (strcmp (type, "PHM") == 0)
pn_contact_set_mobile_phone (user, tmp);
g_free (tmp);
}
}
}
static void
not_cmd_post(MsnCmdProc *cmdproc, MsnCommand *cmd, char *payload, size_t len)
{
pn_info ("incoming notification: [%s]", payload);
}
static void
not_cmd(MsnCmdProc *cmdproc, MsnCommand *cmd)
{
cmd->payload_len = atoi(cmd->params[0]);
cmdproc->last_cmd->payload_cb = not_cmd_post;
}
void
msn_sync_init (void)
{
/* TODO: check prp, blp, bpr */
cbs_table = msn_table_new ();
/* Syncing */
msn_table_add_cmd (cbs_table, NULL, "GTC", NULL);
msn_table_add_cmd (cbs_table, NULL, "BLP", blp_cmd);
msn_table_add_cmd (cbs_table, NULL, "PRP", prp_cmd);
msn_table_add_cmd (cbs_table, NULL, "LSG", lsg_cmd);
msn_table_add_cmd (cbs_table, NULL, "LST", lst_cmd);
msn_table_add_cmd (cbs_table, NULL, "BPR", bpr_cmd);
msn_table_add_cmd (cbs_table, NULL, "NOT", not_cmd);
}
void
msn_sync_end (void)
{
msn_table_destroy (cbs_table);
}
MsnSync *
msn_sync_new (MsnSession *session)
{
MsnSync *sync;
sync = g_new0 (MsnSync, 1);
sync->session = session;
sync->cbs_table = cbs_table;
return sync;
}
void
msn_sync_destroy (MsnSync *sync)
{
g_free (sync);
}