Changeset - eaf1bcd9eeff
[Not reviewed]
CMakeLists.txt
Show inline comments
 
@@ -165,12 +165,13 @@ if (PROTOBUF_FOUND)
 
		message("Skype plugin      : no (install dbus-glib-devel)")
 
	endif()
 

	
 
	if(YAHOO2_FOUND)
 
#	We have our own copy now...
 
# 	if(YAHOO2_FOUND)
 
		message("Libyahoo2 plugin  : yes")
 
		include_directories(${YAHOO2_INCLUDE_DIR})
 
	else()
 
		message("Libyahoo2 plugin  : no (install libyahoo2-devel)")
 
	endif()
 
# 		include_directories(${YAHOO2_INCLUDE_DIR})
 
# 	else()
 
# 		message("Libyahoo2 plugin  : no (install libyahoo2-devel)")
 
# 	endif()
 

	
 
else()
 
	message("Network plugins   : no (install libprotobuf-dev)")
backends/CMakeLists.txt
Show inline comments
 
@@ -11,9 +11,9 @@ if (PROTOBUF_FOUND)
 

	
 
	ADD_SUBDIRECTORY(swiften)
 

	
 
	if(YAHOO2_FOUND)
 
# 	if(YAHOO2_FOUND)
 
		ADD_SUBDIRECTORY(libyahoo2)
 
	endif()
 
# 	endif()
 

	
 
	ADD_SUBDIRECTORY(template)
 

	
backends/libyahoo2/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
 
FILE(GLOB SRC *.c *.cpp)
 
FILE(GLOB_RECURSE SRC *.c *.cpp)
 
 
ADD_DEFINITIONS(-DHAVE_STDINT_H=1)
 
 
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/yahoo)
 
 
ADD_EXECUTABLE(spectrum2_libyahoo2_backend ${SRC})
 
 
target_link_libraries(spectrum2_libyahoo2_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES} ${YAHOO2_LIBRARY})
 
target_link_libraries(spectrum2_libyahoo2_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
 
INSTALL(TARGETS spectrum2_libyahoo2_backend RUNTIME DESTINATION bin)
 
backends/libyahoo2/yahoo/crypt.c
Show inline comments
 
new file 100644
 
/* One way encryption based on MD5 sum.
 
   Copyright (C) 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
 
   This file is part of the GNU C Library.
 
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
 

	
 
   The GNU C Library is free software; you can redistribute it and/or
 
   modify it under the terms of the GNU Lesser General Public
 
   License as published by the Free Software Foundation; either
 
   version 2.1 of the License, or (at your option) any later version.
 

	
 
   The GNU C Library is distributed in the hope that it will be useful,
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
   Lesser General Public License for more details.
 

	
 
   You should have received a copy of the GNU Lesser General Public
 
   License along with the GNU C Library; if not, write to the Free
 
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 
   02111-1307 USA.  */
 

	
 
/* warmenhoven took this file and made it work with the md5.[ch] we
 
 * already had. isn't that lovely. people should just use linux or
 
 * freebsd, crypt works properly on those systems. i hate solaris */
 

	
 
#if HAVE_CONFIG_H
 
#  include <config.h>
 
#endif
 

	
 
#if HAVE_STRING_H
 
#  include <string.h>
 
#elif HAVE_STRINGS_H
 
#  include <strings.h>
 
#endif
 

	
 
#include <stdlib.h>
 
#include "yahoo_util.h"
 

	
 
#include "md5.h"
 

	
 
/* Define our magic string to mark salt for MD5 "encryption"
 
   replacement.  This is meant to be the same as for other MD5 based
 
   encryption implementations.  */
 
static const char md5_salt_prefix[] = "$1$";
 

	
 
/* Table with characters for base64 transformation.  */
 
static const char b64t[64] =
 
	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 

	
 
char *yahoo_crypt(char *key, char *salt)
 
{
 
	char *buffer = NULL;
 
	int buflen = 0;
 
	int needed = 3 + strlen(salt) + 1 + 26 + 1;
 

	
 
	md5_byte_t alt_result[16];
 
	md5_state_t ctx;
 
	md5_state_t alt_ctx;
 
	size_t salt_len;
 
	size_t key_len;
 
	size_t cnt;
 
	char *cp;
 

	
 
	if (buflen < needed) {
 
		buflen = needed;
 
		if ((buffer = realloc(buffer, buflen)) == NULL)
 
			return NULL;
 
	}
 

	
 
	/* Find beginning of salt string.  The prefix should normally always
 
	   be present.  Just in case it is not.  */
 
	if (strncmp(md5_salt_prefix, salt, sizeof(md5_salt_prefix) - 1) == 0)
 
		/* Skip salt prefix.  */
 
		salt += sizeof(md5_salt_prefix) - 1;
 

	
 
	salt_len = MIN(strcspn(salt, "$"), 8);
 
	key_len = strlen(key);
 

	
 
	/* Prepare for the real work.  */
 
	md5_init(&ctx);
 

	
 
	/* Add the key string.  */
 
	md5_append(&ctx, (md5_byte_t *)key, key_len);
 

	
 
	/* Because the SALT argument need not always have the salt prefix we
 
	   add it separately.  */
 
	md5_append(&ctx, (md5_byte_t *)md5_salt_prefix,
 
		sizeof(md5_salt_prefix) - 1);
 

	
 
	/* The last part is the salt string.  This must be at most 8
 
	   characters and it ends at the first `$' character (for
 
	   compatibility which existing solutions).  */
 
	md5_append(&ctx, (md5_byte_t *)salt, salt_len);
 

	
 
	/* Compute alternate MD5 sum with input KEY, SALT, and KEY.  The
 
	   final result will be added to the first context.  */
 
	md5_init(&alt_ctx);
 

	
 
	/* Add key.  */
 
	md5_append(&alt_ctx, (md5_byte_t *)key, key_len);
 

	
 
	/* Add salt.  */
 
	md5_append(&alt_ctx, (md5_byte_t *)salt, salt_len);
 

	
 
	/* Add key again.  */
 
	md5_append(&alt_ctx, (md5_byte_t *)key, key_len);
 

	
 
	/* Now get result of this (16 bytes) and add it to the other
 
	   context.  */
 
	md5_finish(&alt_ctx, alt_result);
 

	
 
	/* Add for any character in the key one byte of the alternate sum.  */
 
	for (cnt = key_len; cnt > 16; cnt -= 16)
 
		md5_append(&ctx, alt_result, 16);
 
	md5_append(&ctx, alt_result, cnt);
 

	
 
	/* For the following code we need a NUL byte.  */
 
	alt_result[0] = '\0';
 

	
 
	/* The original implementation now does something weird: for every 1
 
	   bit in the key the first 0 is added to the buffer, for every 0
 
	   bit the first character of the key.  This does not seem to be
 
	   what was intended but we have to follow this to be compatible.  */
 
	for (cnt = key_len; cnt > 0; cnt >>= 1)
 
		md5_append(&ctx,
 
			(cnt & 1) != 0 ? alt_result : (md5_byte_t *)key, 1);
 

	
 
	/* Create intermediate result.  */
 
	md5_finish(&ctx, alt_result);
 

	
 
	/* Now comes another weirdness.  In fear of password crackers here
 
	   comes a quite long loop which just processes the output of the
 
	   previous round again.  We cannot ignore this here.  */
 
	for (cnt = 0; cnt < 1000; ++cnt) {
 
		/* New context.  */
 
		md5_init(&ctx);
 

	
 
		/* Add key or last result.  */
 
		if ((cnt & 1) != 0)
 
			md5_append(&ctx, (md5_byte_t *)key, key_len);
 
		else
 
			md5_append(&ctx, alt_result, 16);
 

	
 
		/* Add salt for numbers not divisible by 3.  */
 
		if (cnt % 3 != 0)
 
			md5_append(&ctx, (md5_byte_t *)salt, salt_len);
 

	
 
		/* Add key for numbers not divisible by 7.  */
 
		if (cnt % 7 != 0)
 
			md5_append(&ctx, (md5_byte_t *)key, key_len);
 

	
 
		/* Add key or last result.  */
 
		if ((cnt & 1) != 0)
 
			md5_append(&ctx, alt_result, 16);
 
		else
 
			md5_append(&ctx, (md5_byte_t *)key, key_len);
 

	
 
		/* Create intermediate result.  */
 
		md5_finish(&ctx, alt_result);
 
	}
 

	
 
	/* Now we can construct the result string.  It consists of three
 
	   parts.  */
 

	
 
	strncpy(buffer, md5_salt_prefix, MAX(0, buflen));
 
	cp = buffer + strlen(buffer);
 
	buflen -= sizeof(md5_salt_prefix);
 

	
 
	strncpy(cp, salt, MIN((size_t) buflen, salt_len));
 
	cp = cp + strlen(cp);
 
	buflen -= MIN((size_t) buflen, salt_len);
 

	
 
	if (buflen > 0) {
 
		*cp++ = '$';
 
		--buflen;
 
	}
 
#define b64_from_24bit(B2, B1, B0, N) \
 
	do { \
 
		unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \
 
		int n = (N); \
 
		while (n-- > 0 && buflen > 0) { \
 
			*cp++ = b64t[w & 0x3f]; \
 
			--buflen; \
 
			w >>= 6; \
 
		}\
 
	} while (0)
 

	
 
	b64_from_24bit(alt_result[0], alt_result[6], alt_result[12], 4);
 
	b64_from_24bit(alt_result[1], alt_result[7], alt_result[13], 4);
 
	b64_from_24bit(alt_result[2], alt_result[8], alt_result[14], 4);
 
	b64_from_24bit(alt_result[3], alt_result[9], alt_result[15], 4);
 
	b64_from_24bit(alt_result[4], alt_result[10], alt_result[5], 4);
 
	b64_from_24bit(0, 0, alt_result[11], 2);
 
	if (buflen <= 0) {
 
		FREE(buffer);
 
	} else
 
		*cp = '\0';	/* Terminate the string.  */
 

	
 
	/* Clear the buffer for the intermediate result so that people
 
	   attaching to processes or reading core dumps cannot get any
 
	   information.  We do it in this way to clear correct_words[]
 
	   inside the MD5 implementation as well.  */
 
	md5_init(&ctx);
 
	md5_finish(&ctx, alt_result);
 
	memset(&ctx, '\0', sizeof(ctx));
 
	memset(&alt_ctx, '\0', sizeof(alt_ctx));
 

	
 
	return buffer;
 
}
backends/libyahoo2/yahoo/libyahoo2.c
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2: libyahoo2.c
 
 *
 
 * Some code copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 * YMSG16 code copyright (C) 2009, 
 
 * 		Siddhesh Poyarekar <siddhesh dot poyarekar at gmail dot com>
 
 *
 
 * Yahoo Search copyright (C) 2003, Konstantin Klyagin <konst AT konst.org.ua>
 
 *
 
 * Much of this code was taken and adapted from the yahoo module for
 
 * gaim released under the GNU GPL.  This code is also released under the 
 
 * GNU GPL.
 
 *
 
 * This code is derivitive of Gaim <http://gaim.sourceforge.net>
 
 * copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
 
 *	       1998-1999, Adam Fritzler <afritz@marko.net>
 
 *	       1998-2002, Rob Flynn <rob@marko.net>
 
 *	       2000-2002, Eric Warmenhoven <eric@warmenhoven.org>
 
 *	       2001-2002, Brian Macke <macke@strangelove.net>
 
 *		    2001, Anand Biligiri S <abiligiri@users.sf.net>
 
 *		    2001, Valdis Kletnieks
 
 *		    2002, Sean Egan <bj91704@binghamton.edu>
 
 *		    2002, Toby Gray <toby.gray@ntlworld.com>
 
 *
 
 * This library also uses code from other libraries, namely:
 
 *     Portions from libfaim copyright 1998, 1999 Adam Fritzler
 
 *     <afritz@auk.cx>
 
 *     Portions of Sylpheed copyright 2000-2002 Hiroyuki Yamamoto
 
 *     <hiro-y@kcn.ne.jp>
 
 *
 
 * YMSG16 authentication code based mostly on write-up at:
 
 * 	http://www.carbonize.co.uk/ymsg16.html
 
 *
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
#if HAVE_CONFIG_H
 
# include <config.h>
 
#endif
 

	
 
#if HAVE_UNISTD_H
 
#include <unistd.h>
 
#endif
 
#include <errno.h>
 
#include <stdio.h>
 
#include <stdarg.h>
 

	
 
#if STDC_HEADERS
 
# include <string.h>
 
#else
 
# if !HAVE_STRCHR
 
#  define strchr index
 
#  define strrchr rindex
 
# endif
 
char *strchr(), *strrchr();
 
# if !HAVE_MEMCPY
 
#  define memcpy(d, s, n) bcopy ((s), (d), (n))
 
#  define memmove(d, s, n) bcopy ((s), (d), (n))
 
# endif
 
#endif
 

	
 
#include <sys/types.h>
 

	
 
#include <stdlib.h>
 
#include <ctype.h>
 

	
 
#include "sha1.h"
 
#include "md5.h"
 
#include "yahoo2.h"
 
#include "yahoo_httplib.h"
 
#include "yahoo_util.h"
 
#include "yahoo_fn.h"
 

	
 
#include "yahoo2_callbacks.h"
 
#include "yahoo_debug.h"
 
#ifdef __MINGW32__
 
#define snprintf _snprintf
 
#define vsnprintf _vsnprintf
 
#endif
 

	
 
struct yahoo_callbacks *yc = NULL;
 

	
 
void yahoo_register_callbacks(struct yahoo_callbacks *tyc)
 
{
 
	yc = tyc;
 
}
 

	
 
#define YAHOO_CALLBACK(x)	yc->x
 

	
 
static int yahoo_send_data(void *fd, void *data, int len);
 
static void _yahoo_http_connected(int id, void *fd, int error, void *data);
 
static void yahoo_connected(void *fd, int error, void *data);
 

	
 
int yahoo_log_message(char *fmt, ...)
 
{
 
	char out[1024];
 
	va_list ap;
 
	va_start(ap, fmt);
 
	vsnprintf(out, sizeof(out), fmt, ap);
 
	va_end(ap);
 
	return YAHOO_CALLBACK(ext_yahoo_log) ("%s", out);
 
}
 

	
 
int yahoo_connect(char *host, int port)
 
{
 
	return YAHOO_CALLBACK(ext_yahoo_connect) (host, port);
 
}
 

	
 
static enum yahoo_log_level log_level = YAHOO_LOG_NONE;
 

	
 
enum yahoo_log_level yahoo_get_log_level()
 
{
 
	return log_level;
 
}
 

	
 
int yahoo_set_log_level(enum yahoo_log_level level)
 
{
 
	enum yahoo_log_level l = log_level;
 
	log_level = level;
 
	return l;
 
}
 

	
 
/* default values for servers */
 
static char *default_pager_hosts[] = {	"scs.msg.yahoo.com",
 
					"scsa.msg.yahoo.com",
 
					"scsb.msg.yahoo.com",
 
					"scsc.msg.yahoo.com",
 
					NULL};
 

	
 
static int pager_port = 5050;
 
static int fallback_ports[] = { 23, 25, 80, 20, 119, 8001, 8002, 5050, 0 };
 

	
 
static char filetransfer_host[] = "filetransfer.msg.yahoo.com";
 
static int filetransfer_port = 80;
 
static char webcam_host[] = "webcam.yahoo.com";
 
static int webcam_port = 5100;
 
static char webcam_description[] = "";
 
static char local_host[] = "";
 
static int conn_type = Y_WCM_DSL;
 

	
 
static char profile_url[] = "http://profiles.yahoo.com/";
 

	
 
struct connect_callback_data {
 
	struct yahoo_data *yd;
 
	int tag;
 
	int i;
 
	int server_i;
 
};
 

	
 
struct yahoo_pair {
 
	int key;
 
	char *value;
 
};
 

	
 
struct yahoo_packet {
 
	unsigned short int service;
 
	unsigned int status;
 
	unsigned int id;
 
	YList *hash;
 
};
 

	
 
struct yahoo_search_state {
 
	int lsearch_type;
 
	char *lsearch_text;
 
	int lsearch_gender;
 
	int lsearch_agerange;
 
	int lsearch_photo;
 
	int lsearch_yahoo_only;
 
	int lsearch_nstart;
 
	int lsearch_nfound;
 
	int lsearch_ntotal;
 
};
 

	
 
struct data_queue {
 
	unsigned char *queue;
 
	int len;
 
};
 

	
 
struct yahoo_input_data {
 
	struct yahoo_data *yd;
 
	struct yahoo_webcam *wcm;
 
	struct yahoo_webcam_data *wcd;
 
	struct yahoo_search_state *ys;
 

	
 
	void *fd;
 
	enum yahoo_connection_type type;
 

	
 
	unsigned char *rxqueue;
 
	int rxlen;
 
	int read_tag;
 

	
 
	YList *txqueues;
 
	int write_tag;
 
};
 

	
 
struct yahoo_server_settings {
 
	char *pager_host;
 
	int pager_port;
 
	char *filetransfer_host;
 
	int filetransfer_port;
 
	char *webcam_host;
 
	int webcam_port;
 
	char *webcam_description;
 
	char *local_host;
 
	int conn_type;
 
	char **pager_host_list;
 
};
 

	
 
static void yahoo_process_ft_connection(struct yahoo_input_data *yid, int over);
 

	
 
static void yahoo_process_filetransfer(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt);
 
static void yahoo_process_filetransferinfo(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt);
 
static void yahoo_process_filetransferaccept(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt);
 

	
 
static void *_yahoo_default_server_settings()
 
{
 
	struct yahoo_server_settings *yss =
 
		y_new0(struct yahoo_server_settings, 1);
 

	
 
	/* Give preference to the default host list
 
	 * Make sure that only one of the two is set at any time 
 
	 */
 
	yss->pager_host = NULL;
 
	yss->pager_host_list = default_pager_hosts;
 

	
 
	yss->pager_port = pager_port;
 
	yss->filetransfer_host = strdup(filetransfer_host);
 
	yss->filetransfer_port = filetransfer_port;
 
	yss->webcam_host = strdup(webcam_host);
 
	yss->webcam_port = webcam_port;
 
	yss->webcam_description = strdup(webcam_description);
 
	yss->local_host = strdup(local_host);
 
	yss->conn_type = conn_type;
 

	
 
	return yss;
 
}
 

	
 
static void *_yahoo_assign_server_settings(va_list ap)
 
{
 
	struct yahoo_server_settings *yss = _yahoo_default_server_settings();
 
	char *key;
 
	char *svalue;
 
	int nvalue;
 
	char **pvalue;
 

	
 
	while (1) {
 
		key = va_arg(ap, char *);
 
		if (key == NULL)
 
			break;
 

	
 
		if (!strcmp(key, "pager_host")) {
 
			svalue = va_arg(ap, char *);
 
			free(yss->pager_host);
 
			yss->pager_host = strdup(svalue);
 
			yss->pager_host_list = NULL;
 
		} else if (!strcmp(key, "pager_host_list")) {
 
			pvalue = va_arg(ap, char **);
 
			yss->pager_host_list = pvalue;
 
			free(yss->pager_host);
 
			yss->pager_host = NULL;
 
		} else if (!strcmp(key, "pager_port")) {
 
			nvalue = va_arg(ap, int);
 
			yss->pager_port = nvalue;
 
		} else if (!strcmp(key, "filetransfer_host")) {
 
			svalue = va_arg(ap, char *);
 
			free(yss->filetransfer_host);
 
			yss->filetransfer_host = strdup(svalue);
 
		} else if (!strcmp(key, "filetransfer_port")) {
 
			nvalue = va_arg(ap, int);
 
			yss->filetransfer_port = nvalue;
 
		} else if (!strcmp(key, "webcam_host")) {
 
			svalue = va_arg(ap, char *);
 
			free(yss->webcam_host);
 
			yss->webcam_host = strdup(svalue);
 
		} else if (!strcmp(key, "webcam_port")) {
 
			nvalue = va_arg(ap, int);
 
			yss->webcam_port = nvalue;
 
		} else if (!strcmp(key, "webcam_description")) {
 
			svalue = va_arg(ap, char *);
 
			free(yss->webcam_description);
 
			yss->webcam_description = strdup(svalue);
 
		} else if (!strcmp(key, "local_host")) {
 
			svalue = va_arg(ap, char *);
 
			free(yss->local_host);
 
			yss->local_host = strdup(svalue);
 
		} else if (!strcmp(key, "conn_type")) {
 
			nvalue = va_arg(ap, int);
 
			yss->conn_type = nvalue;
 
		} else {
 
			WARNING(("Unknown key passed to yahoo_init, "
 
					"perhaps you didn't terminate the list "
 
					"with NULL"));
 
		}
 
	}
 

	
 
	return yss;
 
}
 

	
 
static void yahoo_free_server_settings(struct yahoo_server_settings *yss)
 
{
 
	if (!yss)
 
		return;
 

	
 
	free(yss->pager_host);
 
	free(yss->filetransfer_host);
 
	free(yss->webcam_host);
 
	free(yss->webcam_description);
 
	free(yss->local_host);
 

	
 
	free(yss);
 
}
 

	
 
static YList *conns = NULL;
 
static YList *inputs = NULL;
 
static int last_id = 0;
 

	
 
static void add_to_list(struct yahoo_data *yd)
 
{
 
	conns = y_list_prepend(conns, yd);
 
}
 

	
 
static struct yahoo_data *find_conn_by_id(int id)
 
{
 
	YList *l;
 
	for (l = conns; l; l = y_list_next(l)) {
 
		struct yahoo_data *yd = l->data;
 
		if (yd->client_id == id)
 
			return yd;
 
	}
 
	return NULL;
 
}
 

	
 
static void del_from_list(struct yahoo_data *yd)
 
{
 
	conns = y_list_remove(conns, yd);
 
}
 

	
 
/* call repeatedly to get the next one */
 
/*
 
static struct yahoo_input_data * find_input_by_id(int id)
 
{
 
	YList *l;
 
	for(l = inputs; l; l = y_list_next(l)) {
 
		struct yahoo_input_data *yid = l->data;
 
		if(yid->yd->client_id == id)
 
			return yid;
 
	}
 
	return NULL;
 
}
 
*/
 

	
 
static struct yahoo_input_data *find_input_by_id_and_webcam_user(int id,
 
	const char *who)
 
{
 
	YList *l;
 
	LOG(("find_input_by_id_and_webcam_user"));
 
	for (l = inputs; l; l = y_list_next(l)) {
 
		struct yahoo_input_data *yid = l->data;
 
		if (yid->type == YAHOO_CONNECTION_WEBCAM
 
			&& yid->yd->client_id == id && yid->wcm && ((who
 
					&& yid->wcm->user
 
					&& !strcmp(who, yid->wcm->user))
 
				|| !(yid->wcm->user && !who)))
 
			return yid;
 
	}
 
	return NULL;
 
}
 

	
 
static struct yahoo_input_data *find_input_by_id_and_type(int id,
 
	enum yahoo_connection_type type)
 
{
 
	YList *l;
 
	LOG(("find_input_by_id_and_type"));
 
	for (l = inputs; l; l = y_list_next(l)) {
 
		struct yahoo_input_data *yid = l->data;
 
		if (yid->type == type && yid->yd->client_id == id)
 
			return yid;
 
	}
 
	return NULL;
 
}
 

	
 
static struct yahoo_input_data *find_input_by_id_and_fd(int id, void *fd)
 
{
 
	YList *l;
 
	LOG(("find_input_by_id_and_fd"));
 
	for (l = inputs; l; l = y_list_next(l)) {
 
		struct yahoo_input_data *yid = l->data;
 
		if (yid->fd == fd && yid->yd->client_id == id)
 
			return yid;
 
	}
 
	return NULL;
 
}
 

	
 
static int count_inputs_with_id(int id)
 
{
 
	int c = 0;
 
	YList *l;
 
	LOG(("counting %d", id));
 
	for (l = inputs; l; l = y_list_next(l)) {
 
		struct yahoo_input_data *yid = l->data;
 
		if (yid->yd->client_id == id)
 
			c++;
 
	}
 
	LOG(("%d", c));
 
	return c;
 
}
 

	
 
extern char *yahoo_crypt(char *, char *);
 

	
 
/* Free a buddy list */
 
static void yahoo_free_buddies(YList *list)
 
{
 
	YList *l;
 

	
 
	for (l = list; l; l = l->next) {
 
		struct yahoo_buddy *bud = l->data;
 
		if (!bud)
 
			continue;
 

	
 
		FREE(bud->group);
 
		FREE(bud->id);
 
		FREE(bud->real_name);
 
		if (bud->yab_entry) {
 
			FREE(bud->yab_entry->fname);
 
			FREE(bud->yab_entry->lname);
 
			FREE(bud->yab_entry->nname);
 
			FREE(bud->yab_entry->id);
 
			FREE(bud->yab_entry->email);
 
			FREE(bud->yab_entry->hphone);
 
			FREE(bud->yab_entry->wphone);
 
			FREE(bud->yab_entry->mphone);
 
			FREE(bud->yab_entry);
 
		}
 
		FREE(bud);
 
		l->data = bud = NULL;
 
	}
 

	
 
	y_list_free(list);
 
}
 

	
 
/* Free an identities list */
 
static void yahoo_free_identities(YList *list)
 
{
 
	while (list) {
 
		YList *n = list;
 
		FREE(list->data);
 
		list = y_list_remove_link(list, list);
 
		y_list_free_1(n);
 
	}
 
}
 

	
 
/* Free webcam data */
 
static void yahoo_free_webcam(struct yahoo_webcam *wcm)
 
{
 
	if (wcm) {
 
		FREE(wcm->user);
 
		FREE(wcm->server);
 
		FREE(wcm->key);
 
		FREE(wcm->description);
 
		FREE(wcm->my_ip);
 
	}
 
	FREE(wcm);
 
}
 

	
 
static void yahoo_free_data(struct yahoo_data *yd)
 
{
 
	FREE(yd->user);
 
	FREE(yd->password);
 
	FREE(yd->cookie_y);
 
	FREE(yd->cookie_t);
 
	FREE(yd->cookie_b);
 
	FREE(yd->cookie_c);
 
	FREE(yd->login_cookie);
 
	FREE(yd->login_id);
 

	
 
	yahoo_free_buddies(yd->buddies);
 
	yahoo_free_buddies(yd->ignore);
 
	yahoo_free_identities(yd->identities);
 

	
 
	yahoo_free_server_settings(yd->server_settings);
 

	
 
	FREE(yd);
 
}
 

	
 
#define YAHOO_PACKET_HDRLEN (4 + 2 + 2 + 2 + 2 + 4 + 4)
 

	
 
static struct yahoo_packet *yahoo_packet_new(enum yahoo_service service,
 
	enum ypacket_status status, int id)
 
{
 
	struct yahoo_packet *pkt = y_new0(struct yahoo_packet, 1);
 

	
 
	pkt->service = service;
 
	pkt->status = status;
 
	pkt->id = id;
 

	
 
	return pkt;
 
}
 

	
 
static void yahoo_packet_hash(struct yahoo_packet *pkt, int key,
 
	const char *value)
 
{
 
	struct yahoo_pair *pair = y_new0(struct yahoo_pair, 1);
 
	pair->key = key;
 
	pair->value = strdup(value);
 
	pkt->hash = y_list_append(pkt->hash, pair);
 
}
 

	
 
static int yahoo_packet_length(struct yahoo_packet *pkt)
 
{
 
	YList *l;
 

	
 
	int len = 0;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		int tmp = pair->key;
 
		do {
 
			tmp /= 10;
 
			len++;
 
		} while (tmp);
 
		len += 2;
 
		len += strlen(pair->value);
 
		len += 2;
 
	}
 

	
 
	return len;
 
}
 

	
 
#define yahoo_put16(buf, data) ( \
 
		(*(buf) = (unsigned char)((data)>>8)&0xff), \
 
		(*((buf)+1) = (unsigned char)(data)&0xff),  \
 
		2)
 
#define yahoo_get16(buf) ((((*(buf))&0xff)<<8) + ((*((buf)+1)) & 0xff))
 
#define yahoo_put32(buf, data) ( \
 
		(*((buf)) = (unsigned char)((data)>>24)&0xff), \
 
		(*((buf)+1) = (unsigned char)((data)>>16)&0xff), \
 
		(*((buf)+2) = (unsigned char)((data)>>8)&0xff), \
 
		(*((buf)+3) = (unsigned char)(data)&0xff), \
 
		4)
 
#define yahoo_get32(buf) ((((*(buf)   )&0xff)<<24) + \
 
			 (((*((buf)+1))&0xff)<<16) + \
 
			 (((*((buf)+2))&0xff)<< 8) + \
 
			 (((*((buf)+3))&0xff)))
 

	
 
static void yahoo_packet_read(struct yahoo_packet *pkt, unsigned char *data,
 
	int len)
 
{
 
	int pos = 0;
 

	
 
	while (pos + 1 < len) {
 
		char *key, *value = NULL;
 
		int accept;
 
		int x;
 

	
 
		struct yahoo_pair *pair = y_new0(struct yahoo_pair, 1);
 

	
 
		key = malloc(len + 1);
 
		x = 0;
 
		while (pos + 1 < len) {
 
			if (data[pos] == 0xc0 && data[pos + 1] == 0x80)
 
				break;
 
			key[x++] = data[pos++];
 
		}
 
		key[x] = 0;
 
		pos += 2;
 
		pair->key = strtol(key, NULL, 10);
 
		free(key);
 

	
 
		accept = x;
 

	
 
		if (pos + 1 > len) {
 
			/* Malformed packet! (Truncated--garbage or something) */
 
			accept = 0;
 
		}
 

	
 
		/* if x is 0 there was no key, so don't accept it */
 
		if (accept)
 
			value = malloc(len - pos + 1);
 
		x = 0;
 
		while (pos + 1 < len) {
 
			if (data[pos] == 0xc0 && data[pos + 1] == 0x80)
 
				break;
 
			if (accept)
 
				value[x++] = data[pos++];
 
		}
 
		if (accept)
 
			value[x] = 0;
 
		pos += 2;
 
		if (accept) {
 
			pair->value = strdup(value);
 
			FREE(value);
 
			pkt->hash = y_list_append(pkt->hash, pair);
 
			DEBUG_MSG(("Key: %d  \tValue: %s", pair->key,
 
					pair->value));
 
		} else {
 
			FREE(pair);
 
		}
 
	}
 
}
 

	
 
static void yahoo_packet_write(struct yahoo_packet *pkt, unsigned char *data)
 
{
 
	YList *l;
 
	int pos = 0;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		unsigned char buf[100];
 

	
 
		snprintf((char *)buf, sizeof(buf), "%d", pair->key);
 
		strcpy((char *)data + pos, (char *)buf);
 
		pos += strlen((char *)buf);
 
		data[pos++] = 0xc0;
 
		data[pos++] = 0x80;
 

	
 
		strcpy((char *)data + pos, pair->value);
 
		pos += strlen(pair->value);
 
		data[pos++] = 0xc0;
 
		data[pos++] = 0x80;
 
	}
 
}
 

	
 
static void yahoo_dump_unhandled(struct yahoo_packet *pkt)
 
{
 
	YList *l;
 

	
 
	NOTICE(("Service: 0x%02x\tStatus: %d", pkt->service, pkt->status));
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		NOTICE(("\t%d => %s", pair->key, pair->value));
 
	}
 
}
 

	
 
static void yahoo_packet_dump(unsigned char *data, int len)
 
{
 
	if (yahoo_get_log_level() >= YAHOO_LOG_DEBUG) {
 
		int i;
 
		for (i = 0; i < len; i++) {
 
			if ((i % 8 == 0) && i)
 
				YAHOO_CALLBACK(ext_yahoo_log) (" ");
 
			if ((i % 16 == 0) && i)
 
				YAHOO_CALLBACK(ext_yahoo_log) ("\n");
 
			YAHOO_CALLBACK(ext_yahoo_log) ("%02x ", data[i]);
 
		}
 
		YAHOO_CALLBACK(ext_yahoo_log) ("\n");
 
		for (i = 0; i < len; i++) {
 
			if ((i % 8 == 0) && i)
 
				YAHOO_CALLBACK(ext_yahoo_log) (" ");
 
			if ((i % 16 == 0) && i)
 
				YAHOO_CALLBACK(ext_yahoo_log) ("\n");
 
			if (isprint(data[i]))
 
				YAHOO_CALLBACK(ext_yahoo_log) (" %c ", data[i]);
 
			else
 
				YAHOO_CALLBACK(ext_yahoo_log) (" . ");
 
		}
 
		YAHOO_CALLBACK(ext_yahoo_log) ("\n");
 
	}
 
}
 

	
 
