aboutsummaryrefslogtreecommitdiffstats
path: root/protocols/oscar/ssi.c
blob: 76b5b4276c2e24685f8b9d19b1034e1866c22f8c (plain)
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
#ifndef __OSCAR_IM_H__
#define __OSCAR_IM_H__

#define AIM_CB_FAM_MSG 0x0004

/*
 * SNAC Family: Messaging Services.
 */ 
#define AIM_CB_MSG_ERROR 0x0001
#define AIM_CB_MSG_PARAMINFO 0x0005
#define AIM_CB_MSG_INCOMING 0x0007
#define AIM_CB_MSG_EVIL 0x0009
#define AIM_CB_MSG_MISSEDCALL 0x000a
#define AIM_CB_MSG_CLIENTAUTORESP 0x000b
#define AIM_CB_MSG_ACK 0x000c
#define AIM_CB_MSG_MTN 0x0014
#define AIM_CB_MSG_DEFAULT 0xffff

#define AIM_IMFLAGS_AWAY		0x0001 /* mark as an autoreply */
#define AIM_IMFLAGS_ACK			0x0002 /* request a receipt notice */
#define AIM_IMFLAGS_UNICODE		0x0004
#define AIM_IMFLAGS_ISO_8859_1		0x0008
#define AIM_IMFLAGS_BUDDYREQ		0x0010 /* buddy icon requested */
#define AIM_IMFLAGS_HASICON		0x0020 /* already has icon */
#define AIM_IMFLAGS_SUBENC_MACINTOSH	0x0040 /* damn that Steve Jobs! */
#define AIM_IMFLAGS_CUSTOMFEATURES 	0x0080 /* features field present */
#define AIM_IMFLAGS_EXTDATA		0x0100
#define AIM_IMFLAGS_CUSTOMCHARSET	0x0200 /* charset fields set */
#define AIM_IMFLAGS_MULTIPART		0x0400 /* ->mpmsg section valid */
#define AIM_IMFLAGS_OFFLINE		0x0800 /* send to offline user */

/*
 * Multipart message structures.
 */
typedef struct aim_mpmsg_section_s {
	guint16 charset;
	guint16 charsubset;
	guint8 *data;
	guint16 datalen;
	struct aim_mpmsg_section_s *next;
} aim_mpmsg_section_t;

typedef struct aim_mpmsg_s {
	int numparts;
	aim_mpmsg_section_t *parts;
} aim_mpmsg_t;

int aim_mpmsg_init(aim_session_t *sess, aim_mpmsg_t *mpm);
int aim_mpmsg_addraw(aim_session_t *sess, aim_mpmsg_t *mpm, guint16 charset, guint16 charsubset, const guint8 *data, guint16 datalen);
int aim_mpmsg_addascii(aim_session_t *sess, aim_mpmsg_t *mpm, const char *ascii);
int aim_mpmsg_addunicode(aim_session_t *sess, aim_mpmsg_t *mpm, const guint16 *unicode, guint16 unicodelen);
void aim_mpmsg_free(aim_session_t *sess, aim_mpmsg_t *mpm);

/*
 * Arguments to aim_send_im_ext().
 *
 * This is really complicated.  But immensely versatile.
 *
 */
struct aim_sendimext_args {

	/* These are _required_ */
	const char *destsn;
	guint32 flags; /* often 0 */

	/* Only required if not using multipart messages */
	const char *msg;
	int msglen;

	/* Required if ->msg is not provided */
	aim_mpmsg_t *mpmsg;

	/* Only used if AIM_IMFLAGS_HASICON is set */
	guint32 iconlen;
	time_t iconstamp;
	guint32 iconsum;

	/* Only used if AIM_IMFLAGS_CUSTOMFEATURES is set */
	guint8 *features;
	guint8 featureslen;

	/* Only used if AIM_IMFLAGS_CUSTOMCHARSET is set and mpmsg not used */
	guint16 charset;
	guint16 charsubset;
};

/*
 * Arguments to aim_send_rtfmsg().
 */
struct aim_sendrtfmsg_args {
	const char *destsn;
	guint32 fgcolor;
	guint32 bgcolor;
	const char *rtfmsg; /* must be in RTF */
};

/*
 * This information is provided in the Incoming ICBM callback for
 * Channel 1 ICBM's.  
 *
 * Note that although CUSTOMFEATURES and CUSTOMCHARSET say they
 * are optional, both are always set by the current libfaim code.
 * That may or may not change in the future.  It is mainly for
 * consistency with aim_sendimext_args.
 *
 * Multipart messages require some explanation. If you want to use them,
 * I suggest you read all the comments in im.c.
 *
 */
struct aim_incomingim_ch1_args {

	/* Always provided */
	aim_mpmsg_t mpmsg;
	guint32 icbmflags; /* some flags apply only to ->msg, not all mpmsg */
	
	/* Only provided if message has a human-readable section */
	char *msg;
	int msglen;

	/* Only provided if AIM_IMFLAGS_HASICON is set */
	time_t iconstamp;
	guint32 iconlen;
	guint16 iconsum;

	/* Only provided if AIM_IMFLAGS_CUSTOMFEATURES is set */
	guint8 *features;
	guint8 featureslen;

	/* Only provided if AIM_IMFLAGS_EXTDATA is set */
	guint8 extdatalen;
	guint8 *extdata;

	/* Only used if AIM_IMFLAGS_CUSTOMCHARSET is set */
	guint16 charset;
	guint16 charsubset;
};

/* Valid values for channel 2 args->status */
#define AIM_RENDEZVOUS_PROPOSE 0x0000
#define AIM_RENDEZVOUS_CANCEL  0x0001
#define AIM_RENDEZVOUS_ACCEPT  0x0002

struct aim_incomingim_ch2_args {
	guint8 cookie[8];
	guint16 reqclass;
	guint16 status;
	guint16 errorcode;
	const char *clientip;
	const char *clientip2;
	const char *verifiedip;
	guint16 port;
	const char *msg; /* invite message or file description */
	const char *encoding;
	const char *language;
	union {
		struct {
			guint32 checksum;
			guint32 length;
			time_t timestamp;
			guint8 *icon;
		} icon;
		struct {
			struct aim_chat_roominfo roominfo;
		} chat;
		struct {
			guint32 fgcolor;
			guint32 bgcolor;
			const char *rtfmsg;
		} rtfmsg;
	} info;
	void *destructor; /* used internally only */
};

/* Valid values for channel 4 args->type */
#define AIM_ICQMSG_AUTHREQUEST 0x0006
#define AIM_ICQMSG_AUTHDENIED 0x0007
#define AIM_ICQMSG_AUTHGRANTED 0x0008

struct aim_incomingim_ch4_args {
	guint32 uin; /* Of the sender of the ICBM */
	guint16 type;
	char *msg; /* Reason for auth request, deny, or accept */
};

int aim_send_rtfmsg(aim_session_t *sess, struct aim_sendrtfmsg_args *args);
int aim_send_im_ext(aim_session_t *sess, struct aim_sendimext_args *args);
int aim_send_im(aim_session_t *, const char *destsn, unsigned short flags, const char *msg);
int aim_send_icon(aim_session_t *sess, const char *sn, const guint8 *icon, int iconlen, time_t stamp, guint16 iconsum);
guint16 aim_iconsum(const guint8 *buf, int buflen);
int aim_send_typing(aim_session_t *sess, aim_conn_t *conn, int typing);
int aim_send_im_direct(aim_session_t *, aim_conn_t *, const char *msg, int len);
const char *aim_directim_getsn(aim_conn_t *conn);
aim_conn_t *aim_directim_initiate(aim_session_t *, const char *destsn);
aim_conn_t *aim_directim_connect(aim_session_t *, const char *sn, const char *addr, const guint8 *cookie);

