aboutsummaryrefslogtreecommitdiffstats
path: root/protocols/yahoo/yahoo_list.c
diff options
context:
space:
mode:
Diffstat (limited to 'protocols/yahoo/yahoo_list.c')
-rw-r--r--protocols/yahoo/yahoo_list.c236
1 files changed, 236 insertions, 0 deletions
diff --git a/protocols/yahoo/yahoo_list.c b/protocols/yahoo/yahoo_list.c
new file mode 100644
index 00000000..cda631c6
--- /dev/null
+++ b/protocols/yahoo/yahoo_list.c
@@ -0,0 +1,236 @@
+/*
+ * yahoo_list.c: linked list routines
+ *
+ * Some code copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
+ * Other code copyright Meredydd Luff <meredydd AT everybuddy.com>
+ *
+ * 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
+ *
+ * Some of this code was borrowed from elist.c in the eb-lite sources
+ *
+ */
+
+#include <stdlib.h>
+
+#include "yahoo_list.h"
+
+YList *y_list_append(YList * list, void *data)
+{
+ YList *n;
+ YList *new_list = malloc(sizeof(YList));
+ YList *attach_to = NULL;
+
+ new_list->next = NULL;
+ new_list->data = data;
+
+ for (n = list; n != NULL; n = n->next) {
+ attach_to = n;
+ }
+
+ if (attach_to == NULL) {
+ new_list->prev = NULL;
+ return new_list;
+ } else {
+ new_list->prev = attach_to;
+ attach_to->next = new_list;
+ return list;
+ }
+}
+
+YList *y_list_prepend(YList * list, void *data)
+{
+ YList *n = malloc(sizeof(YList));
+
+ n->next = list;
+ n->prev = NULL;
+ n->data = data;
+ if (list)
+ list->prev = n;
+
+ return n;
+}
+
+YList *y_list_concat(YList * list, YList * add)
+{
+ YList *l;
+
+ if(!list)
+ return add;
+
+ if(!add)
+ return list;
+
+ for (l = list; l->next; l = l->next)
+ ;
+
+ l->next = add;
+ add->prev = l;
+
+ return list;
+}
+
+YList *y_list_remove(YList * list, void *data)
+{
+ YList *n;
+
+ for (n = list; n != NULL; n = n->next) {
+ if (n->data == data) {
+ list=y_list_remove_link(list, n);
+ y_list_free_1(n);
+ break;
+ }
+ }
+
+ return list;
+}
+
+/* Warning */
+/* link MUST be part of list */
+/* caller must free link using y_list_free_1 */
+YList *y_list_remove_link(YList * list, const YList * link)
+{
+ if (!link)
+ return list;
+
+ if (link->next)
+ link->next->prev = link->prev;
+ if (link->prev)
+ link->prev->next = link->next;
+
+ if (link == list)
+ list = link->next;
+
+ return list;
+}
+
+int y_list_length(const YList * list)
+{
+ int retval = 0;
+ const YList *n = list;
+
+ for (n = list; n != NULL; n = n->next) {
+ retval++;
+ }
+
+ return retval;
+}
+
+/* well, you could just check for list == NULL, but that would be
+ * implementation dependent
+ */
+int y_list_empty(const YList * list)
+{
+ if(!list)
+ return 1;
+ else
+ return 0;
+}
+
+int y_list_singleton(const YList * list)
+{
+ if(!list || list->next)
+ return 0;
+ return 1;
+}
+
+YList *y_list_copy(YList * list)
+{
+ YList *n;
+ YList *copy = NULL;
+
+ for (n = list; n != NULL; n = n->next) {
+ copy = y_list_append(copy, n->data);
+ }
+
+ return copy;
+}
+
+void y_list_free_1(YList * list)
+{
+ free(list);
+}
+
+void y_list_free(YList * list)
+{
+ YList *n = list;
+
+ while (n != NULL) {
+ YList *next = n->next;
+ free(n);
+ n = next;
+ }
+}
+
+YList *y_list_find(YList * list, const void *data)
+{
+ YList *l;
+ for (l = list; l && l->data != data; l = l->next)
+ ;
+
+ return l;
+}
+
+void y_list_foreach(YList * list, YListFunc fn, void * user_data)
+{
+ for (; list; list = list->next)
+ fn(list->data, user_data);
+}
+
+YList *y_list_find_custom(YList * list, const void *data, YListCompFunc comp)
+{
+ YList *l;
+ for (l = list; l; l = l->next)
+ if (comp(l->data, data) == 0)
+ return l;
+
+ return NULL;
+}
+
+YList *y_list_nth(YList * list, int n)
+{
+ int i=n;
+ for ( ; list && i; list = list->next, i--)
+ ;
+
+ return list;
+}
+
+YList *y_list_insert_sorted(YList * list, void *data, YListCompFunc comp)
+{
+ YList *l, *n, *prev = NULL;
+ if (!list)
+ return y_list_append(list, data);
+
+ n = malloc(sizeof(YList));
+ n->data = data;
+ for (l = list; l && comp(l->data, n->data) <= 0; l = l->next)
+ prev = l;
+
+ if (l) {
+ n->prev = l->prev;
+ l->prev = n;
+ } else
+ n->prev = prev;
+
+ n->next = l;
+
+ if(n->prev) {
+ n->prev->next = n;
+ return list;
+ } else {
+ return n;
+ }
+
+}
f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
  /********************************************************************\
  * BitlBee -- An IRC to other IM-networks gateway                     *
  *                                                                    *
  * Copyright 2002-2004 Wilmer van der Gaast and others                *
  \********************************************************************/