static const char base64digits[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
	"abcdefghijklmnopqrstuvwxyz" "0123456789._";
 
static void to_y64(unsigned char *out, const unsigned char *in, int inlen)
 
/* raw bytes in quasi-big-endian order to base 64 string (NUL-terminated) */
 
{
 
	for (; inlen >= 3; inlen -= 3) {
 
		*out++ = base64digits[in[0] >> 2];
 
		*out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)];
 
		*out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
 
		*out++ = base64digits[in[2] & 0x3f];
 
		in += 3;
 
	}
 
	if (inlen > 0) {
 
		unsigned char fragment;
 

	
 
		*out++ = base64digits[in[0] >> 2];
 
		fragment = (in[0] << 4) & 0x30;
 
		if (inlen > 1)
 
			fragment |= in[1] >> 4;
 
		*out++ = base64digits[fragment];
 
		*out++ = (inlen < 2) ? '-' : base64digits[(in[1] << 2) & 0x3c];
 
		*out++ = '-';
 
	}
 
	*out = '\0';
 
}
 

	
 
static void yahoo_add_to_send_queue(struct yahoo_input_data *yid, void *data,
 
	int length)
 
{
 
	struct data_queue *tx = y_new0(struct data_queue, 1);
 
	tx->queue = y_new0(unsigned char, length);
 
	tx->len = length;
 
	memcpy(tx->queue, data, length);
 

	
 
	yid->txqueues = y_list_append(yid->txqueues, tx);
 

	
 
	if (!yid->write_tag)
 
		yid->write_tag =
 
			YAHOO_CALLBACK(ext_yahoo_add_handler) (yid->yd->
 
			client_id, yid->fd, YAHOO_INPUT_WRITE, yid);
 
}
 

	
 
static void yahoo_send_packet(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt, int extra_pad)
 
{
 
	int pktlen = yahoo_packet_length(pkt);
 
	int len = YAHOO_PACKET_HDRLEN + pktlen;
 
	unsigned char *data;
 
	int pos = 0;
 

	
 
	if (!yid->fd)
 
		return;
 

	
 
	data = y_new0(unsigned char, len + 1);
 

	
 
	memcpy(data + pos, "YMSG", 4);
 
	pos += 4;
 
	pos += yahoo_put16(data + pos, YAHOO_PROTO_VER);	/* version [latest 12 0x000c] */
 
	pos += yahoo_put16(data + pos, 0x0000);	/* HIWORD pkt length??? */
 
	pos += yahoo_put16(data + pos, pktlen + extra_pad);	/* LOWORD pkt length? */
 
	pos += yahoo_put16(data + pos, pkt->service);	/* service */
 
	pos += yahoo_put32(data + pos, pkt->status);	/* status [4bytes] */
 
	pos += yahoo_put32(data + pos, pkt->id);	/* session [4bytes] */
 

	
 
	yahoo_packet_write(pkt, data + pos);
 

	
 
	yahoo_packet_dump(data, len);
 

	
 
	if (yid->type == YAHOO_CONNECTION_FT)
 
		yahoo_send_data(yid->fd, data, len);
 
	else
 
		yahoo_add_to_send_queue(yid, data, len);
 
	FREE(data);
 
}
 

	
 
static void yahoo_packet_free(struct yahoo_packet *pkt)
 
{
 
	while (pkt->hash) {
 
		struct yahoo_pair *pair = pkt->hash->data;
 
		YList *tmp;
 
		FREE(pair->value);
 
		FREE(pair);
 
		tmp = pkt->hash;
 
		pkt->hash = y_list_remove_link(pkt->hash, pkt->hash);
 
		y_list_free_1(tmp);
 
	}
 
	FREE(pkt);
 
}
 

	
 
static int yahoo_send_data(void *fd, void *data, int len)
 
{
 
	int ret;
 
	int e;
 

	
 
	if (fd == NULL)
 
		return -1;
 

	
 
	yahoo_packet_dump(data, len);
 

	
 
	do {
 
		ret = YAHOO_CALLBACK(ext_yahoo_write) (fd, data, len);
 
	} while (ret == -1 && errno == EINTR);
 
	e = errno;
 

	
 
	if (ret == -1) {
 
		LOG(("wrote data: ERR %s", strerror(errno)));
 
	} else {
 
		LOG(("wrote data: OK"));
 
	}
 

	
 
	errno = e;
 
	return ret;
 
}
 

	
 
void yahoo_close(int id)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	if (!yd)
 
		return;
 

	
 
	del_from_list(yd);
 

	
 
	yahoo_free_data(yd);
 
	if (id == last_id)
 
		last_id--;
 
}
 

	
 
static void yahoo_input_close(struct yahoo_input_data *yid)
 
{
 
	inputs = y_list_remove(inputs, yid);
 

	
 
	LOG(("yahoo_input_close(read)"));
 
	YAHOO_CALLBACK(ext_yahoo_remove_handler) (yid->yd->client_id,
 
		yid->read_tag);
 
	LOG(("yahoo_input_close(write)"));
 
	YAHOO_CALLBACK(ext_yahoo_remove_handler) (yid->yd->client_id,
 
		yid->write_tag);
 
	yid->read_tag = yid->write_tag = 0;
 
	if (yid->fd)
 
		YAHOO_CALLBACK(ext_yahoo_close) (yid->fd);
 
	yid->fd = 0;
 
	FREE(yid->rxqueue);
 
	if (count_inputs_with_id(yid->yd->client_id) == 0) {
 
		LOG(("closing %d", yid->yd->client_id));
 
		yahoo_close(yid->yd->client_id);
 
	}
 
	yahoo_free_webcam(yid->wcm);
 
	if (yid->wcd)
 
		FREE(yid->wcd);
 
	if (yid->ys) {
 
		FREE(yid->ys->lsearch_text);
 
		FREE(yid->ys);
 
	}
 
	FREE(yid);
 
}
 

	
 
static int is_same_bud(const void *a, const void *b)
 
{
 
	const struct yahoo_buddy *subject = a;
 
	const struct yahoo_buddy *object = b;
 

	
 
	return strcmp(subject->id, object->id);
 
}
 

	
 
static char *getcookie(char *rawcookie)
 
{
 
	char *cookie = NULL;
 
	char *tmpcookie;
 
	char *cookieend;
 

	
 
	if (strlen(rawcookie) < 2)
 
		return NULL;
 

	
 
	tmpcookie = strdup(rawcookie + 2);
 
	cookieend = strchr(tmpcookie, ';');
 

	
 
	if (cookieend)
 
		*cookieend = '\0';
 

	
 
	cookie = strdup(tmpcookie);
 
	FREE(tmpcookie);
 
	/* cookieend=NULL;  not sure why this was there since the value is not preserved in the stack -dd */
 

	
 
	return cookie;
 
}
 

	
 
static char *getlcookie(char *cookie)
 
{
 
	char *tmp;
 
	char *tmpend;
 
	char *login_cookie = NULL;
 

	
 
	tmpend = strstr(cookie, "n=");
 
	if (tmpend) {
 
		tmp = strdup(tmpend + 2);
 
		tmpend = strchr(tmp, '&');
 
		if (tmpend)
 
			*tmpend = '\0';
 
		login_cookie = strdup(tmp);
 
		FREE(tmp);
 
	}
 

	
 
	return login_cookie;
 
}
 

	
 
static void yahoo_process_notify(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *msg = NULL;
 
	char *from = NULL;
 
	char *to = NULL;
 
	int stat = 0;
 
	int accept = 0;
 
	char *ind = NULL;
 
	YList *l;
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 4)
 
			from = pair->value;
 
		if (pair->key == 5)
 
			to = pair->value;
 
		if (pair->key == 49)
 
			msg = pair->value;
 
		if (pair->key == 13)
 
			stat = atoi(pair->value);
 
		if (pair->key == 14)
 
			ind = pair->value;
 
		if (pair->key == 16) {	/* status == -1 */
 
			NOTICE((pair->value));
 
			return;
 
		}
 

	
 
	}
 

	
 
	if (!msg)
 
		return;
 

	
 
	if (!strncasecmp(msg, "TYPING", strlen("TYPING")))
 
		YAHOO_CALLBACK(ext_yahoo_typing_notify) (yd->client_id, to,
 
			from, stat);
 
	else if (!strncasecmp(msg, "GAME", strlen("GAME")))
 
		YAHOO_CALLBACK(ext_yahoo_game_notify) (yd->client_id, to, from,
 
			stat, ind);
 
	else if (!strncasecmp(msg, "WEBCAMINVITE", strlen("WEBCAMINVITE"))) {
 
		if (!strcmp(ind, " ")) {
 
			YAHOO_CALLBACK(ext_yahoo_webcam_invite) (yd->client_id,
 
				to, from);
 
		} else {
 
			accept = atoi(ind);
 
			/* accept the invitation (-1 = deny 1 = accept) */
 
			if (accept < 0)
 
				accept = 0;
 
			YAHOO_CALLBACK(ext_yahoo_webcam_invite_reply) (yd->
 
				client_id, to, from, accept);
 
		}
 
	} else
 
		LOG(("Got unknown notification: %s", msg));
 
}
 

	
 
static void yahoo_process_conference(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *msg = NULL;
 
	char *host = NULL;
 
	char *who = NULL;
 
	char *room = NULL;
 
	char *id = NULL;
 
	int utf8 = 0;
 
	YList *members = NULL;
 
	YList *l;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 50)
 
			host = pair->value;
 

	
 
		if (pair->key == 52) {	/* invite */
 
			members = y_list_append(members, strdup(pair->value));
 
		}
 
		if (pair->key == 53)	/* logon */
 
			who = pair->value;
 
		if (pair->key == 54)	/* decline */
 
			who = pair->value;
 
		if (pair->key == 55)	/* unavailable (status == 2) */
 
			who = pair->value;
 
		if (pair->key == 56)	/* logoff */
 
			who = pair->value;
 

	
 
		if (pair->key == 57)
 
			room = pair->value;
 

	
 
		if (pair->key == 58)	/* join message */
 
			msg = pair->value;
 
		if (pair->key == 14)	/* decline/conf message */
 
			msg = pair->value;
 

	
 
		if (pair->key == 13) ;
 
		if (pair->key == 16)	/* error */
 
			msg = pair->value;
 

	
 
		if (pair->key == 1)	/* my id */
 
			id = pair->value;
 
		if (pair->key == 3)	/* message sender */
 
			who = pair->value;
 

	
 
		if (pair->key == 97)
 
			utf8 = atoi(pair->value);
 
	}
 

	
 
	if (!room)
 
		return;
 

	
 
	if (host) {
 
		for (l = members; l; l = l->next) {
 
			char *w = l->data;
 
			if (!strcmp(w, host))
 
				break;
 
		}
 
		if (!l)
 
			members = y_list_append(members, strdup(host));
 
	}
 
	/* invite, decline, join, left, message -> status == 1 */
 

	
 
	switch (pkt->service) {
 
	case YAHOO_SERVICE_CONFINVITE:
 
		if (pkt->status == 2) ;
 
		else if (members)
 
			YAHOO_CALLBACK(ext_yahoo_got_conf_invite) (yd->
 
				client_id, id, host, room, msg, members);
 
		else if (msg)
 
			YAHOO_CALLBACK(ext_yahoo_error) (yd->client_id, msg, 0,
 
				E_CONFNOTAVAIL);
 
		break;
 
	case YAHOO_SERVICE_CONFADDINVITE:
 
		if (pkt->status == 1)
 
			YAHOO_CALLBACK(ext_yahoo_got_conf_invite) (yd->
 
				client_id, id, host, room, msg, members);
 
		break;
 
	case YAHOO_SERVICE_CONFDECLINE:
 
		if (who)
 
			YAHOO_CALLBACK(ext_yahoo_conf_userdecline) (yd->
 
				client_id, id, who, room, msg);
 
		break;
 
	case YAHOO_SERVICE_CONFLOGON:
 
		if (who)
 
			YAHOO_CALLBACK(ext_yahoo_conf_userjoin) (yd->client_id,
 
				id, who, room);
 
		break;
 
	case YAHOO_SERVICE_CONFLOGOFF:
 
		if (who)
 
			YAHOO_CALLBACK(ext_yahoo_conf_userleave) (yd->client_id,
 
				id, who, room);
 
		break;
 
	case YAHOO_SERVICE_CONFMSG:
 
		if (who)
 
			YAHOO_CALLBACK(ext_yahoo_conf_message) (yd->client_id,
 
				id, who, room, msg, utf8);
 
		break;
 
	}
 
}
 

	
 
static void yahoo_process_chat(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	char *msg = NULL;
 
	char *id = NULL;
 
	char *who = NULL;
 
	char *room = NULL;
 
	char *topic = NULL;
 
	YList *members = NULL;
 
	struct yahoo_chat_member *currentmember = NULL;
 
	int msgtype = 1;
 
	int utf8 = 0;
 
	int firstjoin = 0;
 
	int membercount = 0;
 
	int chaterr = 0;
 
	YList *l;
 

	
 
	yahoo_dump_unhandled(pkt);
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		if (pair->key == 1) {
 
			/* My identity */
 
			id = pair->value;
 
		}
 

	
 
		if (pair->key == 104) {
 
			/* Room name */
 
			room = pair->value;
 
		}
 

	
 
		if (pair->key == 105) {
 
			/* Room topic */
 
			topic = pair->value;
 
		}
 

	
 
		if (pair->key == 108) {
 
			/* Number of members in this packet */
 
			membercount = atoi(pair->value);
 
		}
 

	
 
		if (pair->key == 109) {
 
			/* message sender */
 
			who = pair->value;
 

	
 
			if (pkt->service == YAHOO_SERVICE_CHATJOIN) {
 
				currentmember =
 
					y_new0(struct yahoo_chat_member, 1);
 
				currentmember->id = strdup(pair->value);
 
				members = y_list_append(members, currentmember);
 
			}
 
		}
 

	
 
		if (pair->key == 110) {
 
			/* age */
 
			if (pkt->service == YAHOO_SERVICE_CHATJOIN)
 
				currentmember->age = atoi(pair->value);
 
		}
 

	
 
		if (pair->key == 113) {
 
			/* attribs */
 
			if (pkt->service == YAHOO_SERVICE_CHATJOIN)
 
				currentmember->attribs = atoi(pair->value);
 
		}
 

	
 
		if (pair->key == 141) {
 
			/* alias */
 
			if (pkt->service == YAHOO_SERVICE_CHATJOIN)
 
				currentmember->alias = strdup(pair->value);
 
		}
 

	
 
		if (pair->key == 142) {
 
			/* location */
 
			if (pkt->service == YAHOO_SERVICE_CHATJOIN)
 
				currentmember->location = strdup(pair->value);
 
		}
 

	
 
		if (pair->key == 130) {
 
			/* first join */
 
			firstjoin = 1;
 
		}
 

	
 
		if (pair->key == 117) {
 
			/* message */
 
			msg = pair->value;
 
		}
 

	
 
		if (pair->key == 124) {
 
			/* Message type */
 
			msgtype = atoi(pair->value);
 
		}
 
		if (pair->key == 114) {
 
			/* message error not sure what all the pair values mean */
 
			/* but -1 means no session in room */
 
			chaterr = atoi(pair->value);
 
		}
 
	}
 

	
 
	if (!room) {
 
		if (pkt->service == YAHOO_SERVICE_CHATLOGOUT) {	/* yahoo originated chat logout */
 
			YAHOO_CALLBACK(ext_yahoo_chat_yahoologout) (yid->yd->
 
				client_id, id);
 
			return;
 
		}
 
		if (pkt->service == YAHOO_SERVICE_COMMENT && chaterr) {
 
			YAHOO_CALLBACK(ext_yahoo_chat_yahooerror) (yid->yd->
 
				client_id, id);
 
			return;
 
		}
 

	
 
		WARNING(("We didn't get a room name, ignoring packet"));
 
		return;
 
	}
 

	
 
	switch (pkt->service) {
 
	case YAHOO_SERVICE_CHATJOIN:
 
		if (y_list_length(members) != membercount) {
 
			WARNING(("Count of members doesn't match No. of members we got"));
 
		}
 
		if (firstjoin && members) {
 
			YAHOO_CALLBACK(ext_yahoo_chat_join) (yid->yd->client_id,
 
				id, room, topic, members, yid->fd);
 
		} else if (who) {
 
			if (y_list_length(members) != 1) {
 
				WARNING(("Got more than 1 member on a normal join"));
 
			}
 
			/* this should only ever have one, but just in case */
 
			while (members) {
 
				YList *n = members->next;
 
				currentmember = members->data;
 
				YAHOO_CALLBACK(ext_yahoo_chat_userjoin) (yid->
 
					yd->client_id, id, room, currentmember);
 
				y_list_free_1(members);
 
				members = n;
 
			}
 
		}
 
		break;
 
	case YAHOO_SERVICE_CHATEXIT:
 
		if (who) {
 
			YAHOO_CALLBACK(ext_yahoo_chat_userleave) (yid->yd->
 
				client_id, id, room, who);
 
		}
 
		break;
 
	case YAHOO_SERVICE_COMMENT:
 
		if (who) {
 
			YAHOO_CALLBACK(ext_yahoo_chat_message) (yid->yd->
 
				client_id, id, who, room, msg, msgtype, utf8);
 
		}
 
		break;
 
	}
 
}
 

	
 
static void yahoo_process_message(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	YList *l;
 
	YList *messages = NULL;
 

	
 
	struct m {
 
		int i_31;
 
		int i_32;
 
		char *to;
 
		char *from;
 
		long tm;
 
		char *msg;
 
		int utf8;
 
		char *gunk;
 
	} *message = y_new0(struct m, 1);
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 1 || pair->key == 4) {
 
			if (!message->from)
 
				message->from = pair->value;
 
		} else if (pair->key == 5)
 
			message->to = pair->value;
 
		else if (pair->key == 15)
 
			message->tm = strtol(pair->value, NULL, 10);
 
		else if (pair->key == 97)
 
			message->utf8 = atoi(pair->value);
 
		/* This comes when the official client sends us a message */
 
		else if (pair->key == 429)
 
			message->gunk = pair->value;
 
		/* user message *//* sys message */
 
		else if (pair->key == 14 || pair->key == 16)
 
			message->msg = pair->value;
 
		else if (pair->key == 31) {
 
			if (message->i_31) {
 
				messages = y_list_append(messages, message);
 
				message = y_new0(struct m, 1);
 
			}
 
			message->i_31 = atoi(pair->value);
 
		} else if (pair->key == 32)
 
			message->i_32 = atoi(pair->value);
 
		else
 
			LOG(("yahoo_process_message: status: %d, key: %d, value: %s", pkt->status, pair->key, pair->value));
 
	}
 

	
 
	messages = y_list_append(messages, message);
 

	
 
	for (l = messages; l; l = l->next) {
 
		message = l->data;
 
		if (pkt->service == YAHOO_SERVICE_SYSMESSAGE) {
 
			YAHOO_CALLBACK(ext_yahoo_system_message) (yd->client_id,
 
				message->to, message->from, message->msg);
 
		} else if (pkt->status <= 2 || pkt->status == 5) {
 
			/* Confirm message receipt if we got the gunk */
 
			if(message->gunk) {
 
				struct yahoo_packet *outpkt;
 
                        
 
				outpkt = yahoo_packet_new(YAHOO_SERVICE_MESSAGE_CONFIRM,
 
					YPACKET_STATUS_DEFAULT, 0);
 
				yahoo_packet_hash(outpkt, 1, yd->user);
 
				yahoo_packet_hash(outpkt, 5, message->from);
 
				yahoo_packet_hash(outpkt, 302, "430");
 
				yahoo_packet_hash(outpkt, 430, message->gunk);
 
				yahoo_packet_hash(outpkt, 303, "430");
 
				yahoo_packet_hash(outpkt, 450, "0");
 
				yahoo_send_packet(yid, outpkt, 0);
 
                        
 
				yahoo_packet_free(outpkt);
 
			}
 

	
 
			if (!strcmp(message->msg, "<ding>"))
 
				YAHOO_CALLBACK(ext_yahoo_got_buzz) (yd->client_id,
 
					message->to, message->from, message->tm);
 
			else
 
				YAHOO_CALLBACK(ext_yahoo_got_im) (yd->client_id,
 
					message->to, message->from, message->msg,
 
					message->tm, pkt->status, message->utf8);
 
		} else if (pkt->status == 0xffffffff) {
 
			YAHOO_CALLBACK(ext_yahoo_error) (yd->client_id,
 
				message->msg, 0, E_SYSTEM);
 
		}
 
		FREE(message);
 
	}
 

	
 
	y_list_free(messages);
 
}
 

	
 
/*
 
 * Here's what multi-level packets look like. Data in brackets is the value.
 
 *
 
 * 3 level:
 
 * =======
 
 * 
 
 * 302 (318) - Beginning level 1
 
 * 	300 (318) - Begin level 2
 
 * 	302 (319) - End level 2 header
 
 * 		300 (319) - Begin level 3
 
 * 		301 (319) - End level 3
 
 * 	303 (319) - End level 2
 
 * 303 (318) - End level 1
 
 *
 
 * 2 level:
 
 * =======
 
 *
 
 * 302 (315) - Beginning level 1
 
 * 	300 (315) - Begin level 2
 
 * 	301 (315) - End level 2
 
 * 303 (315) - End level 1
 
 *
 
 */
 
static void yahoo_process_status(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	YList *l;
 
	struct yahoo_data *yd = yid->yd;
 

	
 
	struct yahoo_process_status_entry *u;
 

	
 
	YList *users = 0;
 

	
 
	if (pkt->service == YAHOO_SERVICE_LOGOFF && pkt->status == -1) {
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yd->client_id,
 
			YAHOO_LOGIN_DUPL, NULL);
 
		return;
 
	}
 

	
 
	/* 
 
	 * Status updates may be spread accross multiple packets and not 
 
	 * even on buddy boundaries, so keeping some state is important. 
 
	 * So, continue where we left off, and only add a user entry to 
 
	 * the list once it's complete (301-315 End buddy).
 
	 */
 
	u = yd->half_user;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		switch (pair->key) {
 
		case 300:	/* Begin buddy */
 
			if (!strcmp(pair->value, "315") && !u) {
 
				u = yd->half_user = y_new0(struct yahoo_process_status_entry, 1);
 
			}
 
			break;
 
		case 301:	/* End buddy */
 
			if (!strcmp(pair->value, "315") && u) {
 
				users = y_list_prepend(users, u);
 
				u = yd->half_user = NULL;
 
			}
 
			break;
 
		case 0:	/* we won't actually do anything with this */
 
			NOTICE(("key %d:%s", pair->key, pair->value));
 
			break;
 
		case 1:	/* we don't get the full buddy list here. */
 
			if (!yd->logged_in) {
 
				yd->logged_in = 1;
 
				if (yd->current_status < 0)
 
					yd->current_status = yd->initial_status;
 
				YAHOO_CALLBACK(ext_yahoo_login_response) (yd->
 
					client_id, YAHOO_LOGIN_OK, NULL);
 
			}
 
			break;
 
		case 8:	/* how many online buddies we have */
 
			NOTICE(("key %d:%s", pair->key, pair->value));
 
			break;
 
		case 7:	/* the current buddy */
 
			if (!u) {
 
				/* This will only happen in case of a single level message */
 
				u = y_new0(struct yahoo_process_status_entry, 1);
 
				users = y_list_prepend(users, u);
 
			}
 
			u->name = pair->value;
 
			break;
 
		case 10:	/* state */
 
			u->state = strtol(pair->value, NULL, 10);
 
			break;
 
		case 19:	/* custom status message */
 
			u->msg = pair->value;
 
			break;
 
		case 47:	/* is it an away message or not. Not applicable for YMSG16 anymore */
 
			u->away = atoi(pair->value);
 
			break;
 
		case 137:	/* seconds idle */
 
			u->idle = atoi(pair->value);
 
			break;
 
		case 11:	/* this is the buddy's session id */
 
			u->buddy_session = atoi(pair->value);
 
			break;
 
		case 17:	/* in chat? */
 
			u->f17 = atoi(pair->value);
 
			break;
 
		case 13:	/* bitmask, bit 0 = pager, bit 1 = chat, bit 2 = game */
 
			u->flags = atoi(pair->value);
 
			break;
 
		case 60:	/* SMS -> 1 MOBILE USER */
 
			/* sometimes going offline makes this 2, but invisible never sends it */
 
			u->mobile = atoi(pair->value);
 
			break;
 
		case 138:
 
			u->f138 = atoi(pair->value);
 
			break;
 
		case 184:
 
			u->f184 = pair->value;
 
			break;
 
		case 192:
 
			u->f192 = atoi(pair->value);
 
			break;
 
		case 10001:
 
			u->f10001 = atoi(pair->value);
 
			break;
 
		case 10002:
 
			u->f10002 = atoi(pair->value);
 
			break;
 
		case 198:
 
			u->f198 = atoi(pair->value);
 
			break;
 
		case 197:
 
			u->f197 = pair->value;
 
			break;
 
		case 205:
 
			u->f205 = pair->value;
 
			break;
 
		case 213:
 
			u->f213 = atoi(pair->value);
 
			break;
 
		case 16:	/* Custom error message */
 
			YAHOO_CALLBACK(ext_yahoo_error) (yd->client_id,
 
				pair->value, 0, E_CUSTOM);
 
			break;
 
		default:
 
			WARNING(("unknown status key %d:%s", pair->key,
 
					pair->value));
 
			break;
 
		}
 
	}
 

	
 
	while (users) {
 
		YList *t = users;
 
		struct yahoo_process_status_entry *u = users->data;
 

	
 
		if (u->name != NULL) {
 
			if (pkt->service ==
 
				YAHOO_SERVICE_LOGOFF
 
				/*|| u->flags == 0 No flags for YMSG16 */ ) {
 
				YAHOO_CALLBACK(ext_yahoo_status_changed) (yd->
 
					client_id, u->name,
 
					YAHOO_STATUS_OFFLINE, NULL, 1, 0, 0);
 
			} else {
 
				/* Key 47 always seems to be 1 for YMSG16 */
 
				if (!u->state)
 
					u->away = 0;
 
				else
 
					u->away = 1;
 

	
 
				YAHOO_CALLBACK(ext_yahoo_status_changed) (yd->
 
					client_id, u->name, u->state, u->msg,
 
					u->away, u->idle, u->mobile);
 
			}
 
		}
 

	
 
		users = y_list_remove_link(users, users);
 
		y_list_free_1(t);
 
		FREE(u);
 
	}
 
}
 

	
 
static void yahoo_process_buddy_list(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	YList *l;
 
	int last_packet = 0;
 
	char *cur_group = NULL;
 
	struct yahoo_buddy *newbud = NULL;
 

	
 
	/* we could be getting multiple packets here */
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		switch (pair->key) {
 
		case 300:
 
		case 301:
 
		case 302:
 
			break;	/* Separators. Our logic does not need them */
 
		case 303:
 
			if (318 == atoi(pair->value))
 
				last_packet = 1;
 
			break;
 
		case 65:
 
			cur_group = strdup(pair->value);
 
			break;
 
		case 7:
 
			newbud = y_new0(struct yahoo_buddy, 1);
 
			newbud->id = strdup(pair->value);
 
			if (cur_group)
 
				newbud->group = strdup(cur_group);
 
			else if (yd->buddies) {
 
				struct yahoo_buddy *lastbud =
 
					(struct yahoo_buddy *)y_list_nth(yd->
 
					buddies,
 
					y_list_length(yd->buddies) - 1)->data;
 
				newbud->group = strdup(lastbud->group);
 
			} else
 
				newbud->group = strdup("Buddies");
 

	
 
			yd->buddies = y_list_append(yd->buddies, newbud);
 

	
 
			break;
 
		}
 
	}
 

	
 
	/* we could be getting multiple packets here */
 
	if (pkt->hash && !last_packet)
 
		return;
 

	
 
	/* Logged in */
 
	if (!yd->logged_in) {
 
		yd->logged_in = 1;
 
		if (yd->current_status < 0)
 
			yd->current_status = yd->initial_status;
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yd->client_id,
 
			YAHOO_LOGIN_OK, NULL);
 

	
 
		yahoo_set_away(yd->client_id, yd->initial_status, NULL,
 
			(yd->initial_status == YAHOO_STATUS_AVAILABLE) ? 0 : 1);
 

	
 
		yahoo_get_yab(yd->client_id);
 
	}
 

	
 
	YAHOO_CALLBACK(ext_yahoo_got_buddies) (yd->client_id, yd->buddies);
 

	
 
}
 

	
 
static void yahoo_process_list(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	YList *l;
 

	
 
	/* we could be getting multiple packets here */
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		switch (pair->key) {
 
		case 89:	/* identities */
 
			{
 
				char **identities =
 
					y_strsplit(pair->value, ",", -1);
 
				int i;
 
				for (i = 0; identities[i]; i++)
 
					yd->identities =
 
						y_list_append(yd->identities,
 
						strdup(identities[i]));
 
				y_strfreev(identities);
 
			}
 
			YAHOO_CALLBACK(ext_yahoo_got_identities) (yd->client_id,
 
				yd->identities);
 
			break;
 
		case 59:	/* cookies */
 
			if (pair->value[0] == 'Y') {
 
				FREE(yd->cookie_y);
 
				FREE(yd->login_cookie);
 

	
 
				yd->cookie_y = getcookie(pair->value);
 
				yd->login_cookie = getlcookie(yd->cookie_y);
 

	
 
			} else if (pair->value[0] == 'T') {
 
				FREE(yd->cookie_t);
 
				yd->cookie_t = getcookie(pair->value);
 

	
 
			} else if (pair->value[0] == 'C') {
 
				FREE(yd->cookie_c);
 
				yd->cookie_c = getcookie(pair->value);
 
			}
 

	
 
			break;
 
		case 3:	/* my id */
 
		case 90:	/* 1 */
 
		case 100:	/* 0 */
 
		case 101:	/* NULL */
 
		case 102:	/* NULL */
 
		case 93:	/* 86400/1440 */
 
			break;
 
		}
 
	}
 

	
 
	if (yd->cookie_y && yd->cookie_t)	/* We don't get cookie_c anymore */
 
		YAHOO_CALLBACK(ext_yahoo_got_cookies) (yd->client_id);
 
}
 

	
 
static void yahoo_process_verify(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 

	
 
	if (pkt->status != 0x01) {
 
		DEBUG_MSG(("expected status: 0x01, got: %d", pkt->status));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yd->client_id,
 
			YAHOO_LOGIN_LOCK, "");
 
		return;
 
	}
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_AUTH, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 

	
 
}
 

	
 
static void yahoo_process_picture_checksum(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *from;
 
	char *to;
 
	int checksum = 0;
 
	YList *l;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		switch (pair->key) {
 
		case 1:
 
		case 4:
 
			from = pair->value;
 
		case 5:
 
			to = pair->value;
 
			break;
 
		case 212:
 
			break;
 
		case 192:
 
			checksum = atoi(pair->value);
 
			break;
 
		}
 
	}
 

	
 
	YAHOO_CALLBACK(ext_yahoo_got_buddyicon_checksum) (yd->client_id, to,
 
		from, checksum);
 
}
 

	
 
static void yahoo_process_picture(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *url;
 
	char *from;
 
	char *to;
 
	int status = 0;
 
	int checksum = 0;
 
	YList *l;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		switch (pair->key) {
 
		case 1:
 
		case 4:	/* sender */
 
			from = pair->value;
 
			break;
 
		case 5:	/* we */
 
			to = pair->value;
 
			break;
 
		case 13:	/* request / sending */
 
			status = atoi(pair->value);
 
			break;
 
		case 20:	/* url */
 
			url = pair->value;
 
			break;
 
		case 192:	/*checksum */
 
			checksum = atoi(pair->value);
 
			break;
 
		}
 
	}
 

	
 
	switch (status) {
 
	case 1:		/* this is a request, ignore for now */
 
		YAHOO_CALLBACK(ext_yahoo_got_buddyicon_request) (yd->client_id,
 
			to, from);
 
		break;
 
	case 2:		/* this is cool - we get a picture :) */
 
		YAHOO_CALLBACK(ext_yahoo_got_buddyicon) (yd->client_id, to,
 
			from, url, checksum);
 
		break;
 
	}
 
}
 

	
 
static void yahoo_process_picture_upload(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	YList *l;
 
	char *url;
 

	
 
	if (pkt->status != 1)
 
		return;		/* something went wrong */
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		switch (pair->key) {
 
		case 5:	/* we */
 
			break;
 
		case 20:	/* url */
 
			url = pair->value;
 
			break;
 
		case 27:	/* local filename */
 
			break;
 
		case 38:	/* time */
 
			break;
 
		}
 
	}
 

	
 
	YAHOO_CALLBACK(ext_yahoo_buddyicon_uploaded) (yd->client_id, url);
 
}
 

	
 