int aim_send_im_ch2_geticqmessage(aim_session_t *sess, const char *sn, int type);
int aim_im_sendmtn(aim_session_t *sess, guint16 type1, const char *sn, guint16 type2);
int aim_send_im_ch2_statusmessage(aim_session_t *sess, const char *sender, const guint8 *cookie, const char *message, const guint8 state, const guint16 dc);

#endif /* __OSCAR_IM_H__ */
' href='#n1202'>1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
/*
 * Server-Side/Stored Information.
 *
 * Relatively new facility that allows storing of certain types of information,
 * such as a users buddy list, permit/deny list, and permit/deny preferences, 
 * to be stored on the server, so that they can be accessed from any client.
 *
 * We keep a copy of the ssi data in sess->ssi, because the data needs to be 
 * accessed for various reasons.  So all the "aim_ssi_itemlist_bleh" functions 
 * near the top just manage the local data.
 *
 * The SNAC sending and receiving functions are lower down in the file, and 
 * they're simpler.  They are in the order of the subtypes they deal with, 
 * starting with the request rights function (subtype 0x0002), then parse 
 * rights (subtype 0x0003), then--well, you get the idea.
 *
 * This is entirely too complicated.
 * You don't know the half of it.
 *
 * XXX - Test for memory leaks
 * XXX - Better parsing of rights, and use the rights info to limit adds
 *
 */

#include <aim.h>
#include "ssi.h"

/**
 * Locally add a new item to the given item list.
 *
 * @param list A pointer to a pointer to the current list of items.
 * @param parent A pointer to the parent group, or NULL if the item should have no 
 *        parent group (ie. the group ID# should be 0).
 * @param name A null terminated string of the name of the new item, or NULL if the 
 *        item should have no name.
 * @param type The type of the item, 0x0001 for a contact, 0x0002 for a group, etc.
 * @return The newly created item.
 */
static struct aim_ssi_item *aim_ssi_itemlist_add(struct aim_ssi_item **list, struct aim_ssi_item *parent, char *name, guint16 type)
{
	int i;
	struct aim_ssi_item *cur, *newitem;

	if (!(newitem = g_new0(struct aim_ssi_item, 1)))
		return NULL;

	/* Set the name */
	if (name) {
		if (!(newitem->name = (char *)g_malloc((strlen(name)+1)*sizeof(char)))) {
			g_free(newitem);
			return NULL;
		}
		strcpy(newitem->name, name);
	} else
		newitem->name = NULL;

	/* Set the group ID# and the buddy ID# */
	newitem->gid = 0x0000;
	newitem->bid = 0x0000;
	if (type == AIM_SSI_TYPE_GROUP) {
		if (name)
			do {
				newitem->gid += 0x0001;
				for (cur=*list, i=0; ((cur) && (!i)); cur=cur->next)
					if ((cur->gid == newitem->gid) && (cur->gid == newitem->gid))
						i=1;
			} while (i);
	} else {
		if (parent)
			newitem->gid = parent->gid;
		do {
			newitem->bid += 0x0001;
			for (cur=*list, i=0; ((cur) && (!i)); cur=cur->next)
				if ((cur->bid == newitem->bid) && (cur->gid == newitem->gid))
					i=1;
		} while (i);
	}

	/* Set the rest */
	newitem->type = type;
	newitem->data = NULL;
	newitem->next = *list;
	*list = newitem;

	return newitem;
}

/**
 * Locally rebuild the 0x00c8 TLV in the additional data of the given group.
 *
 * @param list A pointer to a pointer to the current list of items.
 * @param parentgroup A pointer to the group who's additional data you want to rebuild.
 * @return Return 0 if no errors, otherwise return the error number.
 */
static int aim_ssi_itemlist_rebuildgroup(struct aim_ssi_item **list, struct aim_ssi_item *parentgroup)
{
	int newlen; //, i;
	struct aim_ssi_item *cur;

	/* Free the old additional data */
	if (parentgroup->data) {
		aim_freetlvchain((aim_tlvlist_t **)&parentgroup->data);
		parentgroup->data = NULL;
	}

	/* Find the length for the new additional data */
	newlen = 0;
	if (parentgroup->gid == 0x0000) {
		for (cur=*list; cur; cur=cur->next)
			if ((cur->gid != 0x0000) && (cur->type == AIM_SSI_TYPE_GROUP))
				newlen += 2;
	} else {
		for (cur=*list; cur; cur=cur->next)
			if ((cur->gid == parentgroup->gid) && (cur->type == AIM_SSI_TYPE_BUDDY))
				newlen += 2;
	}

	/* Rebuild the additional data */
	if (newlen>0) {
		guint8 *newdata;

		if (!(newdata = (guint8 *)g_malloc((newlen)*sizeof(guint8))))
			return -ENOMEM;
		newlen = 0;
		if (parentgroup->gid == 0x0000) {
			for (cur=*list; cur; cur=cur->next)
				if ((cur->gid != 0x0000) && (cur->type == AIM_SSI_TYPE_GROUP))
						newlen += aimutil_put16(newdata+newlen, cur->gid);
		} else {
			for (cur=*list; cur; cur=cur->next)
				if ((cur->gid == parentgroup->gid) && (cur->type == AIM_SSI_TYPE_BUDDY))
						newlen += aimutil_put16(newdata+newlen, cur->bid);
		}
		aim_addtlvtochain_raw((aim_tlvlist_t **)&(parentgroup->data), 0x00c8, newlen, newdata);

		g_free(newdata);
	}

	return 0;
}

/**
 * Locally free all of the stored buddy list information.
 *
 * @param sess The oscar session.
 * @return Return 0 if no errors, otherwise return the error number.
 */
static int aim_ssi_freelist(aim_session_t *sess)
{
	struct aim_ssi_item *cur, *delitem;

	cur = sess->ssi.items;
	while (cur) {
		if (cur->name)  g_free(cur->name);
		if (cur->data)  aim_freetlvchain((aim_tlvlist_t **)&cur->data);
		delitem = cur;
		cur = cur->next;
		g_free(delitem);
	}

	sess->ssi.items = NULL;
	sess->ssi.revision = 0;
	sess->ssi.timestamp = (time_t)0;

	return 0;
}

/**
 * Locally find an item given a group ID# and a buddy ID#.
 *
 * @param list A pointer to the current list of items.
 * @param gid The group ID# of the desired item.
 * @param bid The buddy ID# of the desired item.
 * @return Return a pointer to the item if found, else return NULL;
 */
struct aim_ssi_item *aim_ssi_itemlist_find(struct aim_ssi_item *list, guint16 gid, guint16 bid)
{
	struct aim_ssi_item *cur;
	for (cur=list; cur; cur=cur->next)
		if ((cur->gid == gid) && (cur->bid == bid))
			return cur;
	return NULL;
}

/**
 * Locally find an item given a group name, screen name, and type.  If group name 
 * and screen name are null, then just return the first item of the given type.
 *
 * @param list A pointer to the current list of items.
 * @param gn The group name of the desired item.
 * @param bn The buddy name of the desired item.
 * @param type The type of the desired item.
 * @return Return a pointer to the item if found, else return NULL;
 */
struct aim_ssi_item *aim_ssi_itemlist_finditem(struct aim_ssi_item *list, char *gn, char *sn, guint16 type)
{
	struct aim_ssi_item *cur;
	if (!list)
		return NULL;

