/
app.d
175 lines (151 loc) · 3.49 KB
/
app.d
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/++
Connects to a server and joins all specified channels.
Logs all events that occur to stdout.
+/
module app;
import core.time;
import std.functional;
import std.stdio;
import std.string;
import vibe.core.args;
import vibe.core.core;
import vibeirc;
IRCClient bot;
string host;
string nickname;
string password;
string[] channels;
ushort port = 6667;
shared static this()
{
host = readRequiredOption!string(
"a|host",
"Host to connect to",
);
nickname = readRequiredOption!string(
"n|nickname",
"Nickname to use",
);
//readOption can't handle string[] :(
auto channelList = readRequiredOption!string(
"c|channels",
"List of channels to join, separated by commas",
);
channels = channelList.split(",");
readOption(
"p|port",
&port,
"Port to connect on",
);
readOption(
"P|password",
&password,
"Password to use when connecting, if any",
);
bot = new IRCClient;
bot.nickname = nickname;
bot.onDisconnect = toDelegate(&onDisconnect);
bot.onLogin = toDelegate(&onLogin);
bot.onMessage = toDelegate(&onMessage);
bot.onNotice = toDelegate(&onMessage);
bot.onUserJoin = toDelegate(&onUserJoin);
bot.onUserPart = toDelegate(&onUserPart);
bot.onUserQuit = toDelegate(&onUserQuit);
bot.onUserRename = toDelegate(&onUserRename);
bot.onUserKick = toDelegate(&onUserKick);
//defer connection until after arguments have been processed
runTask(toDelegate(&connect));
}
void connect()
{
bot.connect(host, port, password);
}
void onDisconnect(string reason)
{
writeln("Disconnected: ", reason);
sleep(10.seconds);
writeln("Attempting to reconnect");
connect;
}
void onLogin()
{
writeln("Logged in");
foreach(channel; channels)
{
writeln("Joining ", channel);
bot.join(channel);
}
}
void onMessage(Message message)
{
string bodyFormat;
string bracketText;
if(message.isCTCP)
{
if(message.ctcpCommand != "ACTION")
{
writefln(
"%s sends CTCP %s",
message.sender.nickname,
message.ctcpCommand,
);
return;
}
bodyFormat = "* %s %s";
}
else
bodyFormat = "<%s> %s";
if(message.target == bot.nickname)
bracketText = "Private Message";
else
bracketText = message.target;
writefln(
"[%s] " ~ bodyFormat,
bracketText,
message.sender.nickname,
message.message,
);
}
void onUserJoin(User user, string channel)
{
writefln(
"[%s] %s joined",
channel,
user.nickname,
);
}
void onUserPart(User user, string channel, string reason)
{
writefln(
"[%s] %s left (%s)",
channel,
user.nickname,
reason == null ? "No reason given" : reason,
);
}
void onUserQuit(User user, string reason)
{
writefln(
"%s quit (%s)",
user.nickname,
reason == null ? "No reason given" : reason,
);
}
void onUserRename(User user, string newNick)
{
writefln(
"%s is now known as %s",
user.nickname,
newNick,
);
}
void onUserKick(User kicker, string kickee, string channel, string reason)
{
writefln(
"[%s] %s kicked %s (%s)",
channel,
kicker.nickname,
kickee,
reason == null ? "No reason given" : reason,
);
}