-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.pike
102 lines (88 loc) · 3.06 KB
/
bot.pike
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
#!/usr/bin/env pike
import Stdio;
object con = File();
int main(){
string server = "irc.eighthbit.net";
string nick = "Spike";
string userln = "spike spike spike spike";
int port = 6667;
array channels = ({"#bots"});
connect(server,nick,userln,port,channels,1);
// Still alive \o/
string data;
while(1){
data = con->read();
if(data) write(data + "\n");
if(data != 0){
if(data == ""){
write("Reconnecting in 5 seconds.\n");
sleep(5);
if(connect(server,nick,userln,port,channels,1) == 1){
// We can add something here to do something once we reconnect.
// Not necessary, but doable.
}
}
if(Regexp.match("^PING",data) == 1){
// Probably a better way to parse this.
if(array ping = Regexp.split2("PING (.*)",data)){
sendln("PONG " + ping[1]);
}
}
array pts = Regexp.split2(":(.+)!(.+)@(.+) PRIVMSG (.+) :(.+)", data);
if(Regexp.match("!test",data) == 1){
sendln("PRIVMSG " + pts[4] + " :you are " + pts[1]);
}
if(Regexp.match("!time",data) == 1){
int hour = localtime(time())["hour"];
int minute = localtime(time())["min"];
sendln("PRIVMSG " + pts[4] + " :time is " + hour + ":" + minute);
}
if(Regexp.match("!randstr",data) == 1){
sendln("PRIVMSG " + pts[4] + " :" + replace(replace(random_string(255),"\n",""),"\r",""));
}
if(Regexp.match("!join",data) == 1){
if(array channel = Regexp.split2("^!join (.+)",pts[5])){
sendln("JOIN " + channel[1]);
}
}
if(Regexp.match("!murder",data) == 1){
if(array who = Regexp.split2("^!murder (.+)",pts[5])){
sendln("PRIVMSG " + pts[4] + " :\001ACTION murders " + (who[1] - "\r" - "\n" - " ") + " as per " + pts[1] + "'s command.\001");
}
}
if(Regexp.match("an idea", data) || Regexp.match("a taco", data)){
sendln("PRIVMSG " + pts[4] + " :" + replace(pts[5], "an idea", "a taco"));
}
}
}
}
int connect(string server, string nick, string userln, int port, array channels, int firstconnect) {
// If we're just starting the script and unable to connect,
// don't keep trying, just die. Butt if we've been connected
// and just lost the connection for some reason, then try to
// regain it.
con->close();
if(con->connect(server,port)) {
err("Connected to server!");
} else {
con->connect(server,port);
}
// We've connected.
con->set_nonblocking();
sendln("NICK " + nick);
sendln("USER " + userln);
foreach(channels, string channel) {
sendln("JOIN " + channel);
}
if(firstconnect == 0){
sendln("PRIVMSG #bots :I reconnected. Yay me.");
}
return 1;
}
void err(string message){
write("+++ " + message + " +++\n");
}
void sendln(string raw) {
write("<- " + raw + "\n");
con->write(raw + "\r\n");
}