	if (gn && sn) { /* For finding buddies in groups */
		for (cur=list; cur; cur=cur->next)
			if ((cur->type == type) && (cur->name) && !(aim_sncmp(cur->name, sn))) {
				struct aim_ssi_item *curg;
				for (curg=list; curg; curg=curg->next)
					if ((curg->type == AIM_SSI_TYPE_GROUP) && (curg->gid == cur->gid) && (curg->name) && !(aim_sncmp(curg->name, gn)))
						return cur;
			}

	} else if (sn) { /* For finding groups, permits, denies, and ignores */
		for (cur=list; cur; cur=cur->next)
			if ((cur->type == type) && (cur->name) && !(aim_sncmp(cur->name, sn)))
				return cur;

	/* For stuff without names--permit deny setting, visibility mask, etc. */
	} else for (cur=list; cur; cur=cur->next) {
		if (cur->type == type)
			return cur;
	}

	return NULL;
}

/**
 * Locally find the parent item of the given buddy name.
 *
 * @param list A pointer to the current list of items.
 * @param bn The buddy name of the desired item.
 * @return Return a pointer to the item if found, else return NULL;
 */
struct aim_ssi_item *aim_ssi_itemlist_findparent(struct aim_ssi_item *list, char *sn)
{
	struct aim_ssi_item *cur, *curg;
	if (!list || !sn)
		return NULL;
	if (!(cur = aim_ssi_itemlist_finditem(list, NULL, sn, AIM_SSI_TYPE_BUDDY)))
		return NULL;
	for (curg=list; curg; curg=curg->next)
		if ((curg->type == AIM_SSI_TYPE_GROUP) && (curg->gid == cur->gid))
			return curg;
	return NULL;
}

/**
 * Locally find the permit/deny setting item, and return the setting.
 *
 * @param list A pointer to the current list of items.
 * @return Return the current SSI permit deny setting, or 0 if no setting was found.
 */
int aim_ssi_getpermdeny(struct aim_ssi_item *list)
{
	struct aim_ssi_item *cur = aim_ssi_itemlist_finditem(list, NULL, NULL, AIM_SSI_TYPE_PDINFO);
	if (cur) {
		aim_tlvlist_t *tlvlist = cur->data;
		if (tlvlist) {
			aim_tlv_t *tlv = aim_gettlv(tlvlist, 0x00ca, 1);
			if (tlv && tlv->value)
				return aimutil_get8(tlv->value);
		}
	}
	return 0;
}

/**
 * Locally find the presence flag item, and return the setting.  The returned setting is a 
 * bitmask of the user flags that you are visible to.  See the AIM_FLAG_* #defines 
 * in aim.h
 *
 * @param list A pointer to the current list of items.
 * @return Return the current visibility mask.
 */
guint32 aim_ssi_getpresence(struct aim_ssi_item *list)
{
	struct aim_ssi_item *cur = aim_ssi_itemlist_finditem(list, NULL, NULL, AIM_SSI_TYPE_PRESENCEPREFS);
	if (cur) {
		aim_tlvlist_t *tlvlist = cur->data;
		if (tlvlist) {
			aim_tlv_t *tlv = aim_gettlv(tlvlist, 0x00c9, 1);
			if (tlv && tlv->length)
				return aimutil_get32(tlv->value);
		}
	}
	return 0xFFFFFFFF;
}

/**
 * Add the given packet to the holding queue.  We totally need to send SSI SNACs one at 
 * a time, so we have a local queue where packets get put before they are sent, and 
 * then we send stuff one at a time, nice and orderly-like.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @param fr The newly created SNAC that you want to send.
 * @return Return 0 if no errors, otherwise return the error number.
 */
static int aim_ssi_enqueue(aim_session_t *sess, aim_conn_t *conn, aim_frame_t *fr)
{
	aim_frame_t *cur;

	if (!sess || !conn || !fr)
		return -EINVAL;

	fr->next = NULL;
	if (sess->ssi.holding_queue == NULL) {
		sess->ssi.holding_queue = fr;
		if (!sess->ssi.waiting_for_ack)
			aim_ssi_modbegin(sess, conn);
	} else {
		for (cur = sess->ssi.holding_queue; cur->next; cur = cur->next) ;
		cur->next = fr;
	}

	return 0;
}

/**
 * Send the next SNAC from the holding queue.  This is called 
 * automatically when an ack from an add, mod, or del is received.  
 * If the queue is empty, it sends the modend SNAC.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @return Return 0 if no errors, otherwise return the error number.
 */
static int aim_ssi_dispatch(aim_session_t *sess, aim_conn_t *conn)
{
	aim_frame_t *cur;

	if (!sess || !conn)
		return -EINVAL;

	if (!sess->ssi.waiting_for_ack) {
		if (sess->ssi.holding_queue) {
			sess->ssi.waiting_for_ack = 1;
			cur = sess->ssi.holding_queue->next;
			sess->ssi.holding_queue->next = NULL;
			aim_tx_enqueue(sess, sess->ssi.holding_queue);
			sess->ssi.holding_queue = cur;
		} else
			aim_ssi_modend(sess, conn);
	}

	return 0;
}

/**
 * Send SNACs necessary to remove all SSI data from the server list, 
 * and then free the local copy as well.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @return Return 0 if no errors, otherwise return the error number.
 */
int aim_ssi_deletelist(aim_session_t *sess, aim_conn_t *conn)
{
	int num;
	struct aim_ssi_item *cur, **items;

	for (cur=sess->ssi.items, num=0; cur; cur=cur->next)
		num++;

	if (!(items = g_new0(struct aim_ssi_item *, num)))
		return -ENOMEM;

	for (cur=sess->ssi.items, num=0; cur; cur=cur->next) {
		items[num] = cur;
		num++;
	}

	aim_ssi_addmoddel(sess, conn, items, num, AIM_CB_SSI_DEL);
	g_free(items);
	aim_ssi_dispatch(sess, conn);
	aim_ssi_freelist(sess);

	return 0;
}

/**
 * This "cleans" the ssi list.  It does a few things, with the intent of making 
 * sure there ain't nothin' wrong with your SSI.
 *   -Make sure all buddies are in a group, and all groups have the correct 
 *     additional data.
 *   -Make sure there are no empty groups in the list.  While there is nothing 
 *     wrong empty groups in the SSI, it's wiser to not have them.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @return Return 0 if no errors, otherwise return the error number.
 */
