view src/protocols/oscar/rxqueue.c @ 12645:fc28451f5d96

[gaim-migrate @ 14983] SF Patch #1314512 from Sadrul (who has a patch for everything) "This patch introduces a flag for protocol plugins that support offline messages (like Y!M and ICQ). This was encouraged by the following conversation: <sadrul> should offline buddies be listed/enabled in the send-to menu? <rekkanoryo> i would think only for protocols that support offline messaging, if it's indicated that the buddy is offline -- <snip> -- <Bleeter> sadrul: personally, I'd like to see a 'supports offline' flag of some description <Bleeter> one could then redirect (via plugins) through email or alternative methods <Bleeter> just a thought <Paco-Paco> yeah, that sounds like a reasonble thing to have This patch uses this flag to disable the buddies in the send-to menu who are offline and the protocol doesn't support offline messages." I made this make the label insensitive instead of the whole menuitem. This should address SimGuy's concerns about inconsistency (i.e. you could create a conversation with someone via the buddy list that you couldn't create via the Send To menu). I also hacked up some voodoo to show the label as sensitive when moused-over, as that looks better (given the label-insensitive thing is itself a hack). I think this works quite well. BUG NOTE: This makes more obvious an existing bug. The Send To menu isn't updated when buddies sign on or off or change status (at least under some circumstances). We need to fix that anyway, so I'm not going to let it hold up this commit. Switching tabs will clear it up. I'm thinking we just might want to build the contents of that menu when it is selected. That would save us a mess of inefficient signal callbacks that update the Send To menus in open windows all the time. AIM NOTE: This assumes that AIM can't offline message. That's not strictly true. You can message invisible users on AIM. However, by design, we can't tell when a user is invisible without resorting to dirty hackery. In practice, this isn't a problem, as you can still select the AIM user from the menu. And really, how often will you be choosing the Invisible contact, rather than the user going Invisible in the middle of a conversation or IMing you while they're Invisible? JABBER NOTE: This assumes that Jabber can always offline message. This isn't strictly true. Sadrul said: I have updated Jabber according to this link which seems to talk about how to determine the existence offline-message support in a server: http://www.jabber.org/jeps/jep-0013.html#discover However, jabber.org doesn't seem to send the required info. So I am not sure about it. He later said: I talked to Nathan and he said offline message support is mostly assumed for most jabber servers. GTalk doesn't yet support it, but they are working on it. So I have made jabber to always return TRUE. If there is truly no way to detect offline messaging capability, then this is an acceptable solution. We could special case Google Talk because of its popularity, and remove that later. It's probably not worth it though. MSN NOTE: This assumes that MSN can never offline message. That's effectively true, but to be technically correct, MSN can offline message if there's already a switchboard conversation open with a user. We could write an offline_message function in the MSN prpl to detect that, but it'd be of limited usefulness, especially given that under most circumstances (where this might matter), the switchboard connection will be closed almost immediately. CVS NOTE: I'm writing to share a tragic little story. I have a PC that I use for Gaim development. One day, I was writing a commit message on it, when all of a suddent it went berserk. The screen started flashing, and the whole commit message just disappeared. All of it. And it was a good commit message! I had to cram and rewrite it really quickly. Needless to say, my rushed commit message wasn't nearly as good, and I blame the PC for that. Seriously, though, what kind of version control system loses your commit message on a broken connection to the server? Stupid! committer: Tailor Script <tailor@pidgin.im>
author Richard Laager <rlaager@wiktel.com>
date Fri, 23 Dec 2005 19:26:04 +0000
parents c5c0f714d8bc
children af3d6c6aee6b
line wrap: on
line source

/*
 * This file contains the management routines for the receive
 * (incoming packet) queue.  The actual packet handlers are in
 * rxhandlers.c.
 */

#define FAIM_INTERNAL
#include <aim.h>

#ifndef _WIN32
#include <sys/socket.h>
#endif

/*
 *
 */
faim_internal int aim_recv(int fd, void *buf, size_t count)
{
	int left, cur;

	for (cur = 0, left = count; left; ) {
		int ret;

		ret = recv(fd, ((unsigned char *)buf)+cur, left, 0);

		/* Of course EOF is an error, only morons disagree with that. */
		if (ret <= 0)
			return -1;

		cur += ret;
		left -= ret;
	}

	return cur;
}

/*
 * Read into a byte stream.  Will not read more than count, but may read
 * less if there is not enough room in the stream buffer.
 */
faim_internal int aim_bstream_recv(aim_bstream_t *bs, int fd, size_t count)
{
	int red = 0;

	if (!bs || (fd < 0))
		return -1;

	if (count > (bs->len - bs->offset))
		count = bs->len - bs->offset; /* truncate to remaining space */

	if (count) {

		red = aim_recv(fd, bs->data + bs->offset, count);

		if (red <= 0)
			return -1;
	}

	bs->offset += red;

	return red;
}

/**
 * Free an aim_frame_t
 *
 * @param frame The frame to free.
 * @return -1 on error; 0 on success.
 */
faim_internal void aim_frame_destroy(aim_frame_t *frame)
{

	free(frame->data.data); /* XXX aim_bstream_free */
	free(frame);

	return;
}

/*
 * Read a FLAP header from conn into fr, and return the number of 
 * bytes in the payload.
 *
 * @return -1 on error, otherwise return the length of the payload.
 */
