source: protocols/oscar/ssi.c @ 9f8bb17

Last change on this file since 9f8bb17 was 6042a54, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-10-19T23:38:33Z

Massive cleanup in OSCAR.

  • Property mode set to 100644
File size: 35.1 KB
Line 
1/*
2 * Server-Side/Stored Information.
3 *
4 * Relatively new facility that allows storing of certain types of information,
5 * such as a users buddy list, permit/deny list, and permit/deny preferences,
6 * to be stored on the server, so that they can be accessed from any client.
7 *
8 * We keep a copy of the ssi data in sess->ssi, because the data needs to be
9 * accessed for various reasons.  So all the "aim_ssi_itemlist_bleh" functions
10 * near the top just manage the local data.
11 *
12 * The SNAC sending and receiving functions are lower down in the file, and
13 * they're simpler.  They are in the order of the subtypes they deal with,
14 * starting with the request rights function (subtype 0x0002), then parse
15 * rights (subtype 0x0003), then--well, you get the idea.
16 *
17 * This is entirely too complicated.
18 * You don't know the half of it.
19 *
20 * XXX - Test for memory leaks
21 * XXX - Better parsing of rights, and use the rights info to limit adds
22 *
23 */
24
25#include <aim.h>
26#include "ssi.h"
27
28/**
29 * Locally add a new item to the given item list.
30 *
31 * @param list A pointer to a pointer to the current list of items.
32 * @param parent A pointer to the parent group, or NULL if the item should have no
33 *        parent group (ie. the group ID# should be 0).
34 * @param name A null terminated string of the name of the new item, or NULL if the
35 *        item should have no name.
36 * @param type The type of the item, 0x0001 for a contact, 0x0002 for a group, etc.
37 * @return The newly created item.
38 */
39static struct aim_ssi_item *aim_ssi_itemlist_add(struct aim_ssi_item **list, struct aim_ssi_item *parent, char *name, guint16 type)
40{
41        int i;
42        struct aim_ssi_item *cur, *newitem;
43
44        if (!(newitem = g_new0(struct aim_ssi_item, 1)))
45                return NULL;
46
47        /* Set the name */
48        if (name) {
49                if (!(newitem->name = (char *)g_malloc((strlen(name)+1)*sizeof(char)))) {
50                        g_free(newitem);
51                        return NULL;
52                }
53                strcpy(newitem->name, name);
54        } else
55                newitem->name = NULL;
56
57        /* Set the group ID# and the buddy ID# */
58        newitem->gid = 0x0000;
59        newitem->bid = 0x0000;
60        if (type == AIM_SSI_TYPE_GROUP) {
61                if (name)
62                        do {
63                                newitem->gid += 0x0001;
64                                for (cur=*list, i=0; ((cur) && (!i)); cur=cur->next)
65                                        if ((cur->gid == newitem->gid) && (cur->gid == newitem->gid))
66                                                i=1;
67                        } while (i);
68        } else {
69                if (parent)
70                        newitem->gid = parent->gid;
71                do {
72                        newitem->bid += 0x0001;
73                        for (cur=*list, i=0; ((cur) && (!i)); cur=cur->next)
74                                if ((cur->bid == newitem->bid) && (cur->gid == newitem->gid))
75                                        i=1;
76                } while (i);
77        }
78
79        /* Set the rest */
80        newitem->type = type;
81        newitem->data = NULL;
82        newitem->next = *list;
83        *list = newitem;
84
85        return newitem;
86}
87
88/**
89 * Locally rebuild the 0x00c8 TLV in the additional data of the given group.
90 *
91 * @param list A pointer to a pointer to the current list of items.
92 * @param parentgroup A pointer to the group who's additional data you want to rebuild.
93 * @return Return 0 if no errors, otherwise return the error number.
94 */
95static int aim_ssi_itemlist_rebuildgroup(struct aim_ssi_item **list, struct aim_ssi_item *parentgroup)
96{
97        int newlen; //, i;
98        struct aim_ssi_item *cur;
99
100        /* Free the old additional data */
101        if (parentgroup->data) {
102                aim_freetlvchain((aim_tlvlist_t **)&parentgroup->data);
103                parentgroup->data = NULL;
104        }
105
106        /* Find the length for the new additional data */
107        newlen = 0;
108        if (parentgroup->gid == 0x0000) {
109                for (cur=*list; cur; cur=cur->next)
110                        if ((cur->gid != 0x0000) && (cur->type == AIM_SSI_TYPE_GROUP))
111                                newlen += 2;
112        } else {
113                for (cur=*list; cur; cur=cur->next)
114                        if ((cur->gid == parentgroup->gid) && (cur->type == AIM_SSI_TYPE_BUDDY))
115                                newlen += 2;
116        }
117
118        /* Rebuild the additional data */
119        if (newlen>0) {
120                guint8 *newdata;
121
122                if (!(newdata = (guint8 *)g_malloc((newlen)*sizeof(guint8))))
123                        return -ENOMEM;
124                newlen = 0;
125                if (parentgroup->gid == 0x0000) {
126                        for (cur=*list; cur; cur=cur->next)
127                                if ((cur->gid != 0x0000) && (cur->type == AIM_SSI_TYPE_GROUP))
128                                                newlen += aimutil_put16(newdata+newlen, cur->gid);
129                } else {
130                        for (cur=*list; cur; cur=cur->next)
131                                if ((cur->gid == parentgroup->gid) && (cur->type == AIM_SSI_TYPE_BUDDY))
132                                                newlen += aimutil_put16(newdata+newlen, cur->bid);
133                }
134                aim_addtlvtochain_raw((aim_tlvlist_t **)&(parentgroup->data), 0x00c8, newlen, newdata);
135
136                g_free(newdata);
137        }
138
139        return 0;
140}
141
142/**
143 * Locally free all of the stored buddy list information.
144 *
145 * @param sess The oscar session.
146 * @return Return 0 if no errors, otherwise return the error number.
147 */
148static int aim_ssi_freelist(aim_session_t *sess)
149{
150        struct aim_ssi_item *cur, *delitem;
151
152        cur = sess->ssi.items;
153        while (cur) {
154                if (cur->name)  g_free(cur->name);
155                if (cur->data)  aim_freetlvchain((aim_tlvlist_t **)&cur->data);
156                delitem = cur;
157                cur = cur->next;
158                g_free(delitem);
159        }
160
161        sess->ssi.items = NULL;
162        sess->ssi.revision = 0;
163        sess->ssi.timestamp = (time_t)0;
164
165        return 0;
166}
167
168/**
169 * Locally find an item given a group ID# and a buddy ID#.
170 *
171 * @param list A pointer to the current list of items.
172 * @param gid The group ID# of the desired item.
173 * @param bid The buddy ID# of the desired item.
174 * @return Return a pointer to the item if found, else return NULL;
175 */
176struct aim_ssi_item *aim_ssi_itemlist_find(struct aim_ssi_item *list, guint16 gid, guint16 bid)
177{
178        struct aim_ssi_item *cur;
179        for (cur=list; cur; cur=cur->next)
180                if ((cur->gid == gid) && (cur->bid == bid))
181                        return cur;
182        return NULL;
183}
184
185/**
186 * Locally find an item given a group name, screen name, and type.  If group name
187 * and screen name are null, then just return the first item of the given type.
188 *
189 * @param list A pointer to the current list of items.
190 * @param gn The group name of the desired item.
191 * @param bn The buddy name of the desired item.
192 * @param type The type of the desired item.
193 * @return Return a pointer to the item if found, else return NULL;
194 */
195struct aim_ssi_item *aim_ssi_itemlist_finditem(struct aim_ssi_item *list, char *gn, char *sn, guint16 type)
196{
197        struct aim_ssi_item *cur;
198        if (!list)
199                return NULL;
200
201        if (gn && sn) { /* For finding buddies in groups */
202                for (cur=list; cur; cur=cur->next)
203                        if ((cur->type == type) && (cur->name) && !(aim_sncmp(cur->name, sn))) {
204                                struct aim_ssi_item *curg;
205                                for (curg=list; curg; curg=curg->next)
206                                        if ((curg->type == AIM_SSI_TYPE_GROUP) && (curg->gid == cur->gid) && (curg->name) && !(aim_sncmp(curg->name, gn)))
207                                                return cur;
208                        }
209
210        } else if (sn) { /* For finding groups, permits, denies, and ignores */
211                for (cur=list; cur; cur=cur->next)
212                        if ((cur->type == type) && (cur->name) && !(aim_sncmp(cur->name, sn)))
213                                return cur;
214
215        /* For stuff without names--permit deny setting, visibility mask, etc. */
216        } else for (cur=list; cur; cur=cur->next) {
217                if (cur->type == type)
218                        return cur;
219        }
220
221        return NULL;
222}
223
224/**
225 * Locally find the parent item of the given buddy name.
226 *
227 * @param list A pointer to the current list of items.
228 * @param bn The buddy name of the desired item.
229 * @return Return a pointer to the item if found, else return NULL;
230 */
231struct aim_ssi_item *aim_ssi_itemlist_findparent(struct aim_ssi_item *list, char *sn)
232{
233        struct aim_ssi_item *cur, *curg;
234        if (!list || !sn)
235                return NULL;
236        if (!(cur = aim_ssi_itemlist_finditem(list, NULL, sn, AIM_SSI_TYPE_BUDDY)))
237                return NULL;
238        for (curg=list; curg; curg=curg->next)
239                if ((curg->type == AIM_SSI_TYPE_GROUP) && (curg->gid == cur->gid))
240                        return curg;
241        return NULL;
242}
243
244/**
245 * Locally find the permit/deny setting item, and return the setting.
246 *
247 * @param list A pointer to the current list of items.
248 * @return Return the current SSI permit deny setting, or 0 if no setting was found.
249 */
250int aim_ssi_getpermdeny(struct aim_ssi_item *list)
251{
252        struct aim_ssi_item *cur = aim_ssi_itemlist_finditem(list, NULL, NULL, AIM_SSI_TYPE_PDINFO);
253        if (cur) {
254                aim_tlvlist_t *tlvlist = cur->data;
255                if (tlvlist) {
256                        aim_tlv_t *tlv = aim_gettlv(tlvlist, 0x00ca, 1);
257                        if (tlv && tlv->value)
258                                return aimutil_get8(tlv->value);
259                }
260        }
261        return 0;
262}
263
264/**
265 * Add the given packet to the holding queue.  We totally need to send SSI SNACs one at
266 * a time, so we have a local queue where packets get put before they are sent, and
267 * then we send stuff one at a time, nice and orderly-like.
268 *
269 * @param sess The oscar session.
270 * @param conn The bos connection for this session.
271 * @param fr The newly created SNAC that you want to send.
272 * @return Return 0 if no errors, otherwise return the error number.
273 */
274static int aim_ssi_enqueue(aim_session_t *sess, aim_conn_t *conn, aim_frame_t *fr)
275{
276        aim_frame_t *cur;
277
278        if (!sess || !conn || !fr)
279                return -EINVAL;
280
281        fr->next = NULL;
282        if (sess->ssi.holding_queue == NULL) {
283                sess->ssi.holding_queue = fr;
284                if (!sess->ssi.waiting_for_ack)
285                        aim_ssi_modbegin(sess, conn);
286        } else {
287                for (cur = sess->ssi.holding_queue; cur->next; cur = cur->next) ;
288                cur->next = fr;
289        }
290
291        return 0;
292}
293
294/**
295 * Send the next SNAC from the holding queue.  This is called
296 * automatically when an ack from an add, mod, or del is received. 
297 * If the queue is empty, it sends the modend SNAC.
298 *
299 * @param sess The oscar session.
300 * @param conn The bos connection for this session.
301 * @return Return 0 if no errors, otherwise return the error number.
302 */
303static int aim_ssi_dispatch(aim_session_t *sess, aim_conn_t *conn)
304{
305        aim_frame_t *cur;
306
307        if (!sess || !conn)
308                return -EINVAL;
309
310        if (!sess->ssi.waiting_for_ack) {
311                if (sess->ssi.holding_queue) {
312                        sess->ssi.waiting_for_ack = 1;
313                        cur = sess->ssi.holding_queue->next;
314                        sess->ssi.holding_queue->next = NULL;
315                        aim_tx_enqueue(sess, sess->ssi.holding_queue);
316                        sess->ssi.holding_queue = cur;
317                } else
318                        aim_ssi_modend(sess, conn);
319        }
320
321        return 0;
322}
323
324/**
325 * Add an array of screen names to the given group.
326 *
327 * @param sess The oscar session.
328 * @param conn The bos connection for this session.
329 * @param gn The name of the group to which you want to add these names.
330 * @param sn An array of null terminated strings of the names you want to add.
331 * @param num The number of screen names you are adding (size of the sn array).
332 * @param flags 1 - Add with TLV(0x66)
333 * @return Return 0 if no errors, otherwise return the error number.
334 */
335int aim_ssi_addbuddies(aim_session_t *sess, aim_conn_t *conn, char *gn, char **sn, unsigned int num, unsigned int flags)
336{
337        struct aim_ssi_item *parentgroup, **newitems;
338        guint16 i;
339
340        if (!sess || !conn || !gn || !sn || !num)
341                return -EINVAL;
342
343        /* Look up the parent group */
344        if (!(parentgroup = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, gn, AIM_SSI_TYPE_GROUP))) {
345                aim_ssi_addgroups(sess, conn, &gn, 1);
346                if (!(parentgroup = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, gn, AIM_SSI_TYPE_GROUP)))
347                        return -ENOMEM;
348        }
349
350        /* Allocate an array of pointers to each of the new items */
351        if (!(newitems = g_new0(struct aim_ssi_item *, num)))
352                return -ENOMEM;
353
354        /* Add items to the local list, and index them in the array */
355        for (i=0; i<num; i++)
356                if (!(newitems[i] = aim_ssi_itemlist_add(&sess->ssi.items, parentgroup, sn[i], AIM_SSI_TYPE_BUDDY))) {
357                        g_free(newitems);
358                        return -ENOMEM;
359                } else if (flags & 1) {
360                        aim_tlvlist_t *tl = NULL;
361                        aim_addtlvtochain_noval(&tl, 0x66);
362                        newitems[i]->data = tl;
363                }
364
365        /* Send the add item SNAC */
366        if ((i = aim_ssi_addmoddel(sess, conn, newitems, num, AIM_CB_SSI_ADD))) {
367                g_free(newitems);
368                return -i;
369        }
370
371        /* Free the array of pointers to each of the new items */
372        g_free(newitems);
373
374        /* Rebuild the additional data in the parent group */
375        if ((i = aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, parentgroup)))
376                return i;
377
378        /* Send the mod item SNAC */
379        if ((i = aim_ssi_addmoddel(sess, conn, &parentgroup, 1, AIM_CB_SSI_MOD )))
380                return i;
381
382        /* Begin sending SSI SNACs */
383        if (!(i = aim_ssi_dispatch(sess, conn)))
384                return i;
385
386        return 0;
387}
388
389/**
390 * Add the master group (the group containing all groups).  This is called by
391 * aim_ssi_addgroups, if necessary.
392 *
393 * @param sess The oscar session.
394 * @param conn The bos connection for this session.
395 * @return Return 0 if no errors, otherwise return the error number.
396 */
397int aim_ssi_addmastergroup(aim_session_t *sess, aim_conn_t *conn)
398{
399        struct aim_ssi_item *newitem;
400
401        if (!sess || !conn)
402                return -EINVAL;
403
404        /* Add the item to the local list, and keep a pointer to it */
405        if (!(newitem = aim_ssi_itemlist_add(&sess->ssi.items, NULL, NULL, AIM_SSI_TYPE_GROUP)))
406                return -ENOMEM;
407
408        /* If there are any existing groups (technically there shouldn't be, but */
409        /* just in case) then add their group ID#'s to the additional data */
410        aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, newitem);
411
412        /* Send the add item SNAC */
413        aim_ssi_addmoddel(sess, conn, &newitem, 1, AIM_CB_SSI_ADD);
414
415        /* Begin sending SSI SNACs */
416        aim_ssi_dispatch(sess, conn);
417
418        return 0;
419}
420
421/**
422 * Add an array of groups to the list.
423 *
424 * @param sess The oscar session.
425 * @param conn The bos connection for this session.
426 * @param gn An array of null terminated strings of the names you want to add.
427 * @param num The number of groups names you are adding (size of the sn array).
428 * @return Return 0 if no errors, otherwise return the error number.
429 */
430int aim_ssi_addgroups(aim_session_t *sess, aim_conn_t *conn, char **gn, unsigned int num)
431{
432        struct aim_ssi_item *parentgroup, **newitems;
433        guint16 i;
434
435        if (!sess || !conn || !gn || !num)
436                return -EINVAL;
437
438        /* Look up the parent group */
439        if (!(parentgroup = aim_ssi_itemlist_find(sess->ssi.items, 0, 0))) {
440                aim_ssi_addmastergroup(sess, conn);
441                if (!(parentgroup = aim_ssi_itemlist_find(sess->ssi.items, 0, 0)))
442                        return -ENOMEM;
443        }
444
445        /* Allocate an array of pointers to each of the new items */
446        if (!(newitems = g_new0(struct aim_ssi_item *, num)))
447                return -ENOMEM;
448
449        /* Add items to the local list, and index them in the array */
450        for (i=0; i<num; i++)
451                if (!(newitems[i] = aim_ssi_itemlist_add(&sess->ssi.items, parentgroup, gn[i], AIM_SSI_TYPE_GROUP))) {
452                        g_free(newitems);
453                        return -ENOMEM;
454                }
455
456        /* Send the add item SNAC */
457        if ((i = aim_ssi_addmoddel(sess, conn, newitems, num, AIM_CB_SSI_ADD))) {
458                g_free(newitems);
459                return -i;
460        }
461
462        /* Free the array of pointers to each of the new items */
463        g_free(newitems);
464
465        /* Rebuild the additional data in the parent group */
466        if ((i = aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, parentgroup)))
467                return i;
468
469        /* Send the mod item SNAC */
470        if ((i = aim_ssi_addmoddel(sess, conn, &parentgroup, 1, AIM_CB_SSI_MOD)))
471                return i;
472
473        /* Begin sending SSI SNACs */
474        if (!(i = aim_ssi_dispatch(sess, conn)))
475                return i;
476
477        return 0;
478}
479
480/**
481 * Add an array of a certain type of item to the list.  This can be used for
482 * permit buddies, deny buddies, ICQ's ignore buddies, and probably other
483 * types, also.
484 *
485 * @param sess The oscar session.
486 * @param conn The bos connection for this session.
487 * @param sn An array of null terminated strings of the names you want to add.
488 * @param num The number of groups names you are adding (size of the sn array).
489 * @param type The type of item you want to add.  See the AIM_SSI_TYPE_BLEH
490 *        #defines in aim.h.
491 * @return Return 0 if no errors, otherwise return the error number.
492 */
493int aim_ssi_addpord(aim_session_t *sess, aim_conn_t *conn, char **sn, unsigned int num, guint16 type)
494{
495        struct aim_ssi_item **newitems;
496        guint16 i;
497
498        if (!sess || !conn || !sn || !num)
499                return -EINVAL;
500
501        /* Allocate an array of pointers to each of the new items */
502        if (!(newitems = g_new0(struct aim_ssi_item *, num)))
503                return -ENOMEM;
504
505        /* Add items to the local list, and index them in the array */
506        for (i=0; i<num; i++)
507                if (!(newitems[i] = aim_ssi_itemlist_add(&sess->ssi.items, NULL, sn[i], type))) {
508                        g_free(newitems);
509                        return -ENOMEM;
510                }
511
512        /* Send the add item SNAC */
513        if ((i = aim_ssi_addmoddel(sess, conn, newitems, num, AIM_CB_SSI_ADD))) {
514                g_free(newitems);
515                return -i;
516        }
517
518        /* Free the array of pointers to each of the new items */
519        g_free(newitems);
520
521        /* Begin sending SSI SNACs */
522        if (!(i = aim_ssi_dispatch(sess, conn)))
523                return i;
524
525        return 0;
526}
527
528/**
529 * Move a buddy from one group to another group.  This basically just deletes the
530 * buddy and re-adds it.
531 *
532 * @param sess The oscar session.
533 * @param conn The bos connection for this session.
534 * @param oldgn The group that the buddy is currently in.
535 * @param newgn The group that the buddy should be moved in to.
536 * @param sn The name of the buddy to be moved.
537 * @return Return 0 if no errors, otherwise return the error number.
538 */
539int aim_ssi_movebuddy(aim_session_t *sess, aim_conn_t *conn, char *oldgn, char *newgn, char *sn)
540{
541        struct aim_ssi_item **groups, *buddy, *cur;
542        guint16 i;
543
544        if (!sess || !conn || !oldgn || !newgn || !sn)
545                return -EINVAL;
546
547        /* Look up the buddy */
548        if (!(buddy = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, sn, AIM_SSI_TYPE_BUDDY)))
549                return -ENOMEM;
550
551        /* Allocate an array of pointers to the two groups */
552        if (!(groups = g_new0(struct aim_ssi_item *, 2)))
553                return -ENOMEM;
554
555        /* Look up the old parent group */
556        if (!(groups[0] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, oldgn, AIM_SSI_TYPE_GROUP))) {
557                g_free(groups);
558                return -ENOMEM;
559        }
560
561        /* Look up the new parent group */
562        if (!(groups[1] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, newgn, AIM_SSI_TYPE_GROUP))) {
563                g_free(groups);
564                return -ENOMEM;
565        }
566
567        /* Send the delete item SNAC */
568        aim_ssi_addmoddel(sess, conn, &buddy, 1, AIM_CB_SSI_DEL);
569
570        /* Put the buddy in the new group */
571        buddy->gid = groups[1]->gid;
572
573        /* Assign a new buddy ID#, because the new group might already have a buddy with this ID# */
574        buddy->bid = 0;
575        do {
576                buddy->bid += 0x0001;
577                for (cur=sess->ssi.items, i=0; ((cur) && (!i)); cur=cur->next)
578                        if ((cur->bid == buddy->bid) && (cur->gid == buddy->gid) && (cur->type == AIM_SSI_TYPE_BUDDY) && (cur->name) && aim_sncmp(cur->name, buddy->name))
579                                i=1;
580        } while (i);
581
582        /* Rebuild the additional data in the two parent groups */
583        aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, groups[0]);
584        aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, groups[1]);
585
586        /* Send the add item SNAC */
587        aim_ssi_addmoddel(sess, conn, &buddy, 1, AIM_CB_SSI_ADD);
588
589        /* Send the mod item SNAC */
590        aim_ssi_addmoddel(sess, conn, groups, 2, AIM_CB_SSI_MOD);
591
592        /* Free the temporary array */
593        g_free(groups);
594
595        /* Begin sending SSI SNACs */
596        aim_ssi_dispatch(sess, conn);
597
598        return 0;
599}
600
601/**
602 * Delete an array of screen names from the given group.
603 *
604 * @param sess The oscar session.
605 * @param conn The bos connection for this session.
606 * @param gn The name of the group from which you want to delete these names.
607 * @param sn An array of null terminated strings of the names you want to delete.
608 * @param num The number of screen names you are deleting (size of the sn array).
609 * @return Return 0 if no errors, otherwise return the error number.
610 */
611int aim_ssi_delbuddies(aim_session_t *sess, aim_conn_t *conn, char *gn, char **sn, unsigned int num)
612{
613        struct aim_ssi_item *cur, *parentgroup, **delitems;
614        int i;
615
616        if (!sess || !conn || !gn || !sn || !num)
617                return -EINVAL;
618
619        /* Look up the parent group */
620        if (!(parentgroup = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, gn, AIM_SSI_TYPE_GROUP)))
621                return -EINVAL;
622
623        /* Allocate an array of pointers to each of the items to be deleted */
624        delitems = g_new0(struct aim_ssi_item *, num);
625
626        /* Make the delitems array a pointer to the aim_ssi_item structs to be deleted */
627        for (i=0; i<num; i++) {
628                if (!(delitems[i] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, sn[i], AIM_SSI_TYPE_BUDDY))) {
629                        g_free(delitems);
630                        return -EINVAL;
631                }
632
633                /* Remove the delitems from the item list */
634                if (sess->ssi.items == delitems[i]) {
635                        sess->ssi.items = sess->ssi.items->next;
636                } else {
637                        for (cur=sess->ssi.items; (cur->next && (cur->next!=delitems[i])); cur=cur->next);
638                        if (cur->next)
639                                cur->next = cur->next->next;
640                }
641        }
642
643        /* Send the del item SNAC */
644        aim_ssi_addmoddel(sess, conn, delitems, num, AIM_CB_SSI_DEL);
645
646        /* Free the items */
647        for (i=0; i<num; i++) {
648                if (delitems[i]->name)
649                        g_free(delitems[i]->name);
650                if (delitems[i]->data)
651                        aim_freetlvchain((aim_tlvlist_t **)&delitems[i]->data);
652                g_free(delitems[i]);
653        }
654        g_free(delitems);
655
656        /* Rebuild the additional data in the parent group */
657        aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, parentgroup);
658
659        /* Send the mod item SNAC */
660        aim_ssi_addmoddel(sess, conn, &parentgroup, 1, AIM_CB_SSI_MOD);
661
662        /* Delete the group, but only if it's empty */
663        if (!parentgroup->data)
664                aim_ssi_delgroups(sess, conn, &parentgroup->name, 1);
665
666        /* Begin sending SSI SNACs */
667        aim_ssi_dispatch(sess, conn);
668
669        return 0;
670}
671
672/**
673 * Delete the master group from the item list.  There can be only one.
674 * Er, so just find the one master group and delete it.
675 *
676 * @param sess The oscar session.
677 * @param conn The bos connection for this session.
678 * @return Return 0 if no errors, otherwise return the error number.
679 */
680int aim_ssi_delmastergroup(aim_session_t *sess, aim_conn_t *conn)
681{
682        struct aim_ssi_item *cur, *delitem;
683
684        if (!sess || !conn)
685                return -EINVAL;
686
687        /* Make delitem a pointer to the aim_ssi_item to be deleted */
688        if (!(delitem = aim_ssi_itemlist_find(sess->ssi.items, 0, 0)))
689                return -EINVAL;
690
691        /* Remove delitem from the item list */
692        if (sess->ssi.items == delitem) {
693                sess->ssi.items = sess->ssi.items->next;
694        } else {
695                for (cur=sess->ssi.items; (cur->next && (cur->next!=delitem)); cur=cur->next);
696                if (cur->next)
697                        cur->next = cur->next->next;
698        }
699
700        /* Send the del item SNAC */
701        aim_ssi_addmoddel(sess, conn, &delitem, 1, AIM_CB_SSI_DEL);
702
703        /* Free the item */
704        if (delitem->name)
705                g_free(delitem->name);
706        if (delitem->data)
707                aim_freetlvchain((aim_tlvlist_t **)&delitem->data);
708        g_free(delitem);
709
710        /* Begin sending SSI SNACs */
711        aim_ssi_dispatch(sess, conn);
712
713        return 0;
714}
715
716/**
717 * Delete an array of groups.
718 *
719 * @param sess The oscar session.
720 * @param conn The bos connection for this session.
721 * @param gn An array of null terminated strings of the groups you want to delete.
722 * @param num The number of groups you are deleting (size of the gn array).
723 * @return Return 0 if no errors, otherwise return the error number.
724 */
725int aim_ssi_delgroups(aim_session_t *sess, aim_conn_t *conn, char **gn, unsigned int num) {
726        struct aim_ssi_item *cur, *parentgroup, **delitems;
727        int i;
728
729        if (!sess || !conn || !gn || !num)
730                return -EINVAL;
731
732        /* Look up the parent group */
733        if (!(parentgroup = aim_ssi_itemlist_find(sess->ssi.items, 0, 0)))
734                return -EINVAL;
735
736        /* Allocate an array of pointers to each of the items to be deleted */
737        delitems = g_new0(struct aim_ssi_item *, num);
738
739        /* Make the delitems array a pointer to the aim_ssi_item structs to be deleted */
740        for (i=0; i<num; i++) {
741                if (!(delitems[i] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, gn[i], AIM_SSI_TYPE_GROUP))) {
742                        g_free(delitems);
743                        return -EINVAL;
744                }
745
746                /* Remove the delitems from the item list */
747                if (sess->ssi.items == delitems[i]) {
748                        sess->ssi.items = sess->ssi.items->next;
749                } else {
750                        for (cur=sess->ssi.items; (cur->next && (cur->next!=delitems[i])); cur=cur->next);
751                        if (cur->next)
752                                cur->next = cur->next->next;
753                }
754        }
755
756        /* Send the del item SNAC */
757        aim_ssi_addmoddel(sess, conn, delitems, num, AIM_CB_SSI_DEL);
758
759        /* Free the items */
760        for (i=0; i<num; i++) {
761                if (delitems[i]->name)
762                        g_free(delitems[i]->name);
763                if (delitems[i]->data)
764                        aim_freetlvchain((aim_tlvlist_t **)&delitems[i]->data);
765                g_free(delitems[i]);
766        }
767        g_free(delitems);
768
769        /* Rebuild the additional data in the parent group */
770        aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, parentgroup);
771
772        /* Send the mod item SNAC */
773        aim_ssi_addmoddel(sess, conn, &parentgroup, 1, AIM_CB_SSI_MOD);
774
775        /* Delete the group, but only if it's empty */
776        if (!parentgroup->data)
777                aim_ssi_delmastergroup(sess, conn);
778
779        /* Begin sending SSI SNACs */
780        aim_ssi_dispatch(sess, conn);
781
782        return 0;
783}
784
785/**
786 * Delete an array of a certain type of item from the list.  This can be
787 * used for permit buddies, deny buddies, ICQ's ignore buddies, and
788 * probably other types, also.
789 *
790 * @param sess The oscar session.
791 * @param conn The bos connection for this session.
792 * @param sn An array of null terminated strings of the items you want to delete.
793 * @param num The number of items you are deleting (size of the sn array).
794 * @return Return 0 if no errors, otherwise return the error number.
795 */
796int aim_ssi_delpord(aim_session_t *sess, aim_conn_t *conn, char **sn, unsigned int num, guint16 type) {
797        struct aim_ssi_item *cur, **delitems;
798        int i;
799
800        if (!sess || !conn || !sn || !num || (type!=AIM_SSI_TYPE_PERMIT && type!=AIM_SSI_TYPE_DENY))
801                return -EINVAL;
802
803        /* Allocate an array of pointers to each of the items to be deleted */
804        delitems = g_new0(struct aim_ssi_item *, num);
805
806        /* Make the delitems array a pointer to the aim_ssi_item structs to be deleted */
807        for (i=0; i<num; i++) {
808                if (!(delitems[i] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, sn[i], type))) {
809                        g_free(delitems);
810                        return -EINVAL;
811                }
812
813                /* Remove the delitems from the item list */
814                if (sess->ssi.items == delitems[i]) {
815                        sess->ssi.items = sess->ssi.items->next;
816                } else {
817                        for (cur=sess->ssi.items; (cur->next && (cur->next!=delitems[i])); cur=cur->next);
818                        if (cur->next)
819                                cur->next = cur->next->next;
820                }
821        }
822
823        /* Send the del item SNAC */
824        aim_ssi_addmoddel(sess, conn, delitems, num, AIM_CB_SSI_DEL);
825
826        /* Free the items */
827        for (i=0; i<num; i++) {
828                if (delitems[i]->name)
829                        g_free(delitems[i]->name);
830                if (delitems[i]->data)
831                        aim_freetlvchain((aim_tlvlist_t **)&delitems[i]->data);
832                g_free(delitems[i]);
833        }
834        g_free(delitems);
835
836        /* Begin sending SSI SNACs */
837        aim_ssi_dispatch(sess, conn);
838
839        return 0;
840}
841
842/*
843 * Request SSI Rights.
844 */
845int aim_ssi_reqrights(aim_session_t *sess, aim_conn_t *conn)
846{
847        return aim_genericreq_n(sess, conn, AIM_CB_FAM_SSI, AIM_CB_SSI_REQRIGHTS);
848}
849
850/*
851 * SSI Rights Information.
852 */
853static int parserights(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
854{
855        int ret = 0;
856        aim_rxcallback_t userfunc;
857
858        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
859                ret = userfunc(sess, rx);
860
861        return ret;
862}
863
864int aim_ssi_reqalldata(aim_session_t *sess, aim_conn_t *conn)
865{
866        aim_frame_t *fr;
867        aim_snacid_t snacid;
868
869        if (!sess || !conn)
870                return -EINVAL;
871
872        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10)))
873                return -ENOMEM;
874
875        snacid = aim_cachesnac(sess, AIM_CB_FAM_SSI, AIM_CB_SSI_REQFULLLIST, 0x0000, NULL, 0);
876
877        aim_putsnac(&fr->data, AIM_CB_FAM_SSI, AIM_CB_SSI_REQFULLLIST, 0x0000, snacid);
878
879        aim_tx_enqueue(sess, fr);
880
881        return 0;
882}
883
884/*
885 * SSI Data.
886 */
887static int parsedata(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
888{
889        int ret = 0;
890        aim_rxcallback_t userfunc;
891        struct aim_ssi_item *cur = NULL;
892        guint8 fmtver; /* guess */
893        guint16 revision;
894        guint32 timestamp;
895
896        /* When you set the version for the SSI family to 2-4, the beginning of this changes.
897         * Instead of the version and then the revision, there is "0x0006" and then a type
898         * 0x0001 TLV containing the 2 byte SSI family version that you sent earlier.  Also,
899         * the SNAC flags go from 0x0000 to 0x8000.  I guess the 0x0006 is the length of the
900         * TLV(s) that follow.  The rights SNAC does the same thing, with the differing flag
901         * and everything.
902         */
903
904        fmtver = aimbs_get8(bs); /* Version of ssi data.  Should be 0x00 */
905        revision = aimbs_get16(bs); /* # of times ssi data has been modified */
906        if (revision != 0)
907                sess->ssi.revision = revision;
908
909        for (cur = sess->ssi.items; cur && cur->next; cur=cur->next) ;
910
911        while (aim_bstream_empty(bs) > 4) { /* last four bytes are stamp */
912                guint16 namelen, tbslen;
913
914                if (!sess->ssi.items) {
915                        if (!(sess->ssi.items = g_new0(struct aim_ssi_item, 1)))
916                                return -ENOMEM;
917                        cur = sess->ssi.items;
918                } else {
919                        if (!(cur->next = g_new0(struct aim_ssi_item, 1)))
920                                return -ENOMEM;
921                        cur = cur->next;
922                }
923
924                if ((namelen = aimbs_get16(bs)))
925                        cur->name = aimbs_getstr(bs, namelen);
926                cur->gid = aimbs_get16(bs);
927                cur->bid = aimbs_get16(bs);
928                cur->type = aimbs_get16(bs);
929
930                if ((tbslen = aimbs_get16(bs))) {
931                        aim_bstream_t tbs;
932
933                        aim_bstream_init(&tbs, bs->data + bs->offset /* XXX */, tbslen);
934                        cur->data = (void *)aim_readtlvchain(&tbs);
935                        aim_bstream_advance(bs, tbslen);
936                }
937        }
938
939        timestamp = aimbs_get32(bs);
940        if (timestamp != 0)
941                sess->ssi.timestamp = timestamp;
942        sess->ssi.received_data = 1;
943
944        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
945                ret = userfunc(sess, rx, fmtver, sess->ssi.revision, sess->ssi.timestamp, sess->ssi.items);
946
947        return ret;
948}
949
950/*
951 * SSI Data Enable Presence.
952 *
953 * Should be sent after receiving 13/6 or 13/f to tell the server you
954 * are ready to begin using the list.  It will promptly give you the
955 * presence information for everyone in your list and put your permit/deny
956 * settings into effect.
957 *
958 */
959int aim_ssi_enable(aim_session_t *sess, aim_conn_t *conn)
960{
961        return aim_genericreq_n(sess, conn, AIM_CB_FAM_SSI, 0x0007);
962}
963
964/*
965 * Stuff for SSI authorizations. The code used to work with the old im_ch4
966 * messages, but those are supposed to be obsolete. This is probably
967 * ICQ-specific.
968 */
969
970/**
971 * Request authorization to add someone to the server-side buddy list.
972 *
973 * @param sess The oscar session.
974 * @param conn The bos connection for this session.
975 * @param uin The contact's ICQ UIN.
976 * @param reason The reason string to send with the request.
977 * @return Return 0 if no errors, otherwise return the error number.
978 */
979int aim_ssi_auth_request( aim_session_t *sess, aim_conn_t *conn, char *uin, char *reason )
980{
981        aim_frame_t *fr;
982        aim_snacid_t snacid;
983        int snaclen;
984       
985        snaclen = 10 + 1 + strlen( uin ) + 2 + strlen( reason ) + 2;
986       
987        if( !( fr = aim_tx_new( sess, conn, AIM_FRAMETYPE_FLAP, 0x02, snaclen ) ) )
988                return -ENOMEM;
989
990        snacid = aim_cachesnac( sess, AIM_CB_FAM_SSI, AIM_CB_SSI_SENDAUTHREQ, 0x0000, NULL, 0 );
991        aim_putsnac( &fr->data, AIM_CB_FAM_SSI, AIM_CB_SSI_SENDAUTHREQ, 0x0000, snacid );
992       
993        aimbs_put8( &fr->data, strlen( uin ) );
994        aimbs_putraw( &fr->data, (guint8 *)uin, strlen( uin ) );
995        aimbs_put16( &fr->data, strlen( reason ) );
996        aimbs_putraw( &fr->data, (guint8 *)reason, strlen( reason ) );
997        aimbs_put16( &fr->data, 0 );
998       
999        aim_tx_enqueue( sess, fr );
1000       
1001        return( 0 );
1002}
1003
1004/**
1005 * Reply to an authorization request to add someone to the server-side buddy list.
1006 *
1007 * @param sess The oscar session.
1008 * @param conn The bos connection for this session.
1009 * @param uin The contact's ICQ UIN.
1010 * @param yesno 1 == Permit, 0 == Deny
1011 * @param reason The reason string to send with the request.
1012 * @return Return 0 if no errors, otherwise return the error number.
1013 */
1014int aim_ssi_auth_reply( aim_session_t *sess, aim_conn_t *conn, char *uin, int yesno, char *reason )
1015{
1016        aim_frame_t *fr;
1017        aim_snacid_t snacid;
1018        int snaclen;
1019       
1020        snaclen = 10 + 1 + strlen( uin ) + 3 + strlen( reason );
1021       
1022        if( !( fr = aim_tx_new( sess, conn, AIM_FRAMETYPE_FLAP, 0x02, snaclen ) ) )
1023                return -ENOMEM;
1024       
1025        snacid = aim_cachesnac( sess, AIM_CB_FAM_SSI, AIM_CB_SSI_SENDAUTHREP, 0x0000, NULL, 0 );
1026        aim_putsnac( &fr->data, AIM_CB_FAM_SSI, AIM_CB_SSI_SENDAUTHREP, 0x0000, snacid );
1027       
1028        aimbs_put8( &fr->data, strlen( uin ) );
1029        aimbs_putraw( &fr->data, (guint8 *)uin, strlen( uin ) );
1030        aimbs_put8( &fr->data, yesno );
1031        aimbs_put16( &fr->data, strlen( reason ) );
1032        aimbs_putraw( &fr->data, (guint8 *)reason, strlen( reason ) );
1033       
1034        aim_tx_enqueue( sess, fr );
1035       
1036        return( 0 );
1037}
1038
1039
1040/*
1041 * SSI Add/Mod/Del Item(s).
1042 *
1043 * Sends the SNAC to add, modify, or delete an item from the server-stored
1044 * information.  These 3 SNACs all have an identical structure.  The only
1045 * difference is the subtype that is set for the SNAC.
1046 *
1047 */
1048int aim_ssi_addmoddel(aim_session_t *sess, aim_conn_t *conn, struct aim_ssi_item **items, unsigned int num, guint16 subtype)
1049{
1050        aim_frame_t *fr;
1051        aim_snacid_t snacid;
1052        int i, snaclen, listlen;
1053        char *list = NULL;
1054
1055        if (!sess || !conn || !items || !num)
1056                return -EINVAL;
1057
1058        snaclen = 10; /* For family, subtype, flags, and SNAC ID */
1059        listlen = 0;
1060        for (i=0; i<num; i++) {
1061                snaclen += 10; /* For length, GID, BID, type, and length */
1062                if (items[i]->name) {
1063                        snaclen += strlen(items[i]->name);
1064                       
1065                        if (subtype == AIM_CB_SSI_ADD) {
1066                                list = g_realloc(list, listlen + strlen(items[i]->name) + 1);
1067                                strcpy(list + listlen, items[i]->name);
1068                                listlen += strlen(items[i]->name) + 1;
1069                        }
1070                } else {
1071                        if (subtype == AIM_CB_SSI_ADD) {
1072                                list = g_realloc(list, listlen + 1);
1073                                list[listlen] = '\0';
1074                                listlen ++;
1075                        }
1076                }
1077                if (items[i]->data)
1078                        snaclen += aim_sizetlvchain((aim_tlvlist_t **)&items[i]->data);
1079        }
1080       
1081        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, snaclen)))
1082                return -ENOMEM;
1083
1084        snacid = aim_cachesnac(sess, AIM_CB_FAM_SSI, subtype, 0x0000, list, list ? listlen : 0);
1085        aim_putsnac(&fr->data, AIM_CB_FAM_SSI, subtype, 0x0000, snacid);
1086       
1087        g_free(list);
1088
1089        for (i=0; i<num; i++) {
1090                aimbs_put16(&fr->data, items[i]->name ? strlen(items[i]->name) : 0);
1091                if (items[i]->name)
1092                        aimbs_putraw(&fr->data, (guint8 *)items[i]->name, strlen(items[i]->name));
1093                aimbs_put16(&fr->data, items[i]->gid);
1094                aimbs_put16(&fr->data, items[i]->bid);
1095                aimbs_put16(&fr->data, items[i]->type);
1096                aimbs_put16(&fr->data, items[i]->data ? aim_sizetlvchain((aim_tlvlist_t **)&items[i]->data) : 0);
1097                if (items[i]->data)
1098                        aim_writetlvchain(&fr->data, (aim_tlvlist_t **)&items[i]->data);
1099        }
1100
1101        aim_ssi_enqueue(sess, conn, fr);
1102
1103        return 0;
1104}
1105
1106/*
1107 * SSI Add/Mod/Del Ack.
1108 *
1109 * Response to add, modify, or delete SNAC (sent with aim_ssi_addmoddel).
1110 *
1111 */
1112static int parseack(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1113{
1114        int ret = 0;
1115        aim_rxcallback_t userfunc;
1116        aim_snac_t *origsnac;
1117
1118        sess->ssi.waiting_for_ack = 0;
1119        aim_ssi_dispatch(sess, rx->conn);
1120       
1121        origsnac = aim_remsnac(sess, snac->id);
1122       
1123        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1124                ret = userfunc(sess, rx, origsnac);
1125       
1126        if (origsnac) {
1127                g_free(origsnac->data);
1128                g_free(origsnac);
1129        }
1130       
1131        return ret;
1132}
1133
1134/*
1135 * SSI Begin Data Modification.
1136 *
1137 * Tells the server you're going to start modifying data.
1138 *
1139 */
1140int aim_ssi_modbegin(aim_session_t *sess, aim_conn_t *conn)
1141{
1142        return aim_genericreq_n(sess, conn, AIM_CB_FAM_SSI, AIM_CB_SSI_EDITSTART);
1143}
1144
1145/*
1146 * SSI End Data Modification.
1147 *
1148 * Tells the server you're done modifying data.
1149 *
1150 */
1151int aim_ssi_modend(aim_session_t *sess, aim_conn_t *conn)
1152{
1153        return aim_genericreq_n(sess, conn, AIM_CB_FAM_SSI, AIM_CB_SSI_EDITSTOP);
1154}
1155
1156/*
1157 * SSI Data Unchanged.
1158 *
1159 * Response to aim_ssi_reqdata() if the server-side data is not newer than
1160 * posted local stamp/revision.
1161 *
1162 */
1163static int parsedataunchanged(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1164{
1165        int ret = 0;
1166        aim_rxcallback_t userfunc;
1167
1168        sess->ssi.received_data = 1;
1169
1170        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1171                ret = userfunc(sess, rx);
1172
1173        return ret;
1174}
1175
1176static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1177{
1178
1179        if (snac->subtype == AIM_CB_SSI_RIGHTSINFO)
1180                return parserights(sess, mod, rx, snac, bs);
1181        else if (snac->subtype == AIM_CB_SSI_LIST)
1182                return parsedata(sess, mod, rx, snac, bs);
1183        else if (snac->subtype == AIM_CB_SSI_SRVACK)
1184                return parseack(sess, mod, rx, snac, bs);
1185        else if (snac->subtype == AIM_CB_SSI_NOLIST)
1186                return parsedataunchanged(sess, mod, rx, snac, bs);
1187
1188        return 0;
1189}
1190
1191static void ssi_shutdown(aim_session_t *sess, aim_module_t *mod)
1192{
1193        aim_ssi_freelist(sess);
1194
1195        return;
1196}
1197
1198int ssi_modfirst(aim_session_t *sess, aim_module_t *mod)
1199{
1200
1201        mod->family = AIM_CB_FAM_SSI;
1202        mod->version = 0x0003;
1203        mod->toolid = 0x0110;
1204        mod->toolversion = 0x0629;
1205        mod->flags = 0;
1206        strncpy(mod->name, "ssi", sizeof(mod->name));
1207        mod->snachandler = snachandler;
1208        mod->shutdown = ssi_shutdown;
1209
1210        return 0;
1211}
Note: See TracBrowser for help on using the repository browser.