/* Stuff to handle, save and search IRC buddies                         */

/*
  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 with
  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  Suite 330, Boston, MA  02111-1307  USA
*/

#include "bitlbee.h"

irc_user_t *irc_user_new( irc_t *irc, const char *nick )
{
	irc_user_t *iu = g_new0( irc_user_t, 1 );
	
	iu->irc = irc;
	iu->nick = g_strdup( nick );
	iu->user = iu->host = iu->fullname = iu->nick;
	
	if( set_getbool( &irc->b->set, "private" ) )
		iu->last_channel = NULL;
	else
		iu->last_channel = irc->default_channel;
	
	iu->key = g_strdup( nick );
	nick_lc( iu->key );
	/* Using the hash table for speed and irc->users for easy iteration
	   through the list (since the GLib API doesn't have anything sane
	   for that.) */
	g_hash_table_insert( irc->nick_user_hash, iu->key, iu );
	irc->users = g_slist_insert_sorted( irc->users, iu, irc_user_cmp );
	
	return iu;
}

int irc_user_free( irc_t *irc, irc_user_t *iu )
{
	GSList *l;
	gboolean send_quit = FALSE;
	
	if( !iu )
		return 0;
	
	irc->users = g_slist_remove( irc->users, iu );
	g_hash_table_remove( irc->nick_user_hash, iu->key );
	
	for( l = irc->channels; l; l = l->next )
		send_quit |= irc_channel_del_user( (irc_channel_t*) l->data, iu, TRUE, NULL );
	
	if( send_quit )
	{
		static struct im_connection *last_ic;
		static char *msg;
		
		if( iu->bu &&
		    ( iu->bu->ic->flags & OPT_LOGGING_OUT ) &&
		    iu->bu->ic != last_ic )
		{
			char host_prefix[] = "bitlbee.";
			char *s;
			
			/* Irssi recognises netsplits by quitmsgs with two
			   hostnames, where a hostname is a "word" with one
			   of more dots. Mangle no-dot hostnames a bit. */
			if( strchr( irc->root->host, '.' ) )
				*host_prefix = '\0';
			
			last_ic = iu->bu->ic;
			g_free( msg );
			if( !set_getbool( &irc->b->set, "simulate_netsplit" ) )
				msg = g_strdup( "Account off-line" );
			else if( ( s = strchr( iu->bu->ic->acc->user, '@' ) ) )
				msg = g_strdup_printf( "%s%s %s", host_prefix,
				        irc->root->host, s + 1 );
			else
				msg = g_strdup_printf( "%s%s %s.%s",
					host_prefix, irc->root->host,
					iu->bu->ic->acc->prpl->name, irc->root->host );
		}
		else if( !iu->bu || !( iu->bu->ic->flags & OPT_LOGGING_OUT ) )
		{
			g_free( msg );
			msg = g_strdup( "Removed" );
			last_ic = NULL;
		}
		irc_send_quit( iu, msg );
	}
	
	g_free( iu->nick );
	if( iu->nick != iu->user ) g_free( iu->user );
	if( iu->nick != iu->host ) g_free( iu->host );
	if( iu->nick != iu->fullname ) g_free( iu->fullname );
	g_free( iu->pastebuf );
	if( iu->pastebuf_timer ) b_event_remove( iu->pastebuf_timer );
	g_free( iu->key );
	g_free( iu );
	
	return 1;
}