int aim_ssi_cleanlist(aim_session_t *sess, aim_conn_t *conn)
{
	unsigned int i;
	struct aim_ssi_item *cur, *parentgroup;

	/* Make sure we actually need to clean out the list */
	for (cur=sess->ssi.items, i=0; cur && !i; cur=cur->next)
		/* Any buddies directly in the master group */
		if ((cur->type == AIM_SSI_TYPE_BUDDY) && (cur->gid == 0x0000))
			i++;
	if (!i)
		return 0;

	/* Remove all the additional data from all groups */
	for (cur=sess->ssi.items; cur; cur=cur->next)
		if ((cur->data) && (cur->type == AIM_SSI_TYPE_GROUP)) {
			aim_freetlvchain((aim_tlvlist_t **)&cur->data);
			cur->data = NULL;
		}

	/* If there are buddies directly in the master group, make sure  */
	/* there is a group to put them in.  Any group, any group at all. */
	for (cur=sess->ssi.items; ((cur) && ((cur->type != AIM_SSI_TYPE_BUDDY) || (cur->gid != 0x0000))); cur=cur->next);
	if (!cur) {
		for (parentgroup=sess->ssi.items; ((parentgroup) && (parentgroup->type!=AIM_SSI_TYPE_GROUP) && (parentgroup->gid==0x0000)); parentgroup=parentgroup->next);
		if (!parentgroup) {
			char *newgroup;
			newgroup = (char*)g_malloc(strlen("Unknown")*sizeof(char));
			strcpy(newgroup, "Unknown");
			aim_ssi_addgroups(sess, conn, &newgroup, 1);
		}
	}

	/* Set parentgroup equal to any arbitray group */
	for (parentgroup=sess->ssi.items; parentgroup->gid==0x0000 || parentgroup->type!=AIM_SSI_TYPE_GROUP; parentgroup=parentgroup->next);

	/* If there are any buddies directly in the master group, put them in a real group */
	for (cur=sess->ssi.items; cur; cur=cur->next)
		if ((cur->type == AIM_SSI_TYPE_BUDDY) && (cur->gid == 0x0000)) {
			aim_ssi_addmoddel(sess, conn, &cur, 1, AIM_CB_SSI_DEL);
			cur->gid = parentgroup->gid;
			aim_ssi_addmoddel(sess, conn, &cur, 1, AIM_CB_SSI_ADD);
		}

	/* Rebuild additional data for all groups */
	for (parentgroup=sess->ssi.items; parentgroup; parentgroup=parentgroup->next)
		if (parentgroup->type == AIM_SSI_TYPE_GROUP)
			aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, parentgroup);

	/* Send a mod snac for all groups */
	i = 0;
	for (cur=sess->ssi.items; cur; cur=cur->next)
		if (cur->type == AIM_SSI_TYPE_GROUP)
			i++;
	if (i > 0) {
		/* Allocate an array of pointers to each of the groups */
		struct aim_ssi_item **groups;
		if (!(groups = g_new0(struct aim_ssi_item *, i)))
			return -ENOMEM;

		for (cur=sess->ssi.items, i=0; cur; cur=cur->next)
			if (cur->type == AIM_SSI_TYPE_GROUP)
				groups[i] = cur;

		aim_ssi_addmoddel(sess, conn, groups, i, AIM_CB_SSI_MOD);
		g_free(groups);
	}

	/* Send a del snac for any empty groups */
	i = 0;
	for (cur=sess->ssi.items; cur; cur=cur->next)
		if ((cur->type == AIM_SSI_TYPE_GROUP) && !(cur->data))
			i++;
	if (i > 0) {
		/* Allocate an array of pointers to each of the groups */
		struct aim_ssi_item **groups;
		if (!(groups = g_new0(struct aim_ssi_item *, i)))
			return -ENOMEM;

		for (cur=sess->ssi.items, i=0; cur; cur=cur->next)
			if ((cur->type == AIM_SSI_TYPE_GROUP) && !(cur->data))
				groups[i] = cur;

		aim_ssi_addmoddel(sess, conn, groups, i, AIM_CB_SSI_DEL);
		g_free(groups);
	}

	/* Begin sending SSI SNACs */
	aim_ssi_dispatch(sess, conn);

	return 0;
}

/**
 * Add an array of screen names to the given group.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @param gn The name of the group to which you want to add these names.
 * @param sn An array of null terminated strings of the names you want to add.
 * @param num The number of screen names you are adding (size of the sn array).
 * @param flags 1 - Add with TLV(0x66)
 * @return Return 0 if no errors, otherwise return the error number.
 */
int aim_ssi_addbuddies(aim_session_t *sess, aim_conn_t *conn, char *gn, char **sn, unsigned int num, unsigned int flags)
{
	struct aim_ssi_item *parentgroup, **newitems;
	guint16 i;

	if (!sess || !conn || !gn || !sn || !num)
		return -EINVAL;

	/* Look up the parent group */
	if (!(parentgroup = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, gn, AIM_SSI_TYPE_GROUP))) {
		aim_ssi_addgroups(sess, conn, &gn, 1);
		if (!(parentgroup = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, gn, AIM_SSI_TYPE_GROUP)))
			return -ENOMEM;
	}

	/* Allocate an array of pointers to each of the new items */
	if (!(newitems = g_new0(struct aim_ssi_item *, num)))
		return -ENOMEM;

	/* Add items to the local list, and index them in the array */
	for (i=0; i<num; i++)
		if (!(newitems[i] = aim_ssi_itemlist_add(&sess->ssi.items, parentgroup, sn[i], AIM_SSI_TYPE_BUDDY))) {
			g_free(newitems);
			return -ENOMEM;
		} else if (flags & 1) {
			aim_tlvlist_t *tl = NULL;
			aim_addtlvtochain_noval(&tl, 0x66);
			newitems[i]->data = tl;
		}

	/* Send the add item SNAC */
	if ((i = aim_ssi_addmoddel(sess, conn, newitems, num, AIM_CB_SSI_ADD))) {
		g_free(newitems);
		return -i;
	}

	/* Free the array of pointers to each of the new items */
	g_free(newitems);

	/* Rebuild the additional data in the parent group */
	if ((i = aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, parentgroup)))
		return i;

	/* Send the mod item SNAC */
	if ((i = aim_ssi_addmoddel(sess, conn, &parentgroup, 1, AIM_CB_SSI_MOD )))
		return i;

	/* Begin sending SSI SNACs */
	if (!(i = aim_ssi_dispatch(sess, conn)))
		return i;

	return 0;
}

/**
 * Add the master group (the group containing all groups).  This is called by 
 * aim_ssi_addgroups, if necessary.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @return Return 0 if no errors, otherwise return the error number.
 */
int aim_ssi_addmastergroup(aim_session_t *sess, aim_conn_t *conn)
{
	struct aim_ssi_item *newitem;

	if (!sess || !conn)
		return -EINVAL;

	/* Add the item to the local list, and keep a pointer to it */
	if (!(newitem = aim_ssi_itemlist_add(&sess->ssi.items, NULL, NULL, AIM_SSI_TYPE_GROUP)))
		return -ENOMEM;

	/* If there are any existing groups (technically there shouldn't be, but */
	/* just in case) then add their group ID#'s to the additional data */
	aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, newitem);

	/* Send the add item SNAC */
	aim_ssi_addmoddel(sess, conn, &newitem, 1, AIM_CB_SSI_ADD);

	/* Begin sending SSI SNACs */
	aim_ssi_dispatch(sess, conn);

	return 0;
}

/**
 * Add an array of groups to the list.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @param gn An array of null terminated strings of the names you want to add.
 * @param num The number of groups names you are adding (size of the sn array).
 * @return Return 0 if no errors, otherwise return the error number.
 */
int aim_ssi_addgroups(aim_session_t *sess, aim_conn_t *conn, char **gn, unsigned int num)
{
	struct aim_ssi_item *parentgroup, **newitems;
	guint16 i;

	if (!sess || !conn || !gn || !num)
		return -EINVAL;

	/* Look up the parent group */
	if (!(parentgroup = aim_ssi_itemlist_find(sess->ssi.items, 0, 0))) {
		aim_ssi_addmastergroup(sess, conn);
		if (!(parentgroup = aim_ssi_itemlist_find(sess->ssi.items, 0, 0)))
			return -ENOMEM;
	}

	/* Allocate an array of pointers to each of the new items */
	if (!(newitems = g_new0(struct aim_ssi_item *, num)))
		return -ENOMEM;

	/* Add items to the local list, and index them in the array */
	for (i=0; i<num; i++)
		if (!(newitems[i] = aim_ssi_itemlist_add(&sess->ssi.items, parentgroup, gn[i], AIM_SSI_TYPE_GROUP))) {
			g_free(newitems);
			return -ENOMEM;
		}

	/* Send the add item SNAC */
	if ((i = aim_ssi_addmoddel(sess, conn, newitems, num, AIM_CB_SSI_ADD))) {
		g_free(newitems);
		return -i;
	}

	/* Free the array of pointers to each of the new items */
	g_free(newitems);

	/* Rebuild the additional data in the parent group */
	if ((i = aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, parentgroup)))
		return i;

	/* Send the mod item SNAC */
	if ((i = aim_ssi_addmoddel(sess, conn, &parentgroup, 1, AIM_CB_SSI_MOD)))
		return i;

	/* Begin sending SSI SNACs */
	if (!(i = aim_ssi_dispatch(sess, conn)))
		return i;

	return 0;
}

