shithub: ircd

ref: c86384b841687ea975571db934fb0e353ce5b5d4
dir: /reply.c/

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

char*
getreplies(Client *c)
{
	return s_to_c((String*)c->replies.reply);
}

void
flushreplies(Client *c)
{
	String *s;
	if (!c->replies.reply)
		return;
	s = c->replies.reply;
	s_reset(s);
}

void
reply(Client *c, Reply repl, ...)
{
	char buf[1024];
	int i;
	va_list arg;
	String *s;
	
	i = snprint(buf, sizeof buf, ":%s %03d %s ", sysnameb, repl.nr, c->nick);
	
	va_start(arg, repl);
	i += vsnprint(&buf[i], sizeof(buf) - i, repl.msg, arg);
	va_end(arg);
	
	snprint(&buf[i], sizeof(buf) - i, "\r\n");
	
	if (debug > 1)
		fprint(2, "reply: '%s'\n", buf);
	qlock(&c->replies);
	s = c->replies.reply;
	s_append(s, buf);
	qunlock(&c->replies);
}

void
ircsend(Client *c, Client *sender, Reply repl, ...)
{
	char buf[1024];
	int i;
	va_list arg;
	String *s;
	
	if (sender) {
		i = snprint(buf, sizeof buf, ":%I ", sender);
	} else {
		i = snprint(buf, sizeof buf, ":%s ", sysnameb);
	}
	
	va_start(arg, repl);
	i += vsnprint(&buf[i], sizeof(buf) - i, repl.msg, arg);
	va_end(arg);
	
	snprint(&buf[i], sizeof(buf) - i, "\r\n");
	
	if (debug > 1)
		fprint(2, "ircsend (%s): '%s'\n", c->nick, buf);
	
	qlock(&c->replies);
	s = c->replies.reply;
	s_append(s, buf);
	qunlock(&c->replies);
}

void
ircerror(Client *c, char *msg, ...)
{
	char buf[1024];
	int i;
	va_list arg;
	String *s;
	
	i = snprint(buf, sizeof buf, ":%s ERROR :", sysnameb);
	
	va_start(arg, msg);
	i += vsnprint(&buf[i], sizeof(buf) - i, msg, arg);
	va_end(arg);
	
	snprint(&buf[i], sizeof(buf) - i, "\r\n");
	
	if (debug > 1)
		fprint(2, "error: '%s'\n", buf);
	
	qlock(&c->replies);
	s = c->replies.reply;
	s_append(s, buf);
	qunlock(&c->replies);
}