shithub: ircs

ref: 1f0209482526cfabe74ad96f4182c7d333a1c5f1
dir: /ircfmt.c/

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

static char *wday[] = {
	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
	"Sat",
};
static char *mon[] = {
	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
};

int
ircfmttime(char *buf, int bufsize, long time, int *mday)
{
	Tm *t;
	int n;

	n = 0;
	t = localtime(time);

	if(t->mday != *mday){
		*mday = t->mday;
		n += snprint(buf, bufsize,
			"-- %s, %02d %s %d --\n",
			wday[t->wday], t->mday, mon[t->mon],
			t->year + 1900);
		bufsize -= n;
		buf += n;
	}
	n += snprint(buf, bufsize,
		"%02d:%02d:%02d ",
		t->hour, t->min, t->sec);
	return n;
}

void
ircfmtjoin(Ircmsg *irc, char *buf, int bufsize)
{
	if(irc->pre != nil){
		if(logdir != nil)
			snprint(buf, bufsize,
				"JOIN (%s)",
				irc->nick);
		else
			snprint(buf, bufsize,
				"JOIN (%s)\t%s",
				irc->nick, irc->par[0]);
	} else
		snprint(buf, bufsize,
			"JOIN %s",
			irc->par[0]);
}

void
ircfmtnick(Ircmsg *irc, char *buf, int bufsize)
{
	if(irc->pre != nil){
		snprint(buf, bufsize,
			"NICK %s → %s",
			irc->nick, irc->par[0]);
	} else
		snprint(buf, bufsize,
			"NICK %s",
			irc->par[0]);
}

void
ircfmtquit(Ircmsg *irc, char *buf, int bufsize)
{
	if(irc->pre != nil){
		snprint(buf, bufsize,
			"QUIT (%s)\t%s",
			irc->nick, irc->trail);
	} else
		snprint(buf, bufsize,
			"QUIT\t%s",
			irc->trail);
}

void
ircfmtpart(Ircmsg *irc, char *buf, int bufsize)
{
	if(irc->pre != nil){
		if(logdir != nil)
			snprint(buf, bufsize,
				"PART (%s)",
				irc->nick);
		else
			snprint(buf, bufsize,
				"PART (%s)\t%s",
				irc->nick, irc->par[0]);
	} else
		snprint(buf, bufsize,
			"PART %s",
			irc->par[0]);
}

void
ircfmtpriv(Ircmsg *irc, char *buf, int bufsize)
{
	if(irc->pre != nil){
		if(logdir != nil && ircischan(irc->par[0]))
			snprint(buf, bufsize,
				"%s → %s",
				irc->nick, irc->trail);
		else
			snprint(buf, bufsize,
				"(%s) %s → %s",
				irc->par[0], irc->nick, irc->trail);
	} else {
		if(logdir != nil && ircischan(irc->par[0]))
			snprint(buf, bufsize,
				"⇐ %s",
				irc->trail);
		else
			snprint(buf, bufsize,
				"(%s) ⇐ %s",
				irc->par[0], irc->trail);
	}
}

void
ircfmtaction(Ircmsg *irc, char *buf, int bufsize)
{
	int n;

	n = strlen(irc->trail);
	if(irc->trail[n-1] == 1)
		irc->trail[n-1] = 0;

	if(logdir != nil && ircischan(irc->par[0]))
		snprint(buf, bufsize,
			"* %s %s",
			irc->nick, irc->trail+8);
	else
		snprint(buf, bufsize,
			"(%s) * %s %s",
			irc->par[0], irc->nick, irc->trail+8);
}

void
ircfmtnumeric(Ircmsg *irc, char *buf, int bufsize)
{
	int i, n;

	*buf = 0;
	n = snprint(buf, bufsize, irc->cmd);
	bufsize -= n;
	buf += n;

	for(i = 1; i < irc->npar; i++){
		n = snprint(buf, bufsize, " %s", irc->par[i]);
		bufsize -= n;
		buf += n;
	}
}