-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
165 lines (120 loc) · 5.38 KB
/
main.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
"""
Copyright 2022-2024 PoligonTeam
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import logging, socket, threading, re, sys, io, time, config, argparse
from datetime import datetime
from bot import Bot
start_time = time.time()
class TCPServer:
def __init__(self, port, max_clients):
self.port = port
self.max_clients = max_clients
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind(("127.0.0.1", self.port))
self.server.listen(self.max_clients)
self.clients = []
self.accept_thread = threading.Thread(target=self.accept_clients, daemon=True)
self.accept_thread.start()
def send(self, message):
data = message.encode("utf-8")
for client in self.clients:
try:
client.send(data)
except Exception:
self.clients.remove(client)
client.close()
continue
def accept_clients(self):
while True:
client, _ = self.server.accept()
self.clients.append(client)
class StreamHandler(io.StringIO):
def __init__(self, stream, path, blacklist, command_line, tcp_server):
super().__init__()
self.stream = stream
self.path = path
self.blacklist = blacklist
self.command_line = command_line
self.tcp_server = tcp_server
self.date = None
self.file = None
while self.path and self.path[-1] == "/":
self.path = self.path[:-1]
self.create_file()
def create_file(self):
self.date = datetime.now()
if self.path:
self.file = open(self.path + "/%s.log" % self.date.strftime("%Y-%m-%d"), "a")
self.file.write("\n\n\n\n\nTIMEZONE: %s; DATE: %s; TIMESTAMP: %d; BLACKLIST: %s; COMMAND LINE: \"<python> %s\"\n\n\n\n\n\n" % (str(time.tzname), self.date.strftime("%Y-%m-%d %H:%M:%S"), self.date.timestamp(), str([expression.pattern for expression in self.blacklist]), self.command_line))
def write(self, content):
for pattern in self.blacklist:
if pattern.match(content):
return
if not self.date.date() == datetime.now().date():
self.create_file()
if self.path is not None:
self.file.write(content)
self.stream.write(content)
if self.tcp_server is not None:
threading.Thread(target=self.tcp_server.send, args=(content,), daemon=True).start()
def main():
parser = argparse.ArgumentParser(description="cenzura is a bot, the bot is cenzura")
parser.add_argument("--logging", "-l", help="enable logging")
parser.add_argument("--logterminal", "-lt", help="prints logs to specified terminal")
parser.add_argument("--path", "-p", help="saves logs to specified path")
parser.add_argument("--blacklist", "-bl", nargs="*", action="append", help="for example \"^[\\d\\s:,-]+\\[DEBUG:.+\\].(sent.)?op:.(HEARTBEAT|PRESENCE_UPDATE).+$" or "^.+event.name:.PRESENCE_UPDATE$\"")
parser.add_argument("--serverport", "-sp", type=int, help="tcp server port")
parser.add_argument("--clients", "-c", type=int, help="maximum number of clients")
parser.add_argument("--token", "-t", help="bot token")
parser.add_argument("--bot", "-b", help="true if token is a bot token", choices=("true", "false"))
args = parser.parse_args()
stream = sys.stdout
logging_enabled = False
path = None
blacklist = []
server_port = None
max_clients = None
token = None
bot = True
if args.logging:
logging_enabled = True
if args.logterminal:
logging_enabled = True
stream = open("/dev/pts/" + args.logterminal, "w")
if args.path:
path = args.path
if args.blacklist:
blacklist = [re.compile(blacklisted[0]) for blacklisted in args.blacklist]
if args.serverport:
server_port = args.serverport
if args.clients:
max_clients = args.clients
if args.token:
token = args.token
if args.bot:
bot = args.bot == "true"
if logging_enabled is True:
tcp_server = None
if server_port is not None:
tcp_server = TCPServer(server_port, max_clients or 5)
logging.basicConfig(stream=StreamHandler(stream, path, blacklist, " ".join(sys.argv), tcp_server), level=logging.DEBUG, format="%(asctime)s [%(levelname)s:%(module)s] %(message)s")
Bot(start_time=start_time).run(token or config.TOKEN, bot=bot)
if tcp_server is not None:
try:
while True:
time.sleep(60)
except (Exception, KeyboardInterrupt, SystemExit):
tcp_server.server.close()
for client in tcp_server.clients:
client.close()
if __name__ == "__main__":
main()