/**
 * Add an array of a certain type of item to the list.  This can be used for 
 * permit buddies, deny buddies, ICQ's ignore buddies, and probably other 
 * types, also.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @param sn An array of null terminated strings of the names you want to add.
 * @param num The number of groups names you are adding (size of the sn array).
 * @param type The type of item you want to add.  See the AIM_SSI_TYPE_BLEH 
 *        #defines in aim.h.
 * @return Return 0 if no errors, otherwise return the error number.
 */
int aim_ssi_addpord(aim_session_t *sess, aim_conn_t *conn, char **sn, unsigned int num, guint16 type)
{
	struct aim_ssi_item **newitems;
	guint16 i;

	if (!sess || !conn || !sn || !num)
		return -EINVAL;

	/* Allocate an array of pointers to each of the new items */
	if (!(newitems = g_new0(struct aim_ssi_item *, num)))
		return -ENOMEM;

	/* Add items to the local list, and index them in the array */
	for (i=0; i<num; i++)
		if (!(newitems[i] = aim_ssi_itemlist_add(&sess->ssi.items, NULL, sn[i], type))) {
			g_free(newitems);
			return -ENOMEM;
		}

	/* Send the add item SNAC */
	if ((i = aim_ssi_addmoddel(sess, conn, newitems, num, AIM_CB_SSI_ADD))) {
		g_free(newitems);
		return -i;
	}

	/* Free the array of pointers to each of the new items */
	g_free(newitems);

	/* Begin sending SSI SNACs */
	if (!(i = aim_ssi_dispatch(sess, conn)))
		return i;

	return 0;
}

/**
 * Move a buddy from one group to another group.  This basically just deletes the 
 * buddy and re-adds it.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @param oldgn The group that the buddy is currently in.
 * @param newgn The group that the buddy should be moved in to.
 * @param sn The name of the buddy to be moved.
 * @return Return 0 if no errors, otherwise return the error number.
 */
int aim_ssi_movebuddy(aim_session_t *sess, aim_conn_t *conn, char *oldgn, char *newgn, char *sn)
{
	struct aim_ssi_item **groups, *buddy, *cur;
	guint16 i;

	if (!sess || !conn || !oldgn || !newgn || !sn)
		return -EINVAL;

	/* Look up the buddy */
	if (!(buddy = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, sn, AIM_SSI_TYPE_BUDDY)))
		return -ENOMEM;

	/* Allocate an array of pointers to the two groups */
	if (!(groups = g_new0(struct aim_ssi_item *, 2)))
		return -ENOMEM;

	/* Look up the old parent group */
	if (!(groups[0] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, oldgn, AIM_SSI_TYPE_GROUP))) {
		g_free(groups);
		return -ENOMEM;
	}

	/* Look up the new parent group */
	if (!(groups[1] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, newgn, AIM_SSI_TYPE_GROUP))) {
		g_free(groups);
		return -ENOMEM;
	}

	/* Send the delete item SNAC */
	aim_ssi_addmoddel(sess, conn, &buddy, 1, AIM_CB_SSI_DEL);

	/* Put the buddy in the new group */
	buddy->gid = groups[1]->gid;

	/* Assign a new buddy ID#, because the new group might already have a buddy with this ID# */
	buddy->bid = 0;
	do {
		buddy->bid += 0x0001;
		for (cur=sess->ssi.items, i=0; ((cur) && (!i)); cur=cur->next)
			if ((cur->bid == buddy->bid) && (cur->gid == buddy->gid) && (cur->type == AIM_SSI_TYPE_BUDDY) && (cur->name) && aim_sncmp(cur->name, buddy->name))
				i=1;
	} while (i);

	/* Rebuild the additional data in the two parent groups */
	aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, groups[0]);
	aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, groups[1]);

	/* Send the add item SNAC */
	aim_ssi_addmoddel(sess, conn, &buddy, 1, AIM_CB_SSI_ADD);

	/* Send the mod item SNAC */
	aim_ssi_addmoddel(sess, conn, groups, 2, AIM_CB_SSI_MOD);

	/* Free the temporary array */
	g_free(groups);

	/* Begin sending SSI SNACs */
	aim_ssi_dispatch(sess, conn);

	return 0;
}

/**
 * Delete an array of screen names from the given group.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @param gn The name of the group from which you want to delete these names.
 * @param sn An array of null terminated strings of the names you want to delete.
 * @param num The number of screen names you are deleting (size of the sn array).
 * @return Return 0 if no errors, otherwise return the error number.
 */
int aim_ssi_delbuddies(aim_session_t *sess, aim_conn_t *conn, char *gn, char **sn, unsigned int num)
{
	struct aim_ssi_item *cur, *parentgroup, **delitems;
	int i;

	if (!sess || !conn || !gn || !sn || !num)
		return -EINVAL;

	/* Look up the parent group */
	if (!(parentgroup = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, gn, AIM_SSI_TYPE_GROUP)))
		return -EINVAL;

	/* Allocate an array of pointers to each of the items to be deleted */
	delitems = g_new0(struct aim_ssi_item *, num);

	/* Make the delitems array a pointer to the aim_ssi_item structs to be deleted */
	for (i=0; i<num; i++) {
		if (!(delitems[i] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, sn[i], AIM_SSI_TYPE_BUDDY))) {
			g_free(delitems);
			return -EINVAL;
		}

		/* Remove the delitems from the item list */
		if (sess->ssi.items == delitems[i]) {
			sess->ssi.items = sess->ssi.items->next;
		} else {
			for (cur=sess->ssi.items; (cur->next && (cur->next!=delitems[i])); cur=cur->next);
			if (cur->next)
				cur->next = cur->next->next;
		}
	}

	/* Send the del item SNAC */
	aim_ssi_addmoddel(sess, conn, delitems, num, AIM_CB_SSI_DEL);

	/* Free the items */
	for (i=0; i<num; i++) {
		if (delitems[i]->name)
			g_free(delitems[i]->name);
		if (delitems[i]->data)
			aim_freetlvchain((aim_tlvlist_t **)&delitems[i]->data);
		g_free(delitems[i]);
	}
	g_free(delitems);

	/* Rebuild the additional data in the parent group */
	aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, parentgroup);

	/* Send the mod item SNAC */
	aim_ssi_addmoddel(sess, conn, &parentgroup, 1, AIM_CB_SSI_MOD);

	/* Delete the group, but only if it's empty */
	if (!parentgroup->data)
		aim_ssi_delgroups(sess, conn, &parentgroup->name, 1);

	/* Begin sending SSI SNACs */
	aim_ssi_dispatch(sess, conn);

	return 0;
}

/**
 * Delete the master group from the item list.  There can be only one.
 * Er, so just find the one master group and delete it.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @return Return 0 if no errors, otherwise return the error number.
 */
int aim_ssi_delmastergroup(aim_session_t *sess, aim_conn_t *conn)
{
	struct aim_ssi_item *cur, *delitem;

	if (!sess || !conn)
		return -EINVAL;

	/* Make delitem a pointer to the aim_ssi_item to be deleted */
	if (!(delitem = aim_ssi_itemlist_find(sess->ssi.items, 0, 0)))
		return -EINVAL;

	/* Remove delitem from the item list */
	if (sess->ssi.items == delitem) {
		sess->ssi.items = sess->ssi.items->next;
	} else {
		for (cur=sess->ssi.items; (cur->next && (cur->next!=delitem)); cur=cur->next);
		if (cur->next)
			cur->next = cur->next->next;
	}

	/* Send the del item SNAC */
	aim_ssi_addmoddel(sess, conn, &delitem, 1, AIM_CB_SSI_DEL);

	/* Free the item */
	if (delitem->name)
		g_free(delitem->name);
	if (delitem->data)
		aim_freetlvchain((aim_tlvlist_t **)&delitem->data);
	g_free(delitem);

	/* Begin sending SSI SNACs */
	aim_ssi_dispatch(sess, conn);

	return 0;
}

