shithub: ircs

ref: a90d1130f47c07132f1816366b05f18929911e60
dir: /irc.c/

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

static int
ircprefix(Ircmsg *irc)
{
	char *p;

	irc->nick = nil;
	irc->user = nil;
	irc->host = nil;
	irc->prebuf[0] = 0;
	
	if(irc->pre == nil)
		return -1;

	snprint(irc->prebuf, sizeof(irc->prebuf), "%s", irc->pre);

	irc->nick = irc->prebuf;

	for(p = irc->prebuf; *p != 0; p++){
		if(*p == '@'){
			*p = 0;
			irc->host = p+1;
			break;
		}
		if(irc->user == nil && *p == '!'){
			*p = 0;
			irc->user = p+1;
		}
	}
	return 0;
}

int
ircparse(Ircmsg *irc, char *msg)
{
	int i;
	char *p;

	snprint(irc->buf, sizeof(irc->buf), "%s", msg);

	irc->pre = nil;
	irc->cmd = nil;
	for(i = 0; i < 15; i++)
		irc->par[i] = nil;
	irc->trail = nil;
	irc->npar = 0;

	i = 0;
	p = irc->buf;

	if(*p == ':')
		irc->pre = ++p;
	else
		irc->cmd = p;

	for(; *p != 0; p++){
		if(*p == ' '){
			*p = 0;
			if(irc->cmd == nil)
				irc->cmd = p+1;
			else {
				if(i == 14 || *(p+1) == ':'){
					if(*(p+1) == ':')
						irc->trail = p+2;
					else
						irc->trail = p+1;
					irc->par[i++] = irc->trail;
					break;
				}
				irc->par[i++] = p+1;
			}
		}
	}
	assert(irc->trail == nil || irc->trail == irc->par[i-1]);
	irc->npar = i;
	ircprefix(irc);
	return 0;
}

void
ircprint(Ircmsg *irc)
{
	int i;

	fprint(2, "pre=%s cmd=%s", irc->pre, irc->cmd);

	for(i = 0; i < irc->npar; i++)
		fprint(2, " par[%d]=%s", i, irc->par[i]);

	fprint(2, " trail=%s\n", irc->trail != nil ? "y" : "n");
}

int
ircischan(char *target)
{
	return target != nil && target[0] == '#';
}