shithub: ircd

ref: b402639434e9360c99fc8b9cb12d51614abfbeea
dir: /ircd.c/

View raw version
#include <u.h>
#include <libc.h>
#include <auth.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
#include <bio.h>
#include "dat.h"
#include "fns.h"
#include "version.h"

void
usage(void)
{
	fprint(2, "usage: %s\n", argv0);
	exits("usage");
}

static Biobuf *bio;
static char *sysnameb = "nil";
static char *versionstring = nil;
int debug = 0;

char*
getversion()
{
	if (versionstring)
		return versionstring;
	
	versionstring = smprint("%s.%d %s :%s", Vversion, debug, Vname, Vcomment);
	return versionstring;
}

static void
parselines(void)
{
	char *line;
	Request parsedrequest;
	
	while (line = Brdstr(bio, '\n', 1)) {
		parsedrequest = parseline(line);
		if (debug > 1)
			fprint(2, "request: %R\n", parsedrequest);
		execrequest(parsedrequest);
		free(line);
	}
}

char *mtpt = "/mnt/ircd";
char *srvname = "ircd";
Reqqueue *reqqueue = nil;

// TODO: implement hasdata for client. put all replies in a bulk buffer.
int hasdata = 0;
QLock datalock;

static void
readproc(Req *r)
{
	while (1) {
		qlock(&datalock);
		if (hasdata) {
			readstr(r, "hasdata\n");
			fprint(2, "responded with hasdata\n");
			hasdata = 0;
			qunlock(&datalock);
			respond(r, nil);
			return;
		}
		qunlock(&datalock);
	}
}

static void
fsread(Req *r)
{
	reqqueuepush(reqqueue, r, readproc);
}

static void
fswrite(Req *r)
{
	char line[512];
	char *c;
	Request parsed;
	
	if (r->ifcall.count > 512) {
		respond(r, "too many bytes");
		return;
	}
	memcpy(line, r->ifcall.data, r->ifcall.count);
	r->ofcall.count = r->ifcall.count;
	line[r->ifcall.count-1] = 0; /* last char is either \n or \r */
	
	c = strchr(line, '
');
	if (c)
		*c = 0;
	
	parsed = parseline(line);
	if (debug > 1)
		fprint(2, "request:%R", parsed);
	execrequest(parsed);
	
	qlock(&datalock);
	hasdata = 1;
	qunlock(&datalock);
	
	respond(r, nil);
}

static void
fsflush(Req *r)
{
	reqqueueflush(reqqueue, r->oldreq);
	respond(r, nil);
}

Srv fs = {
	.read = fsread,
	.write = fswrite,
	.flush = fsflush,
};

void
populatetree(File *root)
{
	File *f;
	
	if ((f = createfile(root, "irc", nil, 0666, nil)) != nil) {
		f->length = 0;
	}
}

void
threadmain(int argc, char **argv)
{
	ARGBEGIN{
	case 'h':
		usage();
		break;
	case 'v':
		fprint(2, "version: %s\n", getversion());
		exits(nil);
	case 's':
		srvname = EARGF(usage());
		break;
	case 'm':
		mtpt = EARGF(usage());
		break;
	case 'd':
		debug++;
		break;
	case 'D':
		chatty9p++;
		break;
	}ARGEND;
	
	fmtinstall('R', Rfmt);
	
	sysnameb = sysname();
	reqqueue = reqqueuecreate();
	
	fs.tree = alloctree(nil, nil, DMDIR|0777, nil);
	populatetree(fs.tree->root);
	threadpostmountsrv(&fs, srvname, mtpt, MREPL|MCREATE);
	exits(nil);
}

void
reply(Reply repl, ...)
{
	char buf[511];
	va_list arg;
	
	va_start(arg, repl);
	vsnprint(buf, sizeof buf, repl.msg, arg);
	va_end(arg);
	print(":%s %03d %s\r\n", sysnameb, repl.nr, buf);
}