/**
 * Delete an array of groups.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @param gn An array of null terminated strings of the groups you want to delete.
 * @param num The number of groups you are deleting (size of the gn array).
 * @return Return 0 if no errors, otherwise return the error number.
 */
int aim_ssi_delgroups(aim_session_t *sess, aim_conn_t *conn, char **gn, unsigned int num) {
	struct aim_ssi_item *cur, *parentgroup, **delitems;
	int i;

	if (!sess || !conn || !gn || !num)
		return -EINVAL;

	/* Look up the parent group */
	if (!(parentgroup = aim_ssi_itemlist_find(sess->ssi.items, 0, 0)))
		return -EINVAL;

	/* Allocate an array of pointers to each of the items to be deleted */
	delitems = g_new0(struct aim_ssi_item *, num);

	/* Make the delitems array a pointer to the aim_ssi_item structs to be deleted */
	for (i=0; i<num; i++) {
		if (!(delitems[i] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, gn[i], AIM_SSI_TYPE_GROUP))) {
			g_free(delitems);
			return -EINVAL;
		}

		/* Remove the delitems from the item list */
		if (sess->ssi.items == delitems[i]) {
			sess->ssi.items = sess->ssi.items->next;
		} else {
			for (cur=sess->ssi.items; (cur->next && (cur->next!=delitems[i])); cur=cur->next);
			if (cur->next)
				cur->next = cur->next->next;
		}
	}

	/* Send the del item SNAC */
	aim_ssi_addmoddel(sess, conn, delitems, num, AIM_CB_SSI_DEL);

	/* Free the items */
	for (i=0; i<num; i++) {
		if (delitems[i]->name)
			g_free(delitems[i]->name);
		if (delitems[i]->data)
			aim_freetlvchain((aim_tlvlist_t **)&delitems[i]->data);
		g_free(delitems[i]);
	}
	g_free(delitems);

	/* Rebuild the additional data in the parent group */
	aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, parentgroup);

	/* Send the mod item SNAC */
	aim_ssi_addmoddel(sess, conn, &parentgroup, 1, AIM_CB_SSI_MOD);

	/* Delete the group, but only if it's empty */
	if (!parentgroup->data)
		aim_ssi_delmastergroup(sess, conn);

	/* Begin sending SSI SNACs */
	aim_ssi_dispatch(sess, conn);

	return 0;
}

/**
 * Delete an array of a certain type of item from the list.  This can be 
 * used for permit buddies, deny buddies, ICQ's ignore buddies, and 
 * probably other types, also.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @param sn An array of null terminated strings of the items you want to delete.
 * @param num The number of items you are deleting (size of the sn array).
 * @return Return 0 if no errors, otherwise return the error number.
 */
int aim_ssi_delpord(aim_session_t *sess, aim_conn_t *conn, char **sn, unsigned int num, guint16 type) {
	struct aim_ssi_item *cur, **delitems;
	int i;

	if (!sess || !conn || !sn || !num || (type!=AIM_SSI_TYPE_PERMIT && type!=AIM_SSI_TYPE_DENY))
		return -EINVAL;

	/* Allocate an array of pointers to each of the items to be deleted */
	delitems = g_new0(struct aim_ssi_item *, num);

	/* Make the delitems array a pointer to the aim_ssi_item structs to be deleted */
	for (i=0; i<num; i++) {
		if (!(delitems[i] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, sn[i], type))) {
			g_free(delitems);
			return -EINVAL;
		}

		/* Remove the delitems from the item list */
		if (sess->ssi.items == delitems[i]) {
			sess->ssi.items = sess->ssi.items->next;
		} else {
			for (cur=sess->ssi.items; (cur->next && (cur->next!=delitems[i])); cur=cur->next);
			if (cur->next)
				cur->next = cur->next->next;
		}
	}

	/* Send the del item SNAC */
	aim_ssi_addmoddel(sess, conn, delitems, num, AIM_CB_SSI_DEL);

	/* Free the items */
	for (i=0; i<num; i++) {
		if (delitems[i]->name)
			g_free(delitems[i]->name);
		if (delitems[i]->data)
			aim_freetlvchain((aim_tlvlist_t **)&delitems[i]->data);
		g_free(delitems[i]);
	}
	g_free(delitems);

	/* Begin sending SSI SNACs */
	aim_ssi_dispatch(sess, conn);

	return 0;
}

/**
 * Stores your permit/deny setting on the server, and starts using it.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @param permdeny Your permit/deny setting.  Can be one of the following:
 *        1 - Allow all users
 *        2 - Block all users
 *        3 - Allow only the users below
 *        4 - Block only the users below
 *        5 - Allow only users on my buddy list
 * @param vismask A bitmask of the class of users to whom you want to be 
 *        visible.  See the AIM_FLAG_BLEH #defines in aim.h
 * @return Return 0 if no errors, otherwise return the error number.
 */
int aim_ssi_setpermdeny(aim_session_t *sess, aim_conn_t *conn, guint8 permdeny, guint32 vismask) {
	struct aim_ssi_item *cur; //, *tmp;
//	guint16 j;
	aim_tlv_t *tlv;

	if (!sess || !conn)
		return -EINVAL;

	/* Look up the permit/deny settings item */
	cur = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, NULL, AIM_SSI_TYPE_PDINFO);

	if (cur) {
		/* The permit/deny item exists */
		if (cur->data && (tlv = aim_gettlv(cur->data, 0x00ca, 1))) {
			/* Just change the value of the x00ca TLV */
			if (tlv->length != 1) {
				tlv->length = 1;
				g_free(tlv->value);
				tlv->value = (guint8 *)g_malloc(sizeof(guint8));
			}
			tlv->value[0] = permdeny;
		} else {
			/* Need to add the x00ca TLV to the TLV chain */
			aim_addtlvtochain8((aim_tlvlist_t**)&cur->data, 0x00ca, permdeny);
		}

		if (cur->data && (tlv = aim_gettlv(cur->data, 0x00cb, 1))) {
			/* Just change the value of the x00cb TLV */
			if (tlv->length != 4) {
				tlv->length = 4;
				g_free(tlv->value);
				tlv->value = (guint8 *)g_malloc(4*sizeof(guint8));
			}
			aimutil_put32(tlv->value, vismask);
		} else {
			/* Need to add the x00cb TLV to the TLV chain */
			aim_addtlvtochain32((aim_tlvlist_t**)&cur->data, 0x00cb, vismask);
		}

		/* Send the mod item SNAC */
		aim_ssi_addmoddel(sess, conn, &cur, 1, AIM_CB_SSI_MOD);
	} else {
		/* Need to add the permit/deny item */
		if (!(cur = aim_ssi_itemlist_add(&sess->ssi.items, NULL, NULL, AIM_SSI_TYPE_PDINFO)))
			return -ENOMEM;
		aim_addtlvtochain8((aim_tlvlist_t**)&cur->data, 0x00ca, permdeny);
		aim_addtlvtochain32((aim_tlvlist_t**)&cur->data, 0x00cb, vismask);
		aim_ssi_addmoddel(sess, conn, &cur, 1, AIM_CB_SSI_ADD);
	}

	/* Begin sending SSI SNACs */
	aim_ssi_dispatch(sess, conn);

	return 0;
}

/**
 * Stores your setting for whether you should show up as idle or not.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @param presence I think it's a bitmask, but I only know what one of the bits is:
 *        0x00000400 - Allow others to see your idle time
 * @return Return 0 if no errors, otherwise return the error number.
 */
