/
airbrus.mt
303 lines (254 loc) · 11 KB
/
airbrus.mt
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import "lib/codec" =~ [=> composeCodec :DeepFrozen]
import "lib/codec/utf8" =~ [=> UTF8 :DeepFrozen]
import "irc/client" =~ [=> makeIRCConnector :DeepFrozen]
import "lib/entropy/entropy" =~ [=> makeEntropy :DeepFrozen]
import "lib/json" =~ [=> JSON :DeepFrozen]
import "lib/help" =~ [=> help :DeepFrozen]
import "lib/words" =~ [=> Word :DeepFrozen]
exports (main)
def chooseAddress(addrs) :NullOk[Bytes] as DeepFrozen:
for addr in (addrs):
if (addr.getFamily() == "INET" && addr.getSocketType() == "stream"):
return addr.getAddress()
def partition(iterable, pred) as DeepFrozen:
def yes := [].diverge()
def no := [].diverge()
for i in (iterable):
pred(i).pick(yes, no).push(i)
return [yes.snapshot(), no.snapshot()]
def parseArguments(var argv) as DeepFrozen:
var channels :List[Str] := []
var nick :Str := "airbrus"
while (argv.size() > 0):
switch (argv):
match [=="-n", n] + tail:
traceln(`Using nick '$nick'`)
nick := n
argv := tail
match [channel] + tail:
traceln(`Adding channel '$channel'`)
channels with= (channel)
argv := tail
return object configuration:
to channels() :List[Str]:
return channels
to nick() :Str:
return nick
def makeAirbrusHelp(sayer) as DeepFrozen:
return object airbrusHelp:
match [verb, args, namedArgs]:
def s :Str := M.call(help, verb, args, namedArgs)
for line in (s.split("\n")):
sayer(line)
def makeDie(entropy) as DeepFrozen:
return def roll1d20():
return 1 + entropy.nextInt(20)
def UTF8JSON :DeepFrozen := composeCodec(UTF8, JSON)
def makeGiftExchange() :Any as DeepFrozen:
def givers := [].asMap().diverge()
def wanters := [].asMap().diverge()
return def giftExchange(nickname :Str) :Any:
if (!givers.contains(nickname)):
givers[nickname] := [].asMap().diverge()
def gifts := givers[nickname]
if (!wanters.contains(nickname)):
wanters[nickname] := [].asMap().diverge()
def wants := wanters[nickname]
object gifter:
"
To give, `.give(nick, giftName, gift)`.
To receive, `.accept(nick, giftName)`.
"
to give(acceptor :Str, giftName :Str, gift :Any) :Void:
if (wanters.contains(acceptor)):
def wants := wanters[acceptor]
if (wants.contains(giftName)):
wants[giftName][1].resolve(&gift)
return
gifts[giftName] := &gift
to accept(giver :Str, giftName :Str) :Any:
def [promise, resolver] := Ref.promise()
if (givers.contains(giver)):
def gifts := givers[giver]
if (gifts.contains(giftName)):
return gifts[giftName]
if (wants.contains(giftName)):
return wants[giftName][0]
wants[giftName] := [promise, resolver]
return promise
return &&gifter
def main(argv, => Timer,
=> currentRuntime,
=> getAddrInfo,
=> makeFileResource,
=> makeTCP4ClientEndpoint,
=> unsealException) as DeepFrozen:
var todoMap :Map[Str, List[Str]] := [].asMap()
def todoFile := makeFileResource("todo.list")
def putTodo():
todoFile.setContents(UTF8JSON.encode(todoMap.snapshot(), null))
def getTodo():
def p := todoFile.getContents()
when (p) ->
todoMap := UTF8JSON.decode(p, null)
getTodo()
def giftExchange := makeGiftExchange()
def putTodoItem(nick, item):
def items := todoMap.fetch(nick, fn {[]}).with(item)
todoMap with= (nick, items)
putTodo()
def showTodoItems(name, sayer):
def items :List[Str] := todoMap.fetch(name, fn {[]})
if (items.size() == 0):
sayer(`$name has nothing to do.`)
else:
sayer(`$name should do:`)
for item in (items):
sayer(`• $item`)
def removeTodoItem(name, needle, sayer):
def items := todoMap.fetch(name, fn {[]})
if (items.size() > 0):
def [crossedOff,
remaining] := partition(items, fn s {s =~ `@_$needle@_`})
switch (crossedOff):
match []:
sayer(`I'm not seeing it on $name's list…`)
match [single]:
todoMap with= (name, remaining)
putTodo()
sayer(`Crossed off "$single". Good work!`)
match several:
sayer(`I found a couple things; which one did you mean?`)
for item in (several):
sayer(`• $item`)
else:
sayer(`But $name's list is empty.`)
def config := parseArguments(argv)
def nick :Str := config.nick()
def crypt := currentRuntime.getCrypt()
def d20 := makeDie(makeEntropy(crypt.makeSecureEntropy()))
def baseEnv := safeScope | [
# Superpowers.
=> &&getAddrInfo,
# Crypto services. Totally safe on IRC; the worst they can do is
# gently munch on the OS's entropy pool.
=> &&crypt,
# Some safe conveniences.
=> &&UTF8, => &&JSON, => &&Word,
]
def performEval(text, env, sayer):
try:
def [result, newEnv] := eval.evalToPair(text, env,
"inRepl" => true)
# If the result is eventual, then don't say it yet, but set up a
# callback for when it resolves.
if (Ref.isResolved(result)):
sayer(M.toQuote(result))
else:
sayer("I'll let you know when that's ready.")
when (result) ->
sayer(`Here you are: ${M.toQuote(result)}`)
catch problem:
def description := M.toQuote(switch (problem) {
match via (unsealException) [head, _] {head}
match unsealed {unsealed}
})
sayer(`There was a problem: $description`)
return newEnv
catch via (unsealException) [problem, _]:
sayer(`Exception: $problem`)
return env
def userEnvironments := [].asMap().diverge()
object handler:
to getNick():
return nick
to loggedIn(client):
for channel in (config.channels()):
traceln(`Joining #$channel...`)
client.join(`#$channel`)
to ctcp(client, user, message):
switch (message):
match =="VERSION":
def name := "Airbrus"
def version := "0.0.1"
def environment := "Written in Monte, running on Typhon"
client.ctcp(user.getNick(), `VERSION $name ($version): $environment`)
match =="SOURCE":
def url := "https://github.com/MostAwesomeDude/airbrus"
client.ctcp(user.getNick(), `SOURCE $url`)
match _:
traceln(`Unknown CTCP $message`)
to privmsg(client, user, channel, message):
if (message =~ `> @text`):
# Customize help so that its output doesn't get quoted.
def brusHelp := makeAirbrusHelp(fn s {client.say(channel, s)})
def instanceEnv := [
"&&help" => &&brusHelp,
"&&giftExchange" => giftExchange(user.getNick()),
]
def userEnv := userEnvironments.fetch(user.getNick(),
fn { baseEnv | instanceEnv })
def sayer(s :Str):
for line in (s.split("\n")):
client.say(channel, line)
def newEnv := performEval(text, userEnv, sayer)
userEnvironments[user.getNick()] := newEnv
else if (message =~ `$nick: @action`):
switch (action):
match `speak`:
client.say(channel, "Hi there!")
match `quit` ? (user.getNick() == "simpson"):
client.say(channel, "Okay, bye!")
client.quit("ma'a tarci pulce")
match `kill`:
client.say(channel,
`${user.getNick()}: Sorry, I don't know how to do that. Yet.`)
match `in @seconds say @utterance`:
try:
def delta := _makeInt(seconds)
when (Timer.fromNow(seconds)) ->
client.say(channel,
`${user.getNick()}: "$utterance"`)
catch _:
client.say(channel,
`${user.getNick()}: Not an integer: $seconds`)
match `todo`:
showTodoItems(user.getNick(),
fn s {client.say(channel, s)})
match `todo @name`:
if (name == ""):
# They typed "todo ".
showTodoItems(user.getNick(),
fn s {client.say(channel, s)})
else:
showTodoItems(name, fn s {client.say(channel, s)})
match `@{var name} should @things`:
if (name == "I" || name == "i"):
name := user.getNick()
putTodoItem(name, things)
client.say(channel,
`$name: I've put that on your list.`)
match `@{var name} did @needle`:
if (name == "I" || name == "i"):
name := user.getNick()
removeTodoItem(name, needle,
fn s {client.say(channel, s)})
match `@stat check`:
def roll := d20()
def luck := if (roll == 20) {
" ☘"
} else if (roll == 1) {
" ☠"
} else { "" }
client.say(channel, `$stat check: $roll$luck`)
match _:
client.say(channel, `${user.getNick()}: I don't understand.`)
def addrs := getAddrInfo(b`irc.freenode.net`, b``)
when (addrs) ->
def address := chooseAddress(addrs)
if (address == null):
traceln("Couldn't choose an address to connect to!")
def connector := makeIRCConnector(handler, Timer)
def ep := makeTCP4ClientEndpoint(address, 6667)
connector.connect(ep)
return 0