void yahoo_login(int id, int initial)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	struct connect_callback_data *ccd;
 
	struct yahoo_server_settings *yss;
 
	int tag;
 

	
 
	char *host;
 

	
 
	struct yahoo_input_data *yid = y_new0(struct yahoo_input_data, 1);
 
	yid->yd = yd;
 
	yid->type = YAHOO_CONNECTION_PAGER;
 
	inputs = y_list_prepend(inputs, yid);
 

	
 
	yd->initial_status = initial;
 
	yss = yd->server_settings;
 

	
 
	ccd = y_new0(struct connect_callback_data, 1);
 
	ccd->yd = yd;
 

	
 
	host = yss->pager_host;
 

	
 
	if (!host)
 
		host = yss->pager_host_list[0];
 

	
 
	tag = YAHOO_CALLBACK(ext_yahoo_connect_async) (yd->client_id,
 
		host, yss->pager_port, yahoo_connected, ccd, 0);
 

	
 
	/*
 
	 * if tag <= 0, then callback has already been called
 
	 * so ccd will have been freed
 
	 */
 
	if (tag > 0)
 
		ccd->tag = tag;
 
	else if (tag < 0)
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yd->client_id,
 
			YAHOO_LOGIN_SOCK, NULL);
 
}
 

	
 
static void yahoo_auth_https(struct yahoo_data *yd)
 
{
 
	char url[256];
 
	char *user_encoded, *pass_encoded, *seed_encoded;
 

	
 
	struct yahoo_input_data *yid = y_new0(struct yahoo_input_data, 1);
 

	
 
	yid->yd = yd;
 
	yid->type = YAHOO_CONNECTION_AUTH;
 

	
 
	inputs = y_list_prepend(inputs, yid);
 

	
 
	user_encoded = yahoo_urlencode(yid->yd->user);
 
	pass_encoded = yahoo_urlencode(yid->yd->password);
 
	seed_encoded = yahoo_urlencode(yid->yd->seed);
 

	
 
	snprintf(url, sizeof(url),
 
		"https://login.yahoo.com/config/pwtoken_get?src=ymsgr&ts="
 
		"&login=%s&passwd=%s&chal=%s", user_encoded, pass_encoded,
 
		seed_encoded);
 

	
 
	yahoo_http_get(yid->yd->client_id, url, NULL, 1, 0,
 
		_yahoo_http_connected, yid);
 

	
 
	FREE(user_encoded);
 
	FREE(pass_encoded);
 
	FREE(seed_encoded);
 
}
 

	
 
static void yahoo_process_auth(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	char *seed = NULL;
 
	char *sn = NULL;
 
	YList *l = pkt->hash;
 
	int m = 0;
 
	struct yahoo_data *yd = yid->yd;
 

	
 
	while (l) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		switch (pair->key) {
 
		case 94:
 
			seed = pair->value;
 
			break;
 
		case 1:
 
			sn = pair->value;
 
			break;
 
		case 13:
 
			m = atoi(pair->value);
 
			break;
 
		}
 
		l = l->next;
 
	}
 

	
 
	if (!seed)
 
		return;
 

	
 
	yd->seed = strdup(seed);
 

	
 
	if (m==2)
 
		yahoo_auth_https(yd);
 
	else {
 
		/* call error */
 
		WARNING(("unknown auth type %d", m));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
	}
 
}
 

	
 
static void yahoo_send_auth(struct yahoo_data *yd)
 
{
 

	
 
	struct yahoo_packet *packet;
 

	
 
	unsigned char crypt_hash[25];
 

	
 
	md5_byte_t result[16];
 
	md5_state_t ctx;
 

	
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(yd->client_id,
 
			YAHOO_CONNECTION_PAGER);
 

	
 
	/* HTTPS */
 
	md5_init(&ctx);
 
	md5_append(&ctx, (md5_byte_t *)yd->crumb, strlen(yd->crumb));
 
	md5_append(&ctx, (md5_byte_t *)yd->seed, strlen(yd->seed));
 
	md5_finish(&ctx, result);
 

	
 
	to_y64(crypt_hash, result, 16);
 

	
 
	packet = yahoo_packet_new(YAHOO_SERVICE_AUTHRESP,
 
		yd->initial_status, yd->session_id);
 
	yahoo_packet_hash(packet, 1, yd->user);
 
	yahoo_packet_hash(packet, 0, yd->user);
 
	yahoo_packet_hash(packet, 277, yd->cookie_y);
 
	yahoo_packet_hash(packet, 278, yd->cookie_t);
 
	yahoo_packet_hash(packet, 307, (char *)crypt_hash);
 
	yahoo_packet_hash(packet, 244, "4194239");	/* Rekkanoryo says this is the build number */
 
	yahoo_packet_hash(packet, 2, yd->user);
 
	yahoo_packet_hash(packet, 2, "1");
 
	yahoo_packet_hash(packet, 59, yd->cookie_b);
 
	yahoo_packet_hash(packet, 98, "us");	/* TODO Put country code */
 
	yahoo_packet_hash(packet, 135, "9.0.0.2152");
 

	
 
	yahoo_send_packet(yid, packet, 0);
 

	
 
	yahoo_packet_free(packet);
 
}
 

	
 
static void yahoo_process_auth_resp(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *login_id;
 
	char *handle;
 
	char *url = NULL;
 
	int login_status = -1;
 

	
 
	YList *l;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 0)
 
			login_id = pair->value;
 
		else if (pair->key == 1)
 
			handle = pair->value;
 
		else if (pair->key == 20)
 
			url = pair->value;
 
		else if (pair->key == 66)
 
			login_status = atoi(pair->value);
 
	}
 

	
 
	if (pkt->status == YPACKET_STATUS_DISCONNECTED) {
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yd->client_id,
 
			login_status, url);
 
		/*      yahoo_logoff(yd->client_id); */
 
	}
 
}
 

	
 
static void yahoo_process_mail(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *who = NULL;
 
	char *email = NULL;
 
	char *subj = NULL;
 
	int count = 0;
 
	YList *l;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 9)
 
			count = strtol(pair->value, NULL, 10);
 
		else if (pair->key == 43)
 
			who = pair->value;
 
		else if (pair->key == 42)
 
			email = pair->value;
 
		else if (pair->key == 18)
 
			subj = pair->value;
 
		else
 
			LOG(("key: %d => value: %s", pair->key, pair->value));
 
	}
 

	
 
	if (who && email && subj) {
 
		char from[1024];
 
		snprintf(from, sizeof(from), "%s (%s)", who, email);
 
		YAHOO_CALLBACK(ext_yahoo_mail_notify) (yd->client_id, from,
 
			subj, count);
 
	} else if (count > 0)
 
		YAHOO_CALLBACK(ext_yahoo_mail_notify) (yd->client_id, NULL,
 
			NULL, count);
 
}
 

	
 
static void yahoo_process_new_contact(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *me = NULL;
 
	char *who = NULL;
 
	char *msg = NULL;
 
	int online = -1;
 

	
 
	YList *l;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 4)
 
			who = pair->value;
 
		else if (pair->key == 5)
 
			me = pair->value;
 
		else if (pair->key == 14)
 
			msg = pair->value;
 
		else if (pair->key == 13)
 
			online = strtol(pair->value, NULL, 10);
 
	}
 

	
 
	if (who && online < 0)
 
		YAHOO_CALLBACK(ext_yahoo_contact_added) (yd->client_id, me, who,
 
			msg);
 
	else if (online == 2)
 
		YAHOO_CALLBACK(ext_yahoo_rejected) (yd->client_id, who, msg);
 
}
 

	
 
/* UNUSED? */
 
static void yahoo_process_contact(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *id = NULL;
 
	char *who = NULL;
 
	char *msg = NULL;
 
	char *name = NULL;
 
	long tm = 0L;
 
	int state = YAHOO_STATUS_AVAILABLE;
 
	int online = 0;
 
	int away = 0;
 
	int idle = 0;
 
	int mobile = 0;
 

	
 
	YList *l;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 1)
 
			id = pair->value;
 
		else if (pair->key == 3)
 
			who = pair->value;
 
		else if (pair->key == 14)
 
			msg = pair->value;
 
		else if (pair->key == 7)
 
			name = pair->value;
 
		else if (pair->key == 10)
 
			state = strtol(pair->value, NULL, 10);
 
		else if (pair->key == 15)
 
			tm = strtol(pair->value, NULL, 10);
 
		else if (pair->key == 13)
 
			online = strtol(pair->value, NULL, 10);
 
		else if (pair->key == 47)
 
			away = strtol(pair->value, NULL, 10);
 
		else if (pair->key == 137)
 
			idle = strtol(pair->value, NULL, 10);
 
		else if (pair->key == 60)
 
			mobile = strtol(pair->value, NULL, 10);
 

	
 
	}
 

	
 
	if (id)
 
		YAHOO_CALLBACK(ext_yahoo_contact_added) (yd->client_id, id, who,
 
			msg);
 
	else if (name)
 
		YAHOO_CALLBACK(ext_yahoo_status_changed) (yd->client_id, name,
 
			state, msg, away, idle, mobile);
 
	else if (pkt->status == 0x07)
 
		YAHOO_CALLBACK(ext_yahoo_rejected) (yd->client_id, who, msg);
 
}
 

	
 
static void yahoo_process_buddyadd(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *who = NULL;
 
	char *where = NULL;
 
	int status = 0;
 
	char *me = NULL;
 

	
 
	struct yahoo_buddy *bud = NULL;
 

	
 
	YList *l;
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 1)
 
			me = pair->value;
 
		if (pair->key == 7)
 
			who = pair->value;
 
		if (pair->key == 65)
 
			where = pair->value;
 
		if (pair->key == 66)
 
			status = strtol(pair->value, NULL, 10);
 
	}
 

	
 
	if (!who)
 
		return;
 
	if (!where)
 
		where = "Unknown";
 

	
 
	bud = y_new0(struct yahoo_buddy, 1);
 
	bud->id = strdup(who);
 
	bud->group = strdup(where);
 
	bud->real_name = NULL;
 

	
 
	yd->buddies = y_list_append(yd->buddies, bud);
 

	
 
	/* A non-zero status (i've seen 2) seems to mean the buddy is already 
 
	 * added and is online */
 
	if (status) {
 
		LOG(("Setting online see packet for info"));
 
		yahoo_dump_unhandled(pkt);
 
		YAHOO_CALLBACK(ext_yahoo_status_changed) (yd->client_id, who,
 
			YAHOO_STATUS_AVAILABLE, NULL, 0, 0, 0);
 
	}
 
}
 

	
 
static void yahoo_process_buddydel(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *who = NULL;
 
	char *where = NULL;
 
	int unk_66 = 0;
 
	char *me = NULL;
 
	struct yahoo_buddy *bud;
 

	
 
	YList *buddy;
 

	
 
	YList *l;
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 1)
 
			me = pair->value;
 
		else if (pair->key == 7)
 
			who = pair->value;
 
		else if (pair->key == 65)
 
			where = pair->value;
 
		else if (pair->key == 66)
 
			unk_66 = strtol(pair->value, NULL, 10);
 
		else
 
			DEBUG_MSG(("unknown key: %d = %s", pair->key,
 
					pair->value));
 
	}
 

	
 
	if (!who || !where)
 
		return;
 

	
 
	bud = y_new0(struct yahoo_buddy, 1);
 
	bud->id = strdup(who);
 
	bud->group = strdup(where);
 

	
 
	buddy = y_list_find_custom(yd->buddies, bud, is_same_bud);
 

	
 
	FREE(bud->id);
 
	FREE(bud->group);
 
	FREE(bud);
 

	
 
	if (buddy) {
 
		bud = buddy->data;
 
		yd->buddies = y_list_remove_link(yd->buddies, buddy);
 
		y_list_free_1(buddy);
 

	
 
		FREE(bud->id);
 
		FREE(bud->group);
 
		FREE(bud->real_name);
 
		FREE(bud);
 

	
 
		bud = NULL;
 
	}
 
}
 

	
 
static void yahoo_process_ignore(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	char *who = NULL;
 
	int status = 0;
 
	char *me = NULL;
 
	int un_ignore = 0;
 

	
 
	YList *l;
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 0)
 
			who = pair->value;
 
		if (pair->key == 1)
 
			me = pair->value;
 
		if (pair->key == 13)	/* 1 == ignore, 2 == unignore */
 
			un_ignore = strtol(pair->value, NULL, 10);
 
		if (pair->key == 66)
 
			status = strtol(pair->value, NULL, 10);
 
	}
 

	
 
	/*
 
	 * status
 
	 *      0  - ok
 
	 *      2  - already in ignore list, could not add
 
	 *      3  - not in ignore list, could not delete
 
	 *      12 - is a buddy, could not add
 
	 */
 

	
 
/*	if(status)
 
		YAHOO_CALLBACK(ext_yahoo_error)(yd->client_id, who, 0, status);
 
*/
 
}
 

	
 
static void yahoo_process_voicechat(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	char *who = NULL;
 
	char *me = NULL;
 
	char *room = NULL;
 
	char *voice_room = NULL;
 

	
 
	YList *l;
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 4)
 
			who = pair->value;
 
		if (pair->key == 5)
 
			me = pair->value;
 
		if (pair->key == 13)
 
			voice_room = pair->value;
 
		if (pair->key == 57)
 
			room = pair->value;
 
	}
 

	
 
	NOTICE(("got voice chat invite from %s in %s to identity %s", who, room,
 
			me));
 
	/* 
 
	 * send: s:0 1:me 5:who 57:room 13:1
 
	 * ????  s:4 5:who 10:99 19:-1615114531
 
	 * gotr: s:4 5:who 10:99 19:-1615114615
 
	 * ????  s:1 5:me 4:who 57:room 13:3room
 
	 * got:  s:1 5:me 4:who 57:room 13:1room
 
	 * rej:  s:0 1:me 5:who 57:room 13:3
 
	 * rejr: s:4 5:who 10:99 19:-1617114599
 
	 */
 
}
 

	
 
static void yahoo_process_ping(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	char *errormsg = NULL;
 

	
 
	YList *l;
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 16)
 
			errormsg = pair->value;
 
	}
 

	
 
	NOTICE(("got ping packet"));
 
	YAHOO_CALLBACK(ext_yahoo_got_ping) (yid->yd->client_id, errormsg);
 
}
 

	
 
static void yahoo_process_buddy_change_group(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	YList *l;
 
	char *me = NULL;
 
	char *who = NULL;
 
	char *old_group = NULL;
 
	char *new_group = NULL;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 1)
 
			me = pair->value;
 
		if (pair->key == 7)
 
			who = pair->value;
 
		if (pair->key == 224)
 
			old_group = pair->value;
 
		if (pair->key == 264)
 
			new_group = pair->value;
 
	}
 

	
 
	YAHOO_CALLBACK(ext_yahoo_got_buddy_change_group) (yid->yd->client_id,
 
		me, who, old_group, new_group);
 
}
 

	
 
static void _yahoo_webcam_get_server_connected(void *fd, int error, void *d)
 
{
 
	struct yahoo_input_data *yid = d;
 
	char *who = yid->wcm->user;
 
	char *data = NULL;
 
	char *packet = NULL;
 
	unsigned char magic_nr[] = { 0, 1, 0 };
 
	unsigned char header_len = 8;
 
	unsigned int len = 0;
 
	unsigned int pos = 0;
 

	
 
	if (error || !fd) {
 
		FREE(who);
 
		FREE(yid);
 
		return;
 
	}
 

	
 
	yid->fd = fd;
 
	inputs = y_list_prepend(inputs, yid);
 

	
 
	/* send initial packet */
 
	if (who)
 
		data = strdup("<RVWCFG>");
 
	else
 
		data = strdup("<RUPCFG>");
 
	yahoo_add_to_send_queue(yid, data, strlen(data));
 
	FREE(data);
 

	
 
	/* send data */
 
	if (who) {
 
		data = strdup("g=");
 
		data = y_string_append(data, who);
 
		data = y_string_append(data, "\r\n");
 
	} else {
 
		data = strdup("f=1\r\n");
 
	}
 
	len = strlen(data);
 
	packet = y_new0(char, header_len + len);
 
	packet[pos++] = header_len;
 
	memcpy(packet + pos, magic_nr, sizeof(magic_nr));
 
	pos += sizeof(magic_nr);
 
	pos += yahoo_put32(packet + pos, len);
 
	memcpy(packet + pos, data, len);
 
	pos += len;
 
	yahoo_add_to_send_queue(yid, packet, pos);
 
	FREE(packet);
 
	FREE(data);
 

	
 
	yid->read_tag =
 
		YAHOO_CALLBACK(ext_yahoo_add_handler) (yid->yd->client_id, fd,
 
		YAHOO_INPUT_READ, yid);
 
}
 

	
 
static void yahoo_webcam_get_server(struct yahoo_input_data *y, char *who,
 
	char *key)
 
{
 
	struct yahoo_input_data *yid = y_new0(struct yahoo_input_data, 1);
 
	struct yahoo_server_settings *yss = y->yd->server_settings;
 

	
 
	yid->type = YAHOO_CONNECTION_WEBCAM_MASTER;
 
	yid->yd = y->yd;
 
	yid->wcm = y_new0(struct yahoo_webcam, 1);
 
	yid->wcm->user = who ? strdup(who) : NULL;
 
	yid->wcm->direction = who ? YAHOO_WEBCAM_DOWNLOAD : YAHOO_WEBCAM_UPLOAD;
 
	yid->wcm->key = strdup(key);
 

	
 
	YAHOO_CALLBACK(ext_yahoo_connect_async) (yid->yd->client_id,
 
		yss->webcam_host, yss->webcam_port,
 
		_yahoo_webcam_get_server_connected, yid, 0);
 

	
 
}
 

	
 
static YList *webcam_queue = NULL;
 
static void yahoo_process_webcam_key(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	char *me = NULL;
 
	char *key = NULL;
 
	char *who = NULL;
 

	
 
	YList *l;
 
	yahoo_dump_unhandled(pkt);
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 5)
 
			me = pair->value;
 
		if (pair->key == 61)
 
			key = pair->value;
 
	}
 

	
 
	l = webcam_queue;
 
	if (!l)
 
		return;
 
	who = l->data;
 
	webcam_queue = y_list_remove_link(webcam_queue, webcam_queue);
 
	y_list_free_1(l);
 
	yahoo_webcam_get_server(yid, who, key);
 
	FREE(who);
 
}
 

	
 