int aim_ssi_setpresence(aim_session_t *sess, aim_conn_t *conn, guint32 presence) {
	struct aim_ssi_item *cur; //, *tmp;
//	guint16 j;
	aim_tlv_t *tlv;

	if (!sess || !conn)
		return -EINVAL;

	/* Look up the item */
	cur = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, NULL, AIM_SSI_TYPE_PRESENCEPREFS);

	if (cur) {
		/* The item exists */
		if (cur->data && (tlv = aim_gettlv(cur->data, 0x00c9, 1))) {
			/* Just change the value of the x00c9 TLV */
			if (tlv->length != 4) {
				tlv->length = 4;
				g_free(tlv->value);
				tlv->value = (guint8 *)g_malloc(4*sizeof(guint8));
			}
			aimutil_put32(tlv->value, presence);
		} else {
			/* Need to add the x00c9 TLV to the TLV chain */
			aim_addtlvtochain32((aim_tlvlist_t**)&cur->data, 0x00c9, presence);
		}

		/* Send the mod item SNAC */
		aim_ssi_addmoddel(sess, conn, &cur, 1, AIM_CB_SSI_MOD);
	} else {
		/* Need to add the item */
		if (!(cur = aim_ssi_itemlist_add(&sess->ssi.items, NULL, NULL, AIM_SSI_TYPE_PRESENCEPREFS)))
			return -ENOMEM;
		aim_addtlvtochain32((aim_tlvlist_t**)&cur->data, 0x00c9, presence);
		aim_ssi_addmoddel(sess, conn, &cur, 1, AIM_CB_SSI_ADD);
	}

	/* Begin sending SSI SNACs */
	aim_ssi_dispatch(sess, conn);

	return 0;
}

/*
 * Request SSI Rights.
 */
int aim_ssi_reqrights(aim_session_t *sess, aim_conn_t *conn)
{
	return aim_genericreq_n(sess, conn, AIM_CB_FAM_SSI, AIM_CB_SSI_REQRIGHTS);
}

/*
 * SSI Rights Information.
 */
static int parserights(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
{
	int ret = 0;
	aim_rxcallback_t userfunc;

	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
		ret = userfunc(sess, rx);

	return ret;
}

/*
 * Request SSI Data.
 *
 * The data will only be sent if it is newer than the posted local
 * timestamp and revision.
 * 
 * Note that the client should never increment the revision, only the server.
 * 
 */
int aim_ssi_reqdata(aim_session_t *sess, aim_conn_t *conn, time_t localstamp, guint16 localrev)
{
	aim_frame_t *fr;
	aim_snacid_t snacid;

	if (!sess || !conn)
		return -EINVAL;

	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+4+2)))
		return -ENOMEM;

	snacid = aim_cachesnac(sess, AIM_CB_FAM_SSI, AIM_CB_SSI_REQLIST, 0x0000, NULL, 0);

	aim_putsnac(&fr->data, AIM_CB_FAM_SSI, AIM_CB_SSI_REQLIST, 0x0000, snacid);
	aimbs_put32(&fr->data, localstamp);
	aimbs_put16(&fr->data, localrev);

	aim_tx_enqueue(sess, fr);

	return 0;
}

int aim_ssi_reqalldata(aim_session_t *sess, aim_conn_t *conn)
{
	aim_frame_t *fr;
	aim_snacid_t snacid;

	if (!sess || !conn)
		return -EINVAL;

	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10)))
		return -ENOMEM;

	snacid = aim_cachesnac(sess, AIM_CB_FAM_SSI, AIM_CB_SSI_REQFULLLIST, 0x0000, NULL, 0);

	aim_putsnac(&fr->data, AIM_CB_FAM_SSI, AIM_CB_SSI_REQFULLLIST, 0x0000, snacid);

	aim_tx_enqueue(sess, fr);

	return 0;
}

/*
 * SSI Data.
 */
