shithub: ircd

ref: e3bb18d0464ea795f1805186cc68d4e7a9eedbdc
dir: /client.c/

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

// TODO: generalize doubly linked list, or use other structure

static Client *first = nil;

Client*
addclient(ulong fid)
{
	Client *n;
	n = mallocz(sizeof(Client), 1);
	n->fid = fid;
	n->replies.reply = s_new();
	
	if (!first) {
		first = n;
		return n;
	}
	n->next = first;
	first->prev = n;
	first = n;
	return n;
}

void
delclient(Client *c)
{
	String *s;
	if (!c->prev) {
		first = c->next;
		if (c->next)
			c->next->prev = nil;
	} else {
		c->prev->next = c->next;
		if (c->next)
			c->next->prev = c->prev;
	}
	s = c->replies.reply;
	s_free(s);
	free(c);
}

Client*
findclient(ulong fid)
{
	for (Client *c = first; c; c = c->next) {
		if (fid == c->fid)
			return c;
	}
	return nil;
}

Client*
findnick(char *nick)
{
	for (Client *c = first; c; c = c->next) {
		if (c->nick && strcmp(c->nick, nick) == 0)
			return c;
	}
	return nil;
}