This repository has been archived by the owner on May 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchannel_print.py
267 lines (235 loc) · 10.6 KB
/
channel_print.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
from red_star.command_dispatcher import Command
from red_star.plugin_manager import BasePlugin
from red_star.rs_errors import CommandSyntaxError
from red_star.rs_utils import respond, JsonFileDict, decode_json, split_message, verify_embed
from urllib.parse import urlparse
from urllib.request import urlopen
from urllib.error import URLError
import mimetypes
import re
import json
from discord import File, Message
from io import BytesIO
def verify_document(doc: list):
"""
A helper function to verify entire documents.
Verifies that messages are of correct length, embeds are valid and file links are non-mangled.
:param doc:
:return:
"""
result = []
for i, msg in enumerate(doc):
if isinstance(msg, str):
if len(msg) > 2000:
raise ValueError(f"Message {i+1} is too long. (Limit 2000)")
result.append(msg)
else:
try:
_msg = dict()
if 'content' in msg:
if len(str(msg['content'])) > 2000:
raise ValueError(f"Message {i+1} is too long. (Limit 2000)")
_msg['content'] = str(msg['content'])
if 'embed' in msg:
try:
_msg['embed'] = verify_embed(doc[i]['embed'])
except ValueError as e:
raise ValueError(f"Message {i+1} invalid embed: {e}")
if 'file' in msg:
url = urlparse(msg['file'])
if not (url.scheme and url.netloc and url.path):
raise ValueError(f"Message {i+1} invalid attach url.")
_msg['file'] = msg['file']
result.append(_msg)
except TypeError:
raise ValueError(f"Message {i+1} not an object or string.")
return result
def verify_links(doc: list, max_size: int):
"""
A secondary helper functions for the purpose of checking all the links for both existing and being within limit.
:param doc: document, a list of messages or dicts
:param max_size: maximum size of a file in octets
:return:
"""
for i, msg in enumerate(doc):
try:
_file = urlopen(msg['file'])
if int(_file.info()['Content-Length']) > max_size:
raise ValueError(f"Message {i+1} attached file too big.")
except (TypeError, KeyError):
continue
except ValueError as e:
raise ValueError(e)
except Exception as e:
raise ValueError(f"Message {i+1} dead attachment link: {e}.")
class ChannelPrint(BasePlugin):
name = "channel_print"
version = "1.0"
author = "GTG3000"
description = "A plugin that allows printing of prepared multi-message data."
default_config = {
"max_filesize": 1024 * 1024 * 8 # max 8 mb
}
log_events = {"print_event"}
walls: JsonFileDict
async def activate(self):
self.walls = self.config_manager.get_plugin_config_file("walls.json",
json_save_args={'indent': 2, 'ensure_ascii': False})
@Command("Print", "PrintForce",
doc="Prints out the specified document from the storage, allowing to dump large amounts of information "
"into a channel, for example for purposes of a rules channel.\n"
"Document can be specified from the saved ones, or uploaded with the command.\n"
"Use \"PrintForce\" alias to force printing despite broken attachment links.",
syntax="(document)",
perms={"manage_messages"},
category="channel_print",
run_anywhere=True,
delcall=True)
async def _print(self, msg):
gid = str(msg.guild.id)
cmd, *args = msg.clean_content.split(None, 1)
if msg.attachments:
_file = BytesIO()
await msg.attachments[0].save(_file)
try:
wall = decode_json(_file.getvalue())
except json.JSONDecodeError as e:
raise CommandSyntaxError(f"Not a valid JSON file: {e}")
except ValueError as e:
self.logger.exception("Could not decode uploaded document file!", exc_info=True)
raise CommandSyntaxError(e)
else:
if gid not in self.walls:
self.walls[gid] = dict()
return
try:
wall = args[0]
except IndexError:
raise CommandSyntaxError
if wall not in self.walls[gid]:
raise CommandSyntaxError("No such document.")
wall = self.walls[gid][wall]
try:
wall = verify_document(wall)
if not cmd.lower().endswith("force"):
verify_links(wall, self.plugin_config['max_filesize'])
except ValueError as e:
raise CommandSyntaxError(e)
for post in wall:
if isinstance(post, str):
await respond(msg, post)
else:
_file = None
if 'file' in post:
try:
_file = urlopen(post['file'])
if int(_file.info()['Content-Length']) > self.plugin_config['max_filesize']:
raise ValueError("File too big.")
_file = File(_file,
filename="wallfile" + mimetypes.guess_extension(_file.info()['Content-Type']))
except (URLError, TypeError, ValueError) as e:
self.logger.info(f"Attachment file error in {msg.guild}:\n{e}")
await self.plugin_manager.hook_event("on_log_event", msg.guild,
f"**WARNING: Error occured during printout:**\n{e}",
log_type="print_event")
_file = None # Just fail silently if the request doesn't work out
if _file or 'embed' in post or 'content' in post:
await respond(msg,
post.get('content', None),
embed=post['embed'] if 'embed' in post else None,
file=_file)
@Command("DeletePrint", "PrintDelete",
doc="Deletes the specified document.",
syntax="(document)",
perms={"manage_messages"},
category="channel_print")
async def _deleteprint(self, msg):
gid = str(msg.guild.id)
try:
name = msg.clean_content.split(None, 2)[1].lower()
del self.walls[gid][name]
self.walls.save()
await respond(msg, "**AFFIRMATIVE. Document deleted.**")
except IndexError:
raise CommandSyntaxError("Document name required.")
except KeyError:
if gid not in self.walls:
self.walls[gid] = dict()
raise CommandSyntaxError("No document found.")
@Command("ListPrint", "PrintList",
doc="Lists all available documents.",
perms={"manage_messages"},
category="channel_print")
async def _listprint(self, msg):
gid = str(msg.guild.id)
if gid not in self.walls:
self.walls[gid] = dict()
walls = "\n".join(self.walls[gid])
final_msg = f"**ANALYSIS: Following documents are available:**```\n{walls}```"
for split_msg in split_message(final_msg):
await respond(msg, split_msg)
@Command("DumpPrint", "PrintDump",
doc="Uploads the specified document in a json file format.",
syntax="(document)",
perms={"manage_messages"},
category="channel_print")
async def _dumpprint(self, msg):
gid = str(msg.guild.id)
try:
name = msg.clean_content.split(None, 2)[1].lower()
await respond(msg, "**AFFIRMATIVE. Uploading file.**",
file=File(BytesIO(bytes(json.dumps(self.walls[gid][name], indent=2, ensure_ascii=False),
encoding="utf8")), filename=name+'.json'))
except IndexError:
raise CommandSyntaxError("Document name required.")
except KeyError:
if gid not in self.walls:
self.walls[gid] = dict()
raise CommandSyntaxError("No document found.")
@Command("UploadPrint", "PrintUpload",
doc="Allows you to upload a document, in a JSON file format or a JSON code block.",
syntax="(document_id) (code block or attached file)",
perms={"manage_messages"},
category="channel_print")
async def _uploadprint(self, msg: Message):
gid = str(msg.guild.id)
if gid not in self.walls:
self.walls[gid] = dict()
try:
name = msg.clean_content.split(None, 2)[1].lower()
except IndexError:
raise CommandSyntaxError("Document name required.")
if msg.attachments:
_file = BytesIO()
await msg.attachments[0].save(_file)
try:
data = decode_json(_file.getvalue())
except ValueError as e:
self.logger.exception("Could not decode uploaded document file!", exc_info=True)
raise CommandSyntaxError(e)
except Exception as e:
raise CommandSyntaxError(f"Not a valid JSON file: {e}")
else:
try:
data = re.match(r"```.*?(?P<json>\[.+]).*?```", msg.clean_content.split(None, 2)[2], re.DOTALL)['json']
data = json.loads(data)
except IndexError:
raise CommandSyntaxError("JSON code block required.")
except TypeError:
raise CommandSyntaxError("Invalid JSON template. The root must be a list.")
except Exception as e:
raise CommandSyntaxError(f"Not a valid JSON file: {e}")
try:
data = verify_document(data)
except ValueError as e:
raise CommandSyntaxError(e)
self.walls[gid][name] = data
self.walls.save()
await respond(msg, f"**AFFIRMATIVE. Document {name} available for printout.**")
@Command("PrintReload",
doc="Reloads all documents from list. You probably shouldn't be using this too often.",
bot_maintainers_only=True,
category="channel_print")
async def _printreload(self, msg):
self.walls.reload()
await respond(msg, "**AFFIRMATIVE. Printout documents reloaded.**")