-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmoderation.rb
executable file
·232 lines (222 loc) · 7.61 KB
/
moderation.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
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
require 'rubygems'
require 'net/http'
require 'open-uri'
require 'json'
require 'cgi'
require 'digest'
require 'action_view'
require 'similar_text'
include ActionView::Helpers::DateHelper
class Moderation
VALID_WORDS = %w{blacklist_nospace ignore unignore unblacklist status_api admin remote punt notify reload featur}
MODS = %w{iliedaboutcake hephaestus 13hephaestus rustlebot bot destiny ceneza sztanpet righttobeararmslol}.map{|m| m.downcase}
APP_ROOT = File.expand_path(File.dirname(__FILE__))
CACHE_FILE = APP_ROOT+"/cache/"
API_ENDPOINT = "http://api.overrustle.com"
def is_mod?(chatter)
return MODS.include?(chatter.downcase)
end
attr_accessor :regex, :last_message, :chatter
def initialize
@regex = /^!(#{VALID_WORDS.join('|')})/i
@last_message = ""
@chatter = ""
end
def ignored?(name)
thelist = self.baddies || []
return thelist.include?(name)
end
# remove filtered lines
def safe?(line)
thelist = self.chat_filter || []
thelist.each do |l|
return false if line =~ /#{l}/i
end
return true
end
def check(query)
m = trycheck(query)
@last_message = m
return m
rescue Exception => e
puts e.message
puts e.backtrace.join("\n")
m = e.message
" OverRustle Tell hephaestus RustleBot moderation broke. Exception: #{m.to_s}"
end
def trycheck(query)
if MODS.include?(@chatter.downcase)
parts = query.strip.split(' ')
if query =~ /^(!status_api)/i
start_time = Time.now
resp = open("#{API_ENDPOINT}/api")
content = resp.read
request_duration = Time.now - start_time
request_duration = (request_duration.round(3)*1000).round
jsn = JSON.parse(content)
output = "api.overrustle.com Status: #{jsn['viewercount']} viewers, #{jsn['idlecount']} idlers, #{jsn['connections']} connections, #{resp.meta['age']} cache age, #{request_duration}ms request duration "
output << %w{DANKMEMES SoDoge Klappa MLADY WORTH DappaKappa}.sample
return output
elsif query =~ /^(!blacklist_nospace)/i
saved_filter = self.chat_filter || []
if parts.length < 3
return "#{@chatter} didn\'t format the blacklist command correctly"
end
thing_to_blacklist = parts[1] + parts[2]
saved_filter.push(thing_to_blacklist)
self.chat_filter = saved_filter
return "#{parts[1]} #{parts[2]} (no space) added to blacklist by #{@chatter}"
elsif query =~ /^(!unblacklist)/
saved_filter = self.chat_filter || []
if parts.length < 2
return "#{@chatter} didn\'t format the blacklist command correctly"
end
saved_filter.delete(parts[1])
self.chat_filter = saved_filter
return "#{parts[1]} removed from the blacklist by #{@chatter}"
elsif query =~ /^(!(ignore|unignore))/i
saved_list = self.baddies || []
if parts.length < 2
return "#{@chatter} didn\'t format the ignore or unignore command correctly"
end
if query =~ /^(!ignore)/i
saved_list.push(parts[1])
self.baddies = saved_list
return "/me is ignoring #{parts[1]} according to #{@chatter}"
else
saved_list.delete(parts[1])
self.baddies = saved_list
return "/me stopped ignoring #{parts[1]} according to #{@chatter}"
end
elsif query =~ /^(!(enable|disable))/i
saved_list = CHATBOTS || []
if parts.length < 2
return "#{@chatter} didn\'t format the enable or disable command correctly"
end
bot_name = parts[1]
if query =~ /^(!disable)/i
todelete = nil
# todo: could be .select
bot = CHATBOTS.delete_if{|cb| (cb.class.name.underscore == bot_name or cb.class.name == bot_name)}
CHATBOTS.delete(todelete)
# todo: persist
return "#{@chatter} disabled #{bot}"
else
path = "./#{bot_name.underscore}.rb"
if File.exists?(path)
require path
CHATBOTS << Object.const_get(bot_name.camelize).new
return "#{@chatter} enabled #{bot}"
else
return "/me could not find a chat bot at #{path}"
end
end
elsif query =~ /^!(remote|admin)/i
# httpget
if parts.length > 1
parts.delete_at(0)
rce = parts.join(' ')
httppost("#{API_ENDPOINT}/admin", {"code" => rce})
return "injecting remote DANKMEMES ... be careful!"
end
elsif query =~ /^!notify/i
if parts.length > 1
parts.delete_at(0)
rce = parts.join(' ')
httppost("#{API_ENDPOINT}/admin/notify", {"message" => rce})
return "/me Attention Rustlers: #{rce}"
end
elsif query =~ /^!reload/i
if parts.length > 1
httppost("#{API_ENDPOINT}/admin/reload", {"who" => parts[1]})
else
httppost("#{API_ENDPOINT}/admin/reload", {})
end
parts.delete_at(0)
return "forced OverRustle reload for #{parts[0] ? parts[0] : 'everyone'}"
elsif query =~ /^!featur/i
qparams = {}
if parts.length > 2
qparams['who'] = parts[1]
qparams['to'] = parts[2]
elsif parts.length == 2
qparams['who'] = parts[1]
end
httppost("#{API_ENDPOINT}/admin/feature", qparams)
qparams['to'] = "people watching "+qparams['to'] if qparams['to']
qparams['to'] ||= 'everyone'
if parts.length > 1
return "Hey, #{qparams['to']}, check out this stream: rustle.club/#{qparams['who']}"
end
elsif query =~ /^!(redirect|punt)/i
qparams = {}
if parts.length > 2
qparams['who'] = parts[1]
qparams['to'] = parts[2]
elsif parts.length == 2
qparams['to'] = parts[1]
end
httppost("#{API_ENDPOINT}/admin/redirect", qparams)
qparams['who'] ||= everyone
if parts.length > 1
return "/me I'm #{parts[0]}ing #{qparams['who']}'s viewers on OverRustle.com to #{qparams['to']}"
end
end
end
return nil
end
# POST and GET to the OverRustle API
def httppost(raw_url, params={})
params['key'] = ENV['OVERRUSTLE_API_SECRET']
json_headers = {"Content-Type" => "application/json", "Accept" => "application/json"}
uri = URI.parse(raw_url)
http = Net::HTTP.new(uri.host, uri.port)
response = http.post(uri.path, params.to_json, json_headers)
puts response.inspect
end
def httpget(url)
open(url, {'API_SECRET' => ENV['OVERRUSTLE_API_SECRET']}).read
end
# NOTE: this is not sending any API keys
# so don't expect it to work with OverRustle
def getjson(url)
content = open(url).read
return JSON.parse(content)
end
def chat_filter
getcached('chat_filter')
end
def chat_filter=(jsn)
setcached('chat_filter', jsn)
end
def baddies
return getcached('baddies') || []
end
def baddies=(jsn)
setcached('baddies', jsn)
end
# safe cache! won't die if the bot dies
def getcached(url)
_cached = instance_variable_get "@cached_#{hashed(url)}"
return _cached unless _cached.nil?
path = CACHE_FILE + "#{hashed(url)}.json"
if File.exists?(path)
File.open(path, "r:UTF-8") do |f|
_cached = JSON.parse(f.read)
instance_variable_set("@cached_#{hashed(url)}", _cached)
return _cached
end
end
return nil
end
def setcached(url, jsn)
instance_variable_set("@cached_#{hashed(url)}", jsn)
path = CACHE_FILE + "#{hashed(url)}.json"
File.open(path, 'w') do |f2|
f2.puts JSON.unparse(jsn)
end
end
def hashed(url)
return Digest::MD5.hexdigest(url).to_s
end
end