static int aim_get_command_flap(aim_session_t *sess, aim_conn_t *conn, aim_frame_t *fr)
{
	fu8_t hdr_raw[6];
	aim_bstream_t hdr;

	fr->hdrtype = AIM_FRAMETYPE_FLAP;

	/*
	 * Read FLAP header.  Six bytes total.
	 *
	 *   Byte # | Description
	 *   -------|-------------
	 *    0x00  | Always 0x2a
	 *    0x01  | Channel number, usually "2."  "1" is used during login,
	 *          |   4 is used during logoff.
	 *    0x02  | Sequence number, 2 bytes.
	 *    0x04  | Number of data bytes that follow, 2 bytes.
	 */
	aim_bstream_init(&hdr, hdr_raw, sizeof(hdr_raw));
	if (aim_bstream_recv(&hdr, conn->fd, 6) < 6) {
		aim_conn_close(conn);
		return -1;
	}

	aim_bstream_rewind(&hdr);

	/*
	 * This shouldn't happen unless the socket breaks, the server breaks,
	 * or we break.  We must handle it just in case.
	 */
	if (aimbs_get8(&hdr) != 0x2a) {
		gaim_debug_misc("oscar", "Invalid FLAP frame received on FLAP connection!");
		aim_conn_close(conn);
		return -1;
	}

	fr->hdr.flap.channel = aimbs_get8(&hdr);
	fr->hdr.flap.seqnum = aimbs_get16(&hdr);

	return aimbs_get16(&hdr);
}

/*
 * Read a rendezvous header from conn into fr, and return the number of 
 * bytes in the payload.
 *
 * @return -1 on error, otherwise return the length of the payload.
 */
static int aim_get_command_rendezvous(aim_session_t *sess, aim_conn_t *conn, aim_frame_t *fr)
{
	fu8_t hdr_raw[8];
	aim_bstream_t hdr;

	fr->hdrtype = AIM_FRAMETYPE_OFT;

	/*
	 * Read rendezvous header
	 */
	aim_bstream_init(&hdr, hdr_raw, sizeof(hdr_raw));
	if (aim_bstream_recv(&hdr, conn->fd, 8) < 8) {
		aim_conn_close(conn);
		return -1;
	}

	aim_bstream_rewind(&hdr);

	aimbs_getrawbuf(&hdr, fr->hdr.rend.magic, 4);
	fr->hdr.rend.hdrlen = aimbs_get16(&hdr);
	fr->hdr.rend.type = aimbs_get16(&hdr);

	return fr->hdr.rend.hdrlen - 8;
}

/*
 * Grab a single command sequence off the socket, and enqueue it in the incoming event queue 
 * in a separate struct.
 *
 * @return 0 on success, otherwise return the error number.
 */
faim_export int aim_get_command(aim_session_t *sess, aim_conn_t *conn)
{
	aim_frame_t *fr;
	int payloadlen;

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

	if (conn->fd == -1)
		return -1; /* it's an aim_conn_close()'d connection */

	/* If stdin is closed, then zero becomes a valid fd
	if (conn->fd < 3)
		return -1;
	*/

	if (conn->status & AIM_CONN_STATUS_INPROGRESS)
		return aim_conn_completeconnect(sess, conn);

	if (!(fr = (aim_frame_t *)calloc(sizeof(aim_frame_t), 1)))
		return -ENOMEM;

	/*
	 * Rendezvous (client to client) connections do not speak FLAP, so this 
	 * function will break on them.
	 */
	if (conn->type == AIM_CONN_TYPE_RENDEZVOUS)
		payloadlen = aim_get_command_rendezvous(sess, conn, fr);
	else if (conn->type == AIM_CONN_TYPE_LISTENER) {
		gaim_debug_misc("oscar", "AIM_CONN_TYPE_LISTENER on fd %d\n", conn->fd);
		free(fr);
		return -1;
	} else
		payloadlen = aim_get_command_flap(sess, conn, fr);

	if (payloadlen < 0) {
		free(fr);
		return -1;
	}

	if (payloadlen > 0) {
		fu8_t *payload = NULL;

		if (!(payload = (fu8_t *) malloc(payloadlen))) {
			aim_frame_destroy(fr);
			return -1;
		}

		aim_bstream_init(&fr->data, payload, payloadlen);

		/* read the payload */
		if (aim_bstream_recv(&fr->data, conn->fd, payloadlen) < payloadlen) {
			aim_frame_destroy(fr); /* free's payload */
			aim_conn_close(conn);
			return -1;
		}
	} else
		aim_bstream_init(&fr->data, NULL, 0);

	aim_bstream_rewind(&fr->data);

	fr->conn = conn;

	/* Enqueue this puppy */
	fr->next = NULL;  /* this will always be at the bottom */
	if (sess->queue_incoming == NULL)
		sess->queue_incoming = fr;
	else {
		aim_frame_t *cur;
		for (cur = sess->queue_incoming; cur->next; cur = cur->next);
		cur->next = fr;
	}

	fr->conn->lastactivity = time(NULL);

	return 0;
}

/*
 * Purge receive queue of all handled commands (->handled==1).
 *
 */
faim_export void aim_purge_rxqueue(aim_session_t *sess)
{
	aim_frame_t *cur, **prev;

	for (prev = &sess->queue_incoming; (cur = *prev); ) {
		if (cur->handled) {
			*prev = cur->next;
			aim_frame_destroy(cur);
		} else
			prev = &cur->next;
	}

	return;
}

/*
 * Since aim_get_command will aim_conn_kill dead connections, we need
 * to clean up the rxqueue of unprocessed connections on that socket.
 *
 * XXX: this is something that was handled better in the old connection
 * handling method, but eh.
 */
faim_internal void aim_rxqueue_cleanbyconn(aim_session_t *sess, aim_conn_t *conn)
{
	aim_frame_t *currx;

	for (currx = sess->queue_incoming; currx; currx = currx->next) {
		if ((!currx->handled) && (currx->conn == conn))
			currx->handled = 1;
	}

	return;
}