static int parsedata(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
{
	int ret = 0;
	aim_rxcallback_t userfunc;
	struct aim_ssi_item *cur = NULL;
	guint8 fmtver; /* guess */
	guint16 revision;
	guint32 timestamp;

	/* When you set the version for the SSI family to 2-4, the beginning of this changes.
	 * Instead of the version and then the revision, there is "0x0006" and then a type 
	 * 0x0001 TLV containing the 2 byte SSI family version that you sent earlier.  Also, 
	 * the SNAC flags go from 0x0000 to 0x8000.  I guess the 0x0006 is the length of the 
	 * TLV(s) that follow.  The rights SNAC does the same thing, with the differing flag 
	 * and everything.
	 */

	fmtver = aimbs_get8(bs); /* Version of ssi data.  Should be 0x00 */
	revision = aimbs_get16(bs); /* # of times ssi data has been modified */
	if (revision != 0)
		sess->ssi.revision = revision;

	for (cur = sess->ssi.items; cur && cur->next; cur=cur->next) ;

	while (aim_bstream_empty(bs) > 4) { /* last four bytes are stamp */
		guint16 namelen, tbslen;

		if (!sess->ssi.items) {
			if (!(sess->ssi.items = g_new0(struct aim_ssi_item, 1)))
				return -ENOMEM;
			cur = sess->ssi.items;
		} else {
			if (!(cur->next = g_new0(struct aim_ssi_item, 1)))
				return -ENOMEM;
			cur = cur->next;
		}

		if ((namelen = aimbs_get16(bs)))
			cur->name = aimbs_getstr(bs, namelen);
		cur->gid = aimbs_get16(bs);
		cur->bid = aimbs_get16(bs);
		cur->type = aimbs_get16(bs);

		if ((tbslen = aimbs_get16(bs))) {
			aim_bstream_t tbs;

			aim_bstream_init(&tbs, bs->data + bs->offset /* XXX */, tbslen);
			cur->data = (void *)aim_readtlvchain(&tbs);
			aim_bstream_advance(bs, tbslen);
		}
	}

	timestamp = aimbs_get32(bs);
	if (timestamp != 0)
		sess->ssi.timestamp = timestamp;
	sess->ssi.received_data = 1;

	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
		ret = userfunc(sess, rx, fmtver, sess->ssi.revision, sess->ssi.timestamp, sess->ssi.items);

	return ret;
}

/*
 * SSI Data Enable Presence.
 *
 * Should be sent after receiving 13/6 or 13/f to tell the server you
 * are ready to begin using the list.  It will promptly give you the
 * presence information for everyone in your list and put your permit/deny
 * settings into effect.
 * 
 */
int aim_ssi_enable(aim_session_t *sess, aim_conn_t *conn)
{
	return aim_genericreq_n(sess, conn, AIM_CB_FAM_SSI, 0x0007);
}

/*
 * Stuff for SSI authorizations. The code used to work with the old im_ch4
 * messages, but those are supposed to be obsolete. This is probably
 * ICQ-specific.
 */

/**
 * Request authorization to add someone to the server-side buddy list.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @param uin The contact's ICQ UIN.
 * @param reason The reason string to send with the request.
 * @return Return 0 if no errors, otherwise return the error number.
 */
int aim_ssi_auth_request( aim_session_t *sess, aim_conn_t *conn, char *uin, char *reason )
{
	aim_frame_t *fr;
	aim_snacid_t snacid;
	int snaclen;
	
	snaclen = 10 + 1 + strlen( uin ) + 2 + strlen( reason ) + 2;
	
	if( !( fr = aim_tx_new( sess, conn, AIM_FRAMETYPE_FLAP, 0x02, snaclen ) ) )
		return -ENOMEM;

	snacid = aim_cachesnac( sess, AIM_CB_FAM_SSI, AIM_CB_SSI_SENDAUTHREQ, 0x0000, NULL, 0 );
	aim_putsnac( &fr->data, AIM_CB_FAM_SSI, AIM_CB_SSI_SENDAUTHREQ, 0x0000, snacid );
	
	aimbs_put8( &fr->data, strlen( uin ) );
	aimbs_putraw( &fr->data, (guint8 *)uin, strlen( uin ) );
	aimbs_put16( &fr->data, strlen( reason ) );
	aimbs_putraw( &fr->data, (guint8 *)reason, strlen( reason ) );
	aimbs_put16( &fr->data, 0 );
	
	aim_tx_enqueue( sess, fr );
	
	return( 0 );
}

/**
 * Reply to an authorization request to add someone to the server-side buddy list.
 *
 * @param sess The oscar session.
 * @param conn The bos connection for this session.
 * @param uin The contact's ICQ UIN.
 * @param yesno 1 == Permit, 0 == Deny
 * @param reason The reason string to send with the request.
 * @return Return 0 if no errors, otherwise return the error number.
 */
int aim_ssi_auth_reply( aim_session_t *sess, aim_conn_t *conn, char *uin, int yesno, char *reason )
{
	aim_frame_t *fr;
	aim_snacid_t snacid;
	int snaclen;
	
	snaclen = 10 + 1 + strlen( uin ) + 3 + strlen( reason );
	
	if( !( fr = aim_tx_new( sess, conn, AIM_FRAMETYPE_FLAP, 0x02, snaclen ) ) )
		return -ENOMEM;
	
	snacid = aim_cachesnac( sess, AIM_CB_FAM_SSI, AIM_CB_SSI_SENDAUTHREP, 0x0000, NULL, 0 );
	aim_putsnac( &fr->data, AIM_CB_FAM_SSI, AIM_CB_SSI_SENDAUTHREP, 0x0000, snacid );
	
	aimbs_put8( &fr->data, strlen( uin ) );
	aimbs_putraw( &fr->data, (guint8 *)uin, strlen( uin ) );
	aimbs_put8( &fr->data, yesno );
	aimbs_put16( &fr->data, strlen( reason ) );
	aimbs_putraw( &fr->data, (guint8 *)reason, strlen( reason ) );
	
	aim_tx_enqueue( sess, fr );
	
	return( 0 );
}


/*
 * SSI Add/Mod/Del Item(s).
 *
 * Sends the SNAC to add, modify, or delete an item from the server-stored
 * information.  These 3 SNACs all have an identical structure.  The only
 * difference is the subtype that is set for the SNAC.
 * 
 */
int aim_ssi_addmoddel(aim_session_t *sess, aim_conn_t *conn, struct aim_ssi_item **items, unsigned int num, guint16 subtype)
{
	aim_frame_t *fr;
	aim_snacid_t snacid;
	int i, snaclen, listlen;
	char *list = NULL;

	if (!sess || !conn || !items || !num)
		return -EINVAL;

	snaclen = 10; /* For family, subtype, flags, and SNAC ID */
	listlen = 0;
	for (i=0; i<num; i++) {
		snaclen += 10; /* For length, GID, BID, type, and length */
		if (items[i]->name) {
			snaclen += strlen(items[i]->name);
			
			if (subtype == AIM_CB_SSI_ADD) {
				list = g_realloc(list, listlen + strlen(items[i]->name) + 1);
				strcpy(list + listlen, items[i]->name);
				listlen += strlen(items[i]->name) + 1;
			}
		} else {
			if (subtype == AIM_CB_SSI_ADD) {
				list = g_realloc(list, listlen + 1);
				list[listlen] = '\0';
				listlen ++;
			}
		}
		if (items[i]->data)
			snaclen += aim_sizetlvchain((aim_tlvlist_t **)&items[i]->data);
	}
	
	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, snaclen)))
		return -ENOMEM;

	snacid = aim_cachesnac(sess, AIM_CB_FAM_SSI, subtype, 0x0000, list, list ? listlen : 0);
	aim_putsnac(&fr->data, AIM_CB_FAM_SSI, subtype, 0x0000, snacid);
	
	g_free(list);

	for (i=0; i<num; i++) {
		aimbs_put16(&fr->data, items[i]->name ? strlen(items[i]->name) : 0);
		if (items[i]->name)
			aimbs_putraw(&fr->data, (guint8 *)items[i]->name, strlen(items[i]->name));
		aimbs_put16(&fr->data, items[i]->gid);
		aimbs_put16(&fr->data, items[i]->bid);
		aimbs_put16(&fr->data, items[i]->type);
		aimbs_put16(&fr->data, items[i]->data ? aim_sizetlvchain((aim_tlvlist_t **)&items[i]->data) : 0);
		if (items[i]->data)
			aim_writetlvchain(&fr->data, (aim_tlvlist_t **)&items[i]->data);
	}

	aim_ssi_enqueue(sess, conn, fr);

	return 0;
}

/*
 * SSI Add/Mod/Del Ack.
 *
 * Response to add, modify, or delete SNAC (sent with aim_ssi_addmoddel).
 *
 */
static int parseack(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
{
	int ret = 0;
	aim_rxcallback_t userfunc;
	aim_snac_t *origsnac;

	sess->ssi.waiting_for_ack = 0;
	aim_ssi_dispatch(sess, rx->conn);
	
	origsnac = aim_remsnac(sess, snac->id);
	
	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
		ret = userfunc(sess, rx, origsnac);
	
	if (origsnac) {
		g_free(origsnac->data);
		g_free(origsnac);
	}
	
	return ret;
}

/*
 * SSI Begin Data Modification.
 *
 * Tells the server you're going to start modifying data.
 * 
 */
int aim_ssi_modbegin(aim_session_t *sess, aim_conn_t *conn)
{
	return aim_genericreq_n(sess, conn, AIM_CB_FAM_SSI, AIM_CB_SSI_EDITSTART);
}

/*
 * SSI End Data Modification.
 *
 * Tells the server you're done modifying data.
 *
 */
int aim_ssi_modend(aim_session_t *sess, aim_conn_t *conn)
{
	return aim_genericreq_n(sess, conn, AIM_CB_FAM_SSI, AIM_CB_SSI_EDITSTOP);
}

/*
 * SSI Data Unchanged.
 *
 * Response to aim_ssi_reqdata() if the server-side data is not newer than
 * posted local stamp/revision.
 *
 */
static int parsedataunchanged(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
{
	int ret = 0;
	aim_rxcallback_t userfunc;

	sess->ssi.received_data = 1;

	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
		ret = userfunc(sess, rx);

	return ret;
}

static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
{

	if (snac->subtype == AIM_CB_SSI_RIGHTSINFO)
		return parserights(sess, mod, rx, snac, bs);
	else if (snac->subtype == AIM_CB_SSI_LIST)
		return parsedata(sess, mod, rx, snac, bs);
	else if (snac->subtype == AIM_CB_SSI_SRVACK)
		return parseack(sess, mod, rx, snac, bs);
	else if (snac->subtype == AIM_CB_SSI_NOLIST)
		return parsedataunchanged(sess, mod, rx, snac, bs);

	return 0;
}

static void ssi_shutdown(aim_session_t *sess, aim_module_t *mod)
{
	aim_ssi_freelist(sess);

	return;
}

int ssi_modfirst(aim_session_t *sess, aim_module_t *mod)
{

	mod->family = AIM_CB_FAM_SSI;
	mod->version = 0x0003;
	mod->toolid = 0x0110;
	mod->toolversion = 0x0629;
	mod->flags = 0;
	strncpy(mod->name, "ssi", sizeof(mod->name));
	mod->snachandler = snachandler;
	mod->shutdown = ssi_shutdown;

	return 0;
}