Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved structure and readability of main.py #196

Merged
merged 3 commits into from
Jun 28, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 24 additions & 53 deletions src/honeybot/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@

import pkg_resources

try:
from honeybot.api import commands, memory
from honeybot.api.utils import configfile_to_list, get_requirements, prevent_none
except Exception as e:
raise e
from honeybot.api import commands, memory
from honeybot.api.utils import configfile_to_list, get_requirements, prevent_none


plugins = []
Expand All @@ -27,7 +24,7 @@
"""


class Bot_core:
class BotCore:
def __init__(self, info, password=""):
self.info = info
connect_config = configparser.ConfigParser()
Expand All @@ -45,7 +42,7 @@ def __init__(self, info, password=""):
self.time = time.time()

self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.isListenOn = 1
self.is_listen_on = 1
dom = self.server_url.split(".")
self.domain = ".".join(dom[-2:])
self.sp_command = "hbot"
Expand Down Expand Up @@ -126,7 +123,7 @@ def join(self, channel):
"""

def is_valid_plug_name(self, name):
if (name.startswith("__")) or (name == ""):
if name.startswith("__") or name == "":
return False

return True
Expand All @@ -142,7 +139,6 @@ def print_running_infos(self):
print("-" * 3)

def load_plugins_from_folder(self, category_folder, from_conf=None, from_dir=None):

if from_dir is not None:
to_load = [f for f in os.listdir(from_dir) if self.is_valid_plug_name(f)]
elif from_conf is not None:
Expand Down Expand Up @@ -245,49 +241,24 @@ def greet(self):
self.send(commands.join_channel(channel))

def pull(self):
while self.isListenOn:
while self.is_listen_on:
try:
data = self.irc.recv(2048)
raw_msg = data.decode("UTF-8")
msg = raw_msg.strip("\n\r")
self.stay_alive(msg)
self.core_commands_parse(msg)
logger.info(msg)

if len(data) == 0:
try:
logger.critical(f"<must handle reconnection - {len(data)}==0>")
sys.exit()
except Exception as e:
logger.info(e)
data = self.irc.recv(2048).decode("UTF-8", errors="replace")
for line in data.split("\r\n"):
if line != "":
self.core_commands_parse(line)

except KeyboardInterrupt:
self.is_listen_on = False
self.quit()
except Exception as e:
logger.info(e)

"""
ONGOING REQUIREMENT/S
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnastasiosZyngiridis Why did you remove this section?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Abdur-rahmaanJ Sorry for the mistake. As they where ONGOING REQUIREMENT/S i didn't touch them and forgot to put them back in after i was done editing the rest of the code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnastasiosZyngiridis Just add them back, thank you.


def stay_alive(self, incoming):
if not incoming:
logger.critical("<must handle reconnection - incoming is not True>")
sys.exit()
parts = incoming.split(":")
if parts[0].strip().lower() == "ping":
logger.warning(f"ping detected from: {parts[1]}")
self.send(commands.pong_return(self.domain))
self.send(commands.pong_return(parts[1]))

# all in one for registered bot
def registered_run(self):
self.connect()
self.identify()
self.greet()
self.load_plugins()
self.pull()

def unregistered_run(self):
self.print_running_infos()
self.connect()
self.greet()
self.load_plugins()
self.pull()
logger.error(e)
logger.debug("there was an error")
logger.debug(data)
logger.debug("!!")
logger.debug(line)
logger.debug("-" * 50)

def quit(self):
self.send(commands.quit())
self.is_listen_on = False