-
Notifications
You must be signed in to change notification settings - Fork 0
/
crackbot.crk
108 lines (88 loc) · 2.62 KB
/
crackbot.crk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
## crack irc bot
## Copyright 2012 Shannon Weyrick <weyrick@mozek.us>
import crack.time TimeDelta;
import lib.irc Client, User, Channel;
import crack.net.httpsrv HTTPServer, HTTPRequest;
import crack.io cout, cin, StringFormatter, FStr, FDReader;
import crack.lang die, ManagedBuffer;
import crack.functor Function1;
import crack.logger setLogLevel, INFO;
setLogLevel(INFO);
class CrackBot: Client {
oper init(String s, uint p, String n, String un, String rn):
Client(s,p,n,un,rn) {
_debug = true;
}
void _onConnect() {
//join("#crack-lang");
join("#crack-lang-test");
}
void _onJoin(User user, Channel channel) {
cout `$(user.nick) joined $(channel.name)\n`;
say(channel.name, FStr() `yo $(user.nick), whatchu need?`);
}
void _onPart(User user, Channel channel) {
cout `$(user.nick) parted $(channel.name)\n`;
}
void _onPrivMsg(String source, String target, String msg) {
cout `$source said [$msg] on $target\n`;
}
}
bot := CrackBot("irc.roadsend.com", 6667, 'crack|','crackbot','http://crack-lang.org');
//bot := CrackBot("irc.freenode.net", 6667, 'crack|','crackbot','http://crack-lang.org');
if (!bot.connect())
die("unable to connect");
String botStatusPage() {
result := StringFormatter();
result `<html>\n`;
result `Connected to: $(bot.getServerName())<br>\n`;
cList := bot.getChannelList();
result `clist: $cList<br>`;
for (c :in cList) {
result `$(c.key)<br>`;
}
result `</html>\n`;
return result.makeString();
}
bool handler(HTTPRequest req) {
if (req.path == '/') {
if (bot.isConnected()) {
req.sendReply(200, 'text/html', botStatusPage());
}
else
req.sendReply(200, 'text/html', 'IRC not connected');
return true;
} else {
return false;
}
}
bool notFound(HTTPRequest req) {
req.sendReply(404, 'text/plain', 'Not found.');
return true;
}
s := HTTPServer(8080);
if (!(s is null)) {
s.addHandler(Function1[bool, HTTPRequest](handler));
s.addHandler(Function1[bool, HTTPRequest](notFound));
}
// don't block on reads from stdin
cin.setNonBlocking(true);
cbuf := ManagedBuffer(16);
int cresult = 0;
to := TimeDelta(0,200000); // 200 ms
while (true) {
// process irc
bot.processOnce(to);
// process http
if (!(s is null))
s.processOnce(to);
// process console input
cresult = cin.read(cbuf);
if (cresult > 0) {
input := String(cbuf, cresult, false);
if (input.rtrim() == "q")
break;
else
bot.say("#crack-lang", input.rtrim());
}
}