shithub: ircd

ref: eaf9a2c0694335ea7d5139f83b3824f6127fb310
dir: /client.c/

View raw version
#include <u.h>
#include <libc.h>
#include <String.h>
#include "dat.h"
#include "fns.h"
#include "ll.h"

static Linked *clients = nil;

Client*
addclient(ulong fid)
{
	Client *n;
	n = mallocz(sizeof(Client), 1);
	n->fid = fid;
	n->replies.reply = s_new();
	
	ladd(&clients, n);
	return n;
}

static void
freeclient(void *ptr)
{
	String *s;
	Client *c = ptr;
	s = c->replies.reply;
	s_free(s);
	free(c);
}

void
delclient(Client *c)
{
	ldel(&clients, c, freeclient);
}

static int
findbyfid(void *ptr, void *aux)
{
	ulong *fid = aux;
	Client *c = ptr;
	return *fid == c->fid;
}

Client*
findclient(ulong fid)
{
	Client *c = lfind(&clients, findbyfid, &fid);
	return c;
}

static int
findbynick(void *ptr, void *aux)
{
	char *nick = aux;
	Client *c = ptr;
	return c->nick && strcmp(c->nick, nick) == 0;
}

Client*
findnick(char *nick)
{
	return lfind(&clients, findbynick, nick);
}