|
9 | 9 | #
|
10 | 10 | # Commands:
|
11 | 11 | # hubot eval me <lang> <code> - evaluate <code> and show the result
|
| 12 | +# hubot eval on <lang> - start recording |
| 13 | +# hubot eval off|finish|done - evaluate recorded <code> and show the result |
| 14 | +# hubot eval cancel - cancel recording |
12 | 15 | #
|
13 | 16 | # Author:
|
14 | 17 | # aanoaa
|
15 | 18 |
|
16 | 19 | module.exports = (robot) ->
|
| 20 | + robot.respond /eval[,:]? +on +([a-z]+) *$/i, (msg) -> |
| 21 | + robot.brain.data.eval or= {} |
| 22 | + robot.brain.data.eval[msg.message.user.name] = { |
| 23 | + recording: true |
| 24 | + lang: msg.match[1] |
| 25 | + } |
| 26 | + msg.send("OK, recording #{msg.message.user.name}'s codes for evaluate.") |
| 27 | + |
| 28 | + robot.respond /eval[,:]? +(?:off|finish|done) *$/i, (msg) -> |
| 29 | + return unless robot.brain.data.eval?[msg.message.user.name]?.recording |
| 30 | + code = robot.brain.data.eval[msg.message.user.name].code?.join("\n") |
| 31 | + lang = robot.brain.data.eval[msg.message.user.name].lang |
| 32 | + msg |
| 33 | + .http("http://api.dan.co.jp/lleval.cgi") |
| 34 | + .query(s: "#!/usr/bin/#{lang}\n#{code}") |
| 35 | + .get() (err, res, body) -> |
| 36 | + out = JSON.parse(body) |
| 37 | + ret = out.stdout or out.stderr |
| 38 | + msg.send ret.split("\n") |
| 39 | + delete robot.brain.data.eval[msg.message.user.name] |
| 40 | + |
| 41 | + robot.respond /eval[,:]? +cancel *$/i, (msg) -> |
| 42 | + delete robot.brain.data.eval?[msg.message.user.name]? |
| 43 | + msg.send "canceled #{msg.message.user.name}'s evaluation recording" |
| 44 | + |
17 | 45 | robot.respond /eval( me)? ([^ ]+) (.+)/i, (msg) ->
|
| 46 | + return if msg.match[2] in ['on', 'off', 'finish', 'done', 'cancel'] |
18 | 47 | msg
|
19 | 48 | .http("http://api.dan.co.jp/lleval.cgi")
|
20 | 49 | .query(s: "#!/usr/bin/#{msg.match[2]}\n#{msg.match[3]}")
|
21 | 50 | .get() (err, res, body) ->
|
22 | 51 | out = JSON.parse(body)
|
23 |
| - msg.send if out.stderr then out.stderr else out.stdout |
| 52 | + ret = out.stdout or out.stderr |
| 53 | + msg.send ret.split("\n") |
| 54 | + |
| 55 | + robot.catchAll (msg) -> |
| 56 | + return unless robot.brain.data.eval?[msg.message.user.name] |
| 57 | + if robot.brain.data.eval[msg.message.user.name].recording |
| 58 | + robot.brain.data.eval[msg.message.user.name].code or= [] |
| 59 | + unless msg.message.text?.match /eval[,:]? +on +([a-z]+) *$/i |
| 60 | + robot.brain.data.eval[msg.message.user.name].code.push msg.message.text |
0 commit comments