Skip to content

Commit a3444d1

Browse files
Update bot.py
1 parent 3cf6bd3 commit a3444d1

File tree

1 file changed

+41
-56
lines changed

1 file changed

+41
-56
lines changed

‎src/bot.py‎

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
importssl
55
importtime
66
importjson
7-
importbase64
7+
importinspect
88
importsys
99
importos
1010
importimportlib.util
1111
fromsrc.channel_managerimportChannelManager
1212
fromsrc.loggerimportLogger
1313
fromsrc.plugin_baseimportPluginBase
14-
fromsrc.saslimporthandle_sasl, handle_authenticate, handle_903
14+
fromsrc.saslimportSASLHandler
15+
fromsrc.event_handlersimporthandle_ping, handle_cap, handle_authenticate, handle_903, handle_privmsg, handle_001, handle_invite, handle_version
1516

1617
classBot:
1718
def__init__(self, config_file):
@@ -22,20 +23,41 @@ def __init__(self, config_file):
2223
self.ircsock=None
2324
self.running=True
2425
self.plugins= []
26+
self.sasl_handler=SASLHandler(self.config, self._ircsend)
2527
self.load_plugins()
2628

27-
defload_plugins(self, plugin_name=None):
28-
self.plugins= [pforpinself.pluginsifplugin_nameisNoneorp.__class__.__name__!=plugin_name]
29+
self.command_handlers={
30+
'PING': handle_ping,
31+
'CAP': handle_cap,
32+
'AUTHENTICATE': handle_authenticate,
33+
'903': handle_903,
34+
'PRIVMSG': handle_privmsg,
35+
'001': handle_001,
36+
'INVITE': handle_invite,
37+
'VERSION': handle_version,
38+
}
39+
40+
defprocess_command(self, command, args):
41+
handler=self.command_handlers.get(command)
42+
ifhandler:
43+
handler(self, args)
44+
else:
45+
self.logger.debug(f'Received: source: {self} | command: {command} | args: {args}')
46+
self.logger.info(f'No handler for command {command}')
47+
48+
defload_plugins(self):
49+
self.plugins= []
2950
plugin_folder="./plugins"
3051
forfilenameinos.listdir(plugin_folder):
31-
iffilename.endswith('.py')and (plugin_nameisNoneorfilename==plugin_name+'.py'):
52+
iffilename.endswith('.py'):
3253
filepath=os.path.join(plugin_folder, filename)
3354
spec=importlib.util.spec_from_file_location("module.name", filepath)
3455
module=importlib.util.module_from_spec(spec)
3556
spec.loader.exec_module(module)
36-
ifhasattr(module, 'Plugin') andissubclass(module.Plugin, PluginBase):
37-
plugin_instance=module.Plugin(self)
38-
self.plugins.append(plugin_instance)
57+
forname, objininspect.getmembers(module):
58+
ifinspect.isclass(obj) andissubclass(obj, PluginBase) andobjisnotPluginBase:
59+
plugin_instance=obj(self)
60+
self.plugins.append(plugin_instance)
3961

4062
def_load_config(self, config_file):
4163
try:
@@ -95,20 +117,20 @@ def connect(self):
95117
self.ircsock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
96118
self.ircsock.settimeout(240)
97119

98-
ifstr(self.config['BPORT'])[:1] =='+':
120+
ifstr(self.config["Connection"].get("Port"))[:1] =='+':
99121
context=ssl.create_default_context()
100-
self.ircsock=context.wrap_socket(self.ircsock, server_hostname=self.config['BSERVER'])
101-
port=int(self.config['BPORT'][1:])
122+
self.ircsock=context.wrap_socket(self.ircsock, server_hostname=self.config["Connection"].get("Hostname"))
123+
port=int(self.config['Connection'].get('Port')[1:])
102124
else:
103-
port=int(self.config['BPORT'])
125+
port=int(self.config['Connection'].get('Port'))
104126

105-
if'BBINDHOST'inself.config:
106-
self.ircsock.bind((self.config['BBINDHOST'], 0))
127+
if'BindHost'inself.config:
128+
self.ircsock.bind((self.config['Connection'].get('BindHost'), 0))
107129

108-
self.ircsock.connect_ex((self.config['BSERVER'], port))
109-
self._ircsend(f'NICK {self.config["BNICK"]}')
110-
self._ircsend(f'USER {self.config["BIDENT"]} * * :{self.config["BNAME"]}')
111-
ifself.config['UseSASL']:
130+
self.ircsock.connect_ex((self.config['Connection'].get('Hostname'), port))
131+
self._ircsend(f'NICK {self.config["Connection"].get("Nick")}')
132+
self._ircsend(f'USER {self.config["Connection"].get("Ident")} * * :{self.config["Connection"].get("Name")}')
133+
ifself.config["SASL"].get("UseSASL"):
112134
self._ircsend('CAP REQ :sasl')
113135
exceptExceptionase:
114136
self.logger.error(f"Error establishing connection: {e}")
@@ -136,44 +158,7 @@ def start(self):
136158
source, command, args=self._parse_message(ircmsg)
137159
self.logger.debug(f'Received: source: {source} | command: {command} | args: {args}')
138160

139-
ifcommand=='PING':
140-
nospoof=args[0][1:] ifargs[0].startswith(':') elseargs[0]
141-
self._ircsend(f'PONG :{nospoof}')
142-
143-
144-
ifcommand=='CAP'andargs[1] =='ACK'and'sasl'inargs[2]:
145-
handle_sasl(self.config, self._ircsend)
146-
147-
elifcommand=='AUTHENTICATE':
148-
handle_authenticate(args, self.config, self._ircsend)
149-
150-
elifcommand=='903':
151-
handle_903(self._ircsend)
152-
153-
ifcommand=='PRIVMSG'andargs[1].startswith('\x01VERSION\x01'):
154-
source_nick=source.split('!')[0]
155-
self._ircsend(f'NOTICE {source_nick} :\x01VERSION EliteBot 0.1\x01')
156-
157-
ifcommand=='PRIVMSG':
158-
channel, message=args[0], args[1]
159-
source_nick=source.split('!')[0]
160-
ifmessage.startswith('&'):
161-
cmd, *cmd_args=message[1:].split()
162-
self.handle_command(source_nick, channel, cmd, cmd_args)
163-
forplugininself.plugins:
164-
plugin.handle_message(source_nick, channel, message)
165-
166-
ifcommand=='001':
167-
forchannelinself.channel_manager.get_channels():
168-
self._ircsend(f'JOIN {channel}')
169-
170-
ifcommand=='INVITE':
171-
channel=args[1]
172-
self._ircsend(f'join {channel}')
173-
self.channel_manager.save_channel(channel)
174-
175-
ifcommand=='VERSION':
176-
self._ircsend('NOTICE', f'{source_nick} :I am a bot version 1.0.0')
161+
self.process_command(command, args)
177162

178163
exceptsocket.timeout:
179164
self.connected=False

0 commit comments

Comments
(0)