aboutsummaryrefslogtreecommitdiffstats
path: root/protocols/oscar/rxhandlers.c
blob: 6ff106b203001f360f3c61b72d37b6436c9d53a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
.\" Manual page for bitlbee.conf, derived from the modules.conf manpage.
.\" Writing a complete manpage from scratch is just too much work...
.\"
.\" This program is distributed according to the Gnu General Public License.
.\" See the file COPYING in the base distribution directory
.\"
.TH BITLBEE.CONF 5 "07 March 2004"
.UC 4
.SH NAME
bitlbee.conf \- configuration file for
.BR bitlbee (8)
.SH DESCRIPTION
This file contains system-wide settings for the
.BR bitlbee (8)
program. For more information about the file syntax, please read the
example configuration file which comes with the program. The default
file contains lots of comments which explain all the available options.
.SH SEE ALSO
.BR bitlbee (8)
id='n234' href='#n234'>234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
/*
 * aim_rxhandlers.c
 *
 * This file contains most all of the incoming packet handlers, along
 * with aim_rxdispatch(), the Rx dispatcher.  Queue/list management is
 * actually done in aim_rxqueue.c.
 *
 */

#include <aim.h>

struct aim_rxcblist_s {
	guint16 family;
	guint16 type;
	aim_rxcallback_t handler;
	u_short flags;
	struct aim_rxcblist_s *next;
};

aim_module_t *aim__findmodulebygroup(aim_session_t *sess, guint16 group)
{
	aim_module_t *cur;

	for (cur = (aim_module_t *) sess->modlistv; cur; cur = cur->next) {
		if (cur->family == group) {
			return cur;
		}
	}

	return NULL;
}

static aim_module_t *aim__findmodule(aim_session_t *sess, const char *name)
{
	aim_module_t *cur;

	for (cur = (aim_module_t *) sess->modlistv; cur; cur = cur->next) {
		if (strcmp(name, cur->name) == 0) {
			return cur;
		}
	}

	return NULL;
}

int aim__registermodule(aim_session_t *sess, int (*modfirst)(aim_session_t *, aim_module_t *))
{
	aim_module_t *mod;

	if (!sess || !modfirst) {
		return -1;
	}

	if (!(mod = g_new0(aim_module_t, 1))) {
		return -1;
	}

	if (modfirst(sess, mod) == -1) {
		g_free(mod);
		return -1;
	}

	if (aim__findmodule(sess, mod->name)) {
		if (mod->shutdown) {
			mod->shutdown(sess, mod);
		}
		g_free(mod);
		return -1;
	}

	mod->next = (aim_module_t *) sess->modlistv;
	sess->modlistv = mod;


	return 0;
}

void aim__shutdownmodules(aim_session_t *sess)
{
	aim_module_t *cur;

	for (cur = (aim_module_t *) sess->modlistv; cur; ) {
		aim_module_t *tmp;

		tmp = cur->next;

		if (cur->shutdown) {
			cur->shutdown(sess, cur);
		}

		g_free(cur);

		cur = tmp;
	}

	sess->modlistv = NULL;

	return;
}

static int consumesnac(aim_session_t *sess, aim_frame_t *rx)
{
	aim_module_t *cur;
	aim_modsnac_t snac;

	if (aim_bstream_empty(&rx->data) < 10) {
		return 0;
	}

	snac.family = aimbs_get16(&rx->data);
	snac.subtype = aimbs_get16(&rx->data);
	snac.flags = aimbs_get16(&rx->data);
	snac.id = aimbs_get32(&rx->data);

	/* Contains TLV(s) in the FNAC header */
	if (snac.flags & 0x8000) {
		aim_bstream_advance(&rx->data, aimbs_get16(&rx->data));
	} else if (snac.flags & 0x0001) {
		/* Following SNAC will be related */
	}

	for (cur = (aim_module_t *) sess->modlistv; cur; cur = cur->next) {

		if (!(cur->flags & AIM_MODFLAG_MULTIFAMILY) &&
		    (cur->family != snac.family)) {
			continue;
		}

		if (cur->snachandler(sess, cur, rx, &snac, &rx->data)) {
			return 1;
		}

	}

	return 0;
}

static int consumenonsnac(aim_session_t *sess, aim_frame_t *rx, guint16 family, guint16 subtype)
{
	aim_module_t *cur;
	aim_modsnac_t snac;

	snac.family = family;
	snac.subtype = subtype;
	snac.flags = snac.id = 0;

	for (cur = (aim_module_t *) sess->modlistv; cur; cur = cur->next) {

		if (!(cur->flags & AIM_MODFLAG_MULTIFAMILY) &&
		    (cur->family != snac.family)) {
			continue;
		}

		if (cur->snachandler(sess, cur, rx, &snac, &rx->data)) {
			return 1;
		}

	}

	return 0;
}

static int negchan_middle(aim_session_t *sess, aim_frame_t *fr)
{
	aim_tlvlist_t *tlvlist;
	char *msg = NULL;
	guint16 code = 0;
	aim_rxcallback_t userfunc;
	int ret = 1;

	if (aim_bstream_empty(&fr->data) == 0) {
		/* XXX should do something with this */
		return 1;
	}

	/* Used only by the older login protocol */
	/* XXX remove this special case? */
	if (fr->conn->type == AIM_CONN_TYPE_AUTH) {
		return consumenonsnac(sess, fr, 0x0017, 0x0003);
	}

	tlvlist = aim_readtlvchain(&fr->data);

	if (aim_gettlv(tlvlist, 0x0009, 1)) {
		code = aim_gettlv16(tlvlist, 0x0009, 1);
	}

	if (aim_gettlv(tlvlist, 0x000b, 1)) {
		msg = aim_gettlv_str(tlvlist, 0x000b, 1);
	}

	if ((userfunc = aim_callhandler(sess, fr->conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNERR))) {
		ret = userfunc(sess, fr, code, msg);
	}

	aim_freetlvchain(&tlvlist);

	g_free(msg);

	return ret;
}

