⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 im.c

📁 oscar message protocol stack
💻 C
📖 第 1 页 / 共 5 页
字号:
	int i;	fu8_t ck[8];	if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)) || !sn)		return -EINVAL;	for (i = 0; i < 8; i++)		ck[i] = (fu8_t)rand();	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+8+2+1+strlen(sn) + 4+0x5e + 4)))		return -ENOMEM;	snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);	aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);	/* ICBM header */	aim_im_puticbm(&fr->data, ck, 0x0002, sn);	/* TLV t(0005) - Encompasses almost everything below. */	aimbs_put16(&fr->data, 0x0005); /* T */	aimbs_put16(&fr->data, 0x005e); /* L */	{ /* V */		aimbs_put16(&fr->data, 0x0000);		/* Cookie */		aimbs_putraw(&fr->data, ck, 8);		/* Put the 16 byte server relay capability */		aim_putcap(&fr->data, AIM_CAPS_ICQSERVERRELAY);		/* TLV t(000a) */		aimbs_put16(&fr->data, 0x000a);		aimbs_put16(&fr->data, 0x0002);		aimbs_put16(&fr->data, 0x0001);		/* TLV t(000f) */		aimbs_put16(&fr->data, 0x000f);		aimbs_put16(&fr->data, 0x0000);		/* TLV t(2711) */		aimbs_put16(&fr->data, 0x2711);		aimbs_put16(&fr->data, 0x0036);		{ /* V */			aimbs_putle16(&fr->data, 0x001b); /* L */			aimbs_putle16(&fr->data, 0x0008); /* XXX - Protocol version */			aim_putcap(&fr->data, AIM_CAPS_EMPTY);			aimbs_putle16(&fr->data, 0x0000); /* Unknown */			aimbs_putle16(&fr->data, 0x0003); /* Client features? */			aimbs_putle16(&fr->data, 0x0000); /* Unknown */			aimbs_putle8(&fr->data, 0x00); /* Unkizown */			aimbs_putle16(&fr->data, 0xffff); /* Sequence number?  XXX - This should decrement by 1 with each request */			aimbs_putle16(&fr->data, 0x000e); /* L */			aimbs_putle16(&fr->data, 0xffff); /* Sequence number?  XXX - This should decrement by 1 with each request */			aimbs_putle32(&fr->data, 0x00000000); /* Unknown */			aimbs_putle32(&fr->data, 0x00000000); /* Unknown */			aimbs_putle32(&fr->data, 0x00000000); /* Unknown */			/* The type of status message being requested */			if (type & AIM_ICQ_STATE_CHAT)				aimbs_putle16(&fr->data, 0x03ec);			else if(type & AIM_ICQ_STATE_DND)				aimbs_putle16(&fr->data, 0x03eb);			else if(type & AIM_ICQ_STATE_OUT)				aimbs_putle16(&fr->data, 0x03ea);			else if(type & AIM_ICQ_STATE_BUSY)				aimbs_putle16(&fr->data, 0x03e9);			else if(type & AIM_ICQ_STATE_AWAY)				aimbs_putle16(&fr->data, 0x03e8);			aimbs_putle16(&fr->data, 0x0000); /* Status? */			aimbs_putle16(&fr->data, 0x0001); /* Priority of this message? */			aimbs_putle16(&fr->data, 0x0001); /* L */			aimbs_putle8(&fr->data, 0x00); /* String of length L */		} /* End TLV t(2711) */	} /* End TLV t(0005) */	/* TLV t(0003) */	aimbs_put16(&fr->data, 0x0003);	aimbs_put16(&fr->data, 0x0000);	aim_tx_enqueue(sess, fr);	return 0;}/** * Subtype 0x0006 - Send an ICQ-esque ICBM. * * This can be used to send an ICQ authorization reply (deny or grant).  It is the "old way."   * The new way is to use SSI.  I like the new way a lot better.  This seems like such a hack,  * mostly because it's in network byte order.  Figuring this stuff out sometimes takes a while,  * but thats ok, because it gives me time to try to figure out what kind of drugs the AOL people  * were taking when they merged the two protocols. * * @param sn The destination screen name. * @param type The type of message.  0x0007 for authorization denied.  0x0008 for authorization granted. * @param message The message you want to send, it should be null terminated. * @return Return 0 if no errors, otherwise return the error number. */faim_export int aim_im_sendch4(aim_session_t *sess, char *sn, fu16_t type, fu8_t *message){	aim_conn_t *conn;	aim_frame_t *fr;	aim_snacid_t snacid;	int i;	char ck[8];	if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0002)))		return -EINVAL;	if (!sn || !type || !message)		return -EINVAL;	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+8+3+strlen(sn)+12+strlen(message)+1+4)))		return -ENOMEM;	snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);	aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);	/* Cookie */	for (i=0; i<8; i++)		ck[i] = (fu8_t)rand();	/* ICBM header */	aim_im_puticbm(&fr->data, ck, 0x0004, sn);	/*	 * TLV t(0005)	 *	 * ICQ data (the UIN and the message).	 */	aimbs_put16(&fr->data, 0x0005);	aimbs_put16(&fr->data, 4 + 2+2+strlen(message)+1);	/*	 * Your UIN	 */	aimbs_putle32(&fr->data, atoi(sess->sn));	/*	 * TLV t(type) l(strlen(message)+1) v(message+NULL)	 */	aimbs_putle16(&fr->data, type);	aimbs_putle16(&fr->data, strlen(message)+1);	aimbs_putraw(&fr->data, message, strlen(message)+1);	/*	 * TLV t(0006) l(0000) v()	 */	aimbs_put16(&fr->data, 0x0006);	aimbs_put16(&fr->data, 0x0000);	aim_tx_enqueue(sess, fr);	return 0;}/* * XXX - I don't see when this would ever get called... */static int outgoingim(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs){	int i, ret = 0;	aim_rxcallback_t userfunc;	fu8_t cookie[8];	fu16_t channel;	aim_tlvlist_t *tlvlist;	char *sn;	int snlen;	fu16_t icbmflags = 0;	fu8_t flag1 = 0, flag2 = 0;	fu8_t *msg = NULL;	aim_tlv_t *msgblock;	/* ICBM Cookie. */	for (i = 0; i < 8; i++)		cookie[i] = aimbs_get8(bs);	/* Channel ID */	channel = aimbs_get16(bs);	if (channel != 0x01) {		faimdprintf(sess, 0, "icbm: ICBM recieved on unsupported channel.  Ignoring. (chan = %04x)\n", channel);		return 0;	}	snlen = aimbs_get8(bs);	sn = aimbs_getstr(bs, snlen);	tlvlist = aim_tlvlist_read(bs);	if (aim_tlv_gettlv(tlvlist, 0x0003, 1))		icbmflags |= AIM_IMFLAGS_ACK;	if (aim_tlv_gettlv(tlvlist, 0x0004, 1))		icbmflags |= AIM_IMFLAGS_AWAY;	if ((msgblock = aim_tlv_gettlv(tlvlist, 0x0002, 1))) {		aim_bstream_t mbs;		int featurelen, msglen;		aim_bstream_init(&mbs, msgblock->value, msgblock->length);		aimbs_get8(&mbs);		aimbs_get8(&mbs);		for (featurelen = aimbs_get16(&mbs); featurelen; featurelen--)			aimbs_get8(&mbs);		aimbs_get8(&mbs);		aimbs_get8(&mbs);		msglen = aimbs_get16(&mbs) - 4; /* final block length */		flag1 = aimbs_get16(&mbs);		flag2 = aimbs_get16(&mbs);		msg = aimbs_getstr(&mbs, msglen);	}	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))		ret = userfunc(sess, rx, channel, sn, msg, icbmflags, flag1, flag2);	free(sn);	aim_tlvlist_free(&tlvlist);	return ret;}/* * Ahh, the joys of nearly ridiculous over-engineering. * * Not only do AIM ICBM's support multiple channels.  Not only do they * support multiple character sets.  But they support multiple character  * sets / encodings within the same ICBM. * * These multipart messages allow for complex space savings techniques, which * seem utterly unnecessary by today's standards.  In fact, there is only * one client still in popular use that still uses this method: AOL for the * Macintosh, Version 5.0.  Obscure, yes, I know.   * * In modern (non-"legacy") clients, if the user tries to send a character * that is not ISO-8859-1 or ASCII, the client will send the entire message * as UNICODE, meaning that every character in the message will occupy the * full 16 bit UNICODE field, even if the high order byte would be zero. * Multipart messages prevent this wasted space by allowing the client to * only send the characters in UNICODE that need to be sent that way, and * the rest of the message can be sent in whatever the native character  * set is (probably ASCII). * * An important note is that sections will be displayed in the order that * they appear in the ICBM.  There is no facility for merging or rearranging * sections at run time.  So if you have, say, ASCII then UNICODE then ASCII, * you must supply two ASCII sections with a UNICODE in the middle, and incur * the associated overhead. * * Normally I would have laughed and given a firm 'no' to supporting this * seldom-used feature, but something is attracting me to it.  In the future, * it may be possible to abuse this to send mixed-media messages to other * open source clients (like encryption or something) -- see faimtest for * examples of how to do this. * * I would definitely recommend avoiding this feature unless you really * know what you are doing, and/or you have something neat to do with it. * */faim_export int aim_mpmsg_init(aim_session_t *sess, aim_mpmsg_t *mpm){	memset(mpm, 0, sizeof(aim_mpmsg_t));	return 0;}static int mpmsg_addsection(aim_session_t *sess, aim_mpmsg_t *mpm, fu16_t charset, fu16_t charsubset, fu8_t *data, fu16_t datalen){	aim_mpmsg_section_t *sec; 		if (!(sec = malloc(sizeof(aim_mpmsg_section_t))))		return -1;	sec->charset = charset;	sec->charsubset = charsubset;	sec->data = data;	sec->datalen = datalen;	sec->next = NULL;	if (!mpm->parts)		mpm->parts = sec;	else {		aim_mpmsg_section_t *cur;		for (cur = mpm->parts; cur->next; cur = cur->next)			;		cur->next = sec;	}	mpm->numparts++;	return 0;}faim_export int aim_mpmsg_addraw(aim_session_t *sess, aim_mpmsg_t *mpm, fu16_t charset, fu16_t charsubset, const fu8_t *data, fu16_t datalen){	fu8_t *dup;	if (!(dup = malloc(datalen)))		return -1;	memcpy(dup, data, datalen);	if (mpmsg_addsection(sess, mpm, charset, charsubset, dup, datalen) == -1) {		free(dup);		return -1;	}	return 0;}/* XXX - should provide a way of saying ISO-8859-1 specifically */faim_export int aim_mpmsg_addascii(aim_session_t *sess, aim_mpmsg_t *mpm, const char *ascii){	fu8_t *dup;	if (!(dup = strdup(ascii))) 		return -1;	if (mpmsg_addsection(sess, mpm, 0x0000, 0x0000, dup, strlen(ascii)) == -1) {		free(dup);		return -1;	}	return 0;}faim_export int aim_mpmsg_addunicode(aim_session_t *sess, aim_mpmsg_t *mpm, const fu16_t *unicode, fu16_t unicodelen){	fu8_t *buf;	aim_bstream_t bs;	int i;	if (!(buf = malloc(unicodelen * 2)))		return -1;	aim_bstream_init(&bs, buf, unicodelen * 2);	/* We assume unicode is in /host/ byte order -- convert to network */	for (i = 0; i < unicodelen; i++)		aimbs_put16(&bs, unicode[i]);	if (mpmsg_addsection(sess, mpm, 0x0002, 0x0000, buf, aim_bstream_curpos(&bs)) == -1) {		free(buf);		return -1;	}		return 0;}faim_export void aim_mpmsg_free(aim_session_t *sess, aim_mpmsg_t *mpm){	aim_mpmsg_section_t *cur;	for (cur = mpm->parts; cur; ) {		aim_mpmsg_section_t *tmp;				tmp = cur->next;		free(cur->data);		free(cur);		cur = tmp;	}		mpm->numparts = 0;	mpm->parts = NULL;	return;}/* * Start by building the multipart structures, then pick the first  * human-readable section and stuff it into args->msg so no one gets * suspicious. * */static int incomingim_ch1_parsemsgs(aim_session_t *sess, fu8_t *data, int len, struct aim_incomingim_ch1_args *args){	/* Should this be ASCII -> UNICODE -> Custom */	static const fu16_t charsetpri[] = {		AIM_CHARSET_ASCII, /* ASCII first */		AIM_CHARSET_CUSTOM, /* then ISO-8859-1 */		AIM_CHARSET_UNICODE, /* UNICODE as last resort */	};	static const int charsetpricount = 3;	int i;	aim_bstream_t mbs;	aim_mpmsg_section_t *sec;	aim_bstream_init(&mbs, data, len);	while (aim_bstream_empty(&mbs)) {		fu16_t msglen, flag1, flag2;		fu8_t *msgbuf;		aimbs_get8(&mbs); /* 01 */		aimbs_get8(&mbs); /* 01 */		/* Message string length, including character set info. */		msglen = aimbs_get16(&mbs);		/* Character set info */		flag1 = aimbs_get16(&mbs);		flag2 = aimbs_get16(&mbs);		/* Message. */		msglen -= 4;		/*		 * For now, we don't care what the encoding is.  Just copy		 * it into a multipart struct and deal with it later. However,		 * always pad the ending with a NULL.  This makes it easier		 * to treat ASCII sections as strings.  It won't matter for		 * UNICODE or binary data, as you should never read past		 * the specified data length, which will not include the pad.		 *		 * XXX - There's an API bug here.  For sending, the UNICODE is		 * given in host byte order (aim_mpmsg_addunicode), but here		 * the received messages are given in network byte order.		 *		 */		msgbuf = aimbs_getstr(&mbs, msglen);		mpmsg_addsection(sess, &args->mpmsg, flag1, flag2, msgbuf, msglen);	} /* while */	args->icbmflags |= AIM_IMFLAGS_MULTIPART; /* always set */	/*	 * Clients that support multiparts should never use args->msg, as it	 * will point to an arbitrary section.	 *	 * Here, we attempt to provide clients that do not support multipart	 * messages with something to look at -- hopefully a human-readable	 * string.  But, failing that, a UNICODE message, or nothing at all.	 *	 * Which means that even if args->msg is NULL, it does not mean the	 * message was blank.	 *	 */	for (i = 0; i < charsetpricount; i++) {		for (sec = args->mpmsg.parts; sec; sec = sec->next) {			if (sec->charset != charsetpri[i])				continue;			/* Great. We found one.  Fill it in. */			args->charset = sec->charset;			args->charsubset = sec->charsubset;			/* Set up the simple flags */			if (args->charsubset == 0x0000)				; /* standard subencoding? */			else if (args->charsubset == 0x000b)				args->icbmflags |= AIM_IMFLAGS_SUBENC_MACINTOSH;			else if (args->charsubset == 0xffff)				; /* no subencoding */			args->msg = sec->data;			args->msglen = sec->datalen;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -