-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnimbus.nim
More file actions
168 lines (124 loc) · 5.13 KB
/
nimbus.nim
File metadata and controls
168 lines (124 loc) · 5.13 KB
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
import parseopt2
import strutils
import sockets
import osproc
proc version(): string =
result = "v1.0.0"
proc about(): string =
result = "Nimbus - Nim Eval IRC Bot (" & version() & ")"
proc help(topic: string): string =
if topic == "":
result = about() & "\n"
result &= " -s, --server=<server> :: Assigns a server for Nimbus to connect to. Default is irc.freenode.net\n" &
" -p, --port=<port> :: Assigns a port for Nimbus to connect on. Default is 6667\n" &
" -u, --user=<user> :: Assigns a username to Nimbus. Default is NimbusBot\n" &
" -n, --nick=<nick> :: Assigns a nick for to Nimbus. Default is Nimbus\n" &
" -c, --channels=<channels> :: Assigns a list of channels for Nimbus to connect to (seperated by comma and with no #. Default is #nim and #nim-offtopic\n" &
" -l, --log=<log> :: If passed, logs the output to <log>\n" &
" -N, --noverbose :: If passed, outputs the responses recieved from the server\n" &
" -v, --version :: Print the version number\n" &
" -a, --about :: Print information about Nimbus\n" &
" -h, --help=[topic] :: Print this help message, or help about the topic, if passed"
else:
result = "fooey"
proc chansplit(raw: string): seq[string] =
result = @[]
for channel in split(raw, ","):
add(result, "#" & channel)
proc getNick(raw: string): string =
result = raw[1 .. (find(raw, '!') - 1)]
var server: string = "irc.freenode.net"
var port: Port = 6667.Port
var username: string = "NimbusBot"
var nick: string = "Nimbus"
var channels: seq[string] = @["#nim", "#nim-offtopic"]
var log: File
var islogging: bool = false
var outputting: bool = true
for kind, key, val in getopt():
case kind:
of cmdArgument:
discard
of cmdLongOption, cmdShortOption:
case key:
of "v", "version":
echo(version())
quit()
of "a", "about":
echo(about())
quit()
of "h", "help":
echo(help(val))
quit()
of "s", "server":
server = val
of "p", "port":
port = parseInt(val).Port
of "u", "user":
username = val
of "n", "nick":
nick = val
of "c", "channels":
channels = chansplit(val)
of "l", "log":
log = open(val, fmWrite)
islogging = true
of "N", "noverbose":
outputting = false
else:
writeln(stderr, "invalid parameter: " & key)
quit()
of cmdEnd:
assert(false)
var sock: Socket = socket()
var buffer: string = ""
connect(sock, server, port)
send(sock, "NICK " & nick & "\r\n")
send(sock, "USER " & nick & " " & nick & " " & nick & " :Nimbus IRC\r\n")
send(sock, "MODE " & nick & " +i\r\n")
while true:
readLine(sock, buffer)
if islogging:
writeln(log, buffer)
if outputting:
echo(buffer)
if buffer == (":" & nick & " MODE " & nick & " :+i"):
break
for channel in channels:
send(sock, "JOIN " & channel & "\r\n")
while true:
readLine(sock, buffer)
if buffer == "":
echo("connection is closed")
break
if islogging:
writeln(log, buffer)
var ircmsg = split(buffer, " ")
if outputting:
echo($ircmsg)
if ircmsg[0] == "PING":
send(sock, "PONG " & ircmsg[1] & "\r\n")
elif ircmsg[1] == "PRIVMSG":
var nick: string = getNick(ircmsg[0])
if ircmsg[3] == ":.eval":
let filename = "eval.nim"
var outHandle = open(filename, fmWrite)
var result: string
outHandle.writeln("proc eval(): auto =")
for line in split(join(ircmsg[4 .. ircmsg.high], " "), ";"):
outHandle.writeln(" " & line)
outHandle.writeln("when compiles(echo eval()): echo eval()")
outHandle.writeln("else: eval()")
close(outHandle)
var resultInitial = execCmdEx("nim compile --stackTrace:off --lineTrace:off --threads:off --checks:off --fieldChecks:off --rangeChecks:on --boundChecks:on --overflowChecks:on --assertions:on --floatChecks:off --nanChecks:on --infChecks:off --opt:none --warnings:off --hints:off --threadanalysis:off --verbosity:0 --cc:ucc " & filename)
if resultInitial.output == "":
var resultSecond = execCmdEx("./" & filename[0 .. (filename.len - ".nim".len - 1)])
if resultSecond.output == "":
result = "<no output>"
else:
result = resultSecond.output
else:
result = resultInitial.output
send(sock, "PRIVMSG " & ircmsg[2] & " :" & nick & ": " & result & "\r\n")
close(log)
close(sock)