implement Srv;
include "sys.m";
sys: Sys;
include "draw.m";
include "bufio.m";
bufio : Bufio;
include "string.m";
str : String;
include "irc.m";
ircdata : Ircdata;
ircusers : ref Ircdata->Users;
ircservices : ref Ircdata->Services;
Srv: module
{
init: fn(ctxt: ref Draw->Context, args: list of string);
};
init(ctxt: ref Draw->Context, args: list of string)
{
sys = load Sys Sys->PATH;
bufio = load Bufio Bufio->PATH;
str = load String String->PATH;
ircdata = load Ircdata Ircdata->PATH;
ircusers = ircdata->new_users(10);
Services, Service, Users : import ircdata;
ircservices = ref ircdata->Services;
out := chan of string;
(i, c) := sys->dial("tcp!192.168.1.14!6668", nil);
if(i == -1) {
return;
}
spawn deal_with_outgoing(c.dfd, out);
out <-= sys->sprint("PASS %s", "s3rv1c3Z");
out <-= sys->sprint("PROTOCL TOKEN NICKv2 VHP UMODE2 SJOIN SJOIN2 SJ3 NOQUIT TKLEXT");
out <-= sys->sprint("SERVER %s 1:%s", "services.maht0x0r.net", "Srvd, you have been");
out <-= sys->sprint("NICK %s 1 %d %s %s %s 0 +%s * :%s", "NickServ", 1145232570, "NickServ", "services.maht0x0r.net", "services.maht0x0r.net", "io", "Nick Services");
#
service := ircservices.add("NickServ", out);
service.add_command("IDENTIFY", ircdata->identify);
service.add_command("NICK", ircdata->nick);
spawn deal_with_incoming(c.dfd, out);
}
deal_with_outgoing(fd: ref Sys->FD, out: chan of string)
{
msg : string;
while(1) {
msg = <-out;
sys->print("<<OUT<<%s<<\n", msg);
sys->fprint(fd, "%s\n", msg);
};
}
dump_array(indent: string, a: array of string)
{
for(i := 0; i < len a; i ++) {
sys->print("%s[%d] = \"%s\"\n", indent, i, a[i]);
}
}
execute_cmd(service: ref Ircdata->Service, post: ref Ircdata->Post, command: string) : int
{
if(service == nil || post == nil)
return 0;
Service, Command : import ircdata;
cmd := service.find_command(command);
if(cmd == nil) {
service.out <-= sys->sprint(":%s PRIVMSG %s :COMMAND \"%s\" not found", service.nick, post.issuing_nick, command);
return 0;
}
return cmd.func(service, ircusers, post);
}
deal_with_incoming(fd: ref Sys->FD, out: chan of string)
{
Iobuf: import bufio;
User, Users, Services, Service, Command, Post : import ircdata;
post : ref Post;
server_nick, service_name, cmd : string;
connected := 1;
srvio := bufio->fopen(fd, Bufio->OREAD);
while(connected) {
data := srvio.gets('\n');
if(data[len data - 1] == '\n')
data = data[0:len data - 2];
if(data[len data - 1] == '
')
data = data[0:len data - 2];
post = ircdata->new_post(data);
if(post.issuing_nick != nil) {
post.issuer = ircusers.find(post.issuing_nick);
case post.command {
"PRIVMSG" => {
service_name = post.args[0];
cmd = str->toupper(post.args[1]);
}
"NICK" => {
service_name = "NickServ";
cmd = "NICK";
}
}
sys->print(">>CMD %s<<\n", cmd);
service := ircservices.find(str->toupper(service_name));
if(service == nil)
continue;
execute_cmd(service, post, cmd);
} else {
sys->print(">>CMD %s<<\n", post.command);
case post.command {
"PING" => {
connected = 0;
}
"SERVER" => {
server_nick = post.args[0];
ircusers.add(server_nick);
}
"NICK" => {
ircusers.add(post.args[0]);
post.target = ircusers.find(post.args[0]);
}
}
}
post.print();
}
}
|