-
Notifications
You must be signed in to change notification settings - Fork 2
/
eval.rb
89 lines (76 loc) · 2.12 KB
/
eval.rb
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
module Hermes
module Plugin
##
# Evaluates code using <http://eval.in/>.
#
class Eval
include Cinch::Plugin
set :help => 'eval [LANGUAGE] [CODE] - Evaluates the given code ' \
'using http://eval.in',
:plugin_name => 'eval'
##
# The URL of the "API".
#
# @return [String]
#
URL = 'https://eval.in/'
##
# Shortcuts for common languages.
#
# @return [Hash]
#
LANGUAGES = {
'ruby' => 'ruby/mri-2.0.0',
'ruby19' => 'ruby/mri-1.9.3',
'ruby18' => 'ruby/mri-1.8.7',
'python' => 'python/cpython-3.2.3',
'python2' => 'python/cpython-2.7.3'
}
match(/eval\s+(\S+)\s+(.+)/)
##
# Submits the code and displays the output + URL in the IRC channel.
#
# @param [Cinch::Message] message
# @param [String] language
# @param [String] code
#
def execute(message, language, code)
if LANGUAGES.key?(language)
language = LANGUAGES[language]
else
message.reply(
"Unsupported language: #{language}, supported languages: " \
"#{supported_languages}",
true
)
return
end
begin
fields = {:lang => language, :code => code, :execute => '1'}
response = Hermes.http.post(URL, :body => fields)
rescue => error
message.reply("Failed to submit the code: #{error.message}", true)
return
end
url = response.headers['Location'].to_s
if response.code == 302 and !url.empty?
response, json = Hermes.http.get_json(url + '.json')
output = json['output'].split("\n")[0]
message.reply("=> #{output} | #{url}")
else
message.reply(
"The eval.in server shat itself. HTTP status: #{response.code}",
true
)
end
end
private
##
# @return [String]
#
def supported_languages
return LANGUAGES.keys.join(', ')
end
end # Eval
end # Plugin
end # Hermes