ref: 9922e90ae9072e72a7f61c8328ad1f71cee6cf0f
dir: /cmd.c/
#include <u.h>
#include <libc.h>
#include "dat.h"
#include "fns.h"
#include "cmd.h"
#include "version.h"
static void
cversion(Client *c, Request *r)
{
if (r->args[0]) {
fprint(2, "get version of '%s' (not implemented yet!)\n", r->args[0]);
reply(c, Enosuchserver, r->args[0]);
return;
}
reply(c, Rversion, getversion());
}
static void
cuser(Client *c, Request *r)
{
User *u;
if (!r->args[3]) {
fprint(2, "user without enough args%R\n", *r);
reply(c, Eneedmoreparams, r->cmd->name);
return;
}
u = finduser(r->args[0]);
if (u) {
fprint(2, "user already registered%R\n", *r);
reply(c, Ealreadyregistered);
return;
}
u = adduser(r->args[0]);
c->user = u;
}
static void
cwhois(Client *c, Request *r)
{
if (!r->args[0]) {
/* information about self */
reply(c, Rwhoisuser, c->user->name, c->user->name, c->user->name, c->user->name);
return;
// TODO: fill with proper data!
}
}
static Command commands[] = {
{ "whois", cwhois },
{ "version", cversion },
{ "user", cuser },
};
int ncommands = sizeof(commands) / sizeof(Command);
Command*
findcommand(char *s)
{
for (int i = 0; i < ncommands; i++) {
if (cistrcmp(commands[i].name, s) == 0)
return &commands[i];
}
return nil;
}
Command*
findcommandn(int n)
{
return nil;
}
void
execrequest(Client *c, Request r)
{
if (!(r.cmd && r.cmd->func)) {
fprint(2, "cannot execute request: no command\n");
return;
}
if (debug)
fprint(2, "run command '%s'\n", r.cmd->name);
r.cmd->func(c, &r);
}