This repository was archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy patheval.coffee
123 lines (102 loc) · 3.55 KB
/
eval.coffee
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
# Description:
# evaluate code
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# hubot eval me <lang> <code> - evaluate <code> and show the result
# hubot eval on <lang> - start recording
# hubot eval off|finish|done - evaluate recorded <code> and show the result
# hubot eval cancel - cancel recording
# hubot eval list - list available languages
#
# Author:
# aanoaa
util = require 'util'
ready = false
module.exports = (robot) ->
get_languages = (robot, callback) ->
callback or= () ->
if not ready
callback {}
return
url = "http://api.dan.co.jp/lleval.cgi"
robot.logger.info "Loading language data from #{url}"
robot
.http(url)
.query(q: "1")
.get() (err, res, body) ->
langs = JSON.parse(body)
callback(langs)
robot.logger.info "Brain received eval language list #{util.inspect(langs)}"
lang_valid = (robot, lang, callback) ->
callback or= () ->
get_languages robot, (languages) ->
for own id, desc of languages
if lang == id
callback(true)
return
callback(false)
run_eval = (lang, code, msg) ->
msg
.http("http://api.dan.co.jp/lleval.cgi")
.query({s: "#{code}", l: "#{lang}"})
.get() (err, res, body) ->
out = JSON.parse(body)
ret = out.stdout or out.stderr
msg.send ret
robot.brain.on 'loaded', ->
ready = true
get_languages robot
robot.respond /eval[,:]?\s+list$/i, (msg) ->
get_languages robot, (languages) ->
lang_msg = 'Known Languages\n\n'
for own id, desc of languages
lang_msg += "#{id}: #{desc}\n"
msg.send lang_msg
robot.respond /eval[,:]? +on +([a-z]+) *$/i, (msg) ->
robot.brain.data.eval or= {}
lang = msg.match[1]
is_valid = (valid) ->
if not valid
msg.send "Unknown language #{lang} - use eval list command for languages"
return
robot.brain.data.eval[msg.message.user.name] = {
recording: true
lang: msg.match[1]
}
msg.send("OK, recording #{msg.message.user.name}'s codes for evaluate.")
lang_valid robot, lang, is_valid
robot.respond /eval[,:]? +(?:off|finish|done) *$/i, (msg) ->
return unless robot.brain.data.eval?[msg.message.user.name]?.recording
code = robot.brain.data.eval[msg.message.user.name].code?.join("\n")
lang = robot.brain.data.eval[msg.message.user.name].lang
is_valid = (valid) ->
if not valid
msg.send "Unknown language #{lang} - use eval list command for languages"
return
run_eval(lang, code, msg)
delete robot.brain.data.eval[msg.message.user.name]
lang_valid(robot, lang, is_valid)
robot.respond /eval[,:]? +cancel *$/i, (msg) ->
delete robot.brain.data.eval?[msg.message.user.name]?
msg.send "canceled #{msg.message.user.name}'s evaluation recording"
robot.respond /eval( me)? ([^ ]+) (.+)/i, (msg) ->
lang = msg.match[2]
return if lang in ['on', 'off', 'finish', 'done', 'cancel']
is_valid = (valid) ->
if not valid
msg.send "Unknown language #{lang} - use eval list command for languages"
return
run_eval(lang, msg.match[3], msg)
lang_valid(robot, lang, is_valid)
robot.catchAll (msg) ->
return unless robot.brain.data.eval?[msg.message.user.name]
if robot.brain.data.eval[msg.message.user.name].recording
robot.brain.data.eval[msg.message.user.name].code or= []
unless msg.message.text?.match /eval[,:]? +on +([a-z]+) *$/i
robot.brain.data.eval[msg.message.user.name].code.push msg.message.text