static void yahoo_packet_process(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	DEBUG_MSG(("yahoo_packet_process: 0x%02x", pkt->service));
 
	switch (pkt->service) {
 
	case YAHOO_SERVICE_USERSTAT:
 
	case YAHOO_SERVICE_LOGON:
 
	case YAHOO_SERVICE_LOGOFF:
 
	case YAHOO_SERVICE_ISAWAY:
 
	case YAHOO_SERVICE_ISBACK:
 
	case YAHOO_SERVICE_GAMELOGON:
 
	case YAHOO_SERVICE_GAMELOGOFF:
 
	case YAHOO_SERVICE_IDACT:
 
	case YAHOO_SERVICE_IDDEACT:
 
	case YAHOO_SERVICE_Y6_STATUS_UPDATE:
 
	case YAHOO_SERVICE_Y8_STATUS:
 
		yahoo_process_status(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_NOTIFY:
 
		yahoo_process_notify(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_MESSAGE:
 
	case YAHOO_SERVICE_GAMEMSG:
 
	case YAHOO_SERVICE_SYSMESSAGE:
 
		yahoo_process_message(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_NEWMAIL:
 
		yahoo_process_mail(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_Y7_AUTHORIZATION:
 
		yahoo_process_new_contact(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_NEWCONTACT:
 
		yahoo_process_contact(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_LIST:
 
		yahoo_process_list(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_VERIFY:
 
		yahoo_process_verify(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_AUTH:
 
		yahoo_process_auth(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_AUTHRESP:
 
		yahoo_process_auth_resp(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_CONFINVITE:
 
	case YAHOO_SERVICE_CONFADDINVITE:
 
	case YAHOO_SERVICE_CONFDECLINE:
 
	case YAHOO_SERVICE_CONFLOGON:
 
	case YAHOO_SERVICE_CONFLOGOFF:
 
	case YAHOO_SERVICE_CONFMSG:
 
		yahoo_process_conference(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_CHATONLINE:
 
	case YAHOO_SERVICE_CHATGOTO:
 
	case YAHOO_SERVICE_CHATJOIN:
 
	case YAHOO_SERVICE_CHATLEAVE:
 
	case YAHOO_SERVICE_CHATEXIT:
 
	case YAHOO_SERVICE_CHATLOGOUT:
 
	case YAHOO_SERVICE_CHATPING:
 
	case YAHOO_SERVICE_COMMENT:
 
		yahoo_process_chat(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_P2PFILEXFER:
 
	case YAHOO_SERVICE_Y7_FILETRANSFER:
 
		yahoo_process_filetransfer(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_Y7_FILETRANSFERINFO:
 
		yahoo_process_filetransferinfo(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_Y7_FILETRANSFERACCEPT:
 
		yahoo_process_filetransferaccept(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_ADDBUDDY:
 
		yahoo_process_buddyadd(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_REMBUDDY:
 
		yahoo_process_buddydel(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_IGNORECONTACT:
 
		yahoo_process_ignore(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_VOICECHAT:
 
		yahoo_process_voicechat(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_WEBCAM:
 
		yahoo_process_webcam_key(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_PING:
 
		yahoo_process_ping(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_Y7_CHANGE_GROUP:
 
		yahoo_process_buddy_change_group(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_IDLE:
 
	case YAHOO_SERVICE_MAILSTAT:
 
	case YAHOO_SERVICE_CHATINVITE:
 
	case YAHOO_SERVICE_CALENDAR:
 
	case YAHOO_SERVICE_NEWPERSONALMAIL:
 
	case YAHOO_SERVICE_ADDIDENT:
 
	case YAHOO_SERVICE_ADDIGNORE:
 
	case YAHOO_SERVICE_GOTGROUPRENAME:
 
	case YAHOO_SERVICE_GROUPRENAME:
 
	case YAHOO_SERVICE_PASSTHROUGH2:
 
	case YAHOO_SERVICE_CHATLOGON:
 
	case YAHOO_SERVICE_CHATLOGOFF:
 
	case YAHOO_SERVICE_CHATMSG:
 
	case YAHOO_SERVICE_REJECTCONTACT:
 
	case YAHOO_SERVICE_PEERTOPEER:
 
		WARNING(("unhandled service 0x%02x", pkt->service));
 
		yahoo_dump_unhandled(pkt);
 
		break;
 
	case YAHOO_SERVICE_PICTURE:
 
		yahoo_process_picture(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_PICTURE_CHECKSUM:
 
		yahoo_process_picture_checksum(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_PICTURE_UPLOAD:
 
		yahoo_process_picture_upload(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_Y8_LIST:	/* Buddy List */
 
		yahoo_process_buddy_list(yid, pkt);
 
		break;
 
	default:
 
		WARNING(("unknown service 0x%02x", pkt->service));
 
		yahoo_dump_unhandled(pkt);
 
		break;
 
	}
 
}
 

	
 
static struct yahoo_packet *yahoo_getdata(struct yahoo_input_data *yid)
 
{
 
	struct yahoo_packet *pkt;
 
	struct yahoo_data *yd = yid->yd;
 
	int pos = 0;
 
	int pktlen;
 

	
 
	if (!yd)
 
		return NULL;
 

	
 
	DEBUG_MSG(("rxlen is %d", yid->rxlen));
 
	if (yid->rxlen < YAHOO_PACKET_HDRLEN) {
 
		DEBUG_MSG(("len < YAHOO_PACKET_HDRLEN"));
 
		return NULL;
 
	}
 

	
 
	pos += 4;		/* YMSG */
 
	pos += 2;
 
	pos += 2;
 

	
 
	pktlen = yahoo_get16(yid->rxqueue + pos);
 
	pos += 2;
 
	DEBUG_MSG(("%d bytes to read, rxlen is %d", pktlen, yid->rxlen));
 

	
 
	if (yid->rxlen < (YAHOO_PACKET_HDRLEN + pktlen)) {
 
		DEBUG_MSG(("len < YAHOO_PACKET_HDRLEN + pktlen"));
 
		return NULL;
 
	}
 

	
 
	LOG(("reading packet"));
 
	yahoo_packet_dump(yid->rxqueue, YAHOO_PACKET_HDRLEN + pktlen);
 

	
 
	pkt = yahoo_packet_new(0, 0, 0);
 

	
 
	pkt->service = yahoo_get16(yid->rxqueue + pos);
 
	pos += 2;
 
	pkt->status = yahoo_get32(yid->rxqueue + pos);
 
	pos += 4;
 
	DEBUG_MSG(("Yahoo Service: 0x%02x Status: %d", pkt->service,
 
			pkt->status));
 
	pkt->id = yahoo_get32(yid->rxqueue + pos);
 
	pos += 4;
 

	
 
	yd->session_id = pkt->id;
 

	
 
	yahoo_packet_read(pkt, yid->rxqueue + pos, pktlen);
 

	
 
	yid->rxlen -= YAHOO_PACKET_HDRLEN + pktlen;
 
	DEBUG_MSG(("rxlen == %d, rxqueue == %p", yid->rxlen, yid->rxqueue));
 
	if (yid->rxlen > 0) {
 
		unsigned char *tmp = y_memdup(yid->rxqueue + YAHOO_PACKET_HDRLEN
 
			+ pktlen, yid->rxlen);
 
		FREE(yid->rxqueue);
 
		yid->rxqueue = tmp;
 
		DEBUG_MSG(("new rxlen == %d, rxqueue == %p", yid->rxlen,
 
				yid->rxqueue));
 
	} else {
 
		DEBUG_MSG(("freed rxqueue == %p", yid->rxqueue));
 
		FREE(yid->rxqueue);
 
	}
 

	
 
	return pkt;
 
}
 

	
 
static struct yab *yahoo_yab_read(unsigned char *d, int len)
 
{
 
	char *st, *en;
 
	char *data = (char *)d;
 
	struct yab *yab = NULL;
 

	
 
	data[len] = '\0';
 

	
 
	DEBUG_MSG(("Got yab: %s", data));
 
	st = en = strstr(data, "e0=\"");
 
	if (st) {
 
		yab = y_new0(struct yab, 1);
 

	
 
		st += strlen("e0=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->email = yahoo_xmldecode(st);
 
	}
 

	
 
	if (!en)
 
		return NULL;
 

	
 
	st = strstr(en, "id=\"");
 
	if (st) {
 
		st += strlen("id=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->yid = atoi(yahoo_xmldecode(st));
 
	}
 

	
 
	st = strstr(en, "fn=\"");
 
	if (st) {
 
		st += strlen("fn=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->fname = yahoo_xmldecode(st);
 
	}
 

	
 
	st = strstr(en, "ln=\"");
 
	if (st) {
 
		st += strlen("ln=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->lname = yahoo_xmldecode(st);
 
	}
 

	
 
	st = strstr(en, "nn=\"");
 
	if (st) {
 
		st += strlen("nn=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->nname = yahoo_xmldecode(st);
 
	}
 

	
 
	st = strstr(en, "yi=\"");
 
	if (st) {
 
		st += strlen("yi=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->id = yahoo_xmldecode(st);
 
	}
 

	
 
	st = strstr(en, "hphone=\"");
 
	if (st) {
 
		st += strlen("hphone=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->hphone = yahoo_xmldecode(st);
 
	}
 

	
 
	st = strstr(en, "wphone=\"");
 
	if (st) {
 
		st += strlen("wphone=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->wphone = yahoo_xmldecode(st);
 
	}
 

	
 
	st = strstr(en, "mphone=\"");
 
	if (st) {
 
		st += strlen("mphone=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->mphone = yahoo_xmldecode(st);
 
	}
 

	
 
	st = strstr(en, "dbid=\"");
 
	if (st) {
 
		st += strlen("dbid=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->dbid = atoi(st);
 
	}
 

	
 
	return yab;
 
}
 

	
 
static struct yab *yahoo_getyab(struct yahoo_input_data *yid)
 
{
 
	struct yab *yab = NULL;
 
	int pos = 0, end = 0;
 
	struct yahoo_data *yd = yid->yd;
 

	
 
	if (!yd)
 
		return NULL;
 

	
 
	do {
 
		DEBUG_MSG(("rxlen is %d", yid->rxlen));
 

	
 
		if (yid->rxlen <= strlen("<ct"))
 
			return NULL;
 

	
 
		/* start with <ct */
 
		while (pos < yid->rxlen - strlen("<ct") + 1
 
			&& memcmp(yid->rxqueue + pos, "<ct", strlen("<ct")))
 
			pos++;
 

	
 
		if (pos >= yid->rxlen - 1)
 
			return NULL;
 

	
 
		end = pos + 2;
 
		/* end with > */
 
		while (end < yid->rxlen - strlen(">")
 
			&& memcmp(yid->rxqueue + end, ">", strlen(">")))
 
			end++;
 

	
 
		if (end >= yid->rxlen - 1)
 
			return NULL;
 

	
 
		yab = yahoo_yab_read(yid->rxqueue + pos, end + 2 - pos);
 

	
 
		yid->rxlen -= end + 1;
 
		DEBUG_MSG(("rxlen == %d, rxqueue == %p", yid->rxlen,
 
				yid->rxqueue));
 
		if (yid->rxlen > 0) {
 
			unsigned char *tmp =
 
				y_memdup(yid->rxqueue + end + 1, yid->rxlen);
 
			FREE(yid->rxqueue);
 
			yid->rxqueue = tmp;
 
			DEBUG_MSG(("new rxlen == %d, rxqueue == %p", yid->rxlen,
 
					yid->rxqueue));
 
		} else {
 
			DEBUG_MSG(("freed rxqueue == %p", yid->rxqueue));
 
			FREE(yid->rxqueue);
 
		}
 

	
 
	} while (!yab && end < yid->rxlen - 1);
 

	
 
	return yab;
 
}
 

	
 
static char *yahoo_getwebcam_master(struct yahoo_input_data *yid)
 
{
 
	unsigned int pos = 0;
 
	unsigned int len = 0;
 
	unsigned int status = 0;
 
	char *server = NULL;
 
	struct yahoo_data *yd = yid->yd;
 

	
 
	if (!yid || !yd)
 
		return NULL;
 

	
 
	DEBUG_MSG(("rxlen is %d", yid->rxlen));
 

	
 
	len = yid->rxqueue[pos++];
 
	if (yid->rxlen < len)
 
		return NULL;
 

	
 
	/* extract status (0 = ok, 6 = webcam not online) */
 
	status = yid->rxqueue[pos++];
 

	
 
	if (status == 0) {
 
		pos += 2;	/* skip next 2 bytes */
 
		server = y_memdup(yid->rxqueue + pos, 16);
 
		pos += 16;
 
	} else if (status == 6) {
 
		YAHOO_CALLBACK(ext_yahoo_webcam_closed)
 
			(yd->client_id, yid->wcm->user, 4);
 
	}
 

	
 
	/* skip rest of the data */
 

	
 
	yid->rxlen -= len;
 
	DEBUG_MSG(("rxlen == %d, rxqueue == %p", yid->rxlen, yid->rxqueue));
 
	if (yid->rxlen > 0) {
 
		unsigned char *tmp = y_memdup(yid->rxqueue + pos, yid->rxlen);
 
		FREE(yid->rxqueue);
 
		yid->rxqueue = tmp;
 
		DEBUG_MSG(("new rxlen == %d, rxqueue == %p", yid->rxlen,
 
				yid->rxqueue));
 
	} else {
 
		DEBUG_MSG(("freed rxqueue == %p", yid->rxqueue));
 
		FREE(yid->rxqueue);
 
	}
 

	
 
	return server;
 
}
 

	
 
static int yahoo_get_webcam_data(struct yahoo_input_data *yid)
 
{
 
	unsigned char reason = 0;
 
	unsigned int pos = 0;
 
	unsigned int begin = 0;
 
	unsigned int end = 0;
 
	unsigned int closed = 0;
 
	unsigned char header_len = 0;
 
	char *who;
 
	int connect = 0;
 
	struct yahoo_data *yd = yid->yd;
 

	
 
	if (!yd)
 
		return -1;
 

	
 
	if (!yid->wcm || !yid->wcd || !yid->rxlen)
 
		return -1;
 

	
 
	DEBUG_MSG(("rxlen is %d", yid->rxlen));
 

	
 
	/* if we are not reading part of image then read header */
 
	if (!yid->wcd->to_read) {
 
		header_len = yid->rxqueue[pos++];
 
		yid->wcd->packet_type = 0;
 

	
 
		if (yid->rxlen < header_len)
 
			return 0;
 

	
 
		if (header_len >= 8) {
 
			reason = yid->rxqueue[pos++];
 
			/* next 2 bytes should always be 05 00 */
 
			pos += 2;
 
			yid->wcd->data_size = yahoo_get32(yid->rxqueue + pos);
 
			pos += 4;
 
			yid->wcd->to_read = yid->wcd->data_size;
 
		}
 
		if (header_len >= 13) {
 
			yid->wcd->packet_type = yid->rxqueue[pos++];
 
			yid->wcd->timestamp = yahoo_get32(yid->rxqueue + pos);
 
			pos += 4;
 
		}
 

	
 
		/* skip rest of header */
 
		pos = header_len;
 
	}
 

	
 
	begin = pos;
 
	pos += yid->wcd->to_read;
 
	if (pos > yid->rxlen)
 
		pos = yid->rxlen;
 

	
 
	/* if it is not an image then make sure we have the whole packet */
 
	if (yid->wcd->packet_type != 0x02) {
 
		if ((pos - begin) != yid->wcd->data_size) {
 
			yid->wcd->to_read = 0;
 
			return 0;
 
		} else {
 
			yahoo_packet_dump(yid->rxqueue + begin, pos - begin);
 
		}
 
	}
 

	
 
	DEBUG_MSG(("packet type %.2X, data length %d", yid->wcd->packet_type,
 
			yid->wcd->data_size));
 

	
 
	/* find out what kind of packet we got */
 
	switch (yid->wcd->packet_type) {
 
	case 0x00:
 
		/* user requests to view webcam (uploading) */
 
		if (yid->wcd->data_size &&
 
			yid->wcm->direction == YAHOO_WEBCAM_UPLOAD) {
 
			end = begin;
 
			while (end <= yid->rxlen && yid->rxqueue[end++] != 13) ;
 
			if (end > begin) {
 
				who = y_memdup(yid->rxqueue + begin,
 
					end - begin);
 
				who[end - begin - 1] = 0;
 
				YAHOO_CALLBACK(ext_yahoo_webcam_viewer) (yd->
 
					client_id, who + 2, 2);
 
				FREE(who);
 
			}
 
		}
 

	
 
		if (yid->wcm->direction == YAHOO_WEBCAM_DOWNLOAD) {
 
			/* timestamp/status field */
 
			/* 0 = declined viewing permission */
 
			/* 1 = accepted viewing permission */
 
			if (yid->wcd->timestamp == 0) {
 
				YAHOO_CALLBACK(ext_yahoo_webcam_closed) (yd->
 
					client_id, yid->wcm->user, 3);
 
			}
 
		}
 
		break;
 
	case 0x01:		/* status packets?? */
 
		/* timestamp contains status info */
 
		/* 00 00 00 01 = we have data?? */
 
		break;
 
	case 0x02:		/* image data */
 
		YAHOO_CALLBACK(ext_yahoo_got_webcam_image) (yd->client_id,
 
			yid->wcm->user, yid->rxqueue + begin,
 
			yid->wcd->data_size, pos - begin, yid->wcd->timestamp);
 
		break;
 
	case 0x05:		/* response packets when uploading */
 
		if (!yid->wcd->data_size) {
 
			YAHOO_CALLBACK(ext_yahoo_webcam_data_request) (yd->
 
				client_id, yid->wcd->timestamp);
 
		}
 
		break;
 
	case 0x07:		/* connection is closing */
 
		switch (reason) {
 
		case 0x01:	/* user closed connection */
 
			closed = 1;
 
			break;
 
		case 0x0F:	/* user cancelled permission */
 
			closed = 2;
 
			break;
 
		}
 
		YAHOO_CALLBACK(ext_yahoo_webcam_closed) (yd->client_id,
 
			yid->wcm->user, closed);
 
		break;
 
	case 0x0C:		/* user connected */
 
	case 0x0D:		/* user disconnected */
 
		if (yid->wcd->data_size) {
 
			who = y_memdup(yid->rxqueue + begin, pos - begin + 1);
 
			who[pos - begin] = 0;
 
			if (yid->wcd->packet_type == 0x0C)
 
				connect = 1;
 
			else
 
				connect = 0;
 
			YAHOO_CALLBACK(ext_yahoo_webcam_viewer) (yd->client_id,
 
				who, connect);
 
			FREE(who);
 
		}
 
		break;
 
	case 0x13:		/* user data */
 
		/* i=user_ip (ip of the user we are viewing) */
 
		/* j=user_ext_ip (external ip of the user we */
 
		/*                are viewing) */
 
		break;
 
	case 0x17:		/* ?? */
 
		break;
 
	}
 
	yid->wcd->to_read -= pos - begin;
 

	
 
	yid->rxlen -= pos;
 
	DEBUG_MSG(("rxlen == %d, rxqueue == %p", yid->rxlen, yid->rxqueue));
 
	if (yid->rxlen > 0) {
 
		unsigned char *tmp = y_memdup(yid->rxqueue + pos, yid->rxlen);
 
		FREE(yid->rxqueue);
 
		yid->rxqueue = tmp;
 
		DEBUG_MSG(("new rxlen == %d, rxqueue == %p", yid->rxlen,
 
				yid->rxqueue));
 
	} else {
 
		DEBUG_MSG(("freed rxqueue == %p", yid->rxqueue));
 
		FREE(yid->rxqueue);
 
	}
 

	
 
	/* If we read a complete packet return success */
 
	if (!yid->wcd->to_read)
 
		return 1;
 

	
 
	return 0;
 
}
 

	
 
int yahoo_write_ready(int id, void *fd, void *data)
 
{
 
	struct yahoo_input_data *yid = data;
 
	int len;
 
	struct data_queue *tx;
 

	
 
	LOG(("write callback: id=%d fd=%p data=%p", id, fd, data));
 
	if (!yid || !yid->txqueues)
 
		return -2;
 

	
 
	tx = yid->txqueues->data;
 
	LOG(("writing %d bytes", tx->len));
 
	len = yahoo_send_data(fd, tx->queue, MIN(1024, tx->len));
 

	
 
	if (len == -1 && errno == EAGAIN)
 
		return 1;
 

	
 
	if (len <= 0) {
 
		int e = errno;
 
		DEBUG_MSG(("len == %d (<= 0)", len));
 
		while (yid->txqueues) {
 
			YList *l = yid->txqueues;
 
			tx = l->data;
 
			free(tx->queue);
 
			free(tx);
 
			yid->txqueues =
 
				y_list_remove_link(yid->txqueues,
 
				yid->txqueues);
 
			y_list_free_1(l);
 
		}
 
		LOG(("yahoo_write_ready(%d, %p) len < 0", id, fd));
 
		YAHOO_CALLBACK(ext_yahoo_remove_handler) (id, yid->write_tag);
 
		yid->write_tag = 0;
 
		errno = e;
 
		return 0;
 
	}
 

	
 
	tx->len -= len;
 
	if (tx->len > 0) {
 
		unsigned char *tmp = y_memdup(tx->queue + len, tx->len);
 
		FREE(tx->queue);
 
		tx->queue = tmp;
 
	} else {
 
		YList *l = yid->txqueues;
 
		free(tx->queue);
 
		free(tx);
 
		yid->txqueues =
 
			y_list_remove_link(yid->txqueues, yid->txqueues);
 
		y_list_free_1(l);
 
		/*
 
		   if(!yid->txqueues) 
 
		   LOG(("yahoo_write_ready(%d, %d) !yxqueues", id, fd));
 
		 */
 
		if (!yid->txqueues) {
 
			LOG(("yahoo_write_ready(%d, %p) !txqueues", id, fd));
 
			YAHOO_CALLBACK(ext_yahoo_remove_handler) (id,
 
				yid->write_tag);
 
			yid->write_tag = 0;
 
		}
 
	}
 

	
 
	return 1;
 
}
 

	
 
static void yahoo_process_pager_connection(struct yahoo_input_data *yid,
 
	int over)
 
{
 
	struct yahoo_packet *pkt;
 
	struct yahoo_data *yd = yid->yd;
 
	int id = yd->client_id;
 

	
 
	if (over)
 
		return;
 

	
 
	while (find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER)
 
		&& (pkt = yahoo_getdata(yid)) != NULL) {
 

	
 
		yahoo_packet_process(yid, pkt);
 

	
 
		yahoo_packet_free(pkt);
 
	}
 
}
 

	
 
static void yahoo_process_chatcat_connection(struct yahoo_input_data *yid,
 
	int over)
 
{
 
	if (over)
 
		return;
 

	
 
	if (strstr((char *)yid->rxqueue + (yid->rxlen - 20), "</content>")) {
 
		YAHOO_CALLBACK(ext_yahoo_chat_cat_xml) (yid->yd->client_id,
 
			(char *)yid->rxqueue);
 
	}
 
}
 

	
 
static void yahoo_process_yab_connection(struct yahoo_input_data *yid, int over)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	struct yab *yab;
 
	YList *buds;
 
	int changed = 0;
 
	int id = yd->client_id;
 
	int yab_used = 0;
 

	
 
	LOG(("Got data for YAB"));
 

	
 
	if (over)
 
		return;
 

	
 
	while (find_input_by_id_and_type(id, YAHOO_CONNECTION_YAB)
 
		&& (yab = yahoo_getyab(yid)) != NULL) {
 
		if (!yab->id)
 
			continue;
 

	
 
		changed = 1;
 
		yab_used = 0;
 
		for (buds = yd->buddies; buds; buds = buds->next) {
 
			struct yahoo_buddy *bud = buds->data;
 
			if (!strcmp(bud->id, yab->id)) {
 
				yab_used = 1;
 
				bud->yab_entry = yab;
 
				if (yab->nname) {
 
					bud->real_name = strdup(yab->nname);
 
				} else if (yab->fname && yab->lname) {
 
					bud->real_name = y_new0(char,
 
						strlen(yab->fname) +
 
						strlen(yab->lname) + 2);
 
					sprintf(bud->real_name, "%s %s",
 
						yab->fname, yab->lname);
 
				} else if (yab->fname) {
 
					bud->real_name = strdup(yab->fname);
 
				}
 
				break;	/* for */
 
			}
 
		}
 

	
 
		if (!yab_used) {
 
			FREE(yab->fname);
 
			FREE(yab->lname);
 
			FREE(yab->nname);
 
			FREE(yab->id);
 
			FREE(yab->email);
 
			FREE(yab->hphone);
 
			FREE(yab->wphone);
 
			FREE(yab->mphone);
 
			FREE(yab);
 
		}
 

	
 
	}
 

	
 
	if (changed)
 
		YAHOO_CALLBACK(ext_yahoo_got_buddies) (yd->client_id,
 
			yd->buddies);
 
}
 

	
 
static void yahoo_process_search_connection(struct yahoo_input_data *yid,
 
	int over)
 
{
 
	struct yahoo_found_contact *yct = NULL;
 
	char *p = (char *)yid->rxqueue, *np, *cp;
 
	int k, n;
 
	int start = 0, found = 0, total = 0;
 
	YList *contacts = NULL;
 
	struct yahoo_input_data *pyid =
 
		find_input_by_id_and_type(yid->yd->client_id,
 
		YAHOO_CONNECTION_PAGER);
 

	
 
	if (!over || !pyid)
 
		return;
 

	
 
	if (p && (p = strstr(p, "\r\n\r\n"))) {
 
		p += 4;
 

	
 
		for (k = 0; (p = strchr(p, 4)) && (k < 4); k++) {
 
			p++;
 
			n = atoi(p);
 
			switch (k) {
 
			case 0:
 
				found = pyid->ys->lsearch_nfound = n;
 
				break;
 
			case 2:
 
				start = pyid->ys->lsearch_nstart = n;
 
				break;
 
			case 3:
 
				total = pyid->ys->lsearch_ntotal = n;
 
				break;
 
			}
 
		}
 

	
 
		if (p)
 
			p++;
 

	
 
		k = 0;
 
		while (p && *p) {
 
			cp = p;
 
			np = strchr(p, 4);
 

	
 
			if (!np)
 
				break;
 
			*np = 0;
 
			p = np + 1;
 

	
 
			switch (k++) {
 
			case 1:
 
				if (strlen(cp) > 2
 
					&& y_list_length(contacts) < total) {
 
					yct = y_new0(struct yahoo_found_contact,
 
						1);
 
					contacts = y_list_append(contacts, yct);
 
					yct->id = cp + 2;
 
				} else {
 
					*p = 0;
 
				}
 
				break;
 
			case 2:
 
				yct->online = !strcmp(cp, "2") ? 1 : 0;
 
				break;
 
			case 3:
 
				yct->gender = cp;
 
				break;
 
			case 4:
 
				yct->age = atoi(cp);
 
				break;
 
			case 5:
 
				/* not worth the context switch for strcmp */
 
				if (cp[0] != '\005' || cp[1] != '\000')
 
					yct->location = cp;
 
				k = 0;
 
				break;
 
			}
 
		}
 
	}
 

	
 
	YAHOO_CALLBACK(ext_yahoo_got_search_result) (yid->yd->client_id, found,
 
		start, total, contacts);
 

	
 
	while (contacts) {
 
		YList *node = contacts;
 
		contacts = y_list_remove_link(contacts, node);
 
		free(node->data);
 
		y_list_free_1(node);
 
	}
 
}
 

	
 
static void _yahoo_webcam_connected(void *fd, int error, void *d)
 
{
 
	struct yahoo_input_data *yid = d;
 
	struct yahoo_webcam *wcm = yid->wcm;
 
	struct yahoo_data *yd = yid->yd;
 
	char conn_type[100];
 
	char *data = NULL;
 
	char *packet = NULL;
 
	unsigned char magic_nr[] = { 1, 0, 0, 0, 1 };
 
	unsigned header_len = 0;
 
	unsigned int len = 0;
 
	unsigned int pos = 0;
 

	
 
	if (error || !fd) {
 
		FREE(yid);
 
		return;
 
	}
 

	
 
	yid->fd = fd;
 
	inputs = y_list_prepend(inputs, yid);
 

	
 
	LOG(("Connected"));
 
	/* send initial packet */
 
	switch (wcm->direction) {
 
	case YAHOO_WEBCAM_DOWNLOAD:
 
		data = strdup("<REQIMG>");
 
		break;
 
	case YAHOO_WEBCAM_UPLOAD:
 
		data = strdup("<SNDIMG>");
 
		break;
 
	default:
 
		return;
 
	}
 
	yahoo_add_to_send_queue(yid, data, strlen(data));
 
	FREE(data);
 

	
 
	/* send data */
 
	switch (wcm->direction) {
 
	case YAHOO_WEBCAM_DOWNLOAD:
 
		header_len = 8;
 
		data = strdup("a=2\r\nc=us\r\ne=21\r\nu=");
 
		data = y_string_append(data, yd->user);
 
		data = y_string_append(data, "\r\nt=");
 
		data = y_string_append(data, wcm->key);
 
		data = y_string_append(data, "\r\ni=");
 
		data = y_string_append(data, wcm->my_ip);
 
		data = y_string_append(data, "\r\ng=");
 
		data = y_string_append(data, wcm->user);
 
		data = y_string_append(data, "\r\no=w-2-5-1\r\np=");
 
		snprintf(conn_type, sizeof(conn_type), "%d", wcm->conn_type);
 
		data = y_string_append(data, conn_type);
 
		data = y_string_append(data, "\r\n");
 
		break;
 
	case YAHOO_WEBCAM_UPLOAD:
 
		header_len = 13;
 
		data = strdup("a=2\r\nc=us\r\nu=");
 
		data = y_string_append(data, yd->user);
 
		data = y_string_append(data, "\r\nt=");
 
		data = y_string_append(data, wcm->key);
 
		data = y_string_append(data, "\r\ni=");
 
		data = y_string_append(data, wcm->my_ip);
 
		data = y_string_append(data, "\r\no=w-2-5-1\r\np=");
 
		snprintf(conn_type, sizeof(conn_type), "%d", wcm->conn_type);
 
		data = y_string_append(data, conn_type);
 
		data = y_string_append(data, "\r\nb=");
 
		data = y_string_append(data, wcm->description);
 
		data = y_string_append(data, "\r\n");
 
		break;
 
	}
 

	
 
	len = strlen(data);
 
	packet = y_new0(char, header_len + len);
 
	packet[pos++] = header_len;
 
	packet[pos++] = 0;
 
	switch (wcm->direction) {
 
	case YAHOO_WEBCAM_DOWNLOAD:
 
		packet[pos++] = 1;
 
		packet[pos++] = 0;
 
		break;
 
	case YAHOO_WEBCAM_UPLOAD:
 
		packet[pos++] = 5;
 
		packet[pos++] = 0;
 
		break;
 
	}
 

	
 
	pos += yahoo_put32(packet + pos, len);
 
	if (wcm->direction == YAHOO_WEBCAM_UPLOAD) {
 
		memcpy(packet + pos, magic_nr, sizeof(magic_nr));
 
		pos += sizeof(magic_nr);
 
	}
 
	memcpy(packet + pos, data, len);
 
	yahoo_add_to_send_queue(yid, packet, header_len + len);
 
	FREE(packet);
 
	FREE(data);
 

	
 
	yid->read_tag =
 
		YAHOO_CALLBACK(ext_yahoo_add_handler) (yid->yd->client_id,
 
		yid->fd, YAHOO_INPUT_READ, yid);
 
}
 

	
 
static void yahoo_webcam_connect(struct yahoo_input_data *y)
 
{
 
	struct yahoo_webcam *wcm = y->wcm;
 
	struct yahoo_input_data *yid;
 
	struct yahoo_server_settings *yss;
 

	
 
	if (!wcm || !wcm->server || !wcm->key)
 
		return;
 

	
 
	yid = y_new0(struct yahoo_input_data, 1);
 
	yid->type = YAHOO_CONNECTION_WEBCAM;
 
	yid->yd = y->yd;
 

	
 
	/* copy webcam data to new connection */
 
	yid->wcm = y->wcm;
 
	y->wcm = NULL;
 

	
 
	yss = y->yd->server_settings;
 

	
 
	yid->wcd = y_new0(struct yahoo_webcam_data, 1);
 

	
 
	LOG(("Connecting to: %s:%d", wcm->server, wcm->port));
 
	YAHOO_CALLBACK(ext_yahoo_connect_async) (y->yd->client_id, wcm->server,
 
		wcm->port, _yahoo_webcam_connected, yid, 0);
 

	
 
}
 

	
 
static void yahoo_process_webcam_master_connection(struct yahoo_input_data *yid,
 
	int over)
 
{
 
	char *server;
 
	struct yahoo_server_settings *yss;
 

	
 
	if (over)
 
		return;
 

	
 
	server = yahoo_getwebcam_master(yid);
 

	
 
	if (server) {
 
		yss = yid->yd->server_settings;
 
		yid->wcm->server = strdup(server);
 
		yid->wcm->port = yss->webcam_port;
 
		yid->wcm->conn_type = yss->conn_type;
 
		yid->wcm->my_ip = strdup(yss->local_host);
 
		if (yid->wcm->direction == YAHOO_WEBCAM_UPLOAD)
 
			yid->wcm->description = strdup(yss->webcam_description);
 
		yahoo_webcam_connect(yid);
 
		FREE(server);
 
	}
 
}
 

	
 
static void yahoo_process_webcam_connection(struct yahoo_input_data *yid,
 
	int over)
 
{
 
	int id = yid->yd->client_id;
 
	void *fd = yid->fd;
 

	
 
	if (over)
 
		return;
 

	
 
	/* as long as we still have packets available keep processing them */
 
	while (find_input_by_id_and_fd(id, fd)
 
		&& yahoo_get_webcam_data(yid) == 1) ;
 
}
 

	
 
static void yahoo_process_auth_connection(struct yahoo_input_data *yid,
 
	int over)
 
{
 
	char *line_end;
 
	char *token;
 
	char *cookie;
 

	
 
	int error_code = 0;
 
	int is_ymsgr = 0;
 

	
 
	/* Wait till we get everything */
 
	if (!over)
 
		return;
 

	
 
	if (!yid->rxqueue) {
 
		LOG(("Whoops! Closed it just like that??\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
	}
 

	
 
	token = strstr((char *)yid->rxqueue, "\r\n\r\n");
 

	
 
	if (!token) {
 
		LOG(("Could not find anything after the HTTP headers? huh?\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
	}
 

	
 
	*token = '\0';
 
	token += 4;
 

	
 
	/* Skip the first number */
 
	token = strstr(token, "\r\n");
 

	
 
	if (!token) {
 
		LOG(("Well I tried to skip the first number and found... nothing\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
	}
 

	
 
	token += 2;
 

	
 
	line_end = strstr(token, "\r\n");
 

	
 
	if (line_end) {
 
		*line_end = '\0';
 

	
 
		line_end += 2;
 
	}
 

	
 
	error_code = atoi((char *)token);
 

	
 
	switch (error_code) {
 
	case 0:
 
		/* successful */
 
		break;
 
	case 1212:
 
		/* Incorrect ID or password */
 
		LOG(("Incorrect ID or password\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_PASSWD, NULL);
 

	
 
		return;
 
	case 1213:
 
		/* Security lock from too many failed login attempts */
 
		LOG(("Security lock from too many failed login attempts\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_LOCK, "");
 

	
 
		return;
 

	
 
	case 1214:
 
		/* Security lock */
 
		LOG(("Security lock\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_LOCK, "");
 

	
 
		return;
 

	
 
	case 1235:
 
		/* User ID not taken yet */
 
		LOG(("User ID not taken yet\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNAME, NULL);
 

	
 
		return;
 

	
 
	case 1216:
 
		/* Seems to be a lock, but shows the same generic User ID/Password failure */
 
		LOG(("Seems to be a lock, but shows the same generic User ID/Password failure\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_PASSWD, NULL);
 

	
 
		return;
 
	case 100:
 
		/* Username and password cannot be blank */
 
		LOG(("Username and password cannot be blank\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_PASSWD, NULL);
 

	
 
		return;
 
	default:
 
		/* Unknown error code */
 
		LOG(("Unknown Error\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 

	
 
		return;
 
	}
 

	
 
	if (line_end && !strncmp(line_end, "ymsgr=", 6)) {
 
		is_ymsgr = 1;
 
	} else if (strncmp(line_end, "crumb=", 6)) {
 
		LOG(("Oops! There was no ymsgr=. Where do I get my token from now :("));
 
		LOG(("I got this:\n\n%s\n", line_end));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
		/* Error */
 
	}
 

	
 
	token = line_end + 6;
 

	
 
	line_end = strstr(token, "\r\n");
 

	
 
	if (line_end) {
 
		*line_end = '\0';
 
		line_end += 2;
 
	}
 

	
 
	/* Go for the crumb */
 
	if (is_ymsgr) {
 
		char url[256];
 
		char *token_enc;
 
		struct yahoo_input_data *crumb_yid =
 
			y_new0(struct yahoo_input_data, 1);
 

	
 
		crumb_yid->yd = yid->yd;
 
		crumb_yid->type = YAHOO_CONNECTION_AUTH;
 

	
 
		inputs = y_list_prepend(inputs, crumb_yid);
 

	
 
		token_enc = yahoo_urlencode(token);
 

	
 
		snprintf(url, sizeof(url),
 
			"https://login.yahoo.com/config/pwtoken_login?src=ymsgr&ts="
 
			"&token=%s", token_enc);
 

	
 
		yahoo_http_get(crumb_yid->yd->client_id, url, NULL, 1, 0,
 
			_yahoo_http_connected, crumb_yid);
 

	
 
		FREE(token_enc);
 

	
 
		return;
 
	}
 

	
 
	/* token is actually crumb */
 

	
 
	if (!line_end) {
 
		/* We did not get our cookies. Cry. */
 
		LOG(("NO Cookies!"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
	}
 

	
 
	cookie = strstr((char *)yid->rxqueue, "Set-Cookie: Y=");
 

	
 
	if (!cookie) {
 
		/* Cry. */
 
		LOG(("NO Y Cookie!"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
	}
 

	
 
	cookie += 14;
 

	
 
	line_end = strstr(cookie, "\r\n");
 
	*line_end = '\0';
 

	
 
	yid->yd->cookie_y = strdup(cookie);
 
	*line_end = ';';
 

	
 
	cookie = strstr((char *)yid->rxqueue, "Set-Cookie: T=");
 
	if (!cookie) {
 
		/* Cry. */
 
		LOG(("NO T Cookie!"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
	}
 

	
 
	cookie += 14;
 

	
 
	line_end = strstr(cookie, "\r\n");
 
	*line_end = '\0';
 

	
 
	yid->yd->cookie_t = strdup(cookie);
 
	*line_end = ';';
 

	
 
	cookie = strstr((char *)yid->rxqueue, "Set-Cookie: B=");
 
	if (!cookie) {
 
		/* Cry. */
 
		LOG(("NO B Cookie!"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
	}
 

	
 
	cookie += 14;
 

	
 
	line_end = strstr(cookie, "\r\n");
 
	*line_end = '\0';
 

	
 
	yid->yd->cookie_b = strdup(cookie);
 

	
 
	yid->yd->crumb = strdup(token);
 

	
 
	yahoo_send_auth(yid->yd);
 
}
 

	
 
static void (*yahoo_process_connection[]) (struct yahoo_input_data *,
 
	int over) = {
 
yahoo_process_pager_connection, yahoo_process_ft_connection,
 
		yahoo_process_yab_connection,
 
		yahoo_process_webcam_master_connection,
 
		yahoo_process_webcam_connection,
 
		yahoo_process_chatcat_connection,
 
		yahoo_process_search_connection, yahoo_process_auth_connection};
 

	
 
int yahoo_read_ready(int id, void *fd, void *data)
 
{
 
	struct yahoo_input_data *yid = data;
 
	char buf[1024];
 
	int len;
 

	
 
	LOG(("read callback: id=%d fd=%p data=%p", id, fd, data));
 
	if (!yid)
 
		return -2;
 

	
 
	do {
 
		len = YAHOO_CALLBACK(ext_yahoo_read) (fd, buf, sizeof(buf));
 
	} while (len == -1 && errno == EINTR);
 

	
 
	if (len == -1 && (errno == EAGAIN || errno == EINTR))	/* we'll try again later */
 
		return 1;
 

	
 
	if (len <= 0) {
 
		int e = errno;
 
		DEBUG_MSG(("len == %d (<= 0)", len));
 

	
 
		if (yid->type == YAHOO_CONNECTION_PAGER) {
 
			YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->
 
				client_id, YAHOO_LOGIN_SOCK, NULL);
 
		}
 

	
 
		yahoo_process_connection[yid->type] (yid, 1);
 
		yahoo_input_close(yid);
 

	
 
		/* no need to return an error, because we've already fixed it */
 
		if (len == 0)
 
			return 1;
 

	
 
		errno = e;
 
		LOG(("read error: %s", strerror(errno)));
 
		return -1;
 
	}
 

	
 
	yid->rxqueue =
 
		y_renew(unsigned char, yid->rxqueue, len + yid->rxlen + 1);
 
	memcpy(yid->rxqueue + yid->rxlen, buf, len);
 
	yid->rxlen += len;
 
	yid->rxqueue[yid->rxlen] = 0;
 

	
 
	yahoo_process_connection[yid->type] (yid, 0);
 

	
 
	return len;
 
}
 

	
 
int yahoo_init_with_attributes(const char *username, const char *password, ...)
 
{
 
	va_list ap;
 
	struct yahoo_data *yd;
 

	
 
	yd = y_new0(struct yahoo_data, 1);
 

	
 
	if (!yd)
 
		return 0;
 

	
 
	yd->user = strdup(username);
 
	yd->password = strdup(password);
 

	
 
	yd->initial_status = -1;
 
	yd->current_status = -1;
 

	
 
	yd->client_id = ++last_id;
 

	
 
	add_to_list(yd);
 

	
 
	va_start(ap, password);
 
	yd->server_settings = _yahoo_assign_server_settings(ap);
 
	va_end(ap);
 

	
 
	return yd->client_id;
 
}
 

	
 
int yahoo_init(const char *username, const char *password)
 
{
 
	return yahoo_init_with_attributes(username, password, NULL);
 
}
 

	
 
static void yahoo_connected(void *fd, int error, void *data)
 
{
 
	struct connect_callback_data *ccd = data;
 
	struct yahoo_data *yd = ccd->yd;
 
	struct yahoo_packet *pkt;
 
	struct yahoo_input_data *yid;
 
	struct yahoo_server_settings *yss = yd->server_settings;
 

	
 
	if (error) {
 
		int tag;
 
		if (fallback_ports[ccd->i]) {
 
			char *host = yss->pager_host;
 

	
 
			if (!host)
 
				host = yss->pager_host_list[ccd->server_i];
 

	
 
			yss->pager_port = fallback_ports[ccd->i++];
 
			tag = YAHOO_CALLBACK(ext_yahoo_connect_async) (yd->
 
				client_id, host, yss->pager_port,
 
				yahoo_connected, ccd, 0);
 

	
 
			if (tag > 0)
 
				ccd->tag = tag;
 
		} else if (yss->pager_host_list
 
				&& yss->pager_host_list[ccd->server_i]) {
 

	
 
			/* Get back to the default port */
 
			yss->pager_port = pager_port;
 
			ccd->server_i++;
 
			LOG(("Fallback: Connecting to %s:%d", yss->pager_host_list[ccd->server_i], yss->pager_port));
 

	
 
			ccd->i = 0;
 
			tag = YAHOO_CALLBACK(ext_yahoo_connect_async) (yd->client_id,
 
				yss->pager_host_list[ccd->server_i], yss->pager_port,
 
				yahoo_connected, ccd, 0);
 
		} else {
 
			FREE(ccd);
 
			YAHOO_CALLBACK(ext_yahoo_login_response) (yd->client_id,
 
				YAHOO_LOGIN_SOCK, NULL);
 
		}
 
		return;
 
	}
 

	
 
	FREE(ccd);
 

	
 
	/* fd == NULL && error == 0 means connect was cancelled */
 
	if (!fd)
 
		return;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_AUTH, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 
	NOTICE(("Sending initial packet"));
 

	
 
	yahoo_packet_hash(pkt, 1, yd->user);
 

	
 
	yid = find_input_by_id_and_type(yd->client_id, YAHOO_CONNECTION_PAGER);
 
	yid->fd = fd;
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 

	
 
	yid->read_tag =
 
		YAHOO_CALLBACK(ext_yahoo_add_handler) (yid->yd->client_id,
 
		yid->fd, YAHOO_INPUT_READ, yid);
 
}
 

	
 
void *yahoo_get_fd(int id)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	if (!yid)
 
		return 0;
 
	else
 
		return yid->fd;
 
}
 

	
 
void yahoo_send_buzz(int id, const char *from, const char *who)
 
{
 
	yahoo_send_im(id, from, who, "<ding>", 1, 0);
 
}
 

	
 
void yahoo_send_im(int id, const char *from, const char *who, const char *what,
 
	int utf8, int picture)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_packet *pkt = NULL;
 
	struct yahoo_data *yd;
 
	char pic_str[10];
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_MESSAGE, YAHOO_STATUS_OFFLINE,
 
		yd->session_id);
 

	
 
	snprintf(pic_str, sizeof(pic_str), "%d", picture);
 

	
 
	if (from && strcmp(from, yd->user))
 
		yahoo_packet_hash(pkt, 0, yd->user);
 
	yahoo_packet_hash(pkt, 1, from ? from : yd->user);
 
	yahoo_packet_hash(pkt, 5, who);
 
	yahoo_packet_hash(pkt, 14, what);
 

	
 
	if (utf8)
 
		yahoo_packet_hash(pkt, 97, "1");
 

	
 
	yahoo_packet_hash(pkt, 63, ";0");	/* imvironment name; or ;0 */
 
	yahoo_packet_hash(pkt, 64, "0");
 
	yahoo_packet_hash(pkt, 206, pic_str);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_send_typing(int id, const char *from, const char *who, int typ)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_NOTIFY, YPACKET_STATUS_NOTIFY,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 5, who);
 
	yahoo_packet_hash(pkt, 1, from ? from : yd->user);
 
	yahoo_packet_hash(pkt, 14, " ");
 
	yahoo_packet_hash(pkt, 13, typ ? "1" : "0");
 
	yahoo_packet_hash(pkt, 49, "TYPING");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_set_away(int id, enum yahoo_status state, const char *msg, int away)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 
	int old_status;
 
	char s[4];
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	old_status = yd->current_status;
 

	
 
	if (msg) {
 
		yd->current_status = YAHOO_STATUS_CUSTOM;
 
	} else {
 
		yd->current_status = state;
 
	}
 

	
 
	/* Thank you libpurple :) */
 
	if (yd->current_status == YAHOO_STATUS_INVISIBLE) {
 
		pkt = yahoo_packet_new(YAHOO_SERVICE_Y6_VISIBLE_TOGGLE,
 
			YAHOO_STATUS_AVAILABLE, 0);
 
		yahoo_packet_hash(pkt, 13, "2");
 
		yahoo_send_packet(yid, pkt, 0);
 
		yahoo_packet_free(pkt);
 

	
 
		return;
 
	}
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_Y6_STATUS_UPDATE,
 
		yd->current_status, yd->session_id);
 
	snprintf(s, sizeof(s), "%d", yd->current_status);
 
	yahoo_packet_hash(pkt, 10, s);
 

	
 
	if (yd->current_status == YAHOO_STATUS_CUSTOM) {
 
		yahoo_packet_hash(pkt, 19, msg);
 
	} else {
 
		yahoo_packet_hash(pkt, 19, "");
 
	}
 

	
 
	yahoo_packet_hash(pkt, 47, (away == 2) ? "2" : (away) ? "1" : "0");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 

	
 
	if (old_status == YAHOO_STATUS_INVISIBLE) {
 
		pkt = yahoo_packet_new(YAHOO_SERVICE_Y6_VISIBLE_TOGGLE,
 
			YAHOO_STATUS_AVAILABLE, 0);
 
		yahoo_packet_hash(pkt, 13, "1");
 
		yahoo_send_packet(yid, pkt, 0);
 
		yahoo_packet_free(pkt);
 
	}
 
}
 

	
 
void yahoo_logoff(int id)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	LOG(("yahoo_logoff: current status: %d", yd->current_status));
 

	
 
	if (yd->current_status != -1) {
 
		pkt = yahoo_packet_new(YAHOO_SERVICE_LOGOFF,
 
			YPACKET_STATUS_DEFAULT, yd->session_id);
 
		yd->current_status = -1;
 

	
 
		if (pkt) {
 
			yahoo_send_packet(yid, pkt, 0);
 
			yahoo_packet_free(pkt);
 
		}
 
	}
 

	
 
/*	do {
 
		yahoo_input_close(yid);
 
	} while((yid = find_input_by_id(id)));*/
 

	
 
}
 

	
 
void yahoo_get_list(int id)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_LIST, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	if (pkt) {
 
		yahoo_send_packet(yid, pkt, 0);
 
		yahoo_packet_free(pkt);
 
	}
 
}
 

	
 
static void _yahoo_http_connected(int id, void *fd, int error, void *data)
 
{
 
	struct yahoo_input_data *yid = data;
 
	if (fd == NULL || error) {
 
		inputs = y_list_remove(inputs, yid);
 
		FREE(yid);
 
		return;
 
	}
 

	
 
	yid->fd = fd;
 
	yid->read_tag =
 
		YAHOO_CALLBACK(ext_yahoo_add_handler) (yid->yd->client_id, fd,
 
		YAHOO_INPUT_READ, yid);
 
}
 

	
 
/* FIXME Get address book from address.yahoo.com instead */
 
void yahoo_get_yab(int id)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	struct yahoo_input_data *yid;
 
	char url[1024];
 
	char buff[2048];
 

	
 
	if (!yd)
 
		return;
 

	
 
	yid = y_new0(struct yahoo_input_data, 1);
 
	yid->yd = yd;
 
	yid->type = YAHOO_CONNECTION_YAB;
 

	
 
	LOG(("Sending request for Address Book"));
 

	
 
	snprintf(url, 1024,
 
		"http://address.yahoo.com/yab/us?v=XM&prog=ymsgr&.intl=us"
 
		"&diffs=1&t=0&tags=short&rt=0&prog-ver=8.1.0.249&useutf8=1&legenc=codepage-1252");
 

	
 
	snprintf(buff, sizeof(buff), "Y=%s; T=%s", yd->cookie_y, yd->cookie_t);
 

	
 
	inputs = y_list_prepend(inputs, yid);
 

	
 
	yahoo_http_get(yid->yd->client_id, url, buff, 0, 0,
 
		_yahoo_http_connected, yid);
 
}
 

	
 
struct yahoo_post_data {
 
	struct yahoo_input_data *yid;
 
	char *data;
 
};
 

	
 
static void _yahoo_http_post_connected(int id, void *fd, int error, void *data)
 
{
 
	struct yahoo_post_data *yad = data;
 
	struct yahoo_input_data *yid = yad->yid;
 
	char *buff = yad->data;
 

	
 
	if (!fd) {
 
		inputs = y_list_remove(inputs, yid);
 
		FREE(yid);
 
		return;
 
	}
 

	
 
	YAHOO_CALLBACK(ext_yahoo_write) (fd, buff, strlen(buff));
 

	
 
	yid->fd = fd;
 
	yid->read_tag =
 
		YAHOO_CALLBACK(ext_yahoo_add_handler) (yid->yd->client_id, fd,
 
		YAHOO_INPUT_READ, yid);
 

	
 
	FREE(buff);
 
	FREE(yad);
 
}
 

	
 
/* FIXME This is also likely affected */
 
void yahoo_set_yab(int id, struct yab *yab)
 
{
 
	struct yahoo_post_data *yad = y_new0(struct yahoo_post_data, 1);
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	struct yahoo_input_data *yid;
 
	char url[1024];
 
	char buff[1024];
 
	char post[1024];
 
	int size = 0;
 

	
 
	if (!yd)
 
		return;
 

	
 
	yid = y_new0(struct yahoo_input_data, 1);
 
	yid->type = YAHOO_CONNECTION_YAB;
 
	yid->yd = yd;
 

	
 
	if(yab->yid)
 
		size = snprintf(post, sizeof(post), "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
 
			"<ab k=\"%s\" cc=\"%d\">"
 
			"<ct id=\"%d\" e=\"1\" yi=\"%s\" nn=\"%s\" />"
 
			"</ab>", yd->user, 9, yab->yid,	/* Don't know why */
 
			yab->id, yab->nname?yab->nname:"");
 
	else
 
		size = snprintf(post, sizeof(post), "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
 
			"<ab k=\"%s\" cc=\"%d\">"
 
			"<ct a=\"1\" yi=\"%s\" nn=\"%s\" />"
 
			"</ab>", yd->user, 1,	/* Don't know why */
 
			yab->id, yab->nname?yab->nname:"");
 

	
 
	yad->yid = yid;
 
	yad->data = strdup(post);
 

	
 
	strcpy(url, "http://address.yahoo.com/yab/us?v=XM&prog=ymsgr&.intl=us"
 
		"&sync=1&tags=short&noclear=1&useutf8=1&legenc=codepage-1252");
 

	
 
	snprintf(buff, sizeof(buff), "Y=%s; T=%s", yd->cookie_y, yd->cookie_t);
 

	
 
	inputs = y_list_prepend(inputs, yid);
 

	
 
	yahoo_http_post(yid->yd->client_id, url, buff, size,
 
		_yahoo_http_post_connected, yad);
 
}
 

	
 
void yahoo_set_identity_status(int id, const char *identity, int active)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(active ? YAHOO_SERVICE_IDACT :
 
		YAHOO_SERVICE_IDDEACT, YPACKET_STATUS_DEFAULT, yd->session_id);
 
	yahoo_packet_hash(pkt, 3, identity);
 
	if (pkt) {
 
		yahoo_send_packet(yid, pkt, 0);
 
		yahoo_packet_free(pkt);
 
	}
 
}
 

	
 
void yahoo_refresh(int id)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_USERSTAT, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 
	if (pkt) {
 
		yahoo_send_packet(yid, pkt, 0);
 
		yahoo_packet_free(pkt);
 
	}
 
}
 

	
 
void yahoo_keepalive(int id)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_PING, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_chat_keepalive(int id)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CHATPING, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_add_buddy(int id, const char *who, const char *group,
 
	const char *msg)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	if (!yd->logged_in)
 
		return;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_ADDBUDDY, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 
	if (msg != NULL)	/* add message/request "it's me add me" */
 
		yahoo_packet_hash(pkt, 14, msg);
 
	else
 
		yahoo_packet_hash(pkt, 14, "");
 
	yahoo_packet_hash(pkt, 65, group);
 
	yahoo_packet_hash(pkt, 97, "1");
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 302, "319");
 
	yahoo_packet_hash(pkt, 300, "319");
 
	yahoo_packet_hash(pkt, 7, who);
 
	yahoo_packet_hash(pkt, 334, "0");
 
	yahoo_packet_hash(pkt, 301, "319");
 
	yahoo_packet_hash(pkt, 303, "319");
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_remove_buddy(int id, const char *who, const char *group)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_REMBUDDY, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 7, who);
 
	yahoo_packet_hash(pkt, 65, group);
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_confirm_buddy(int id, const char *who, int reject, const char *msg)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	if (!yd->logged_in)
 
		return;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_Y7_AUTHORIZATION,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 5, who);
 
	if (reject)
 
		yahoo_packet_hash(pkt, 13, "2");
 
	else {
 
		yahoo_packet_hash(pkt, 241, "0");
 
		yahoo_packet_hash(pkt, 13, "1");
 
	}
 

	
 
	yahoo_packet_hash(pkt, 334, "0");
 

	
 
	if (reject) {
 
		yahoo_packet_hash(pkt, 14, msg ? msg : "");
 
		yahoo_packet_hash(pkt, 97, "1");
 
	}
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_ignore_buddy(int id, const char *who, int unignore)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	if (!yd->logged_in)
 
		return;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_IGNORECONTACT,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 7, who);
 
	yahoo_packet_hash(pkt, 13, unignore ? "2" : "1");
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_stealth_buddy(int id, const char *who, int unstealth)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	if (!yd->logged_in)
 
		return;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_STEALTH_PERM,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 7, who);
 
	yahoo_packet_hash(pkt, 31, unstealth ? "2" : "1");
 
	yahoo_packet_hash(pkt, 13, "2");
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_change_buddy_group(int id, const char *who, const char *old_group,
 
	const char *new_group)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_Y7_CHANGE_GROUP,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 302, "240");
 
	yahoo_packet_hash(pkt, 300, "240");
 
	yahoo_packet_hash(pkt, 7, who);
 
	yahoo_packet_hash(pkt, 224, old_group);
 
	yahoo_packet_hash(pkt, 264, new_group);
 
	yahoo_packet_hash(pkt, 301, "240");
 
	yahoo_packet_hash(pkt, 303, "240");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_group_rename(int id, const char *old_group, const char *new_group)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_GROUPRENAME,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 65, old_group);
 
	yahoo_packet_hash(pkt, 67, new_group);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_conference_addinvite(int id, const char *from, const char *who,
 
	const char *room, const YList *members, const char *msg)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CONFADDINVITE,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 51, who);
 
	yahoo_packet_hash(pkt, 57, room);
 
	yahoo_packet_hash(pkt, 58, msg);
 
	yahoo_packet_hash(pkt, 13, "0");
 
	for (; members; members = members->next) {
 
		yahoo_packet_hash(pkt, 52, (char *)members->data);
 
		yahoo_packet_hash(pkt, 53, (char *)members->data);
 
	}
 
	/* 52, 53 -> other members? */
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_conference_invite(int id, const char *from, YList *who,
 
	const char *room, const char *msg)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CONFINVITE, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 50, yd->user);
 
	for (; who; who = who->next) {
 
		yahoo_packet_hash(pkt, 52, (char *)who->data);
 
	}
 
	yahoo_packet_hash(pkt, 57, room);
 
	yahoo_packet_hash(pkt, 58, msg);
 
	yahoo_packet_hash(pkt, 13, "0");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_conference_logon(int id, const char *from, YList *who,
 
	const char *room)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CONFLOGON, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 3, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 57, room);
 
	for (; who; who = who->next)
 
		yahoo_packet_hash(pkt, 3, (char *)who->data);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_conference_decline(int id, const char *from, YList *who,
 
	const char *room, const char *msg)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CONFDECLINE,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 3, (from ? from : yd->user));
 
	for (; who; who = who->next)
 
		yahoo_packet_hash(pkt, 3, (char *)who->data);
 
	yahoo_packet_hash(pkt, 57, room);
 
	yahoo_packet_hash(pkt, 14, msg);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_conference_logoff(int id, const char *from, YList *who,
 
	const char *room)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CONFLOGOFF, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 3, (from ? from : yd->user));
 
	for (; who; who = who->next)
 
		yahoo_packet_hash(pkt, 3, (char *)who->data);
 

	
 
	yahoo_packet_hash(pkt, 57, room);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_conference_message(int id, const char *from, YList *who,
 
	const char *room, const char *msg, int utf8)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CONFMSG, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 53, (from ? from : yd->user));
 
	for (; who; who = who->next)
 
		yahoo_packet_hash(pkt, 53, (char *)who->data);
 

	
 
	yahoo_packet_hash(pkt, 57, room);
 
	yahoo_packet_hash(pkt, 14, msg);
 

	
 
	if (utf8)
 
		yahoo_packet_hash(pkt, 97, "1");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_get_chatrooms(int id, int chatroomid)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	struct yahoo_input_data *yid;
 
	char url[1024];
 
	char buff[1024];
 

	
 
	if (!yd)
 
		return;
 

	
 
	yid = y_new0(struct yahoo_input_data, 1);
 
	yid->yd = yd;
 
	yid->type = YAHOO_CONNECTION_CHATCAT;
 

	
 
	if (chatroomid == 0) {
 
		snprintf(url, 1024,
 
			"http://insider.msg.yahoo.com/ycontent/?chatcat=0");
 
	} else {
 
		snprintf(url, 1024,
 
			"http://insider.msg.yahoo.com/ycontent/?chatroom_%d=0",
 
			chatroomid);
 
	}
 

	
 
	snprintf(buff, sizeof(buff), "Y=%s; T=%s", yd->cookie_y, yd->cookie_t);
 

	
 
	inputs = y_list_prepend(inputs, yid);
 

	
 
	yahoo_http_get(yid->yd->client_id, url, buff, 0, 0,
 
		_yahoo_http_connected, yid);
 
}
 

	
 
void yahoo_chat_logon(int id, const char *from, const char *room,
 
	const char *roomid)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CHATONLINE, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 109, yd->user);
 
	yahoo_packet_hash(pkt, 6, "abcde");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CHATJOIN, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 104, room);
 
	yahoo_packet_hash(pkt, 129, roomid);
 
	yahoo_packet_hash(pkt, 62, "2");	/* ??? */
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_chat_message(int id, const char *from, const char *room,
 
	const char *msg, const int msgtype, const int utf8)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 
	char buf[2];
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_COMMENT, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 104, room);
 
	yahoo_packet_hash(pkt, 117, msg);
 

	
 
	snprintf(buf, sizeof(buf), "%d", msgtype);
 
	yahoo_packet_hash(pkt, 124, buf);
 

	
 
	if (utf8)
 
		yahoo_packet_hash(pkt, 97, "1");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_chat_logoff(int id, const char *from)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CHATLOGOUT, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_buddyicon_request(int id, const char *who)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 5, who);
 
	yahoo_packet_hash(pkt, 13, "1");
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_send_picture_info(int id, const char *who, const char *url,
 
	int checksum)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 
	char checksum_str[10];
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	snprintf(checksum_str, sizeof(checksum_str), "%d", checksum);
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE, YPACKET_STATUS_DEFAULT,
 
		0);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 4, yd->user);
 
	yahoo_packet_hash(pkt, 5, who);
 
	yahoo_packet_hash(pkt, 13, "2");
 
	yahoo_packet_hash(pkt, 20, url);
 
	yahoo_packet_hash(pkt, 192, checksum_str);
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_send_picture_update(int id, const char *who, int type)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 
	char type_str[10];
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	snprintf(type_str, sizeof(type_str), "%d", type);
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE_UPDATE,
 
		YPACKET_STATUS_DEFAULT, 0);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 5, who);
 
	yahoo_packet_hash(pkt, 206, type_str);
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_send_picture_checksum(int id, const char *who, int checksum)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 
	char checksum_str[10];
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	snprintf(checksum_str, sizeof(checksum_str), "%d", checksum);
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE_CHECKSUM,
 
		YPACKET_STATUS_DEFAULT, 0);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	if (who != 0)
 
		yahoo_packet_hash(pkt, 5, who);
 
	yahoo_packet_hash(pkt, 192, checksum_str);
 
	yahoo_packet_hash(pkt, 212, "1");
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_webcam_close_feed(int id, const char *who)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_webcam_user(id, who);
 

	
 
	if (yid)
 
		yahoo_input_close(yid);
 
}
 

	
 
void yahoo_webcam_get_feed(int id, const char *who)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 

	
 
	/* 
 
	 * add the user to the queue.  this is a dirty hack, since
 
	 * the yahoo server doesn't tell us who's key it's returning,
 
	 * we have to just hope that it sends back keys in the same 
 
	 * order that we request them.
 
	 * The queue is popped in yahoo_process_webcam_key
 
	 */
 
	webcam_queue = y_list_append(webcam_queue, who ? strdup(who) : NULL);
 

	
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_WEBCAM, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	if (who != NULL)
 
		yahoo_packet_hash(pkt, 5, who);
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_webcam_send_image(int id, unsigned char *image, unsigned int length,
 
	unsigned int timestamp)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_WEBCAM);
 
	unsigned char *packet;
 
	unsigned char header_len = 13;
 
	unsigned int pos = 0;
 

	
 
	if (!yid)
 
		return;
 

	
 
	packet = y_new0(unsigned char, header_len);
 

	
 
	packet[pos++] = header_len;
 
	packet[pos++] = 0;
 
	packet[pos++] = 5;	/* version byte?? */
 
	packet[pos++] = 0;
 
	pos += yahoo_put32(packet + pos, length);
 
	packet[pos++] = 2;	/* packet type, image */
 
	pos += yahoo_put32(packet + pos, timestamp);
 
	yahoo_add_to_send_queue(yid, packet, header_len);
 
	FREE(packet);
 

	
 
	if (length)
 
		yahoo_add_to_send_queue(yid, image, length);
 
}
 

	
 
void yahoo_webcam_accept_viewer(int id, const char *who, int accept)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_WEBCAM);
 
	char *packet = NULL;
 
	char *data = NULL;
 
	unsigned char header_len = 13;
 
	unsigned int pos = 0;
 
	unsigned int len = 0;
 

	
 
	if (!yid)
 
		return;
 

	
 
	data = strdup("u=");
 
	data = y_string_append(data, (char *)who);
 
	data = y_string_append(data, "\r\n");
 
	len = strlen(data);
 

	
 
	packet = y_new0(char, header_len + len);
 
	packet[pos++] = header_len;
 
	packet[pos++] = 0;
 
	packet[pos++] = 5;	/* version byte?? */
 
	packet[pos++] = 0;
 
	pos += yahoo_put32(packet + pos, len);
 
	packet[pos++] = 0;	/* packet type */
 
	pos += yahoo_put32(packet + pos, accept);
 
	memcpy(packet + pos, data, len);
 
	FREE(data);
 
	yahoo_add_to_send_queue(yid, packet, header_len + len);
 
	FREE(packet);
 
}
 

	
 
void yahoo_webcam_invite(int id, const char *who)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_NOTIFY, YPACKET_STATUS_NOTIFY,
 
		yid->yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 49, "WEBCAMINVITE");
 
	yahoo_packet_hash(pkt, 14, " ");
 
	yahoo_packet_hash(pkt, 13, "0");
 
	yahoo_packet_hash(pkt, 1, yid->yd->user);
 
	yahoo_packet_hash(pkt, 5, who);
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
static void yahoo_search_internal(int id, int t, const char *text, int g,
 
	int ar, int photo, int yahoo_only, int startpos, int total)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	struct yahoo_input_data *yid;
 
	char url[1024];
 
	char buff[1024];
 
	char *ctext, *p;
 

	
 
	if (!yd)
 
		return;
 

	
 
	yid = y_new0(struct yahoo_input_data, 1);
 
	yid->yd = yd;
 
	yid->type = YAHOO_CONNECTION_SEARCH;
 

	
 
	/*
 
	   age range
 
	   .ar=1 - 13-18, 2 - 18-25, 3 - 25-35, 4 - 35-50, 5 - 50-70, 6 - 70+
 
	 */
 

	
 
	snprintf(buff, sizeof(buff), "&.sq=%%20&.tt=%d&.ss=%d", total,
 
		startpos);
 

	
 
	ctext = strdup(text);
 
	while ((p = strchr(ctext, ' ')))
 
		*p = '+';
 

	
 
	snprintf(url, 1024,
 
		"http://members.yahoo.com/interests?.oc=m&.kw=%s&.sb=%d&.g=%d&.ar=0%s%s%s",
 
		ctext, t, g, photo ? "&.p=y" : "", yahoo_only ? "&.pg=y" : "",
 
		startpos ? buff : "");
 

	
 
	FREE(ctext);
 

	
 
	snprintf(buff, sizeof(buff), "Y=%s; T=%s", yd->cookie_y, yd->cookie_t);
 

	
 
	inputs = y_list_prepend(inputs, yid);
 
	yahoo_http_get(yid->yd->client_id, url, buff, 0, 0,
 
		_yahoo_http_connected, yid);
 
}
 

	
 
void yahoo_search(int id, enum yahoo_search_type t, const char *text,
 
	enum yahoo_search_gender g, enum yahoo_search_agerange ar, int photo,
 
	int yahoo_only)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_search_state *yss;
 

	
 
	if (!yid)
 
		return;
 

	
 
	if (!yid->ys)
 
		yid->ys = y_new0(struct yahoo_search_state, 1);
 

	
 
	yss = yid->ys;
 

	
 
	FREE(yss->lsearch_text);
 
	yss->lsearch_type = t;
 
	yss->lsearch_text = strdup(text);
 
	yss->lsearch_gender = g;
 
	yss->lsearch_agerange = ar;
 
	yss->lsearch_photo = photo;
 
	yss->lsearch_yahoo_only = yahoo_only;
 

	
 
	yahoo_search_internal(id, t, text, g, ar, photo, yahoo_only, 0, 0);
 
}
 

	
 
void yahoo_search_again(int id, int start)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_search_state *yss;
 

	
 
	if (!yid || !yid->ys)
 
		return;
 

	
 
	yss = yid->ys;
 

	
 
	if (start == -1)
 
		start = yss->lsearch_nstart + yss->lsearch_nfound;
 

	
 
	yahoo_search_internal(id, yss->lsearch_type, yss->lsearch_text,
 
		yss->lsearch_gender, yss->lsearch_agerange,
 
		yss->lsearch_photo, yss->lsearch_yahoo_only,
 
		start, yss->lsearch_ntotal);
 
}
 

	
 
void yahoo_send_picture(int id, const char *name, unsigned long size,
 
	yahoo_get_fd_callback callback, void *data)
 
{
 
	/* Not Implemented */
 
}
 

	
 
/* File Transfer */
 
static YList *active_file_transfers = NULL;
 

	
 
enum {
 
	FT_STATE_HEAD = 1,
 
	FT_STATE_RECV,
 
	FT_STATE_RECV_START,
 
	FT_STATE_SEND
 
};
 

	
 
struct send_file_data {
 
	int client_id;
 
	char *id;
 
	char *who;
 
	char *filename;
 
	char *ip_addr;
 
	char *token;
 
	int size;
 

	
 
	struct yahoo_input_data *yid;
 
	int state;
 

	
 
	yahoo_get_fd_callback callback;
 
	void *data;
 
};
 

	
 
static char *yahoo_get_random(void)
 
{
 
	int i = 0;
 
	int r = 0;
 
	int c = 0;
 
	char out[25];
 

	
 
	out[24] = '\0';
 
	out[23] = '$';
 
	out[22] = '$';
 
	
 
	for (i = 0; i < 22; i++) {
 
		if(r == 0)
 
			r = rand();
 

	
 
		c = r%61;
 

	
 
		if(c<26)
 
			out[i] = c + 'a';
 
		else if (c<52)
 
			out[i] = c - 26 + 'A';
 
		else
 
			out[i] = c - 52 + '0';
 

	
 
		r /= 61;
 
	}
 

	
 
	return strdup(out);
 
}
 

	
 
static int _are_same_id(const void *sfd1, const void *id)
 
{
 
	return strcmp(((struct send_file_data *)sfd1)->id, (char *)id);
 
}
 

	
 
static int _are_same_yid(const void *sfd1, const void *yid)
 
{
 
	if(((struct send_file_data *)sfd1)->yid == yid)
 
		return 0;
 
	else
 
		return 1;
 
}
 

	
 
static struct send_file_data *yahoo_get_active_transfer(char *id)
 
{
 
	YList *l = y_list_find_custom(active_file_transfers, id,
 
		_are_same_id);
 

	
 
	if(l)
 
		return (struct send_file_data *)l->data;
 
	
 
	return NULL;
 
}
 

	
 
static struct send_file_data *yahoo_get_active_transfer_with_yid(void *yid)
 
{
 
	YList *l = y_list_find_custom(active_file_transfers, yid,
 
		_are_same_yid);
 

	
 
	if(l)
 
		return (struct send_file_data *)l->data;
 
	
 
	return NULL;
 
}
 

	
 
static void yahoo_add_active_transfer(struct send_file_data *sfd)
 
{
 
	active_file_transfers = y_list_prepend(active_file_transfers, sfd);
 
}
 

	
 
static void yahoo_remove_active_transfer(struct send_file_data *sfd)
 
{
 
	active_file_transfers = y_list_remove(active_file_transfers, sfd);
 
	free(sfd->id);
 
	free(sfd->who);
 
	free(sfd->filename);
 
	free(sfd->ip_addr);
 
	FREE(sfd);
 
}
 

	
 
static void _yahoo_ft_upload_connected(int id, void *fd, int error, void *data)
 
{
 
	struct send_file_data *sfd = data;
 
	struct yahoo_input_data *yid = sfd->yid;
 

	
 
	if (!fd) {
 
		inputs = y_list_remove(inputs, yid);
 
		FREE(yid);
 
		return;
 
	}
 

	
 
	sfd->callback(id, fd, error, sfd->data);
 

	
 
	yid->fd = fd;
 
	yid->read_tag =
 
		YAHOO_CALLBACK(ext_yahoo_add_handler) (yid->yd->client_id, fd,
 
		YAHOO_INPUT_READ, yid);
 
}
 

	
 
static void yahoo_file_transfer_upload(struct yahoo_data *yd,
 
	struct send_file_data *sfd)
 
{
 
	char url[256];
 
	char buff[4096];
 
	char *sender_enc = NULL, *recv_enc = NULL, *token_enc = NULL;
 

	
 
	struct yahoo_input_data *yid = y_new0(struct yahoo_input_data, 1);
 

	
 
	yid->yd = yd;
 
	yid->type = YAHOO_CONNECTION_FT;
 

	
 
	inputs = y_list_prepend(inputs, yid);
 
	sfd->yid = yid;
 
	sfd->state = FT_STATE_SEND;
 

	
 
	token_enc = yahoo_urlencode(sfd->token);
 
	sender_enc = yahoo_urlencode(yd->user);
 
	recv_enc = yahoo_urlencode(sfd->who);
 

	
 
	snprintf(url, sizeof(url), 
 
		"http://%s/relay?token=%s&sender=%s&recver=%s", sfd->ip_addr,
 
		token_enc, sender_enc, recv_enc);
 

	
 
	snprintf(buff, sizeof(buff), "T=%s; Y=%s", yd->cookie_t, yd->cookie_y);
 

	
 
	yahoo_http_post(yd->client_id, url, buff, sfd->size,
 
		_yahoo_ft_upload_connected, sfd);
 

	
 
	FREE(token_enc);
 
	FREE(sender_enc);
 
	FREE(recv_enc);
 
}
 

	
 
static void yahoo_init_ft_recv(struct yahoo_data *yd,
 
	struct send_file_data *sfd)
 
{
 
	char url[256];
 
	char buff[1024];
 
	char *sender_enc = NULL, *recv_enc = NULL, *token_enc = NULL;
 

	
 
	struct yahoo_input_data *yid = y_new0(struct yahoo_input_data, 1);
 

	
 
	yid->yd = yd;
 
	yid->type = YAHOO_CONNECTION_FT;
 

	
 
	inputs = y_list_prepend(inputs, yid);
 
	sfd->yid = yid;
 
	sfd->state = FT_STATE_HEAD;
 

	
 
	token_enc = yahoo_urlencode(sfd->token);
 
	sender_enc = yahoo_urlencode(sfd->who);
 
	recv_enc = yahoo_urlencode(yd->user);
 

	
 
	snprintf(url, sizeof(url), 
 
		"http://%s/relay?token=%s&sender=%s&recver=%s", sfd->ip_addr,
 
		token_enc, sender_enc, recv_enc);
 

	
 
	snprintf(buff, sizeof(buff), "Y=%s; T=%s", yd->cookie_y, yd->cookie_t);
 

	
 
	yahoo_http_head(yid->yd->client_id, url, buff, 0, NULL,
 
		_yahoo_http_connected, yid);
 

	
 
	FREE(token_enc);
 
	FREE(sender_enc);
 
	FREE(recv_enc);
 
}
 

	
 
static void yahoo_file_transfer_accept(struct yahoo_input_data *yid,
 
	struct send_file_data *sfd)
 
{
 
	struct yahoo_packet *pkt;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_Y7_FILETRANSFERACCEPT,
 
		YPACKET_STATUS_DEFAULT, yid->yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, yid->yd->user);
 
	yahoo_packet_hash(pkt, 5, sfd->who);
 
	yahoo_packet_hash(pkt, 265, sfd->id);
 
	yahoo_packet_hash(pkt, 27, sfd->filename);
 
	yahoo_packet_hash(pkt, 249, "3");
 
	yahoo_packet_hash(pkt, 251, sfd->token);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 

	
 
	yahoo_init_ft_recv(yid->yd, sfd);
 
}
 

	
 
static void yahoo_process_filetransferaccept(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	YList *l;
 
	struct send_file_data *sfd;
 
	char *who = NULL;
 
	char *filename = NULL;
 
	char *id = NULL;
 
	char *token = NULL;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		switch (pair->key) {
 
		case 4:
 
			who = pair->value;
 
			break;
 
		case 5:
 
			/* Me... don't care */
 
			break;
 
		case 249:
 
			break;
 
		case 265:
 
			id = pair->value;
 
			break;
 
		case 251:
 
			token = pair->value;
 
			break;
 
		case 27:
 
			filename = pair->value;
 
			break;
 
		}
 
	}
 

	
 
	sfd = yahoo_get_active_transfer(id);
 

	
 
	if (sfd) {
 
		sfd->token = strdup(token);
 

	
 
		yahoo_file_transfer_upload(yid->yd, sfd);
 
	}
 
	else {
 
		YAHOO_CALLBACK(ext_yahoo_file_transfer_done)
 
			(yid->yd->client_id, YAHOO_FILE_TRANSFER_UNKNOWN,
 
			sfd->data);
 

	
 
		yahoo_remove_active_transfer(sfd);
 
	}
 
}
 

	
 
static void yahoo_process_filetransferinfo(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	YList *l;
 
	char *who = NULL;
 
	char *filename = NULL;
 
	char *id = NULL;
 
	char *token = NULL;
 
	char *ip_addr = NULL;
 

	
 
	struct send_file_data *sfd;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		switch (pair->key) {
 
		case 1:
 
		case 4:
 
			who = pair->value;
 
			break;
 
		case 5:
 
			/* Me... don't care */
 
			break;
 
		case 249:
 
			break;
 
		case 265:
 
			id = pair->value;
 
			break;
 
		case 250:
 
			ip_addr = pair->value;
 
			break;
 
		case 251:
 
			token = pair->value;
 
			break;
 
		case 27:
 
			filename = pair->value;
 
			break;
 
		}
 
	}
 

	
 
	sfd = yahoo_get_active_transfer(id);
 

	
 
	if (sfd) {
 
		sfd->token = strdup(token);
 
		sfd->ip_addr = strdup(ip_addr);
 

	
 
		yahoo_file_transfer_accept(yid, sfd);
 
	}
 
	else {
 
		YAHOO_CALLBACK(ext_yahoo_file_transfer_done)
 
			(yid->yd->client_id, YAHOO_FILE_TRANSFER_UNKNOWN,
 
			sfd->data);
 

	
 
		yahoo_remove_active_transfer(sfd);
 
	}
 
}
 

	
 
static void yahoo_send_filetransferinfo(struct yahoo_data *yd,
 
	struct send_file_data *sfd)
 
{
 
	struct yahoo_input_data *yid;
 
	struct yahoo_packet *pkt;
 

	
 
	yid = find_input_by_id_and_type(yd->client_id, YAHOO_CONNECTION_PAGER);
 
	sfd->ip_addr = YAHOO_CALLBACK(ext_yahoo_get_ip_addr)("relay.yahoo.com");
 

	
 
	if (!sfd->ip_addr) {
 
		YAHOO_CALLBACK(ext_yahoo_file_transfer_done)
 
			(yd->client_id, YAHOO_FILE_TRANSFER_RELAY, sfd->data);
 

	
 
		yahoo_remove_active_transfer(sfd);
 

	
 
		return;
 
	}
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_Y7_FILETRANSFERINFO,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 5, sfd->who);
 
	yahoo_packet_hash(pkt, 265, sfd->id);
 
	yahoo_packet_hash(pkt, 27, sfd->filename);
 
	yahoo_packet_hash(pkt, 249, "3");
 
	yahoo_packet_hash(pkt, 250, sfd->ip_addr);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
static void yahoo_process_filetransfer(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	YList *l;
 
	char *who = NULL;
 
	char *filename = NULL;
 
	char *msg = NULL;
 
	char *id = NULL;
 
	int action = 0;
 
	int size = 0;
 
	struct yahoo_data *yd = yid->yd;
 

	
 
	struct send_file_data *sfd;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		switch (pair->key) {
 
		case 4:
 
			who = pair->value;
 
			break;
 
		case 5:
 
			/* Me... don't care */
 
			break;
 
		case 222:
 
			action = atoi(pair->value);
 
			break;
 
		case 265:
 
			id = pair->value;
 
			break;
 
		case 266: /* Don't know */
 
			break;
 
		case 302: /* Start Data? */
 
			break;
 
		case 300:
 
			break;
 
		case 27:
 
			filename = pair->value;
 
			break;
 
		case 28:
 
			size = atoi(pair->value);
 
			break;
 
		case 14:
 
			msg = pair->value;
 
		case 301: /* End Data? */
 
			break;
 
		case 303:
 
			break;
 

	
 
		}
 
	}
 

	
 
	if (action == YAHOO_FILE_TRANSFER_INIT) {
 
		/* Received a FT request from buddy */
 
		sfd = y_new0(struct send_file_data, 1);
 
        
 
		sfd->client_id = yd->client_id;
 
		sfd->id = strdup(id);
 
		sfd->who = strdup(who);
 
		sfd->filename = strdup(filename);
 
		sfd->size = size;
 
        
 
		yahoo_add_active_transfer(sfd);
 

	
 
		YAHOO_CALLBACK(ext_yahoo_got_file) (yd->client_id, yd->user,
 
			who, msg, filename, size, sfd->id);
 
	}
 
	else {
 
		/* Response to our request */
 
		sfd = yahoo_get_active_transfer(id);
 

	
 
		if (sfd && action == YAHOO_FILE_TRANSFER_ACCEPT) {
 
			yahoo_send_filetransferinfo(yd, sfd);
 
		}
 
		else if (!sfd || action == YAHOO_FILE_TRANSFER_REJECT) {
 
			YAHOO_CALLBACK(ext_yahoo_file_transfer_done)
 
				(yd->client_id, YAHOO_FILE_TRANSFER_REJECT,
 
				sfd->data);
 

	
 
			yahoo_remove_active_transfer(sfd);
 
		}
 
	}
 
}
 

	
 
void yahoo_send_file(int id, const char *who, const char *msg,
 
	const char *name, unsigned long size,
 
	yahoo_get_fd_callback callback, void *data)
 
{
 
	struct yahoo_packet *pkt = NULL;
 
	char size_str[10];
 
	struct yahoo_input_data *yid;
 
	struct yahoo_data *yd;
 
	struct send_file_data *sfd;
 
	
 
	yid = find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	yd = find_conn_by_id(id);
 
	sfd = y_new0(struct send_file_data, 1);
 

	
 
	sfd->client_id = id;
 
	sfd->id = yahoo_get_random();
 
	sfd->who = strdup(who);
 
	sfd->filename = strdup(name);
 
	sfd->size = size;
 
	sfd->callback = callback;
 
	sfd->data = data;
 

	
 
	yahoo_add_active_transfer(sfd);
 

	
 
	if (!yd)
 
		return;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_Y7_FILETRANSFER,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 

	
 
	snprintf(size_str, sizeof(size_str), "%ld", size);
 

	
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 5, who);
 
	yahoo_packet_hash(pkt, 265, sfd->id);
 
	yahoo_packet_hash(pkt, 222, "1");
 
	yahoo_packet_hash(pkt, 266, "1");
 
	yahoo_packet_hash(pkt, 302, "268");
 
	yahoo_packet_hash(pkt, 300, "268");
 
	yahoo_packet_hash(pkt, 27, name);
 
	yahoo_packet_hash(pkt, 28, size_str);
 
	yahoo_packet_hash(pkt, 301, "268");
 
	yahoo_packet_hash(pkt, 303, "268");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_send_file_transfer_response(int client_id, int response, char *id, void *data)
 
{
 
	struct yahoo_packet *pkt = NULL;
 
	char resp[2];
 
	struct yahoo_input_data *yid;
 
	
 
	struct send_file_data *sfd = yahoo_get_active_transfer(id);
 

	
 
	sfd->data = data;
 

	
 
	yid = find_input_by_id_and_type(client_id, YAHOO_CONNECTION_PAGER);
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_Y7_FILETRANSFER,
 
		YPACKET_STATUS_DEFAULT, yid->yd->session_id);
 

	
 
	snprintf(resp, sizeof(resp), "%d", response);
 

	
 
	yahoo_packet_hash(pkt, 1, yid->yd->user);
 
	yahoo_packet_hash(pkt, 5, sfd->who);
 
	yahoo_packet_hash(pkt, 265, sfd->id);
 
	yahoo_packet_hash(pkt, 222, resp);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 

	
 
	if(response == YAHOO_FILE_TRANSFER_REJECT)
 
		yahoo_remove_active_transfer(sfd);
 
}
 

	
 
static void yahoo_process_ft_connection(struct yahoo_input_data *yid, int over)
 
{
 
	struct send_file_data *sfd;
 
	struct yahoo_data *yd = yid->yd;
 
	
 
	sfd = yahoo_get_active_transfer_with_yid(yid);
 

	
 
	if (!sfd) {
 
		LOG(("Something funny happened. yid %p has no sfd.\n", yid));
 
		return;
 
	}
 

	
 
	/* 
 
	 * We want to handle only the complete data with HEAD since we don't
 
	 * want a situation where both the GET and HEAD are active.
 
	 * With SEND, we really can't do much with partial response
 
	 */
 
	if ((sfd->state == FT_STATE_HEAD || sfd->state == FT_STATE_SEND) 
 
			&& !over)
 
		return;
 

	
 
	if (sfd->state == FT_STATE_HEAD) {
 
		/* Do a GET */
 
		char url[256];
 
		char buff[1024];
 
		char *sender_enc = NULL, *recv_enc = NULL, *token_enc = NULL;
 

	
 
		struct yahoo_input_data *yid_ft = 
 
			y_new0(struct yahoo_input_data, 1);
 
        
 
		yid_ft->yd = yid->yd;
 
		yid_ft->type = YAHOO_CONNECTION_FT;
 
        
 
		inputs = y_list_prepend(inputs, yid_ft);
 
		sfd->yid = yid_ft;
 
		sfd->state = FT_STATE_RECV;
 

	
 
		token_enc = yahoo_urlencode(sfd->token);
 
		sender_enc = yahoo_urlencode(sfd->who);
 
		recv_enc = yahoo_urlencode(yd->user);
 
        
 
		snprintf(url, sizeof(url), 
 
			"http://%s/relay?token=%s&sender=%s&recver=%s", sfd->ip_addr,
 
			token_enc, sender_enc, recv_enc);
 

	
 
		snprintf(buff, sizeof(buff), "Y=%s; T=%s", yd->cookie_y,
 
			yd->cookie_t);
 

	
 

	
 
		yahoo_http_get(yd->client_id, url, buff, 1, 1,
 
			_yahoo_http_connected, yid_ft);
 

	
 
		FREE(token_enc);
 
		FREE(sender_enc);
 
		FREE(recv_enc);
 
	}
 
	else if (sfd->state == FT_STATE_RECV || 
 
		sfd->state == FT_STATE_RECV_START) {
 

	
 
		unsigned char *data_begin = NULL;
 

	
 
		if (yid->rxlen == 0)
 
			yahoo_remove_active_transfer(sfd);
 

	
 
		if (sfd->state != FT_STATE_RECV_START &&
 
			(data_begin = 
 
				(unsigned char *)strstr((char *)yid->rxqueue,
 
				"\r\n\r\n"))) {
 

	
 
			sfd->state = FT_STATE_RECV_START;
 

	
 
			yid->rxlen -= 4+(data_begin-yid->rxqueue)/sizeof(char);
 
			data_begin += 4;
 

	
 
			if (yid->rxlen > 0)
 
				YAHOO_CALLBACK(ext_yahoo_got_ft_data) 
 
					(yd->client_id, data_begin,
 
					yid->rxlen, sfd->data);
 
		}
 
		else if (sfd->state == FT_STATE_RECV_START)
 
			YAHOO_CALLBACK(ext_yahoo_got_ft_data) (yd->client_id,
 
				yid->rxqueue, yid->rxlen, sfd->data);
 

	
 
		FREE(yid->rxqueue);
 
		yid->rxqueue = NULL;
 
		yid->rxlen = 0;
 
	}
 
	else if (sfd->state == FT_STATE_SEND) {
 
		/* Sent file completed */
 
		int len = 0;
 
		char *off = strstr((char *)yid->rxqueue, "Content-Length: ");
 

	
 
		if (off) {
 
			off += 16;
 
			len = atoi(off);
 
		}
 

	
 
		if (len < sfd->size)
 
			YAHOO_CALLBACK(ext_yahoo_file_transfer_done)
 
				(yd->client_id,
 
				YAHOO_FILE_TRANSFER_FAILED, sfd->data);
 
		else
 
			YAHOO_CALLBACK(ext_yahoo_file_transfer_done)
 
				(yd->client_id,
 
				YAHOO_FILE_TRANSFER_DONE, sfd->data);
 

	
 
		yahoo_remove_active_transfer(sfd);
 
	}
 
}
 

	
 
/* End File Transfer */
 

	
 
enum yahoo_status yahoo_current_status(int id)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	if (!yd)
 
		return YAHOO_STATUS_OFFLINE;
 
	return yd->current_status;
 
}
 

	
 
const YList *yahoo_get_buddylist(int id)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	if (!yd)
 
		return NULL;
 
	return yd->buddies;
 
}
 

	
 
const YList *yahoo_get_ignorelist(int id)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	if (!yd)
 
		return NULL;
 
	return yd->ignore;
 
}
 

	
 
const YList *yahoo_get_identities(int id)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	if (!yd)
 
		return NULL;
 
	return yd->identities;
 
}
 

	
 
const char *yahoo_get_cookie(int id, const char *which)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	if (!yd)
 
		return NULL;
 
	if (!strncasecmp(which, "y", 1))
 
		return yd->cookie_y;
 
	if (!strncasecmp(which, "b", 1))
 
		return yd->cookie_b;
 
	if (!strncasecmp(which, "t", 1))
 
		return yd->cookie_t;
 
	if (!strncasecmp(which, "c", 1))
 
		return yd->cookie_c;
 
	if (!strncasecmp(which, "login", 5))
 
		return yd->login_cookie;
 
	return NULL;
 
}
 

	
 
const char *yahoo_get_profile_url(void)
 
{
 
	return profile_url;
 
}
backends/libyahoo2/yahoo/md5.c
Show inline comments
 
new file 100644
 
/*
 
  Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.
 

	
 
  This software is provided 'as-is', without any express or implied
 
  warranty.  In no event will the authors be held liable for any damages
 
  arising from the use of this software.
 

	
 
  Permission is granted to anyone to use this software for any purpose,
 
  including commercial applications, and to alter it and redistribute it
 
  freely, subject to the following restrictions:
 

	
 
  1. The origin of this software must not be misrepresented; you must not
 
     claim that you wrote the original software. If you use this software
 
     in a product, an acknowledgment in the product documentation would be
 
     appreciated but is not required.
 
  2. Altered source versions must be plainly marked as such, and must not be
 
     misrepresented as being the original software.
 
  3. This notice may not be removed or altered from any source distribution.
 

	
 
  L. Peter Deutsch
 
  ghost@aladdin.com
 

	
 
 */
 
/*
 
  Independent implementation of MD5 (RFC 1321).
 

	
 
  This code implements the MD5 Algorithm defined in RFC 1321.
 
  It is derived directly from the text of the RFC and not from the
 
  reference implementation.
 

	
 
  The original and principal author of md5.c is L. Peter Deutsch
 
  <ghost@aladdin.com>.  Other authors are noted in the change history
 
  that follows (in reverse chronological order):
 

	
 
  1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
 
  1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5).
 
  1999-05-03 lpd Original version.
 
 */
 

	
 
#if HAVE_CONFIG_H
 
# include <config.h>
 
#endif
 

	
 
#include "md5.h"
 

	
 
#if STDC_HEADERS
 
# include <string.h>
 
#else
 
# if !HAVE_STRCHR
 
#  define strchr index
 
#  define strrchr rindex
 
# endif
 
char *strchr(), *strrchr();
 
# if !HAVE_MEMCPY
 
#  define memcpy(d, s, n) bcopy ((s), (d), (n))
 
#  define memmove(d, s, n) bcopy ((s), (d), (n))
 
# endif
 
#endif
 

	
 
#ifdef TEST
 
/*
 
 * Compile with -DTEST to create a self-contained executable test program.
 
 * The test program should print out the same values as given in section
 
 * A.5 of RFC 1321, reproduced below.
 
 */
 
main()
 
{
 
	static const char *const test[7] = {
 
		"",		/*d41d8cd98f00b204e9800998ecf8427e */
 
		"945399884.61923487334tuvga",	/*0cc175b9c0f1b6a831c399e269772661 */
 
		"abc",		/*900150983cd24fb0d6963f7d28e17f72 */
 
		"message digest",	/*f96b697d7cb7938d525a2f31aaf161d0 */
 
		"abcdefghijklmnopqrstuvwxyz",	/*c3fcd3d76192e4007dfb496cca67e13b */
 
		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
 
		/*d174ab98d277d9f5a5611c2c9f419d9f */
 
		"12345678901234567890123456789012345678901234567890123456789012345678901234567890"	/*57edf4a22be3c955ac49da2e2107b67a */
 
	};
 
	int i;
 

	
 
	for (i = 0; i < 7; ++i) {
 
		md5_state_t state;
 
		md5_byte_t digest[16];
 
		int di;
 

	
 
		md5_init(&state);
 
		md5_append(&state, (const md5_byte_t *)test[i],
 
			strlen(test[i]));
 
		md5_finish(&state, digest);
 
		printf("MD5 (\"%s\") = ", test[i]);
 
		for (di = 0; di < 16; ++di)
 
			printf("%02x", digest[di]);
 
		printf("\n");
 
	}
 
	return 0;
 
}
 
#endif				/* TEST */
 

	
 
/*
 
 * For reference, here is the program that computed the T values.
 
 */
 
#if 0
 
#include <math.h>
 
main()
 
{
 
	int i;
 
	for (i = 1; i <= 64; ++i) {
 
		unsigned long v =
 
			(unsigned long)(4294967296.0 * fabs(sin((double)i)));
 
		printf("#define T%d 0x%08lx\n", i, v);
 
	}
 
	return 0;
 
}
 
#endif
 
/*
 
 * End of T computation program.
 
 */
 
#define T1 0xd76aa478
 
#define T2 0xe8c7b756
 
#define T3 0x242070db
 
#define T4 0xc1bdceee
 
#define T5 0xf57c0faf
 
#define T6 0x4787c62a
 
#define T7 0xa8304613
 
#define T8 0xfd469501
 
#define T9 0x698098d8
 
#define T10 0x8b44f7af
 
#define T11 0xffff5bb1
 
#define T12 0x895cd7be
 
#define T13 0x6b901122
 
#define T14 0xfd987193
 
#define T15 0xa679438e
 
#define T16 0x49b40821
 
#define T17 0xf61e2562
 
#define T18 0xc040b340
 
#define T19 0x265e5a51
 
#define T20 0xe9b6c7aa
 
#define T21 0xd62f105d
 
#define T22 0x02441453
 
#define T23 0xd8a1e681
 
#define T24 0xe7d3fbc8
 
#define T25 0x21e1cde6
 
#define T26 0xc33707d6
 
#define T27 0xf4d50d87
 
#define T28 0x455a14ed
 
#define T29 0xa9e3e905
 
#define T30 0xfcefa3f8
 
#define T31 0x676f02d9
 
#define T32 0x8d2a4c8a
 
#define T33 0xfffa3942
 
#define T34 0x8771f681
 
#define T35 0x6d9d6122
 
#define T36 0xfde5380c
 
#define T37 0xa4beea44
 
#define T38 0x4bdecfa9
 
#define T39 0xf6bb4b60
 
#define T40 0xbebfbc70
 
#define T41 0x289b7ec6
 
#define T42 0xeaa127fa
 
#define T43 0xd4ef3085
 
#define T44 0x04881d05
 
#define T45 0xd9d4d039
 
#define T46 0xe6db99e5
 
#define T47 0x1fa27cf8
 
#define T48 0xc4ac5665
 
#define T49 0xf4292244
 
#define T50 0x432aff97
 
#define T51 0xab9423a7
 
#define T52 0xfc93a039
 
#define T53 0x655b59c3
 
#define T54 0x8f0ccc92
 
#define T55 0xffeff47d
 
#define T56 0x85845dd1
 
#define T57 0x6fa87e4f
 
#define T58 0xfe2ce6e0
 
#define T59 0xa3014314
 
#define T60 0x4e0811a1
 
#define T61 0xf7537e82
 
#define T62 0xbd3af235
 
#define T63 0x2ad7d2bb
 
#define T64 0xeb86d391
 

	
 
static void md5_process(md5_state_t *pms, const md5_byte_t *data /*[64] */ )
 
{
 
	md5_word_t
 
		a = pms->abcd[0], b = pms->abcd[1],
 
		c = pms->abcd[2], d = pms->abcd[3];
 
	md5_word_t t;
 

	
 
#ifndef ARCH_IS_BIG_ENDIAN
 
# define ARCH_IS_BIG_ENDIAN 1	/* slower, default implementation */
 
#endif
 
#if ARCH_IS_BIG_ENDIAN
 

	
 
	/*
 
	 * On big-endian machines, we must arrange the bytes in the right
 
	 * order.  (This also works on machines of unknown byte order.)
 
	 */
 
	md5_word_t X[16];
 
	const md5_byte_t *xp = data;
 
	int i;
 

	
 
	for (i = 0; i < 16; ++i, xp += 4)
 
		X[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
 

	
 
#else				/* !ARCH_IS_BIG_ENDIAN */
 

	
 
	/*
 
	 * On little-endian machines, we can process properly aligned data
 
	 * without copying it.
 
	 */
 
	md5_word_t xbuf[16];
 
	const md5_word_t *X;
 

	
 
	if (!((data - (const md5_byte_t *)0) & 3)) {
 
		/* data are properly aligned */
 
		X = (const md5_word_t *)data;
 
	} else {
 
		/* not aligned */
 
		memcpy(xbuf, data, 64);
 
		X = xbuf;
 
	}
 
#endif
 

	
 
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
 

	
 
	/* Round 1. */
 
	/* Let [abcd k s i] denote the operation
 
	   a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
 
#define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
 
#define SET(a, b, c, d, k, s, Ti)\
 
  t = a + F(b,c,d) + X[k] + Ti;\
 
  a = ROTATE_LEFT(t, s) + b
 
	/* Do the following 16 operations. */
 
	SET(a, b, c, d, 0, 7, T1);
 
	SET(d, a, b, c, 1, 12, T2);
 
	SET(c, d, a, b, 2, 17, T3);
 
	SET(b, c, d, a, 3, 22, T4);
 
	SET(a, b, c, d, 4, 7, T5);
 
	SET(d, a, b, c, 5, 12, T6);
 
	SET(c, d, a, b, 6, 17, T7);
 
	SET(b, c, d, a, 7, 22, T8);
 
	SET(a, b, c, d, 8, 7, T9);
 
	SET(d, a, b, c, 9, 12, T10);
 
	SET(c, d, a, b, 10, 17, T11);
 
	SET(b, c, d, a, 11, 22, T12);
 
	SET(a, b, c, d, 12, 7, T13);
 
	SET(d, a, b, c, 13, 12, T14);
 
	SET(c, d, a, b, 14, 17, T15);
 
	SET(b, c, d, a, 15, 22, T16);
 
#undef SET
 

	
 
	/* Round 2. */
 
	/* Let [abcd k s i] denote the operation
 
	   a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
 
#define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
 
#define SET(a, b, c, d, k, s, Ti)\
 
  t = a + G(b,c,d) + X[k] + Ti;\
 
  a = ROTATE_LEFT(t, s) + b
 
	/* Do the following 16 operations. */
 
	SET(a, b, c, d, 1, 5, T17);
 
	SET(d, a, b, c, 6, 9, T18);
 
	SET(c, d, a, b, 11, 14, T19);
 
	SET(b, c, d, a, 0, 20, T20);
 
	SET(a, b, c, d, 5, 5, T21);
 
	SET(d, a, b, c, 10, 9, T22);
 
	SET(c, d, a, b, 15, 14, T23);
 
	SET(b, c, d, a, 4, 20, T24);
 
	SET(a, b, c, d, 9, 5, T25);
 
	SET(d, a, b, c, 14, 9, T26);
 
	SET(c, d, a, b, 3, 14, T27);
 
	SET(b, c, d, a, 8, 20, T28);
 
	SET(a, b, c, d, 13, 5, T29);
 
	SET(d, a, b, c, 2, 9, T30);
 
	SET(c, d, a, b, 7, 14, T31);
 
	SET(b, c, d, a, 12, 20, T32);
 
#undef SET
 

	
 
	/* Round 3. */
 
	/* Let [abcd k s t] denote the operation
 
	   a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
 
#define H(x, y, z) ((x) ^ (y) ^ (z))
 
#define SET(a, b, c, d, k, s, Ti)\
 
  t = a + H(b,c,d) + X[k] + Ti;\
 
  a = ROTATE_LEFT(t, s) + b
 
	/* Do the following 16 operations. */
 
	SET(a, b, c, d, 5, 4, T33);
 
	SET(d, a, b, c, 8, 11, T34);
 
	SET(c, d, a, b, 11, 16, T35);
 
	SET(b, c, d, a, 14, 23, T36);
 
	SET(a, b, c, d, 1, 4, T37);
 
	SET(d, a, b, c, 4, 11, T38);
 
	SET(c, d, a, b, 7, 16, T39);
 
	SET(b, c, d, a, 10, 23, T40);
 
	SET(a, b, c, d, 13, 4, T41);
 
	SET(d, a, b, c, 0, 11, T42);
 
	SET(c, d, a, b, 3, 16, T43);
 
	SET(b, c, d, a, 6, 23, T44);
 
	SET(a, b, c, d, 9, 4, T45);
 
	SET(d, a, b, c, 12, 11, T46);
 
	SET(c, d, a, b, 15, 16, T47);
 
	SET(b, c, d, a, 2, 23, T48);
 
#undef SET
 

	
 
	/* Round 4. */
 
	/* Let [abcd k s t] denote the operation
 
	   a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
 
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
 
#define SET(a, b, c, d, k, s, Ti)\
 
  t = a + I(b,c,d) + X[k] + Ti;\
 
  a = ROTATE_LEFT(t, s) + b
 
	/* Do the following 16 operations. */
 
	SET(a, b, c, d, 0, 6, T49);
 
	SET(d, a, b, c, 7, 10, T50);
 
	SET(c, d, a, b, 14, 15, T51);
 
	SET(b, c, d, a, 5, 21, T52);
 
	SET(a, b, c, d, 12, 6, T53);
 
	SET(d, a, b, c, 3, 10, T54);
 
	SET(c, d, a, b, 10, 15, T55);
 
	SET(b, c, d, a, 1, 21, T56);
 
	SET(a, b, c, d, 8, 6, T57);
 
	SET(d, a, b, c, 15, 10, T58);
 
	SET(c, d, a, b, 6, 15, T59);
 
	SET(b, c, d, a, 13, 21, T60);
 
	SET(a, b, c, d, 4, 6, T61);
 
	SET(d, a, b, c, 11, 10, T62);
 
	SET(c, d, a, b, 2, 15, T63);
 
	SET(b, c, d, a, 9, 21, T64);
 
#undef SET
 

	
 
	/* Then perform the following additions. (That is increment each
 
	   of the four registers by the value it had before this block
 
	   was started.) */
 
	pms->abcd[0] += a;
 
	pms->abcd[1] += b;
 
	pms->abcd[2] += c;
 
	pms->abcd[3] += d;
 
}
 

	
 
void md5_init(md5_state_t *pms)
 
{
 
	pms->count[0] = pms->count[1] = 0;
 
	pms->abcd[0] = 0x67452301;
 
	pms->abcd[1] = 0xefcdab89;
 
	pms->abcd[2] = 0x98badcfe;
 
	pms->abcd[3] = 0x10325476;
 
}
 

	
 
void md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes)
 
{
 
	const md5_byte_t *p = data;
 
	int left = nbytes;
 
	int offset = (pms->count[0] >> 3) & 63;
 
	md5_word_t nbits = (md5_word_t) (nbytes << 3);
 

	
 
	if (nbytes <= 0)
 
		return;
 

	
 
	/* Update the message length. */
 
	pms->count[1] += nbytes >> 29;
 
	pms->count[0] += nbits;
 
	if (pms->count[0] < nbits)
 
		pms->count[1]++;
 

	
 
	/* Process an initial partial block. */
 
	if (offset) {
 
		int copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
 

	
 
		memcpy(pms->buf + offset, p, copy);
 
		if (offset + copy < 64)
 
			return;
 
		p += copy;
 
		left -= copy;
 
		md5_process(pms, pms->buf);
 
	}
 

	
 
	/* Process full blocks. */
 
	for (; left >= 64; p += 64, left -= 64)
 
		md5_process(pms, p);
 

	
 
	/* Process a final partial block. */
 
	if (left)
 
		memcpy(pms->buf, p, left);
 
}
 

	
 
void md5_finish(md5_state_t *pms, md5_byte_t digest[16])
 
{
 
	static const md5_byte_t pad[64] = {
 
		0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 
	};
 
	md5_byte_t data[8];
 
	int i;
 

	
 
	/* Save the length before padding. */
 
	for (i = 0; i < 8; ++i)
 
		data[i] = (md5_byte_t) (pms->count[i >> 2] >> ((i & 3) << 3));
 
	/* Pad to 56 bytes mod 64. */
 
	md5_append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
 
	/* Append the length. */
 
	md5_append(pms, data, 8);
 
	for (i = 0; i < 16; ++i)
 
		digest[i] = (md5_byte_t) (pms->abcd[i >> 2] >> ((i & 3) << 3));
 
}
backends/libyahoo2/yahoo/md5.h
Show inline comments
 
new file 100644
 
/*
 
  Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.
 

	
 
  This software is provided 'as-is', without any express or implied
 
  warranty.  In no event will the authors be held liable for any damages
 
  arising from the use of this software.
 

	
 
  Permission is granted to anyone to use this software for any purpose,
 
  including commercial applications, and to alter it and redistribute it
 
  freely, subject to the following restrictions:
 

	
 
  1. The origin of this software must not be misrepresented; you must not
 
     claim that you wrote the original software. If you use this software
 
     in a product, an acknowledgment in the product documentation would be
 
     appreciated but is not required.
 
  2. Altered source versions must be plainly marked as such, and must not be
 
     misrepresented as being the original software.
 
  3. This notice may not be removed or altered from any source distribution.
 

	
 
  L. Peter Deutsch
 
  ghost@aladdin.com
 

	
 
 */
 
/*
 
  Independent implementation of MD5 (RFC 1321).
 

	
 
  This code implements the MD5 Algorithm defined in RFC 1321.
 
  It is derived directly from the text of the RFC and not from the
 
  reference implementation.
 

	
 
  The original and principal author of md5.h is L. Peter Deutsch
 
  <ghost@aladdin.com>.  Other authors are noted in the change history
 
  that follows (in reverse chronological order):
 

	
 
  1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
 
  1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5);
 
	added conditionalization for C++ compilation from Martin
 
	Purschke <purschke@bnl.gov>.
 
  1999-05-03 lpd Original version.
 
 */
 

	
 
#ifndef md5_INCLUDED
 
#  define md5_INCLUDED
 

	
 
/*
 
 * This code has some adaptations for the Ghostscript environment, but it
 
 * will compile and run correctly in any environment with 8-bit chars and
 
 * 32-bit ints.  Specifically, it assumes that if the following are
 
 * defined, they have the same meaning as in Ghostscript: P1, P2, P3,
 
 * ARCH_IS_BIG_ENDIAN.
 
 */
 

	
 
typedef unsigned char md5_byte_t;	/* 8-bit byte */
 
typedef unsigned int md5_word_t;	/* 32-bit word */
 

	
 
/* Define the state of the MD5 Algorithm. */
 
typedef struct md5_state_s {
 
	md5_word_t count[2];	/* message length in bits, lsw first */
 
	md5_word_t abcd[4];	/* digest buffer */
 
	md5_byte_t buf[64];	/* accumulate block */
 
} md5_state_t;
 

	
 
#ifdef __cplusplus
 
extern "C" {
 
#endif
 

	
 
/* Initialize the algorithm. */
 
#ifdef P1
 
	void md5_init(P1(md5_state_t *pms));
 
#else
 
	void md5_init(md5_state_t *pms);
 
#endif
 

	
 
/* Append a string to the message. */
 
#ifdef P3
 
	void md5_append(P3(md5_state_t *pms, const md5_byte_t *data,
 
			int nbytes));
 
#else
 
	void md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes);
 
#endif
 

	
 
/* Finish the message and return the digest. */
 
#ifdef P2
 
	void md5_finish(P2(md5_state_t *pms, md5_byte_t digest[16]));
 
#else
 
	void md5_finish(md5_state_t *pms, md5_byte_t digest[16]);
 
#endif
 

	
 
#ifdef __cplusplus
 
}				/* end extern "C" */
 
#endif
 
#endif				/* md5_INCLUDED */
backends/libyahoo2/yahoo/sha1.c
Show inline comments
 
new file 100644
 
/*-
 
 * Copyright (c) 2001-2003 Allan Saddi <allan@saddi.com>
 
 * All rights reserved.
 
 *
 
 * Redistribution and use in source and binary forms, with or without
 
 * modification, are permitted provided that the following conditions
 
 * are met:
 
 * 1. Redistributions of source code must retain the above copyright
 
 *    notice, this list of conditions and the following disclaimer.
 
 * 2. Redistributions in binary form must reproduce the above copyright
 
 *    notice, this list of conditions and the following disclaimer in the
 
 *    documentation and/or other materials provided with the distribution.
 
 *
 
 * THIS SOFTWARE IS PROVIDED BY ALLAN SADDI AND HIS CONTRIBUTORS ``AS IS''
 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
 * ARE DISCLAIMED.  IN NO EVENT SHALL ALLAN SADDI OR HIS CONTRIBUTORS BE
 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
 * POSSIBILITY OF SUCH DAMAGE.
 
 */
 

	
 
/*
 
 * Define WORDS_BIGENDIAN if compiling on a big-endian architecture.
 
 *
 
 * Define SHA1_TEST to test the implementation using the NIST's
 
 * sample messages. The output should be:
 
 *
 
 *   a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d
 
 *   84983e44 1c3bd26e baae4aa1 f95129e5 e54670f1
 
 *   34aa973c d4c4daa4 f61eeb2b dbad2731 6534016f
 
 */
 

	
 
#ifdef HAVE_CONFIG_H
 
#include <config.h>
 
#endif				/* HAVE_CONFIG_H */
 

	
 
#if HAVE_INTTYPES_H
 
# include <inttypes.h>
 
#else
 
# if HAVE_STDINT_H
 
#  include <stdint.h>
 
# endif
 
#endif
 

	
 
#include <string.h>
 

	
 
#include "sha1.h"
 

	
 
#define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
 
#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
 

	
 
#define F_0_19(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
 
#define F_20_39(x, y, z) ((x) ^ (y) ^ (z))
 
#define F_40_59(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
 
#define F_60_79(x, y, z) ((x) ^ (y) ^ (z))
 

	
 
#define DO_ROUND(F, K) { \
 
  temp = ROTL(a, 5) + F(b, c, d) + e + *(W++) + K; \
 
  e = d; \
 
  d = c; \
 
  c = ROTL(b, 30); \
 
  b = a; \
 
  a = temp; \
 
}
 

	
 
#define K_0_19 0x5a827999L
 
#define K_20_39 0x6ed9eba1L
 
#define K_40_59 0x8f1bbcdcL
 
#define K_60_79 0xca62c1d6L
 

	
 
#ifndef RUNTIME_ENDIAN
 

	
 
#ifdef WORDS_BIGENDIAN
 

	
 
#define BYTESWAP(x) (x)
 
#define BYTESWAP64(x) (x)
 

	
 
#else				/* WORDS_BIGENDIAN */
 

	
 
#define BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | (ROTL((x), 8) & 0x00ff00ffL))
 

	
 
static uint64_t _byteswap64(uint64_t x)
 
{
 
	uint32_t a = x >> 32;
 
	uint32_t b = (uint32_t) x;
 
	return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a);
 
}
 

	
 
#define BYTESWAP64(x) _byteswap64(x)
 

	
 
#endif				/* WORDS_BIGENDIAN */
 

	
 
#else				/* !RUNTIME_ENDIAN */
 

	
 
#define BYTESWAP(x) _byteswap(sc->littleEndian, x)
 
#define BYTESWAP64(x) _byteswap64(sc->littleEndian, x)
 

	
 
#define _BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | \
 
		      (ROTL((x), 8) & 0x00ff00ffL))
 
#define _BYTESWAP64(x) __byteswap64(x)
 

	
 
static uint64_t __byteswap64(uint64_t x)
 
{
 
	uint32_t a = x >> 32;
 
	uint32_t b = (uint32_t) x;
 
	return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a);
 
}
 

	
 
static uint32_t _byteswap(int littleEndian, uint32_t x)
 
{
 
	if (!littleEndian)
 
		return x;
 
	else
 
		return _BYTESWAP(x);
 
}
 

	
 
static uint64_t _byteswap64(int littleEndian, uint64_t x)
 
{
 
	if (!littleEndian)
 
		return x;
 
	else
 
		return _BYTESWAP64(x);
 
}
 

	
 
static void setEndian(int *littleEndianp)
 
{
 
	union {
 
		uint32_t w;
 
		uint8_t b[4];
 
	} endian;
 

	
 
	endian.w = 1L;
 
	*littleEndianp = endian.b[0] != 0;
 
}
 

	
 
#endif				/* !RUNTIME_ENDIAN */
 

	
 
static const uint8_t padding[64] = {
 
	0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
 
};
 

	
 
void SHA1Init(SHA1Context *sc)
 
{
 
#ifdef RUNTIME_ENDIAN
 
	setEndian(&sc->littleEndian);
 
#endif				/* RUNTIME_ENDIAN */
 

	
 
	sc->totalLength = 0LL;
 
	sc->hash[0] = 0x67452301L;
 
	sc->hash[1] = 0xefcdab89L;
 
	sc->hash[2] = 0x98badcfeL;
 
	sc->hash[3] = 0x10325476L;
 
	sc->hash[4] = 0xc3d2e1f0L;
 
	sc->bufferLength = 0L;
 
}
 

	
 
static void burnStack(int size)
 
{
 
	char buf[128];
 

	
 
	memset(buf, 0, sizeof(buf));
 
	size -= sizeof(buf);
 
	if (size > 0)
 
		burnStack(size);
 
}
 

	
 
static void SHA1Guts(SHA1Context *sc, const uint32_t *cbuf)
 
{
 
	uint32_t buf[80];
 
	uint32_t *W, *W3, *W8, *W14, *W16;
 
	uint32_t a, b, c, d, e, temp;
 
	int i;
 

	
 
	W = buf;
 

	
 
	for (i = 15; i >= 0; i--) {
 
		*(W++) = BYTESWAP(*cbuf);
 
		cbuf++;
 
	}
 

	
 
	W16 = &buf[0];
 
	W14 = &buf[2];
 
	W8 = &buf[8];
 
	W3 = &buf[13];
 

	
 
	for (i = 63; i >= 0; i--) {
 
		*W = *(W3++) ^ *(W8++) ^ *(W14++) ^ *(W16++);
 
		*W = ROTL(*W, 1);
 
		W++;
 
	}
 

	
 
	a = sc->hash[0];
 
	b = sc->hash[1];
 
	c = sc->hash[2];
 
	d = sc->hash[3];
 
	e = sc->hash[4];
 

	
 
	W = buf;
 

	
 
#ifndef SHA1_UNROLL
 
#define SHA1_UNROLL 20
 
#endif				/* !SHA1_UNROLL */
 

	
 
#if SHA1_UNROLL == 1
 
	for (i = 19; i >= 0; i--)
 
		DO_ROUND(F_0_19, K_0_19);
 

	
 
	for (i = 19; i >= 0; i--)
 
		DO_ROUND(F_20_39, K_20_39);
 

	
 
	for (i = 19; i >= 0; i--)
 
		DO_ROUND(F_40_59, K_40_59);
 

	
 
	for (i = 19; i >= 0; i--)
 
		DO_ROUND(F_60_79, K_60_79);
 
#elif SHA1_UNROLL == 2
 
	for (i = 9; i >= 0; i--) {
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
	}
 

	
 
	for (i = 9; i >= 0; i--) {
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
	}
 

	
 
	for (i = 9; i >= 0; i--) {
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
	}
 

	
 
	for (i = 9; i >= 0; i--) {
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
	}
 
#elif SHA1_UNROLL == 4
 
	for (i = 4; i >= 0; i--) {
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
	}
 

	
 
	for (i = 4; i >= 0; i--) {
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
	}
 

	
 
	for (i = 4; i >= 0; i--) {
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
	}
 

	
 
	for (i = 4; i >= 0; i--) {
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
	}
 
#elif SHA1_UNROLL == 5
 
	for (i = 3; i >= 0; i--) {
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
	}
 

	
 
	for (i = 3; i >= 0; i--) {
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
	}
 

	
 
	for (i = 3; i >= 0; i--) {
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
	}
 

	
 
	for (i = 3; i >= 0; i--) {
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
	}
 
#elif SHA1_UNROLL == 10
 
	for (i = 1; i >= 0; i--) {
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
	}
 

	
 
	for (i = 1; i >= 0; i--) {
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
	}
 

	
 
	for (i = 1; i >= 0; i--) {
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
	}
 

	
 
	for (i = 1; i >= 0; i--) {
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
	}
 
#elif SHA1_UNROLL == 20
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 

	
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 

	
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 

	
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
#else				/* SHA1_UNROLL */
 
#error SHA1_UNROLL must be 1, 2, 4, 5, 10 or 20!
 
#endif
 

	
 
	sc->hash[0] += a;
 
	sc->hash[1] += b;
 
	sc->hash[2] += c;
 
	sc->hash[3] += d;
 
	sc->hash[4] += e;
 
}
 

	
 
void SHA1Update(SHA1Context *sc, const void *vdata, uint32_t len)
 
{
 
	const uint8_t *data = vdata;
 
	uint32_t bufferBytesLeft;
 
	uint32_t bytesToCopy;
 
	int needBurn = 0;
 

	
 
#ifdef SHA1_FAST_COPY
 
	if (sc->bufferLength) {
 
		bufferBytesLeft = 64L - sc->bufferLength;
 

	
 
		bytesToCopy = bufferBytesLeft;
 
		if (bytesToCopy > len)
 
			bytesToCopy = len;
 

	
 
		memcpy(&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy);
 

	
 
		sc->totalLength += bytesToCopy * 8L;
 

	
 
		sc->bufferLength += bytesToCopy;
 
		data += bytesToCopy;
 
		len -= bytesToCopy;
 

	
 
		if (sc->bufferLength == 64L) {
 
			SHA1Guts(sc, sc->buffer.words);
 
			needBurn = 1;
 
			sc->bufferLength = 0L;
 
		}
 
	}
 

	
 
	while (len > 63) {
 
		sc->totalLength += 512L;
 

	
 
		SHA1Guts(sc, data);
 
		needBurn = 1;
 

	
 
		data += 64L;
 
		len -= 64L;
 
	}
 

	
 
	if (len) {
 
		memcpy(&sc->buffer.bytes[sc->bufferLength], data, len);
 

	
 
		sc->totalLength += len * 8L;
 

	
 
		sc->bufferLength += len;
 
	}
 
#else				/* SHA1_FAST_COPY */
 
	while (len) {
 
		bufferBytesLeft = 64L - sc->bufferLength;
 

	
 
		bytesToCopy = bufferBytesLeft;
 
		if (bytesToCopy > len)
 
			bytesToCopy = len;
 

	
 
		memcpy(&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy);
 

	
 
		sc->totalLength += bytesToCopy * 8L;
 

	
 
		sc->bufferLength += bytesToCopy;
 
		data += bytesToCopy;
 
		len -= bytesToCopy;
 

	
 
		if (sc->bufferLength == 64L) {
 
			SHA1Guts(sc, sc->buffer.words);
 
			needBurn = 1;
 
			sc->bufferLength = 0L;
 
		}
 
	}
 
#endif				/* SHA1_FAST_COPY */
 

	
 
	if (needBurn)
 
		burnStack(sizeof(uint32_t[86]) + sizeof(uint32_t *[5]) +
 
			sizeof(int));
 
}
 

	
 
void SHA1Final(SHA1Context *sc, uint8_t hash[SHA1_HASH_SIZE])
 
{
 
	uint32_t bytesToPad;
 
	uint64_t lengthPad;
 
	int i;
 

	
 
	bytesToPad = 120L - sc->bufferLength;
 
	if (bytesToPad > 64L)
 
		bytesToPad -= 64L;
 

	
 
	lengthPad = BYTESWAP64(sc->totalLength);
 

	
 
	SHA1Update(sc, padding, bytesToPad);
 
	SHA1Update(sc, &lengthPad, 8L);
 

	
 
	if (hash) {
 
		for (i = 0; i < SHA1_HASH_WORDS; i++) {
 
#ifdef SHA1_FAST_COPY
 
			*((uint32_t *)hash) = BYTESWAP(sc->hash[i]);
 
#else				/* SHA1_FAST_COPY */
 
			hash[0] = (uint8_t) (sc->hash[i] >> 24);
 
			hash[1] = (uint8_t) (sc->hash[i] >> 16);
 
			hash[2] = (uint8_t) (sc->hash[i] >> 8);
 
			hash[3] = (uint8_t) sc->hash[i];
 
#endif				/* SHA1_FAST_COPY */
 
			hash += 4;
 
		}
 
	}
 
}
 

	
 
#ifdef SHA1_TEST
 

	
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <string.h>
 

	
 
int main(int argc, char *argv[])
 
{
 
	SHA1Context foo;
 
	uint8_t hash[SHA1_HASH_SIZE];
 
	char buf[1000];
 
	int i;
 

	
 
	SHA1Init(&foo);
 
	SHA1Update(&foo, "abc", 3);
 
	SHA1Final(&foo, hash);
 

	
 
	for (i = 0; i < SHA1_HASH_SIZE;) {
 
		printf("%02x", hash[i++]);
 
		if (!(i % 4))
 
			printf(" ");
 
	}
 
	printf("\n");
 

	
 
	SHA1Init(&foo);
 
	SHA1Update(&foo,
 
		"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56);
 
	SHA1Final(&foo, hash);
 

	
 
	for (i = 0; i < SHA1_HASH_SIZE;) {
 
		printf("%02x", hash[i++]);
 
		if (!(i % 4))
 
			printf(" ");
 
	}
 
	printf("\n");
 

	
 
	SHA1Init(&foo);
 
	memset(buf, 'a', sizeof(buf));
 
	for (i = 0; i < 1000; i++)
 
		SHA1Update(&foo, buf, sizeof(buf));
 
	SHA1Final(&foo, hash);
 

	
 
	for (i = 0; i < SHA1_HASH_SIZE;) {
 
		printf("%02x", hash[i++]);
 
		if (!(i % 4))
 
			printf(" ");
 
	}
 
	printf("\n");
 

	
 
	exit(0);
 
}
 

	
 
#endif				/* SHA1_TEST */
backends/libyahoo2/yahoo/sha1.h
Show inline comments
 
new file 100644
 
/*-
 
 * Copyright (c) 2001-2003 Allan Saddi <allan@saddi.com>
 
 * All rights reserved.
 
 *
 
 * Redistribution and use in source and binary forms, with or without
 
 * modification, are permitted provided that the following conditions
 
 * are met:
 
 * 1. Redistributions of source code must retain the above copyright
 
 *    notice, this list of conditions and the following disclaimer.
 
 * 2. Redistributions in binary form must reproduce the above copyright
 
 *    notice, this list of conditions and the following disclaimer in the
 
 *    documentation and/or other materials provided with the distribution.
 
 *
 
 * THIS SOFTWARE IS PROVIDED BY ALLAN SADDI AND HIS CONTRIBUTORS ``AS IS''
 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
 * ARE DISCLAIMED.  IN NO EVENT SHALL ALLAN SADDI OR HIS CONTRIBUTORS BE
 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
 * POSSIBILITY OF SUCH DAMAGE.
 
 */
 

	
 
#ifndef _SHA1_H
 
#define _SHA1_H
 

	
 
#if HAVE_INTTYPES_H
 
# include <inttypes.h>
 
#else
 
# if HAVE_STDINT_H
 
#  include <stdint.h>
 
# endif
 
#endif
 

	
 
#define SHA1_HASH_SIZE 20
 

	
 
/* Hash size in 32-bit words */
 
#define SHA1_HASH_WORDS 5
 

	
 
struct _SHA1Context {
 
	uint64_t totalLength;
 
	uint32_t hash[SHA1_HASH_WORDS];
 
	uint32_t bufferLength;
 
	union {
 
		uint32_t words[16];
 
		uint8_t bytes[64];
 
	} buffer;
 
#ifdef RUNTIME_ENDIAN
 
	int littleEndian;
 
#endif				/* RUNTIME_ENDIAN */
 
};
 

	
 
typedef struct _SHA1Context SHA1Context;
 

	
 
#ifdef __cplusplus
 
extern "C" {
 
#endif
 

	
 
	void SHA1Init(SHA1Context *sc);
 
	void SHA1Update(SHA1Context *sc, const void *data, uint32_t len);
 
	void SHA1Final(SHA1Context *sc, uint8_t hash[SHA1_HASH_SIZE]);
 

	
 
#ifdef __cplusplus
 
}
 
#endif
 
#endif				/* _SHA1_H */
backends/libyahoo2/yahoo/yahoo2.h
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2: yahoo2.h
 
 *
 
 * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 *
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
#ifndef YAHOO2_H
 
#define YAHOO2_H
 

	
 
#ifdef __cplusplus
 
extern "C" {
 
#endif
 

	
 
#include "yahoo2_types.h"
 

	
 
/* returns the socket descriptor object for a given pager connection. shouldn't be needed */
 
	void *yahoo_get_fd(int id);
 

	
 
/* says how much logging to do */
 
/* see yahoo2_types.h for the different values */
 
	int yahoo_set_log_level(enum yahoo_log_level level);
 
	enum yahoo_log_level yahoo_get_log_level(void);
 

	
 
/* these functions should be self explanatory */
 
/* who always means the buddy you're acting on */
 
/* id is the successful value returned by yahoo_init */
 

	
 
/* init returns a connection id used to identify the connection hereon */
 
/* or 0 on failure */
 
/* you must call init before calling any other function */
 
/*
 
 * The optional parameters to init are key/value pairs that specify 
 
 * server settings to use.  This list must be NULL terminated - even
 
 * if the list is empty.  If a parameter isn't set, a default value
 
 * will be used.  Parameter keys are strings, parameter values are
 
 * either strings or ints, depending on the key.  Values passed in
 
 * are copied, so you can use const/auto/static/pointers/whatever
 
 * you want.  Parameters are:
 
 * 	NAME			TYPE		DEFAULT
 
 *	pager_host		char *		scs.msg.yahoo.com
 
 *	pager_port		int		5050
 
 *	filetransfer_host	char *		filetransfer.msg.yahoo.com
 
 *	filetransfer_port	int		80
 
 *	webcam_host		char *		webcam.yahoo.com
 
 *	webcam_port		int		5100
 
 *	webcam_description	char *		""
 
 *	local_host		char *		""
 
 *	conn_type		int		Y_WCM_DSL
 
 *
 
 * You should set at least local_host if you intend to use webcams
 
 */
 
	int yahoo_init_with_attributes(const char *username,
 
		const char *password, ...);
 

	
 
/* yahoo_init does the same as yahoo_init_with_attributes, assuming defaults
 
 * for all attributes */
 
	int yahoo_init(const char *username, const char *password);
 

	
 
/* release all resources held by this session */
 
/* you need to call yahoo_close for a session only if
 
 * yahoo_logoff is never called for it (ie, it was never logged in) */
 
	void yahoo_close(int id);
 
/* login logs in to the server */
 
/* initial is of type enum yahoo_status.  see yahoo2_types.h */
 
	void yahoo_login(int id, int initial);
 
	void yahoo_logoff(int id);
 
/* reloads status of all buddies */
 
	void yahoo_refresh(int id);
 
/* activates/deactivates an identity */
 
	void yahoo_set_identity_status(int id, const char *identity,
 
		int active);
 
/* regets the entire buddy list from the server */
 
	void yahoo_get_list(int id);
 
/* download buddy contact information from your yahoo addressbook */
 
	void yahoo_get_yab(int id);
 
/* add/modify an address book entry.  if yab->dbid is set, it will */
 
/* modify that entry else it creates a new entry */
 
	void yahoo_set_yab(int id, struct yab *yab);
 
	void yahoo_keepalive(int id);
 
	void yahoo_chat_keepalive(int id);
 

	
 
/* from is the identity you're sending from.  if NULL, the default is used */
 
/* utf8 is whether msg is a utf8 string or not. */
 
	void yahoo_send_im(int id, const char *from, const char *who,
 
		const char *msg, int utf8, int picture);
 
	void yahoo_send_buzz(int id, const char *from, const char *who);
 
/* if type is true, send typing notice, else send stopped typing notice */
 
	void yahoo_send_typing(int id, const char *from, const char *who,
 
		int typ);
 

	
 
/* used to set away/back status. */
 
/* away says whether the custom message is an away message or a sig */
 
	void yahoo_set_away(int id, enum yahoo_status state, const char *msg,
 
		int away);
 

	
 
	void yahoo_add_buddy(int id, const char *who, const char *group,
 
		const char *msg);
 
	void yahoo_remove_buddy(int id, const char *who, const char *group);
 
	void yahoo_confirm_buddy(int id, const char *who, int reject,
 
		const char *msg);
 
	void yahoo_stealth_buddy(int id, const char *who, int unstealth);
 
/* if unignore is true, unignore, else ignore */
 
	void yahoo_ignore_buddy(int id, const char *who, int unignore);
 
	void yahoo_change_buddy_group(int id, const char *who,
 
		const char *old_group, const char *new_group);
 
	void yahoo_group_rename(int id, const char *old_group,
 
		const char *new_group);
 

	
 
	void yahoo_conference_invite(int id, const char *from, YList *who,
 
		const char *room, const char *msg);
 
	void yahoo_conference_addinvite(int id, const char *from,
 
		const char *who, const char *room, const YList *members,
 
		const char *msg);
 
	void yahoo_conference_decline(int id, const char *from, YList *who,
 
		const char *room, const char *msg);
 
	void yahoo_conference_message(int id, const char *from, YList *who,
 
		const char *room, const char *msg, int utf8);
 
	void yahoo_conference_logon(int id, const char *from, YList *who,
 
		const char *room);
 
	void yahoo_conference_logoff(int id, const char *from, YList *who,
 
		const char *room);
 

	
 
/* Get a list of chatrooms */
 
	void yahoo_get_chatrooms(int id, int chatroomid);
 
/* join room with specified roomname and roomid */
 
	void yahoo_chat_logon(int id, const char *from, const char *room,
 
		const char *roomid);
 
/* Send message "msg" to room with specified roomname, msgtype is 1-normal message or 2-/me mesage */
 
	void yahoo_chat_message(int id, const char *from, const char *room,
 
		const char *msg, const int msgtype, const int utf8);
 
/* Log off chat */
 
	void yahoo_chat_logoff(int id, const char *from);
 

	
 
/* requests a webcam feed */
 
/* who is the person who's webcam you would like to view */
 
/* if who is null, then you're the broadcaster */
 
	void yahoo_webcam_get_feed(int id, const char *who);
 
	void yahoo_webcam_close_feed(int id, const char *who);
 

	
 
/* sends an image when uploading */
 
/* image points to a JPEG-2000 image, length is the length of the image */
 
/* in bytes. The timestamp is the time in milliseconds since we started the */
 
/* webcam. */
 
	void yahoo_webcam_send_image(int id, unsigned char *image,
 
		unsigned int length, unsigned int timestamp);
 

	
 
/* this function should be called if we want to allow a user to watch the */
 
/* webcam. Who is the user we want to accept. */
 
/* Accept user (accept = 1), decline user (accept = 0) */
 
	void yahoo_webcam_accept_viewer(int id, const char *who, int accept);
 

	
 
/* send an invitation to a user to view your webcam */
 
	void yahoo_webcam_invite(int id, const char *who);
 

	
 
/* will set up a connection and initiate file transfer.
 
 * callback will be called with the fd that you should write
 
 * the file data to
 
 */
 
	void yahoo_send_file(int id, const char *who, const char *msg,
 
		const char *name, unsigned long size,
 
		yahoo_get_fd_callback callback, void *data);
 

	
 
/*
 
 * Respond to a file transfer request. Be sure to provide the callback data
 
 * since that is your only chance to recognize future callbacks
 
 */
 
	void yahoo_send_file_transfer_response(int client_id, int response,
 
		char *id, void *data);
 

	
 

	
 
/* send a search request
 
 */
 
	void yahoo_search(int id, enum yahoo_search_type t, const char *text,
 
		enum yahoo_search_gender g, enum yahoo_search_agerange ar,
 
		int photo, int yahoo_only);
 

	
 
/* continue last search
 
 * should be called if only (start+found >= total)
 
 *
 
 * where the above three are passed to ext_yahoo_got_search_result
 
 */
 
	void yahoo_search_again(int id, int start);
 

	
 
/* these should be called when input is available on a fd */
 
/* registered by ext_yahoo_add_handler */
 
/* if these return negative values, errno may be set */
 
	int yahoo_read_ready(int id, void *fd, void *data);
 
	int yahoo_write_ready(int id, void *fd, void *data);
 

	
 
/* utility functions. these do not hit the server */
 
	enum yahoo_status yahoo_current_status(int id);
 
	const YList *yahoo_get_buddylist(int id);
 
	const YList *yahoo_get_ignorelist(int id);
 
	const YList *yahoo_get_identities(int id);
 
/* 'which' could be y, t, c or login.  This may change in later versions. */
 
	const char *yahoo_get_cookie(int id, const char *which);
 

	
 
/* returns the url used to get user profiles - you must append the user id */
 
/* as of now this is http://profiles.yahoo.com/ */
 
/* You'll have to do urlencoding yourself, but see yahoo_httplib.h first */
 
	const char *yahoo_get_profile_url(void);
 

	
 
	void yahoo_buddyicon_request(int id, const char *who);
 

	
 
#include "yahoo_httplib.h"
 

	
 
#ifdef __cplusplus
 
}
 
#endif
 
#endif
backends/libyahoo2/yahoo/yahoo2_callbacks.h
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2: yahoo2_callbacks.h
 
 *
 
 * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 *
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
/*
 
 * The functions in this file *must* be defined in your client program
 
 * If you want to use a callback structure instead of direct functions,
 
 * then you must define USE_STRUCT_CALLBACKS in all files that #include
 
 * this one.
 
 *
 
 * Register the callback structure by calling yahoo_register_callbacks -
 
 * declared in this file and defined in libyahoo2.c
 
 */
 

	
 
#ifndef YAHOO2_CALLBACKS_H
 
#define YAHOO2_CALLBACKS_H
 

	
 
#ifdef __cplusplus
 
extern "C" {
 
#endif
 

	
 
#include "yahoo2_types.h"
 

	
 
/*
 
 * yahoo2_callbacks.h
 
 *
 
 * Callback interface for libyahoo2
 
 */
 

	
 
	typedef enum {
 
		YAHOO_INPUT_READ = 1 << 0,
 
		YAHOO_INPUT_WRITE = 1 << 1,
 
		YAHOO_INPUT_EXCEPTION = 1 << 2
 
	} yahoo_input_condition;
 

	
 
/*
 
 * A callback function called when an asynchronous connect completes.
 
 * 
 
 * Params:
 
 *     fd    - The file descriptor object that has been connected, or NULL on 
 
 *             error
 
 *     error - The value of errno set by the call to connect or 0 if no error
 
 *	       Set both fd and error to 0 if the connect was cancelled by the
 
 *	       user
 
 *     callback_data - the callback_data passed to the ext_yahoo_connect_async
 
 *	       function
 
 */
 
	typedef void (*yahoo_connect_callback) (void *fd, int error,
 
		void *callback_data);
 

	
 
/*
 
 * The following functions need to be implemented in the client
 
 * interface.  They will be called by the library when each
 
 * event occurs.
 
 */
 

	
 
/* 
 
 * should we use a callback structure or directly call functions
 
 * if you want the structure, you *must* define USE_STRUCT_CALLBACKS
 
 * both when you compile the library, and when you compile your code
 
 * that uses the library
 
 */
 

	
 
#define YAHOO_CALLBACK_TYPE(x)	(*x)
 
	struct yahoo_callbacks {
 
		
 
/*
 
 * Name: ext_yahoo_login_response
 
 * 	Called when the login process is complete
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	succ - enum yahoo_login_status
 
 * 	url  - url to reactivate account if locked
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_login_response) (int id, int succ,
 
		const char *url);
 

	
 
/*
 
 * Name: ext_yahoo_got_buddies
 
 * 	Called when the contact list is got from the server
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	buds - the buddy list
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_buddies) (int id, YList *buds);
 

	
 
/*
 
 * Name: ext_yahoo_got_ignore
 
 * 	Called when the ignore list is got from the server
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	igns - the ignore list
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_ignore) (int id, YList *igns);
 

	
 
/*
 
 * Name: ext_yahoo_got_identities
 
 * 	Called when the contact list is got from the server
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	ids  - the identity list
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_identities) (int id, YList *ids);
 

	
 
/*
 
 * Name: ext_yahoo_got_cookies
 
 * 	Called when the cookie list is got from the server
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_cookies) (int id);
 

	
 
/*
 
 * Name: ext_yahoo_got_ping
 
 * 	Called when the ping packet is received from the server
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 *  errormsg - optional error message
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_ping) (int id,
 
		const char *errormsg);
 

	
 
/*
 
 * Name: ext_yahoo_status_changed
 
 * 	Called when remote user's status changes.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	who  - the handle of the remote user
 
 * 	stat - status code (enum yahoo_status)
 
 * 	msg  - the message if stat == YAHOO_STATUS_CUSTOM
 
 * 	away - whether the contact is away or not (YAHOO_STATUS_CUSTOM)
 
 * 	idle - this is the number of seconds he is idle [if he is idle]
 
 *	mobile - this is set for mobile users/buddies
 
 *	TODO: add support for pager, chat, and game states
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_status_changed) (int id,
 
		const char *who, int stat, const char *msg, int away, int idle,
 
		int mobile);
 

	
 
/*
 
 * Name: ext_yahoo_got_buzz
 
 * 	Called when remote user sends you a buzz.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity the message was sent to
 
 * 	who  - the handle of the remote user
 
 * 	tm   - timestamp of message if offline
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_buzz) (int id, const char *me,
 
		const char *who, long tm);
 

	
 
/*
 
 * Name: ext_yahoo_got_im
 
 * 	Called when remote user sends you a message.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity the message was sent to
 
 * 	who  - the handle of the remote user
 
 * 	msg  - the message - NULL if stat == 2
 
 * 	tm   - timestamp of message if offline
 
 * 	stat - message status - 0
 
 * 				1
 
 * 				2 == error sending message
 
 * 				5
 
 * 	utf8 - whether the message is encoded as utf8 or not
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_im) (int id, const char *me,
 
		const char *who, const char *msg, long tm, int stat, int utf8);
 

	
 
/*
 
 * Name: ext_yahoo_got_conf_invite
 
 * 	Called when remote user sends you a conference invitation.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity the invitation was sent to
 
 * 	who  - the user inviting you
 
 * 	room - the room to join
 
 * 	msg  - the message
 
 *	members - the initial members of the conference (null terminated list)
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_conf_invite) (int id,
 
		const char *me, const char *who, const char *room,
 
		const char *msg, YList *members);
 

	
 
/*
 
 * Name: ext_yahoo_conf_userdecline
 
 * 	Called when someone declines to join the conference.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity in the conference
 
 * 	who  - the user who has declined
 
 * 	room - the room
 
 * 	msg  - the declining message
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_conf_userdecline) (int id,
 
		const char *me, const char *who, const char *room,
 
		const char *msg);
 

	
 
/*
 
 * Name: ext_yahoo_conf_userjoin
 
 * 	Called when someone joins the conference.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity in the conference
 
 * 	who  - the user who has joined
 
 * 	room - the room joined
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_conf_userjoin) (int id,
 
		const char *me, const char *who, const char *room);
 

	
 
/*
 
 * Name: ext_yahoo_conf_userleave
 
 * 	Called when someone leaves the conference.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity in the conference
 
 * 	who  - the user who has left
 
 * 	room - the room left
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_conf_userleave) (int id,
 
		const char *me, const char *who, const char *room);
 

	
 
/*
 
 * Name: ext_yahoo_chat_cat_xml
 
 * 	Called when ?
 
 * Params:
 
 * 	id      - the id that identifies the server connection
 
 * 	xml     - ?
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_cat_xml) (int id,
 
		const char *xml);
 

	
 
/*
 
 * Name: ext_yahoo_chat_join
 
 * 	Called when joining the chatroom.
 
 * Params:
 
 * 	id      - the id that identifies the server connection
 
 * 	me   - the identity in the chatroom
 
 * 	room    - the room joined, used in all other chat calls, freed by 
 
 * 	          library after call
 
 * 	topic   - the topic of the room, freed by library after call
 
 *	members - the initial members of the chatroom (null terminated YList 
 
 *	          of yahoo_chat_member's) Must be freed by the client
 
 *	fd	- the object where the connection is coming from (for tracking)
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_join) (int id, const char *me,
 
		const char *room, const char *topic, YList *members, void *fd);
 

	
 
/*
 
 * Name: ext_yahoo_chat_userjoin
 
 * 	Called when someone joins the chatroom.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity in the chatroom
 
 * 	room - the room joined
 
 * 	who  - the user who has joined, Must be freed by the client
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_userjoin) (int id,
 
		const char *me, const char *room,
 
		struct yahoo_chat_member *who);
 

	
 
/*
 
 * Name: ext_yahoo_chat_userleave
 
 * 	Called when someone leaves the chatroom.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity in the chatroom
 
 * 	room - the room left
 
 * 	who  - the user who has left (Just the User ID)
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_userleave) (int id,
 
		const char *me, const char *room, const char *who);
 

	
 
/*
 
 * Name: ext_yahoo_chat_message
 
 * 	Called when someone messages in the chatroom.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity in the chatroom
 
 * 	room - the room
 
 * 	who  - the user who messaged (Just the user id)
 
 * 	msg  - the message
 
 * 	msgtype  - 1 = Normal message
 
 * 		   2 = /me type message
 
 * 	utf8 - whether the message is utf8 encoded or not
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_message) (int id,
 
		const char *me, const char *who, const char *room,
 
		const char *msg, int msgtype, int utf8);
 

	
 
/*
 
 *
 
 * Name: ext_yahoo_chat_yahoologout
 
 *	called when yahoo disconnects your chat session
 
 *	Note this is called whenver a disconnect happens, client or server
 
 *	requested. Care should be taken to make sure you know the origin 
 
 *	of the disconnect request before doing anything here (auto-join's etc)
 
 * Params:
 
 *	id   - the id that identifies this connection
 
 * 	me   - the identity in the chatroom
 
 * Returns:
 
 *	nothing.
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_yahoologout) (int id,
 
		const char *me);
 

	
 
/*
 
 *
 
 * Name: ext_yahoo_chat_yahooerror
 
 *	called when yahoo sends back an error to you
 
 *	Note this is called whenver chat message is sent into a room
 
 *	in error (fd not connected, room doesn't exists etc)
 
 *	Care should be taken to make sure you know the origin 
 
 *	of the error before doing anything about it.
 
 * Params:
 
 *	id   - the id that identifies this connection
 
 * 	me   - the identity in the chatroom
 
 * Returns:
 
 *	nothing.
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_yahooerror) (int id,
 
		const char *me);
 

	
 
/*
 
 * Name: ext_yahoo_conf_message
 
 * 	Called when someone messages in the conference.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity the conf message was sent to
 
 * 	who  - the user who messaged
 
 * 	room - the room
 
 * 	msg  - the message
 
 * 	utf8 - whether the message is utf8 encoded or not
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_conf_message) (int id,
 
		const char *me, const char *who, const char *room,
 
		const char *msg, int utf8);
 

	
 
/*
 
 * Name: ext_yahoo_got_file
 
 * 	Called when someone sends you a file
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity the file was sent to
 
 * 	who  - the user who sent the file
 
 * 	msg  - the message
 
 * 	fname- the file name if direct transfer
 
 * 	fsize- the file size if direct transfer
 
 * 	trid - transfer id. Unique for this transfer
 
 *
 
 * NOTE: Subsequent callbacks for file transfer do not send all of this
 
 * information again since it is wasteful. Implementations are expected to
 
 * save this information and supply it as callback data when the file or
 
 * confirmation is sent
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_file) (int id, const char *me,
 
		const char *who, const char *msg, const char *fname,
 
		unsigned long fesize, char *trid);
 

	
 
/*
 
 * Name: ext_yahoo_got_ft_data
 
 * 	Called multiple times when parts of the file are received
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	in   - The data
 
 * 	len  - Length of the data
 
 * 	data - callback data
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_ft_data) (int id,
 
		const unsigned char *in, int len, void *data);
 

	
 
/*
 
 * Name: ext_yahoo_file_transfer_done
 
 * 	File transfer is done
 
 * Params:
 
 * 	id     - the id that identifies the server connection
 
 * 	result - To notify if it finished successfully or with a failure
 
 * 	data   - callback data
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_file_transfer_done) (int id,
 
		int result, void *data);
 

	
 
/*
 
 * Name: ext_yahoo_contact_added
 
 * 	Called when a contact is added to your list
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	myid - the identity he was added to
 
 * 	who  - who was added
 
 * 	msg  - any message sent
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_contact_added) (int id,
 
		const char *myid, const char *who, const char *msg);
 

	
 
/*
 
 * Name: ext_yahoo_rejected
 
 * 	Called when a contact rejects your add
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	who  - who rejected you
 
 * 	msg  - any message sent
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_rejected) (int id, const char *who,
 
		const char *msg);
 

	
 
/*
 
 * Name: ext_yahoo_typing_notify
 
 * 	Called when remote user starts or stops typing.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the handle of the identity the notification is sent to
 
 * 	who  - the handle of the remote user
 
 * 	stat - 1 if typing, 0 if stopped typing
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_typing_notify) (int id,
 
		const char *me, const char *who, int stat);
 

	
 
/*
 
 * Name: ext_yahoo_game_notify
 
 * 	Called when remote user starts or stops a game.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the handle of the identity the notification is sent to
 
 * 	who  - the handle of the remote user
 
 * 	stat - 1 if game, 0 if stopped gaming
 
 * 	msg  - game description and/or other text
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_game_notify) (int id, const char *me,
 
		const char *who, int stat, const char *msg);
 

	
 
/*
 
 * Name: ext_yahoo_mail_notify
 
 * 	Called when you receive mail, or with number of messages
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	from - who the mail is from - NULL if only mail count
 
 * 	subj - the subject of the mail - NULL if only mail count
 
 * 	cnt  - mail count - 0 if new mail notification
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_mail_notify) (int id,
 
		const char *from, const char *subj, int cnt);
 

	
 
/*
 
 * Name: ext_yahoo_system_message
 
 * 	System message
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the handle of the identity the notification is sent to
 
 * 	who  - the source of the system message (there are different types)
 
 * 	msg  - the message
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_system_message) (int id,
 
		const char *me, const char *who, const char *msg);
 

	
 
/*
 
 * Name: ext_yahoo_got_buddyicon
 
 * 	Buddy icon received
 
 * Params:
 
 * 	id - the id that identifies the server connection
 
 * 	me - the handle of the identity the notification is sent to
 
 * 	who - the person the buddy icon is for
 
 * 	url - the url to use to load the icon
 
 * 	checksum - the checksum of the icon content
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_buddyicon) (int id,
 
		const char *me, const char *who, const char *url, int checksum);
 

	
 
/*
 
 * Name: ext_yahoo_got_buddyicon_checksum
 
 * 	Buddy icon checksum received
 
 * Params:
 
 * 	id - the id that identifies the server connection
 
 * 	me - the handle of the identity the notification is sent to
 
 * 	who - the yahoo id of the buddy icon checksum is for
 
 * 	checksum - the checksum of the icon content
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_buddyicon_checksum) (int id,
 
		const char *me, const char *who, int checksum);
 

	
 
/*
 
 * Name: ext_yahoo_got_buddyicon_request
 
 * 	Buddy icon request received
 
 * Params:
 
 * 	id - the id that identifies the server connection
 
 * 	me - the handle of the identity the notification is sent to
 
 * 	who - the yahoo id of the buddy that requested the buddy icon
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_buddyicon_request) (int id,
 
		const char *me, const char *who);
 

	
 
/*
 
 * Name: ext_yahoo_got_buddyicon_request
 
 * 	Buddy icon request received
 
 * Params:
 
 * 	id - the id that identifies the server connection
 
 * 	url - remote url, the uploaded buddy icon can be fetched from
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_buddyicon_uploaded) (int id,
 
		const char *url);
 

	
 
/*
 
 * Name: ext_yahoo_got_webcam_image
 
 * 	Called when you get a webcam update
 
 *	An update can either be receiving an image, a part of an image or
 
 *	just an update with a timestamp
 
 * Params:
 
 * 	id         - the id that identifies the server connection
 
 * 	who        - the user who's webcam we're viewing
 
 *	image      - image data
 
 *	image_size - length of the image in bytes
 
 *	real_size  - actual length of image data
 
 *	timestamp  - milliseconds since the webcam started
 
 *
 
 *	If the real_size is smaller then the image_size then only part of
 
 *	the image has been read. This function will keep being called till
 
 *	the total amount of bytes in image_size has been read. The image
 
 *	received is in JPEG-2000 Code Stream Syntax (ISO/IEC 15444-1).
 
 *	The size of the image will be either 160x120 or 320x240.
 
 *	Each webcam image contains a timestamp. This timestamp should be
 
 *	used to keep the image in sync since some images can take longer
 
 *	to transport then others. When image_size is 0 we can still receive
 
 *	a timestamp to stay in sync
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_webcam_image) (int id,
 
		const char *who, const unsigned char *image,
 
		unsigned int image_size, unsigned int real_size,
 
		unsigned int timestamp);
 

	
 
/*
 
 * Name: ext_yahoo_webcam_invite
 
 * 	Called when you get a webcam invitation
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - identity the invitation is to
 
 * 	from - who the invitation is from
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_webcam_invite) (int id,
 
		const char *me, const char *from);
 

	
 
/*
 
 * Name: ext_yahoo_webcam_invite_reply
 
 * 	Called when you get a response to a webcam invitation
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - identity the invitation response is to
 
 * 	from - who the invitation response is from
 
 *	accept - 0 (decline), 1 (accept)
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_webcam_invite_reply) (int id,
 
		const char *me, const char *from, int accept);
 

	
 
/*
 
 * Name: ext_yahoo_webcam_closed
 
 * 	Called when the webcam connection closed
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	who  - the user who we where connected to
 
 *	reason - reason why the connection closed
 
 *	         1 = user stopped broadcasting
 
 *	         2 = user cancelled viewing permission
 
 *	         3 = user declines permission
 
 *	         4 = user does not have webcam online
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_webcam_closed) (int id,
 
		const char *who, int reason);
 

	
 
/*
 
 * Name: ext_yahoo_got_search_result
 
 *      Called when the search result received from server
 
 * Params:
 
 *      id  	 - the id that identifies the server connection
 
 * 	found	 - total number of results returned in the current result set
 
 * 	start	 - offset from where the current result set starts
 
 * 	total	 - total number of results available (start + found <= total)
 
 * 	contacts - the list of results as a YList of yahoo_found_contact
 
 * 		   these will be freed after this function returns, so
 
 * 		   if you need to use the information, make a copy
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_search_result) (int id,
 
		int found, int start, int total, YList *contacts);
 

	
 
/*
 
 * Name: ext_yahoo_error
 
 * 	Called on error.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	err  - the error message
 
 * 	fatal- whether this error is fatal to the connection or not
 
 * 	num  - Which error is this
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_error) (int id, const char *err,
 
		int fatal, int num);
 

	
 
/*
 
 * Name: ext_yahoo_webcam_viewer
 
 *	Called when a viewer disconnects/connects/requests to connect
 
 * Params:
 
 *	id  - the id that identifies the server connection
 
 *	who - the viewer
 
 *	connect - 0=disconnect 1=connect 2=request
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_webcam_viewer) (int id,
 
		const char *who, int connect);
 

	
 
/*
 
 * Name: ext_yahoo_webcam_data_request
 
 *	Called when you get a request for webcam images
 
 * Params:
 
 *	id   - the id that identifies the server connection
 
 *	send - whether to send images or not
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_webcam_data_request) (int id,
 
		int send);
 

	
 
/*
 
 * Name: ext_yahoo_log
 
 * 	Called to log a message.
 
 * Params:
 
 * 	fmt  - the printf formatted message
 
 * Returns:
 
 * 	0
 
 */
 
	int YAHOO_CALLBACK_TYPE(ext_yahoo_log) (const char *fmt, ...);
 

	
 
/*
 
 * Name: ext_yahoo_add_handler
 
 * 	Add a listener for the fd.  Must call yahoo_read_ready
 
 * 	when a YAHOO_INPUT_READ fd is ready and yahoo_write_ready
 
 * 	when a YAHOO_INPUT_WRITE fd is ready.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	fd   - the fd object on which to listen
 
 * 	cond - the condition on which to call the callback
 
 * 	data - callback data to pass to yahoo_*_ready
 
 * 	
 
 * Returns: a tag to be used when removing the handler
 
 */
 
	int YAHOO_CALLBACK_TYPE(ext_yahoo_add_handler) (int id, void *fd,
 
		yahoo_input_condition cond, void *data);
 

	
 
/*
 
 * Name: ext_yahoo_remove_handler
 
 * 	Remove the listener for the fd.
 
 * Params:
 
 * 	id   - the id that identifies the connection
 
 * 	tag  - the handler tag to remove
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_remove_handler) (int id, int tag);
 

	
 
/*
 
 * Name: ext_yahoo_connect
 
 * 	Connect to a host:port
 
 * Params:
 
 * 	host - the host to connect to
 
 * 	port - the port to connect on
 
 * Returns:
 
 * 	a unix file descriptor to the socket
 
 */
 
	int YAHOO_CALLBACK_TYPE(ext_yahoo_connect) (const char *host, int port);
 

	
 
/*
 
 * Name: ext_yahoo_connect_async
 
 * 	Connect to a host:port asynchronously. This function should return
 
 * 	immediately returing a tag used to identify the connection handler,
 
 * 	or a pre-connect error (eg: host name lookup failure).
 
 * 	Once the connect completes (successfully or unsuccessfully), callback
 
 * 	should be called (see the signature for yahoo_connect_callback).
 
 * 	The callback may safely be called before this function returns, but
 
 * 	it should not be called twice.
 
 * Params:
 
 * 	id   - the id that identifies this connection
 
 * 	host - the host to connect to
 
 * 	port - the port to connect on
 
 * 	callback - function to call when connect completes
 
 * 	callback_data - data to pass to the callback function
 
 * 	use_ssl - Whether we need an SSL connection
 
 * Returns:
 
 * 	a tag signifying the connection attempt
 
 */
 
	int YAHOO_CALLBACK_TYPE(ext_yahoo_connect_async) (int id,
 
		const char *host, int port, yahoo_connect_callback callback,
 
		void *callback_data, int use_ssl);
 

	
 
/*
 
 * Name: ext_yahoo_get_ip_addr
 
 * 	get IP Address for a domain name
 
 * Params:
 
 * 	domain - Domain name
 
 * Returns:
 
 * 	Newly allocated string containing the IP Address in IPv4 notation
 
 */
 
	char *YAHOO_CALLBACK_TYPE(ext_yahoo_get_ip_addr) (const char *domain);
 

	
 
/*
 
 * Name: ext_yahoo_write
 
 * 	Write data from the buffer into the socket for the specified connection
 
 * Params:
 
 * 	fd  - the file descriptor object that identifies this connection
 
 * 	buf - Buffer to write the data from
 
 * 	len - Length of the data
 
 * Returns:
 
 * 	Number of bytes written or -1 for error
 
 */
 
	int YAHOO_CALLBACK_TYPE(ext_yahoo_write) (void *fd, char *buf, int len);
 

	
 
/*
 
 * Name: ext_yahoo_read
 
 * 	Read data into a buffer from socket for the specified connection
 
 * Params:
 
 * 	fd  - the file descriptor object that identifies this connection
 
 * 	buf - Buffer to read the data into
 
 * 	len - Max length to read
 
 * Returns:
 
 * 	Number of bytes read or -1 for error
 
 */
 
	int YAHOO_CALLBACK_TYPE(ext_yahoo_read) (void *fd, char *buf, int len);
 

	
 
/*
 
 * Name: ext_yahoo_close
 
 * 	Close the file descriptor object and free its resources. Libyahoo2 will not
 
 * 	use this object again.
 
 * Params:
 
 * 	fd  - the file descriptor object that identifies this connection
 
 * Returns:
 
 * 	Nothing
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_close) (void *fd);
 

	
 
/*
 
 * Name: ext_yahoo_got_buddy_change_group
 
 * 	Acknowledgement of buddy changing group
 
 * Params:
 
 * 	id: client id
 
 * 	me: The user
 
 * 	who: Buddy name
 
 * 	old_group: Old group name
 
 * 	new_group: New group name
 
 * Returns:
 
 * 	Nothing
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_buddy_change_group) (int id,
 
		const char *me, const char *who, const char *old_group,
 
		const char *new_group);
 

	
 
};
 

	
 
/*
 
 * if using a callback structure, call yahoo_register_callbacks
 
 * before doing anything else
 
 */
 
void yahoo_register_callbacks(struct yahoo_callbacks *tyc);
 

	
 
#undef YAHOO_CALLBACK_TYPE
 

	
 
#ifdef __cplusplus
 
}
 
#endif
 

	
 
#endif
backends/libyahoo2/yahoo/yahoo2_types.h
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2: yahoo2_types.h
 
 *
 
 * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 *
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
#ifndef YAHOO2_TYPES_H
 
#define YAHOO2_TYPES_H
 

	
 
#include "yahoo_list.h"
 

	
 
#ifdef __cplusplus
 
extern "C" {
 
#endif
 

	
 
	enum yahoo_service {	/* these are easier to see in hex */
 
		YAHOO_SERVICE_LOGON = 1,
 
		YAHOO_SERVICE_LOGOFF,
 
		YAHOO_SERVICE_ISAWAY,
 
		YAHOO_SERVICE_ISBACK,
 
		YAHOO_SERVICE_IDLE,	/* 5 (placemarker) */
 
		YAHOO_SERVICE_MESSAGE,
 
		YAHOO_SERVICE_IDACT,
 
		YAHOO_SERVICE_IDDEACT,
 
		YAHOO_SERVICE_MAILSTAT,
 
		YAHOO_SERVICE_USERSTAT,	/* 0xa */
 
		YAHOO_SERVICE_NEWMAIL,
 
		YAHOO_SERVICE_CHATINVITE,
 
		YAHOO_SERVICE_CALENDAR,
 
		YAHOO_SERVICE_NEWPERSONALMAIL,
 
		YAHOO_SERVICE_NEWCONTACT,
 
		YAHOO_SERVICE_ADDIDENT,	/* 0x10 */
 
		YAHOO_SERVICE_ADDIGNORE,
 
		YAHOO_SERVICE_PING,
 
		YAHOO_SERVICE_GOTGROUPRENAME,	/* < 1, 36(old), 37(new) */
 
		YAHOO_SERVICE_SYSMESSAGE = 0x14,
 
		YAHOO_SERVICE_SKINNAME = 0x15,
 
		YAHOO_SERVICE_PASSTHROUGH2 = 0x16,
 
		YAHOO_SERVICE_CONFINVITE = 0x18,
 
		YAHOO_SERVICE_CONFLOGON,
 
		YAHOO_SERVICE_CONFDECLINE,
 
		YAHOO_SERVICE_CONFLOGOFF,
 
		YAHOO_SERVICE_CONFADDINVITE,
 
		YAHOO_SERVICE_CONFMSG,
 
		YAHOO_SERVICE_CHATLOGON,
 
		YAHOO_SERVICE_CHATLOGOFF,
 
		YAHOO_SERVICE_CHATMSG = 0x20,
 
		YAHOO_SERVICE_GAMELOGON = 0x28,
 
		YAHOO_SERVICE_GAMELOGOFF,
 
		YAHOO_SERVICE_GAMEMSG = 0x2a,
 
		YAHOO_SERVICE_FILETRANSFER = 0x46,
 
		YAHOO_SERVICE_VOICECHAT = 0x4A,
 
		YAHOO_SERVICE_NOTIFY,
 
		YAHOO_SERVICE_VERIFY,
 
		YAHOO_SERVICE_P2PFILEXFER,
 
		YAHOO_SERVICE_PEERTOPEER = 0x4F,	/* Checks if P2P possible */
 
		YAHOO_SERVICE_WEBCAM,
 
		YAHOO_SERVICE_AUTHRESP = 0x54,
 
		YAHOO_SERVICE_LIST,
 
		YAHOO_SERVICE_AUTH = 0x57,
 
		YAHOO_SERVICE_AUTHBUDDY = 0x6d,
 
		YAHOO_SERVICE_ADDBUDDY = 0x83,
 
		YAHOO_SERVICE_REMBUDDY,
 
		YAHOO_SERVICE_IGNORECONTACT,	/* > 1, 7, 13 < 1, 66, 13, 0 */
 
		YAHOO_SERVICE_REJECTCONTACT,
 
		YAHOO_SERVICE_GROUPRENAME = 0x89,	/* > 1, 65(new), 66(0), 67(old) */
 
		YAHOO_SERVICE_Y7_PING = 0x8A,
 
		YAHOO_SERVICE_CHATONLINE = 0x96,	/* > 109(id), 1, 6(abcde) < 0,1 */
 
		YAHOO_SERVICE_CHATGOTO,
 
		YAHOO_SERVICE_CHATJOIN,	/* > 1 104-room 129-1600326591 62-2 */
 
		YAHOO_SERVICE_CHATLEAVE,
 
		YAHOO_SERVICE_CHATEXIT = 0x9b,
 
		YAHOO_SERVICE_CHATADDINVITE = 0x9d,
 
		YAHOO_SERVICE_CHATLOGOUT = 0xa0,
 
		YAHOO_SERVICE_CHATPING,
 
		YAHOO_SERVICE_COMMENT = 0xa8,
 
		YAHOO_SERVICE_GAME_INVITE = 0xb7,
 
		YAHOO_SERVICE_STEALTH_PERM = 0xb9,
 
		YAHOO_SERVICE_STEALTH_SESSION = 0xba,
 
		YAHOO_SERVICE_AVATAR = 0xbc,
 
		YAHOO_SERVICE_PICTURE_CHECKSUM = 0xbd,
 
		YAHOO_SERVICE_PICTURE = 0xbe,
 
		YAHOO_SERVICE_PICTURE_UPDATE = 0xc1,
 
		YAHOO_SERVICE_PICTURE_UPLOAD = 0xc2,
 
		YAHOO_SERVICE_YAB_UPDATE = 0xc4,
 
		YAHOO_SERVICE_Y6_VISIBLE_TOGGLE = 0xc5,	/* YMSG13, key 13: 2 = invisible, 1 = visible */
 
		YAHOO_SERVICE_Y6_STATUS_UPDATE = 0xc6,	/* YMSG13 */
 
		YAHOO_SERVICE_PICTURE_STATUS = 0xc7,	/* YMSG13, key 213: 0 = none, 1 = avatar, 2 = picture */
 
		YAHOO_SERVICE_VERIFY_ID_EXISTS = 0xc8,
 
		YAHOO_SERVICE_AUDIBLE = 0xd0,
 
		YAHOO_SERVICE_Y7_PHOTO_SHARING = 0xd2,
 
		YAHOO_SERVICE_Y7_CONTACT_DETAILS = 0xd3,	/* YMSG13 */
 
		YAHOO_SERVICE_Y7_CHAT_SESSION = 0xd4,
 
		YAHOO_SERVICE_Y7_AUTHORIZATION = 0xd6,	/* YMSG13 */
 
		YAHOO_SERVICE_Y7_FILETRANSFER = 0xdc,	/* YMSG13 */
 
		YAHOO_SERVICE_Y7_FILETRANSFERINFO,	/* YMSG13 */
 
		YAHOO_SERVICE_Y7_FILETRANSFERACCEPT,	/* YMSG13 */
 
		YAHOO_SERVICE_Y7_MINGLE = 0xe1,	/* YMSG13 */
 
		YAHOO_SERVICE_Y7_CHANGE_GROUP = 0xe7,	/* YMSG13 */
 
		YAHOO_SERVICE_MYSTERY = 0xef,	/* Don't know what this is for */
 
		YAHOO_SERVICE_Y8_STATUS = 0xf0,	/* YMSG15 */
 
		YAHOO_SERVICE_Y8_LIST = 0Xf1,	/* YMSG15 */
 
		YAHOO_SERVICE_MESSAGE_CONFIRM = 0xfb,
 
		YAHOO_SERVICE_WEBLOGIN = 0x0226,
 
		YAHOO_SERVICE_SMS_MSG = 0x02ea
 
	};
 

	
 
	enum yahoo_status {
 
		YAHOO_STATUS_AVAILABLE = 0,
 
		YAHOO_STATUS_BRB,
 
		YAHOO_STATUS_BUSY,
 
		YAHOO_STATUS_NOTATHOME,
 
		YAHOO_STATUS_NOTATDESK,
 
		YAHOO_STATUS_NOTINOFFICE,
 
		YAHOO_STATUS_ONPHONE,
 
		YAHOO_STATUS_ONVACATION,
 
		YAHOO_STATUS_OUTTOLUNCH,
 
		YAHOO_STATUS_STEPPEDOUT,
 
		YAHOO_STATUS_INVISIBLE = 12,
 
		YAHOO_STATUS_CUSTOM = 99,
 
		YAHOO_STATUS_IDLE = 999,
 
		YAHOO_STATUS_OFFLINE = 0x5a55aa56	/* don't ask */
 
	};
 

	
 
	enum ypacket_status {
 
		YPACKET_STATUS_DISCONNECTED = -1,
 
		YPACKET_STATUS_DEFAULT = 0,
 
		YPACKET_STATUS_SERVERACK = 1,
 
		YPACKET_STATUS_GAME = 0x2,
 
		YPACKET_STATUS_AWAY = 0x4,
 
		YPACKET_STATUS_CONTINUED = 0x5,
 
		YPACKET_STATUS_INVISIBLE = 12,
 
		YPACKET_STATUS_NOTIFY = 0x16,	/* TYPING */
 
		YPACKET_STATUS_WEBLOGIN = 0x5a55aa55,
 
		YPACKET_STATUS_OFFLINE = 0x5a55aa56
 
	};
 

	
 
#define YAHOO_STATUS_GAME	0x2	/* Games don't fit into the regular status model */
 

	
 
	enum yahoo_login_status {
 
		YAHOO_LOGIN_OK = 0,
 
		YAHOO_LOGIN_LOGOFF = 1,
 
		YAHOO_LOGIN_UNAME = 3,
 
		YAHOO_LOGIN_PASSWD = 13,
 
		YAHOO_LOGIN_LOCK = 14,
 
		YAHOO_LOGIN_DUPL = 99,
 
		YAHOO_LOGIN_SOCK = -1,
 
		YAHOO_LOGIN_UNKNOWN = 999
 
	};
 

	
 
	enum yahoo_error {
 
		E_UNKNOWN = -1,
 
		E_CONNECTION = -2,
 
		E_SYSTEM = -3,
 
		E_CUSTOM = 0,
 

	
 
		/* responses from ignore buddy */
 
		E_IGNOREDUP = 2,
 
		E_IGNORENONE = 3,
 
		E_IGNORECONF = 12,
 

	
 
		/* conference */
 
		E_CONFNOTAVAIL = 20
 
	};
 

	
 
	enum yahoo_log_level {
 
		YAHOO_LOG_NONE = 0,
 
		YAHOO_LOG_FATAL,
 
		YAHOO_LOG_ERR,
 
		YAHOO_LOG_WARNING,
 
		YAHOO_LOG_NOTICE,
 
		YAHOO_LOG_INFO,
 
		YAHOO_LOG_DEBUG
 
	};
 

	
 
	enum yahoo_file_transfer {
 
		YAHOO_FILE_TRANSFER_INIT = 1,
 
		YAHOO_FILE_TRANSFER_ACCEPT = 3,
 
		YAHOO_FILE_TRANSFER_REJECT = 4,
 
		YAHOO_FILE_TRANSFER_DONE = 5,
 
		YAHOO_FILE_TRANSFER_RELAY,
 
		YAHOO_FILE_TRANSFER_FAILED,
 
		YAHOO_FILE_TRANSFER_UNKNOWN
 
	};
 

	
 
#define YAHOO_PROTO_VER 0x0010
 

	
 
/* Yahoo style/color directives */
 
#define YAHOO_COLOR_BLACK "\033[30m"
 
#define YAHOO_COLOR_BLUE "\033[31m"
 
#define YAHOO_COLOR_LIGHTBLUE "\033[32m"
 
#define YAHOO_COLOR_GRAY "\033[33m"
 
#define YAHOO_COLOR_GREEN "\033[34m"
 
#define YAHOO_COLOR_PINK "\033[35m"
 
#define YAHOO_COLOR_PURPLE "\033[36m"
 
#define YAHOO_COLOR_ORANGE "\033[37m"
 
#define YAHOO_COLOR_RED "\033[38m"
 
#define YAHOO_COLOR_OLIVE "\033[39m"
 
#define YAHOO_COLOR_ANY "\033[#"
 
#define YAHOO_STYLE_ITALICON "\033[2m"
 
#define YAHOO_STYLE_ITALICOFF "\033[x2m"
 
#define YAHOO_STYLE_BOLDON "\033[1m"
 
#define YAHOO_STYLE_BOLDOFF "\033[x1m"
 
#define YAHOO_STYLE_UNDERLINEON "\033[4m"
 
#define YAHOO_STYLE_UNDERLINEOFF "\033[x4m"
 
#define YAHOO_STYLE_URLON "\033[lm"
 
#define YAHOO_STYLE_URLOFF "\033[xlm"
 

	
 
	enum yahoo_connection_type {
 
		YAHOO_CONNECTION_PAGER = 0,
 
		YAHOO_CONNECTION_FT,
 
		YAHOO_CONNECTION_YAB,
 
		YAHOO_CONNECTION_WEBCAM_MASTER,
 
		YAHOO_CONNECTION_WEBCAM,
 
		YAHOO_CONNECTION_CHATCAT,
 
		YAHOO_CONNECTION_SEARCH,
 
		YAHOO_CONNECTION_AUTH
 
	};
 

	
 
	enum yahoo_webcam_direction_type {
 
		YAHOO_WEBCAM_DOWNLOAD = 0,
 
		YAHOO_WEBCAM_UPLOAD
 
	};
 

	
 
	enum yahoo_stealth_visibility_type {
 
		YAHOO_STEALTH_DEFAULT = 0,
 
		YAHOO_STEALTH_ONLINE,
 
		YAHOO_STEALTH_PERM_OFFLINE
 
	};
 

	
 
/* chat member attribs */
 
#define YAHOO_CHAT_MALE 0x8000
 
#define YAHOO_CHAT_FEMALE 0x10000
 
#define YAHOO_CHAT_FEMALE 0x10000
 
#define YAHOO_CHAT_DUNNO 0x400
 
#define YAHOO_CHAT_WEBCAM 0x10
 

	
 
	enum yahoo_webcam_conn_type { Y_WCM_DIALUP, Y_WCM_DSL, Y_WCM_T1 };
 

	
 
	struct yahoo_webcam {
 
		int direction;	/* Uploading or downloading */
 
		int conn_type;	/* 0=Dialup, 1=DSL/Cable, 2=T1/Lan */
 

	
 
		char *user;	/* user we are viewing */
 
		char *server;	/* webcam server to connect to */
 
		int port;	/* webcam port to connect on */
 
		char *key;	/* key to connect to the server with */
 
		char *description;	/* webcam description */
 
		char *my_ip;	/* own ip number */
 
	};
 

	
 
	struct yahoo_webcam_data {
 
		unsigned int data_size;
 
		unsigned int to_read;
 
		unsigned int timestamp;
 
		unsigned char packet_type;
 
	};
 

	
 
	struct yahoo_data {
 
		char *user;
 
		char *password;
 

	
 
		char *cookie_y;
 
		char *cookie_t;
 
		char *cookie_c;
 
		char *cookie_b;
 
		char *login_cookie;
 
		char *crumb;
 
		char *seed;
 

	
 
		YList *buddies;
 
		YList *ignore;
 
		YList *identities;
 
		char *login_id;
 

	
 
		int current_status;
 
		int initial_status;
 
		int logged_in;
 

	
 
		int session_id;
 

	
 
		int client_id;
 

	
 
		char *rawbuddylist;
 
		char *ignorelist;
 

	
 
		void *server_settings;
 

	
 
		struct yahoo_process_status_entry *half_user;
 
	};
 

	
 
	struct yab {
 
		int yid;
 
		char *id;
 
		char *fname;
 
		char *lname;
 
		char *nname;
 
		char *email;
 
		char *hphone;
 
		char *wphone;
 
		char *mphone;
 
		int dbid;
 
	};
 

	
 
	struct yahoo_buddy {
 
		char *group;
 
		char *id;
 
		char *real_name;
 
		struct yab *yab_entry;
 
	};
 

	
 
	enum yahoo_search_type {
 
		YAHOO_SEARCH_KEYWORD = 0,
 
		YAHOO_SEARCH_YID,
 
		YAHOO_SEARCH_NAME
 
	};
 

	
 
	enum yahoo_search_gender {
 
		YAHOO_GENDER_NONE = 0,
 
		YAHOO_GENDER_MALE,
 
		YAHOO_GENDER_FEMALE
 
	};
 

	
 
	enum yahoo_search_agerange {
 
		YAHOO_AGERANGE_NONE = 0
 
	};
 

	
 
	struct yahoo_found_contact {
 
		char *id;
 
		char *gender;
 
		char *location;
 
		int age;
 
		int online;
 
	};
 

	
 
/*
 
 * Function pointer to be passed to http get/post and send file
 
 */
 
	typedef void (*yahoo_get_fd_callback) (int id, void *fd, int error,
 
		void *data);
 

	
 
/*
 
 * Function pointer to be passed to yahoo_get_url_handle
 
 */
 
	typedef void (*yahoo_get_url_handle_callback) (int id, void *fd,
 
		int error, const char *filename, unsigned long size,
 
		void *data);
 

	
 
	struct yahoo_chat_member {
 
		char *id;
 
		int age;
 
		int attribs;
 
		char *alias;
 
		char *location;
 
	};
 

	
 
	struct yahoo_process_status_entry { 
 
		char *name;     /* 7      name */ 
 
		int state;      /* 10     state */ 
 
		int flags;      /* 13     flags, bit 0 = pager, bit 1 = chat, bit 2 = game */ 
 
		int mobile;     /* 60     mobile */ 
 
		char *msg;      /* 19     custom status message */ 
 
		int away;       /* 47     away (or invisible) */ 
 
		int buddy_session; /* 11  state */ 
 
		int f17;        /* 17     in chat? then what about flags? */ 
 
		int idle;       /* 137    seconds idle */ 
 
		int f138;       /* 138    state */ 
 
		char *f184;     /* 184    state */ 
 
		int f192;       /* 192    state */ 
 
		int f10001;     /* 10001  state */ 
 
		int f10002;     /* 10002  state */ 
 
		int f198;       /* 198    state */ 
 
		char *f197;     /* 197    state */ 
 
		char *f205;     /* 205    state */ 
 
		int f213;       /* 213    state */ 
 
	};
 

	
 
#ifdef __cplusplus
 
}
 
#endif
 
#endif
backends/libyahoo2/yahoo/yahoo_debug.h
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2: yahoo_debug.h
 
 *
 
 * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 *
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
extern int yahoo_log_message(char *fmt, ...);
 

	
 
#define NOTICE(x) if(yahoo_get_log_level() >= YAHOO_LOG_NOTICE) { yahoo_log_message x; yahoo_log_message("\n"); }
 

	
 
#define LOG(x) if(yahoo_get_log_level() >= YAHOO_LOG_INFO) { yahoo_log_message("%s:%d: ", __FILE__, __LINE__); \
 
	yahoo_log_message x; \
 
	yahoo_log_message("\n"); }
 

	
 
#define WARNING(x) if(yahoo_get_log_level() >= YAHOO_LOG_WARNING) { yahoo_log_message("%s:%d: warning: ", __FILE__, __LINE__); \
 
	yahoo_log_message x; \
 
	yahoo_log_message("\n"); }
 

	
 
#define DEBUG_MSG(x) if(yahoo_get_log_level() >= YAHOO_LOG_DEBUG) { yahoo_log_message("%s:%d: debug: ", __FILE__, __LINE__); \
 
	yahoo_log_message x; \
 
	yahoo_log_message("\n"); }

Changeset was too big and was cut off... Show full diff anyway

0 comments (0 inline, 0 general)