mirror of
https://github.com/nichkara/InfinitumBotty.git
synced 2026-06-10 22:26:23 +02:00
f-stringify connections, and adding option to disable ssl for local testing
This commit is contained in:
@@ -22,11 +22,12 @@ class Connection(object):
|
|||||||
details = None
|
details = None
|
||||||
irc = None
|
irc = None
|
||||||
wraper = None
|
wraper = None
|
||||||
|
|
||||||
def sender(self):
|
def sender(self):
|
||||||
while True:
|
while True:
|
||||||
msg = self.send_queue.get()
|
msg = self.send_queue.get()
|
||||||
if msg[-1] != b'\n':
|
if msg[-1] != b"\n":
|
||||||
msg = msg + b'\n'
|
msg = msg + b"\n"
|
||||||
self.irc.send(msg)
|
self.irc.send(msg)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
@@ -35,14 +36,14 @@ class Connection(object):
|
|||||||
Send to channel
|
Send to channel
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
self.raw_send("PRIVMSG " + self.details.get_channel() + " :" + text[0:])
|
self.raw_send(f"PRIVMSG {self.details.get_channel()} :{text}")
|
||||||
|
|
||||||
def send_to_user(self, user, text):
|
def send_to_user(self, user, text):
|
||||||
"""
|
"""
|
||||||
Send to user
|
Send to user
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
self.raw_send('PRIVMSG ' + user + ' :' + text)
|
self.raw_send(f"PRIVMSG {user} :{text}")
|
||||||
|
|
||||||
def send_back(self, text, data):
|
def send_back(self, text, data):
|
||||||
"""
|
"""
|
||||||
@@ -51,13 +52,13 @@ class Connection(object):
|
|||||||
:param data: needed because of concurrency, there can't be a global variable holding where messages came from
|
:param data: needed because of concurrency, there can't be a global variable holding where messages came from
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
if data['channel'] == self.details.get_nick():
|
if data["channel"] == self.details.get_nick():
|
||||||
self.send_to_user(data['nick'], text)
|
self.send_to_user(data["nick"], text)
|
||||||
else:
|
else:
|
||||||
self.send_channel(text)
|
self.send_channel(text)
|
||||||
|
|
||||||
def raw_send(self, message):
|
def raw_send(self, message):
|
||||||
self.send_queue.put(message.encode() + '\r\n'.encode())
|
self.send_queue.put(f"{message}\r\n".encode())
|
||||||
|
|
||||||
def receive(self):
|
def receive(self):
|
||||||
"""
|
"""
|
||||||
@@ -69,7 +70,7 @@ class Connection(object):
|
|||||||
return False
|
return False
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
return False
|
return False
|
||||||
data = data.decode('UTF-8', errors='replace')
|
data = data.decode("UTF-8", errors="replace")
|
||||||
# print('received: \n' + data)
|
# print('received: \n' + data)
|
||||||
data_lines = self._receiver_buffer.append(data)
|
data_lines = self._receiver_buffer.append(data)
|
||||||
if data is None:
|
if data is None:
|
||||||
@@ -80,24 +81,24 @@ class Connection(object):
|
|||||||
data = data.rstrip()
|
data = data.rstrip()
|
||||||
self.data = data
|
self.data = data
|
||||||
|
|
||||||
splited = data.split(' ')
|
splited = data.split(" ")
|
||||||
if not len(splited) >= 2:
|
if not len(splited) >= 2:
|
||||||
continue
|
continue
|
||||||
command = splited[1]
|
command = splited[1]
|
||||||
# print(command)
|
# print(command)
|
||||||
if data.split(' ')[0] == 'PING':
|
if data.split(" ")[0] == "PING":
|
||||||
self.ping_observable.input(data, self)
|
self.ping_observable.input(data, self)
|
||||||
elif command == 'JOIN':
|
elif command == "JOIN":
|
||||||
self.join_observable.input(data, self)
|
self.join_observable.input(data, self)
|
||||||
elif command == 'PART' or command == 'QUIT':
|
elif command == "PART" or command == "QUIT":
|
||||||
self.leave_observable.input(data, self)
|
self.leave_observable.input(data, self)
|
||||||
elif command == 'KICK':
|
elif command == "KICK":
|
||||||
self.kick_observable.input(data, self)
|
self.kick_observable.input(data, self)
|
||||||
elif command == 'NICK':
|
elif command == "NICK":
|
||||||
self.nick_change_observable.input(data, self)
|
self.nick_change_observable.input(data, self)
|
||||||
elif command == 'NOTICE':
|
elif command == "NOTICE":
|
||||||
self.notice_observable.input(data, self)
|
self.notice_observable.input(data, self)
|
||||||
elif command == 'PRIVMSG':
|
elif command == "PRIVMSG":
|
||||||
self.priv_msg_observable.input(data, self)
|
self.priv_msg_observable.input(data, self)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
@@ -109,7 +110,7 @@ class Connection(object):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def is_idented(self, user: str):
|
def is_idented(self, user: str):
|
||||||
self.send_to_user('NickServ', 'ACC ' + user)
|
self.send_to_user("NickServ", f"ACC {user}")
|
||||||
with self.condition_lock:
|
with self.condition_lock:
|
||||||
while user not in self.idented_look_up:
|
while user not in self.idented_look_up:
|
||||||
self.condition_lock.wait()
|
self.condition_lock.wait()
|
||||||
@@ -136,19 +137,28 @@ class Connection(object):
|
|||||||
"""
|
"""
|
||||||
establish the connection
|
establish the connection
|
||||||
"""
|
"""
|
||||||
self.wraper = ssl.create_default_context()
|
|
||||||
self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
socker = socket.create_connection((self.details.get_server(), self.details.get_port()))
|
socker = socket.create_connection(
|
||||||
self.irc =self.wraper.wrap_socket(socker, server_hostname=self.details.get_server())
|
(self.details.get_server(), self.details.get_port())
|
||||||
|
)
|
||||||
|
if self.details.get_ssl().lower() != "false":
|
||||||
|
self.wraper = ssl.create_default_context()
|
||||||
|
self.irc = self.wraper.wrap_socket(
|
||||||
|
socker, server_hostname=self.details.get_server()
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self.irc = socker
|
||||||
# print(self.irc.recv(512))
|
# print(self.irc.recv(512))
|
||||||
self.irc.send("NICK ".encode() + self.details.get_nick().encode() + "\r\n".encode())
|
self.irc.send(f"NICK {self.details.get_nick()}\r\n".encode())
|
||||||
self.irc.send("USER botty botty botty :Botty \n".encode())
|
self.irc.send("USER botty botty botty :Botty \n".encode())
|
||||||
if (self.details.get_pwd() != ''):
|
if self.details.get_pwd() != "":
|
||||||
self.send_to_user("NICKSERV", "identify " + self.details.get_nick() + " " + self.details.get_pwd() + ' ')
|
self.send_to_user(
|
||||||
self.irc.send("JOIN ".encode() + self.details.get_channel().encode() + '\r\n'.encode())
|
"NICKSERV",
|
||||||
self.irc.send("WHO ".encode() + self.details.get_channel().encode() + '\r\n'.encode())
|
f"identify {self.details.get_nick()} {self.details.get_pwd()} ",
|
||||||
self.irc.send("MODE ".encode()+self.details.get_nick().encode()+" -R".encode()+'\r\n'.encode())
|
)
|
||||||
|
self.irc.send(f"JOIN {self.details.get_channel()}\r\n".encode())
|
||||||
|
self.irc.send(f"WHO {self.details.get_channel()}\r\n".encode())
|
||||||
|
self.irc.send(f"MODE {self.details.get_nick()} -R\r\n".encode())
|
||||||
|
|
||||||
_thread.start_new_thread(self.sender, ())
|
_thread.start_new_thread(self.sender, ())
|
||||||
|
|
||||||
|
|||||||
@@ -3,36 +3,44 @@ class ConnectionDetails(object):
|
|||||||
"""
|
"""
|
||||||
:return: the server to connect to
|
:return: the server to connect to
|
||||||
"""
|
"""
|
||||||
return self._data['server']
|
return self._data["server"]
|
||||||
|
|
||||||
|
def get_ssl(self):
|
||||||
|
"""
|
||||||
|
:return: connect with ssl
|
||||||
|
"""
|
||||||
|
if self._data["ssl"] != "false":
|
||||||
|
return "true"
|
||||||
|
return "false"
|
||||||
|
|
||||||
def get_nick(self):
|
def get_nick(self):
|
||||||
"""
|
"""
|
||||||
:return: own nick
|
:return: own nick
|
||||||
"""
|
"""
|
||||||
return self._data['nick']
|
return self._data["nick"]
|
||||||
|
|
||||||
def get_channel(self):
|
def get_channel(self):
|
||||||
"""
|
"""
|
||||||
:return: the channel connected into
|
:return: the channel connected into
|
||||||
"""
|
"""
|
||||||
return self._data['channel']
|
return self._data["channel"]
|
||||||
|
|
||||||
def get_port(self):
|
def get_port(self):
|
||||||
return int(self._data['port'])
|
return int(self._data["port"])
|
||||||
|
|
||||||
def get_lang(self):
|
def get_lang(self):
|
||||||
return self._data['lang']
|
return self._data["lang"]
|
||||||
|
|
||||||
def change_lang(self, lang):
|
def change_lang(self, lang):
|
||||||
self._data['lang'] = lang
|
self._data["lang"] = lang
|
||||||
|
|
||||||
def get_mods(self):
|
def get_mods(self):
|
||||||
return self._data['mods']
|
return self._data["mods"]
|
||||||
|
|
||||||
def get_pwd(self):
|
def get_pwd(self):
|
||||||
if self._data['pwd'] is None:
|
if self._data["pwd"] is None:
|
||||||
return ''
|
return ""
|
||||||
return self._data['pwd']
|
return self._data["pwd"]
|
||||||
|
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self._data = config
|
self._data = config
|
||||||
|
|||||||
Reference in New Issue
Block a user