/*
 * Some SNACs we do not allow to be hooked, for good reason.
 */
static int checkdisallowed(guint16 group, guint16 type)
{
	static const struct {
		guint16 group;
		guint16 type;
	} dontuse[] = {
		{ 0x0001, 0x0002 },
		{ 0x0001, 0x0003 },
		{ 0x0001, 0x0006 },
		{ 0x0001, 0x0007 },
		{ 0x0001, 0x0008 },
		{ 0x0001, 0x0017 },
		{ 0x0001, 0x0018 },
		{ 0x0000, 0x0000 }
	};
	int i;

	for (i = 0; dontuse[i].group != 0x0000; i++) {
		if ((dontuse[i].group == group) && (dontuse[i].type == type)) {
			return 1;
		}
	}

	return 0;
}

int aim_conn_addhandler(aim_session_t *sess, aim_conn_t *conn, guint16 family, guint16 type,
                        aim_rxcallback_t newhandler, guint16 flags)
{
	struct aim_rxcblist_s *newcb;

	if (!conn) {
		return -1;
	}

	if (checkdisallowed(family, type)) {
		g_assert(0);
		return -1;
	}

	if (!(newcb = (struct aim_rxcblist_s *) g_new0(struct aim_rxcblist_s, 1))) {
		return -1;
	}

	newcb->family = family;
	newcb->type = type;
	newcb->flags = flags;
	newcb->handler = newhandler;
	newcb->next = NULL;

	if (!conn->handlerlist) {
		conn->handlerlist = (void *) newcb;
	} else {
		struct aim_rxcblist_s *cur;

		for (cur = (struct aim_rxcblist_s *) conn->handlerlist; cur->next; cur = cur->next) {
			;
		}
		cur->next = newcb;
	}

	return 0;
}

int aim_clearhandlers(aim_conn_t *conn)
{
	struct aim_rxcblist_s *cur;

	if (!conn) {
		return -1;
	}

	for (cur = (struct aim_rxcblist_s *) conn->handlerlist; cur; ) {
		struct aim_rxcblist_s *tmp;

		tmp = cur->next;
		g_free(cur);
		cur = tmp;
	}
	conn->handlerlist = NULL;

	return 0;
}

aim_rxcallback_t aim_callhandler(aim_session_t *sess, aim_conn_t *conn, guint16 family, guint16 type)
{
	struct aim_rxcblist_s *cur;

	if (!conn) {
		return NULL;
	}

	for (cur = (struct aim_rxcblist_s *) conn->handlerlist; cur; cur = cur->next) {
		if ((cur->family == family) && (cur->type == type)) {
			return cur->handler;
		}
	}

	if (type == AIM_CB_SPECIAL_DEFAULT) {
		return NULL; /* prevent infinite recursion */
	}

	return aim_callhandler(sess, conn, family, AIM_CB_SPECIAL_DEFAULT);
}

static int aim_callhandler_noparam(aim_session_t *sess, aim_conn_t *conn, guint16 family, guint16 type,
                                   aim_frame_t *ptr)
{
	aim_rxcallback_t userfunc;

	if ((userfunc = aim_callhandler(sess, conn, family, type))) {
		return userfunc(sess, ptr);
	}

	return 1; /* XXX */
}

/*
 * aim_rxdispatch()
 *
 * Basically, heres what this should do:
 *   1) Determine correct packet handler for this packet
 *   2) Mark the packet handled (so it can be dequeued in purge_queue())
 *   3) Send the packet to the packet handler
 *   4) Go to next packet in the queue and start over
 *   5) When done, run purge_queue() to purge handled commands
 *
 * TODO: Clean up.
 * TODO: More support for mid-level handlers.
 * TODO: Allow for NULL handlers.
 *
 */
void aim_rxdispatch(aim_session_t *sess)
{
	int i;
	aim_frame_t *cur;

	for (cur = sess->queue_incoming, i = 0; cur; cur = cur->next, i++) {

		/*
		 * XXX: This is still fairly ugly.
		 */

		if (cur->handled) {
			continue;
		}

		if (cur->hdr.flap.type == 0x01) {

			cur->handled = aim_callhandler_noparam(sess, cur->conn, AIM_CB_FAM_SPECIAL,
			                                       AIM_CB_SPECIAL_FLAPVER, cur);                                      /* XXX use consumenonsnac */

			continue;

		} else if (cur->hdr.flap.type == 0x02) {

			if ((cur->handled = consumesnac(sess, cur))) {
				continue;
			}

		} else if (cur->hdr.flap.type == 0x04) {

			cur->handled = negchan_middle(sess, cur);
			continue;

		} else if (cur->hdr.flap.type == 0x05) {
			;
		}

		if (!cur->handled) {
			consumenonsnac(sess, cur, 0xffff, 0xffff); /* last chance! */
			cur->handled = 1;
		}
	}

	/*
	 * This doesn't have to be called here.  It could easily be done
	 * by a separate thread or something. It's an administrative operation,
	 * and can take a while. Though the less you call it the less memory
	 * you'll have :)
	 */
	aim_purge_rxqueue(sess);

	return;
}