/
iouonegirl.py
407 lines (346 loc) · 16.9 KB
/
iouonegirl.py
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# This is a plugin created by iouonegirl(@gmail.com)
# Copyright (c) 2016 iouonegirl
# https://github.com/dsverdlo/minqlx-plugins
#
# Thanks to Minkyn for his idea for an abstract plugin.
#
# DO NOT MANUALLY LOAD THIS ABSTRACT "PLUGIN"
# OR CHANGE ANY CODE IN IT. TRUST ME.
#
# Uses:
# set qlx_autoupdate_iouplugins "0"
# ^ Set to "1" to enable automatic updates for all iou-plugins!
import minqlx
import threading
import time
import random
import os
import urllib
import requests
import re
VERSION = "v0.34 IMPORTANT"
class iouonegirlPlugin(minqlx.Plugin):
def __init__(self, name, vers):
super().__init__()
self._name = name
self._vers = vers
self._loc = "https://raw.githubusercontent.com/dsverdlo/minqlx-plugins/master/"
self._flag = self.iouonegirlplugin_getflag() # flagged for restart
self.cr_latest = "latest"
self.cr_outdated = "outdated"
self.cr_custom = "custom"
self.cr_advanced = "futuristic"
# One time set cvar for automatic updates
self.set_cvar_once("qlx_autoupdate_iouplugins", "0")
# Add custom v_PLUGINNAME command
self.add_command("v_"+name, self.iouonegirlplugin_cmd_version)
# these will be added with every subclass, so use RET_STOP in them
self.add_command(("v_iouonegirlplugin", "v_iouonegirlPlugin", "v_iouonegirl"), self.iouonegirlplugin_cmd_myversion)
self.add_command("update", self.iouonegirlplugin_cmd_autoupdate, 5, usage="<plugin>|all")
self.add_command("install", self.iouonegirlplugin_cmd_install, 5, usage="<plugin>")
self.add_command("iouplugins", self.iouonegirlplugin_cmd_list)
self.add_command("versions", self.iouonegirlplugin_cmd_versions)
self.add_hook("player_connect", self.iouonegirlplugin_handle_player_connect)
# Check version of implementing plugin
def iouonegirlplugin_cmd_version(self, player, msg, channel):
self.iouonegirlplugin_check_version(player, channel)
# command for checking superclass plugin. One handler is enough
def iouonegirlplugin_cmd_myversion(self, player, msg, channel):
self.iouonegirlplugin_check_myversion(player=player, channel=channel)
return minqlx.RET_STOP
@minqlx.thread
def iouonegirlplugin_check_version(self, player=None, channel=None):
@minqlx.next_frame
def reply(m): channel.reply(m)
@minqlx.next_frame
def tell(m): player.tell(m)
url = "{}{}.py".format(self._loc, self._name)
res = requests.get(url)
last_status = res.status_code
if res.status_code != requests.codes.ok:
m = "^7Currently using ^3iou^7one^4girl^7's ^6{}^7 plugin version ^6{}^7."
if channel: reply(m.format(self._name, self._vers))
elif player: tell(m.format(self._name, self._vers))
return
for line in res.iter_lines():
if line.startswith(b'VERSION'):
line = line.replace(b'VERSION = ', b'')
line = line.replace(b'"', b'')
serv_version = line.decode()
comp = self.v_compare(self._vers, serv_version)
# If called manually and outdated
if channel and comp in [self.cr_outdated, self.cr_custom]:
if self.get_cvar("qlx_autoupdate_iouplugins", int):
reply("^1{} ^3iou^7one^4girl^7's ^6{}^7 plugin detected. Autoupdating...".format(comp,self._name))
self.iouonegirlplugin_update(player, None, channel)
else:
reply("^7Currently using ^3iou^7one^4girl^7's ^6{}^7 plugin ^1{}^7 version ^6{}^7.".format(self._name, comp, self._vers))
# If called manually and alright
elif channel and comp in [self.cr_advanced, self.cr_latest]:
reply("^7Currently using ^3iou^7one^4girl^7's {} ^6{}^7 plugin version ^6{}^7.".format(comp, self._name, self._vers))
# If routine check and it's not alright.
elif player and comp in [self.cr_outdated, self.cr_custom]:
if self.get_cvar("qlx_autoupdate_iouplugins", int):
minqlx.console_command("echo Autoupdating iouonegirl's {} plugin.".format(self._name))
self.iouonegirlplugin_update(player, None, player.channel)
else:
time.sleep(15)
try:
tell("^3Plugin update alert^7:^6 {}^7's latest version is ^6{}^7 and you're using ^6{}^7!".format(self._name, line.decode(), self._vers))
except Exception as e: minqlx.console_command("echo Error: {}".format(e))
return
# Check abstract plugin version
@minqlx.thread
def iouonegirlplugin_check_myversion(self, player=None, channel=None):
@minqlx.next_frame
def reply(m): channel.reply(m)
@minqlx.next_frame
def tell(m): player.tell(m)
url = "https://raw.githubusercontent.com/dsverdlo/minqlx-plugins/master/iouonegirl.py"
res = requests.get(url)
last_status = res.status_code
if res.status_code != requests.codes.ok:
m = "^7Currently using ^3iou^7one^4girl^7's ^6iouonegirl^7 superplugin version ^6{}^7."
if channel: reply(m.format(VERSION))
elif player: tell(m.format(VERSION))
return
for line in res.iter_lines():
if line.startswith(b'VERSION'):
line = line.replace(b'VERSION = ', b'')
line = line.replace(b'"', b'')
comp = self.v_compare(VERSION, line.decode())
if channel and self._flag:
reply("^7Latest ^3iou^7one^4girl^7's superplugin update has been downloaded and is waiting for a restart.")
# If called manually and outdated
elif channel and comp in [self.cr_outdated, self.cr_custom]:
reply("^7Currently using ^3iou^7one^4girl^7's superplugin ^1{}^7 version ^6{}^7!".format(comp.upper(), VERSION))
# If called manually and alright
elif channel and comp in [self.cr_latest, self.cr_advanced]:
reply("^7Currently using ^3iou^7one^4girl^7's {} ^6iouonegirl^7 superplugin version ^6{}^7.".format(comp, VERSION))
# If routine check and it's not alright.
elif player and comp in [self.cr_outdated, self.cr_custom]:
if self.get_cvar('qlx_autoupdate_iouplugins', int):
self.iouonegirlplugin_updateAbstractDelayed(player,None,player.channel)
else:
time.sleep(15)
try:
tell("^3Plugin update alert^7:^6 iouonegirl^7's latest version is ^6{}^7 and you're using ^6{}^7! ---> ^2!update iouonegirl".format(line.decode(), VERSION))
except Exception as e: minqlx.console_command("echo IouoneError: {}".format(e))
return
def iouonegirlplugin_cmd_install(self, player, msg, channel):
@minqlx.thread
def fetch(url):
try:
abs_file_path = os.path.join(os.path.dirname(__file__), "{}.py".format(msg))
res = requests.get(url)
if res.status_code != requests.codes.ok: raise
with open(abs_file_path,"a+") as f: f.write(res.text)
done()
except Exception as e:
fail(e)
@minqlx.next_frame
def done():
minqlx.reload_plugin(msg)
channel.reply("{} ^2succesfully ^7installed!".format(msg))
@minqlx.next_frame
def fail(e):
self.msg("{} plugin installation ^1failed^7: {}".format(msg, e))
if len(msg) < 2:
return minqlx.RET_USAGE
msg = msg[1].lower()
url = "https://raw.githubusercontent.com/dsverdlo/minqlx-plugins/master/{}.py"
fetch(url.format(msg))
return minqlx.RET_STOP
def iouonegirlplugin_cmd_autoupdate(self, player, msg, channel):
if len(msg) < 2:
return minqlx.RET_USAGE
if msg[1].startswith("iouonegirl"):
self.iouonegirlplugin_setflag(True) # update all flags, update once
self.iouonegirlplugin_updateAbstract(player, msg, channel)
return minqlx.RET_STOP
if msg[1] == 'all': # do this all just once
for plugin_name in self.plugins:
plugin = self.plugins[plugin_name]
if iouonegirlPlugin in plugin.__class__.__bases__:
plugin.iouonegirlplugin_update(player, msg, channel)
self.iouonegirlplugin_updateAbstractDelayed(player, msg, channel)
return minqlx.RET_STOP
if msg[1] == self._name: # let every iouplugin execute this
self.iouonegirlplugin_update(player, msg, channel)
def iouonegirlplugin_cmd_list(self, player, msg, channel):
m = "^7Currently using following iouonegirl plugins: ^6{}^7."
iou_plugins = []
for plugin_name in self.plugins:
plugin = self.plugins[plugin_name]
if iouonegirlPlugin in plugin.__class__.__bases__:
iou_plugins.append(plugin_name)
iou_plugins.sort()
channel.reply("{}^7: ^2{}".format(player.name, " ".join(msg)))
if iou_plugins:
channel.reply(m.format("^7, ^6".join(iou_plugins)))
else:
channel.reply("^7No iouonegirl plugins installed... Get some from ^6https://github.com/dsverdlo/minqlx-plugins")
return minqlx.RET_STOP # once is enough, thanks
def iouonegirlplugin_cmd_versions(self, player, msg, channel):
for plugin_name in self.plugins:
plugin = self.plugins[plugin_name]
if iouonegirlPlugin in plugin.__class__.__bases__:
plugin.iouonegirlplugin_check_version(player, channel)
return minqlx.RET_STOP # once is enough, thanks
def iouonegirlplugin_handle_player_connect(self, player):
if self.db.has_permission(player, 5):
self.iouonegirlplugin_check_version(player=player)
# If there is no flag and this is the first plugin, check
if (not self._flag) and self.is_first_plugin():
self.iouonegirlplugin_check_myversion(player=player)
def iouonegirlplugin_setflag(self, boolean):
for plugin_name in self.plugins:
plugin = self.plugins[plugin_name]
if iouonegirlPlugin in plugin.__class__.__bases__:
plugin._flag = boolean
def iouonegirlplugin_getflag(self):
for plugin_name in self.plugins:
if plugin_name == self._name: continue
plugin = self.plugins[plugin_name]
if iouonegirlPlugin in plugin.__class__.__bases__:
if plugin._flag: return True
return False
# Lists all the loaded iou-plugin names alphabetically and returns True if this is the first one
def is_first_plugin(self):
iou_plugins = [] # collect names
for plugin_name in self.plugins:
plugin = self.plugins[plugin_name]
if iouonegirlPlugin in plugin.__class__.__bases__:
iou_plugins.append(plugin_name)
# It shouldn't happen that there are 0 iou plugins, but just to prevent an error, we return False
if len(iou_plugins) == 0:
return False
# There are iou plugins, check if we are the first one
iou_plugins.sort()
return self._name == iou_plugins[0]
@minqlx.thread
def iouonegirlplugin_update(self, player, msg, channel):
@minqlx.next_frame
def ready():
minqlx.reload_plugin(self._name)
channel.reply("^2Updated ^3iou^7one^4girl^7's ^6{} ^7plugin to the latest version!".format(self._name))
@minqlx.next_frame
def fail(e):
channel.reply("^1Update failed for {}^7: {}".format(self._name, e))
try:
url = "{}{}.py".format(self._loc, self._name)
res = requests.get(url)
if res.status_code != requests.codes.ok: return
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
abs_file_path = os.path.join(script_dir, "{}.py".format(self._name))
with open(abs_file_path,"w") as f: f.write(res.text)
ready()
return True
except Exception as e :
fail(e)
return False
@minqlx.delay(4)
def iouonegirlplugin_updateAbstractDelayed(self, player, msg, channel):
self.iouonegirlplugin_setflag(True)
self.iouonegirlplugin_updateAbstract(player, msg, channel)
@minqlx.thread
def iouonegirlplugin_updateAbstract(self, player, msg, channel):
@minqlx.next_frame
def ready():
if channel:
channel.reply("^2Updated ^7abstract plugin, but requires a pyrestart for the changes to take effect.")
url = "https://raw.githubusercontent.com/dsverdlo/minqlx-plugins/master/iouonegirl.py"
res = requests.get(url)
if res.status_code != requests.codes.ok: return
abs_file_path = os.path.join(os.path.dirname(__file__), "iouonegirl.py")
with open(abs_file_path,"w") as f: f.write(res.text)
ready()
def find_by_name_or_id(self, player, target):
# Find players returns a list of name-matching players
def find_players(query):
players = []
for p in self.find_player(query):
if p not in players:
players.append(p)
return players
# Tell a player which players matched
def list_alternatives(players, indent=2):
player.tell("A total of ^6{}^7 players matched for {}:".format(len(players),target))
out = ""
for p in players:
out += " " * indent
out += "{}^6:^7 {}\n".format(p.id, p.name)
player.tell(out[:-1])
# Get the list of matching players on name
target_players = find_players(target)
# If id:X is given and it amounts to a player, give it precedence.
# This is to avoid deadlocks
match = re.search("(id[=:][0-9]{1,2})", target)
if match and match.group() == target:
try:
match_id = re.search("([0-9]{1,2})", target)
player = self.player(int(match_id.group()))
if player.steam_id:
return player
except:
pass
# even if we get only 1 person, we need to check if the input was meant as an ID
# if we also get an ID we should return with ambiguity
try:
i = int(target)
target_player = self.player(i)
if not (0 <= i < 64) or not target_player:
raise ValueError
# Add the found ID if the player was not already found
if not target_player in target_players:
target_players.append(target_player)
except ValueError:
pass
# If there were absolutely no matches
if not target_players:
player.tell("Sorry, but no players matched your tokens: {}.".format(target))
return None
# If there were more than 1 matches
if len(target_players) > 1:
list_alternatives(target_players)
return None
# By now there can only be one person left
return target_players.pop()
def delaytell(self, messages, player, interval = 1):
def tell(mess):
return lambda: player.tell("^6{}".format(mess)) if mess else None
self.interval_functions(map(tell, messages), interval)
def delaymsg(self, messages, interval = 1):
def msg(m):
return lambda: minqlx.CHAT_CHANNEL.reply("^7{}".format(m)) if m else None
self.interval_functions(map(msg, messages), interval)
# Executes functions in a seperate thread with a certain interval
@minqlx.thread
def interval_functions(self, items, interval):
@minqlx.next_frame
def do(func): func()
for m in items:
if m: do(m) # allow "" to be used as a skip
time.sleep(interval)
def is_even(self, number):
return number % 2 == 0
def is_odd(self, number):
return not self.is_even(number)
def v_compare(self, old, new):
# If exact same version
if old == new: return self.cr_latest
old = re.findall("[0-9]+", old)
new = re.findall("[0-9]+", new)
# If numbers are the same
if old == new: return self.cr_custom
for i,_ in enumerate(old):
try:
v_old = int(old[i])
v_new = int(new[i])
if v_old > v_new: return self.cr_advanced
if v_old < v_new: return self.cr_outdated
except:
# If new cannot follow
return self.cr_advanced
# New has more numbers
return self.cr_outdated