irc_user_t *irc_user_by_name( irc_t *irc, const char *nick )
{
	char key[strlen(nick)+1];
	
	strcpy( key, nick );
	if( nick_lc( key ) )
		return g_hash_table_lookup( irc->nick_user_hash, key );
	else
		return NULL;
}

int irc_user_set_nick( irc_user_t *iu, const char *new )
{
	irc_t *irc = iu->irc;
	char key[strlen(new)+1];
	GSList *cl;
	
	strcpy( key, new );
	if( iu == NULL || !nick_lc( key ) || irc_user_by_name( irc, new ) )
		return 0;
	
	for( cl = irc->channels; cl; cl = cl->next )
	{
		irc_channel_t *ic = cl->data;
		
		/* Send a NICK update if we're renaming our user, or someone
		   who's in the same channel like our user. */
		if( iu == irc->user ||
		    ( ( ic->flags & IRC_CHANNEL_JOINED ) &&
		      irc_channel_has_user( ic, iu ) ) )
		{
			irc_send_nick( iu, new );
			break;
		}
	}
	
	irc->users = g_slist_remove( irc->users, iu );
	g_hash_table_remove( irc->nick_user_hash, iu->key );
	
	if( iu->nick == iu->user ) iu->user = NULL;
	if( iu->nick == iu->host ) iu->host = NULL;
	if( iu->nick == iu->fullname ) iu->fullname = NULL;
	g_free( iu->nick );
	iu->nick = g_strdup( new );
	if( iu->user == NULL ) iu->user = g_strdup( iu->nick );
	if( iu->host == NULL ) iu->host = g_strdup( iu->nick );
	if( iu->fullname == NULL ) iu->fullname = g_strdup( iu->nick );
	
	iu->key = g_strdup( key );
	g_hash_table_insert( irc->nick_user_hash, iu->key, iu );
	irc->users = g_slist_insert_sorted( irc->users, iu, irc_user_cmp );
	
	return 1;
}

gint irc_user_cmp( gconstpointer a_, gconstpointer b_ )
{
	const irc_user_t *a = a_, *b = b_;
	
	return strcmp( a->key, b->key );
}

const char *irc_user_get_away( irc_user_t *iu )
{
	irc_t *irc = iu->irc;
	bee_user_t *bu = iu->bu;
	
	if( iu == irc->user )
		return set_getstr( &irc->b->set, "away" );
	else if( bu )
	{
		if( !bu->flags & BEE_USER_ONLINE )
			return "Offline";
		else if( bu->flags & BEE_USER_AWAY )
		{
			if( bu->status_msg )
			{
				static char ret[MAX_STRING];
				g_snprintf( ret, MAX_STRING - 1, "%s (%s)",
				            bu->status ? : "Away", bu->status_msg );
				return ret;
			}
			else
				return bu->status ? : "Away";
		}
	}
	
	return NULL;
}

/* User-type dependent functions, for root/NickServ: */
static gboolean root_privmsg( irc_user_t *iu, const char *msg )
{
	char cmd[strlen(msg)+1];
	
	g_free( iu->irc->last_root_cmd );
	iu->irc->last_root_cmd = g_strdup( iu->nick );
	
	strcpy( cmd, msg );
	root_command_string( iu->irc, cmd );
	
	return TRUE;
}

static gboolean root_ctcp( irc_user_t *iu, char * const *ctcp )
{
	if( g_strcasecmp( ctcp[0], "VERSION" ) == 0 )
	{
		irc_send_msg_f( iu, "NOTICE", iu->irc->user->nick, "\001%s %s\001",
		                ctcp[0], "BitlBee " BITLBEE_VERSION " " ARCH "/" CPU );
	}
	else if( g_strcasecmp( ctcp[0], "PING" ) == 0 )
	{
		irc_send_msg_f( iu, "NOTICE", iu->irc->user->nick, "\001%s %s\001",
		                ctcp[0], ctcp[1] ? : "" );
	}
	
	return TRUE;
}

const struct irc_user_funcs irc_user_root_funcs = {
	root_privmsg,
	root_ctcp,
};

/* Echo to yourself: */
static gboolean self_privmsg( irc_user_t *iu, const char *msg )
{
	irc_send_msg_raw( iu, "PRIVMSG", iu->nick, msg );
	
	return TRUE;
}

const struct irc_user_funcs irc_user_self_funcs = {
	self_privmsg,
};