comparison src/protocols/msn/userlist.c @ 9193:502707ca1836

[gaim-migrate @ 9988] Patch by Felipe Contreras to add MSN file transfer and buddy icons. Please test and report any bugs! committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Sun, 06 Jun 2004 02:39:08 +0000
parents
children 364aa73323b5
comparison
equal deleted inserted replaced
9192:5655dcd94d0f 9193:502707ca1836
1 #include "msn.h"
2 #include "userlist.h"
3
4 const char *lists[] = { "FL", "AL", "BL", "RL" };
5
6 typedef struct
7 {
8 GaimConnection *gc;
9 char *who;
10
11 } MsnPermitAdd;
12
13 /**************************************************************************
14 * Callbacks
15 **************************************************************************/
16 static void
17 msn_accept_add_cb(MsnPermitAdd *pa)
18 {
19 if (g_list_find(gaim_connections_get_all(), pa->gc) != NULL)
20 {
21 MsnSession *session = pa->gc->proto_data;
22 MsnUserList *userlist = session->userlist;
23
24 msn_userlist_add_buddy(userlist, pa->who, MSN_LIST_AL, NULL);
25
26 /* TODO: This ask for the alias, right? */
27 gaim_account_notify_added(pa->gc->account, NULL, pa->who, NULL, NULL);
28 }
29
30 g_free(pa->who);
31 g_free(pa);
32 }
33
34 static void
35 msn_cancel_add_cb(MsnPermitAdd *pa)
36 {
37 if (g_list_find(gaim_connections_get_all(), pa->gc) != NULL)
38 {
39 MsnSession *session = pa->gc->proto_data;
40 MsnUserList *userlist = session->userlist;
41
42 msn_userlist_add_buddy(userlist, pa->who, MSN_LIST_BL, NULL);
43 }
44
45 g_free(pa->who);
46 g_free(pa);
47 }
48
49 static void
50 got_new_entry(GaimConnection *gc, const char *passport,
51 const char *friendly)
52 {
53 MsnPermitAdd *pa;
54 char *msg;
55
56 pa = g_new0(MsnPermitAdd, 1);
57 pa->who = g_strdup(passport);
58 pa->gc = gc;
59
60 msg = g_strdup_printf(
61 _("The user %s (%s) wants to add %s to his or her buddy list."),
62 passport, friendly, gaim_account_get_username(gc->account));
63
64 gaim_request_action(gc, NULL, msg, NULL, 0, pa, 2,
65 _("Authorize"), G_CALLBACK(msn_accept_add_cb),
66 _("Deny"), G_CALLBACK(msn_cancel_add_cb));
67
68 g_free(msg);
69 }
70
71 /**************************************************************************
72 * Utility functions
73 **************************************************************************/
74
75 static gboolean
76 user_is_in_group(MsnUser *user, int group_id)
77 {
78 if (user == NULL)
79 return FALSE;
80
81 if (group_id < 0)
82 return FALSE;
83
84 if (g_list_find(user->group_ids, GINT_TO_POINTER(group_id)))
85 return TRUE;
86
87 return FALSE;
88 }
89
90 static gboolean
91 user_is_there(MsnUser *user, int list_id, int group_id)
92 {
93 int list_op;
94
95 if (user == NULL)
96 return FALSE;
97
98 list_op = 1 << list_id;
99
100 if (!(user->list_op & list_op))
101 return FALSE;
102
103 if (list_id == MSN_LIST_FL)
104 {
105 if (group_id >= 0)
106 return user_is_in_group(user, group_id);
107 }
108
109 return TRUE;
110 }
111
112 static const char*
113 get_store_name(MsnUser *user)
114 {
115 const char *store_name;
116
117 g_return_val_if_fail(user != NULL, NULL);
118
119 if ((store_name = msn_user_get_store_name(user)) != NULL)
120 return gaim_url_encode(store_name);
121
122 return msn_user_get_passport(user);
123 }
124
125 static void
126 msn_request_add_group(MsnUserList *userlist, const char *who,
127 const char *old_group_name, const char *new_group_name)
128 {
129 MsnCmdProc *cmdproc;
130 MsnTransaction *trans;
131
132 cmdproc = userlist->session->notification->cmdproc;
133 MsnMoveBuddy *data = g_new0(MsnMoveBuddy, 1);
134
135 data->who = g_strdup(who);
136
137 if (old_group_name)
138 data->old_group_name = g_strdup(old_group_name);
139
140 trans = msn_transaction_new("ADG", "%s %d",
141 gaim_url_encode(new_group_name),
142 0);
143
144 msn_transaction_set_data(trans, data);
145
146 msn_cmdproc_send_trans(cmdproc, trans);
147 }
148
149 /**************************************************************************
150 * Server functions
151 **************************************************************************/
152
153 MsnListId
154 msn_get_list_id(const char *list)
155 {
156 if (list[0] == 'F')
157 return MSN_LIST_FL;
158 else if (list[0] == 'A')
159 return MSN_LIST_AL;
160 else if (list[0] == 'B')
161 return MSN_LIST_BL;
162 else if (list[0] == 'R')
163 return MSN_LIST_RL;
164
165 return -1;
166 }
167
168 void
169 msn_got_add_user(MsnSession *session, MsnUser *user,
170 MsnListId list_id, int group_id)
171 {
172 GaimAccount *account;
173 const char *passport;
174 const char *friendly;
175
176 account = session->account;
177
178 passport = msn_user_get_passport(user);
179 friendly = msn_user_get_friendly_name(user);
180
181 if (list_id == MSN_LIST_FL)
182 {
183 GaimConnection *gc = gaim_account_get_connection(account);
184
185 serv_got_alias(gc, passport, friendly);
186
187 if (group_id >= 0)
188 {
189 msn_user_add_group_id(user, group_id);
190 return;
191 }
192 else
193 {
194 /* session->sync->fl_users_count++; */
195 }
196 }
197 else if (list_id == MSN_LIST_AL)
198 {
199 gaim_privacy_permit_add(account, passport, TRUE);
200 }
201 else if (list_id == MSN_LIST_BL)
202 {
203 gaim_privacy_deny_add(account, passport, TRUE);
204 }
205 else if (list_id == MSN_LIST_RL)
206 {
207 GaimConnection *gc = gaim_account_get_connection(account);
208
209 gaim_debug_info("msn",
210 "%s has added you to his or her contact list.\n",
211 passport);
212
213 if (!(user->list_op & (MSN_LIST_AL_OP | MSN_LIST_BL_OP)))
214 got_new_entry(gc, passport, friendly);
215 }
216
217 user->list_op |= (1 << list_id);
218 /* gaim_user_add_list_id (user, list_id); */
219 }
220
221 void
222 msn_got_rem_user(MsnSession *session, MsnUser *user,
223 MsnListId list_id, int group_id)
224 {
225 GaimAccount *account;
226 const char *passport;
227
228 account = session->account;
229
230 passport = msn_user_get_passport(user);
231
232 if (list_id == MSN_LIST_FL)
233 {
234 /* TODO: When is the user totally removed? */
235 if (group_id >= 0)
236 {
237 msn_user_remove_group_id(user, group_id);
238 return;
239 }
240 else
241 {
242 /* session->sync->fl_users_count--; */
243 }
244 }
245 else if (list_id == MSN_LIST_AL)
246 {
247 gaim_privacy_permit_remove(account, passport, TRUE);
248 }
249 else if (list_id == MSN_LIST_BL)
250 {
251 gaim_privacy_deny_remove(account, passport, TRUE);
252 }
253 else if (list_id == MSN_LIST_RL)
254 {
255 gaim_debug_info("msn",
256 "%s has removed you from his or her contact list.\n",
257 passport);
258 }
259
260 user->list_op &= ~(1 << list_id);
261 /* gaim_user_remove_list_id (user, list_id); */
262
263 if (user->list_op == 0)
264 {
265 gaim_debug_info("msn", "Buddy '%s' shall be deleted?.\n",
266 passport);
267
268 }
269 }
270
271 void
272 msn_got_lst_user(MsnSession *session, MsnUser *user,
273 int list_op, GSList *group_ids)
274 {
275 GaimConnection *gc;
276 GaimAccount *account;
277 const char *passport;
278 const char *store;
279
280 account = session->account;
281 gc = gaim_account_get_connection(account);
282
283 passport = msn_user_get_passport(user);
284 store = msn_user_get_store_name(user);
285
286 if (list_op & MSN_LIST_FL_OP)
287 {
288 GSList *c;
289 for (c = group_ids; c != NULL; c = g_slist_next(c))
290 {
291 int group_id;
292 group_id = GPOINTER_TO_INT(c->data);
293 msn_user_add_group_id(user, group_id);
294 }
295
296 /* FIXME: It might be a real alias */
297 /* serv_got_alias(gc, passport, store); */
298 }
299
300 if (list_op & MSN_LIST_AL_OP)
301 {
302 /* These are users who are allowed to see our status. */
303
304 if (g_slist_find_custom(account->deny, passport,
305 (GCompareFunc)strcmp))
306 {
307 gaim_privacy_deny_remove(gc->account, passport, TRUE);
308 }
309
310 gaim_privacy_permit_add(account, passport, TRUE);
311 }
312
313 if (list_op & MSN_LIST_BL_OP)
314 {
315 /* These are users who are not allowed to see our status. */
316
317 if (g_slist_find_custom(account->permit, passport,
318 (GCompareFunc)strcmp))
319 {
320 gaim_privacy_permit_remove(gc->account, passport, TRUE);
321 }
322
323 gaim_privacy_deny_add(account, passport, TRUE);
324 }
325
326 if (list_op & MSN_LIST_RL_OP)
327 {
328 /* These are users who have us on their contact list. */
329 /* TODO: what does store name is when this happens? */
330
331 if (!(list_op & (MSN_LIST_AL_OP | MSN_LIST_BL_OP)))
332 got_new_entry(gc, passport, store);
333 }
334
335 user->list_op = list_op;
336 }
337
338 /**************************************************************************
339 * UserList functions
340 **************************************************************************/
341
342 MsnUserList*
343 msn_userlist_new(MsnSession *session)
344 {
345 MsnUserList *userlist;
346
347 userlist = g_new0(MsnUserList, 1);
348
349 userlist->session = session;
350
351 return userlist;
352 }
353
354 void
355 msn_userlist_destroy(MsnUserList *userlist)
356 {
357 GList *l;
358
359 for (l = userlist->users; l != NULL; l = l->next)
360 {
361 msn_user_destroy(l->data);
362 }
363
364 g_list_free(userlist->users);
365
366 for (l = userlist->groups; l != NULL; l = l->next)
367 {
368 msn_group_destroy(l->data);
369 }
370
371 g_list_free(userlist->groups);
372 }
373
374 void
375 msn_userlist_add_user(MsnUserList *userlist, MsnUser *user)
376 {
377 userlist->users = g_list_append(userlist->users, user);
378 }
379
380 void
381 msn_userlist_remove_user(MsnUserList *userlist, MsnUser *user)
382 {
383 userlist->users = g_list_remove(userlist->users, user);
384 }
385
386 MsnUser *
387 msn_userlist_find_user(MsnUserList *userlist, const char *passport)
388 {
389 GList *l;
390
391 g_return_val_if_fail(passport != NULL, NULL);
392
393 for (l = userlist->users; l != NULL; l = l->next)
394 {
395 MsnUser *user = (MsnUser *)l->data;
396
397 g_return_val_if_fail(user->passport != NULL, NULL);
398
399 if (!strcmp(passport, user->passport))
400 return user;
401 }
402
403 return NULL;
404 }
405
406 void
407 msn_userlist_add_group(MsnUserList *userlist, MsnGroup *group)
408 {
409 userlist->groups = g_list_append(userlist->groups, group);
410 }
411
412 void
413 msn_userlist_remove_group(MsnUserList *userlist, MsnGroup *group)
414 {
415 userlist->groups = g_list_remove(userlist->groups, group);
416 }
417
418 MsnGroup *
419 msn_userlist_find_group_with_id(MsnUserList *userlist, int id)
420 {
421 GList *l;
422
423 g_return_val_if_fail(userlist != NULL, NULL);
424 g_return_val_if_fail(id >= 0, NULL);
425
426 for (l = userlist->groups; l != NULL; l = l->next)
427 {
428 MsnGroup *group = l->data;
429
430 if (group->id == id)
431 return group;
432 }
433
434 return NULL;
435 }
436
437 MsnGroup *
438 msn_userlist_find_group_with_name(MsnUserList *userlist, const char *name)
439 {
440 GList *l;
441
442 g_return_val_if_fail(userlist != NULL, NULL);
443 g_return_val_if_fail(name != NULL, NULL);
444
445 for (l = userlist->groups; l != NULL; l = l->next)
446 {
447 MsnGroup *group = l->data;
448
449 if ((group->name != NULL) && !g_ascii_strcasecmp(name, group->name))
450 return group;
451 }
452
453 return NULL;
454 }
455
456 int
457 msn_userlist_find_group_id(MsnUserList *userlist, const char *group_name)
458 {
459 MsnGroup *group;
460
461 group = msn_userlist_find_group_with_name(userlist, group_name);
462
463 if (group != NULL)
464 return msn_group_get_id(group);
465 else
466 return -1;
467 }
468
469 const char *
470 msn_userlist_find_group_name(MsnUserList *userlist, int group_id)
471 {
472 MsnGroup *group;
473
474 group = msn_userlist_find_group_with_id(userlist, group_id);
475
476 if (group != NULL)
477 return msn_group_get_name(group);
478 else
479 return NULL;
480 }
481
482 void
483 msn_userlist_rename_group_id(MsnUserList *userlist, int group_id,
484 const char *new_name)
485 {
486 MsnGroup *group;
487
488 group = msn_userlist_find_group_with_id(userlist, group_id);
489
490 if (group != NULL)
491 msn_group_set_name(group, new_name);
492 }
493
494 void
495 msn_userlist_remove_group_id(MsnUserList *userlist, int group_id)
496 {
497 MsnGroup *group;
498
499 group = msn_userlist_find_group_with_id(userlist, group_id);
500
501 if (group != NULL)
502 msn_userlist_remove_group(userlist, group);
503 }
504
505 void
506 msn_userlist_rem_buddy(MsnUserList *userlist,
507 const char *who, int list_id, const char *group_name)
508 {
509 MsnUser *user;
510 int group_id;
511 const char *list;
512
513 user = msn_userlist_find_user(userlist, who);
514 group_id = -1;
515
516 if (group_name != NULL)
517 {
518 group_id = msn_userlist_find_group_id(userlist, group_name);
519
520 if (group_id < 0)
521 {
522 /* Whoa, there is no such group. */
523 gaim_debug_error("msn", "Group doesn't exist: %s\n", group_name);
524 return;
525 }
526 }
527
528 /* First we're going to check if not there. */
529 if (!(user_is_there(user, list_id, group_id)))
530 {
531 list = lists[list_id];
532 gaim_debug_error("msn", "User '%s' is not there: %s\n",
533 who, list);
534 return;
535 }
536
537 /* Then request the rem to the server. */
538 list = lists[list_id];
539
540 msn_notification_rem_buddy(userlist->session->notification, list, who, group_id);
541 }
542
543 void
544 msn_userlist_add_buddy(MsnUserList *userlist,
545 const char *who, int list_id,
546 const char *group_name)
547 {
548 MsnUser *user;
549 int group_id;
550 const char *list;
551 const char *store_name;
552
553 group_id = -1;
554
555 if (group_name != NULL)
556 {
557 group_id = msn_userlist_find_group_id(userlist, group_name);
558
559 if (group_id < 0)
560 {
561 /* Whoa, we must add that group first. */
562 msn_request_add_group(userlist, who, NULL, group_name);
563 return;
564 }
565 }
566
567 user = msn_userlist_find_user(userlist, who);
568
569 /* First we're going to check if it's alredy there. */
570 if (user_is_there(user, list_id, group_id))
571 {
572 list = lists[list_id];
573 gaim_debug_error("msn", "User '%s' is alredy there: %s\n",
574 who, list);
575 return;
576 }
577
578 store_name = (user != NULL) ? get_store_name(user) : who;
579
580 /* Then request the add to the server. */
581 list = lists[list_id];
582
583 msn_notification_add_buddy(userlist->session->notification, list, who,
584 store_name, group_id);
585 }
586
587 void
588 msn_userlist_move_buddy(MsnUserList *userlist, const char *who,
589 const char *old_group_name, const char *new_group_name)
590 {
591 int new_group_id;
592
593 new_group_id = msn_userlist_find_group_id(userlist, new_group_name);
594
595 if (new_group_id < 0)
596 {
597 msn_request_add_group(userlist, who, old_group_name, new_group_name);
598 return;
599 }
600
601 msn_userlist_rem_buddy(userlist, who, MSN_LIST_FL, old_group_name);
602 msn_userlist_add_buddy(userlist, who, MSN_LIST_FL, new_group_name);
603 }