89 lines
3.6 KiB
Python
89 lines
3.6 KiB
Python
# Copyright (C) 2020 by eichhornchen, ÿnérant
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
import curses
|
|
from argparse import ArgumentParser
|
|
from typing import Any
|
|
|
|
from .hazel import Hazelnut, Squirrel
|
|
from .messages import Packet, HelloTLV
|
|
from .term_manager import TermManager
|
|
|
|
|
|
class Squinnondation:
|
|
args: Any
|
|
bind_address: str
|
|
bind_port: int
|
|
no_emoji: bool
|
|
no_markdown: bool
|
|
debug: bool
|
|
screen: Any
|
|
|
|
def parse_arguments(self) -> None:
|
|
parser = ArgumentParser(description="MIRC client.")
|
|
parser.add_argument('bind_address', type=str, default="localhost",
|
|
help="Address of the client.")
|
|
parser.add_argument('bind_port', type=int, default=2500,
|
|
help="Port of the client. Must be between 1024 and 65535.")
|
|
parser.add_argument('--client_address', type=str, default=None,
|
|
help="Address of the first neighbour.")
|
|
parser.add_argument('--client_port', type=int, default=0,
|
|
help="Port of the first neighbour. Must be between 1024 and 65535.")
|
|
parser.add_argument('--no-emoji', '-ne', action='store_true',
|
|
help="Don't replace emojis.")
|
|
parser.add_argument('--no-markdown', '-nm', action='store_true',
|
|
help="Don't replace emojis.")
|
|
parser.add_argument('--debug', '-d', action='store_true', help="Debug mode.")
|
|
self.args = parser.parse_args()
|
|
|
|
if not (1024 <= self.args.bind_port <= 65535) or\
|
|
not (not self.args.client_port or 1024 <= self.args.client_port <= 65535):
|
|
raise ValueError("Ports must be between 1024 and 65535.")
|
|
|
|
self.bind_address = self.args.bind_address
|
|
self.bind_port = self.args.bind_port
|
|
self.no_emoji = self.args.no_emoji
|
|
self.no_markdown = self.args.no_markdown
|
|
self.debug = self.args.debug
|
|
|
|
@staticmethod
|
|
def main() -> None: # pragma: no cover
|
|
instance = Squinnondation()
|
|
instance.parse_arguments()
|
|
|
|
with TermManager() as term_manager:
|
|
screen = term_manager.screen
|
|
instance.screen = screen
|
|
screen.addstr(0, 0, "Enter your nickname: ")
|
|
curses.echo()
|
|
nickname = screen.getstr(225).decode("UTF-8") # Limit nickname length to be included in a DataTLV
|
|
curses.noecho()
|
|
|
|
squirrel = Squirrel(instance, nickname)
|
|
|
|
if not squirrel.squinnondation.no_emoji:
|
|
# Check that the emoji lib is installed
|
|
try:
|
|
import emoji
|
|
_ = emoji
|
|
except ImportError:
|
|
squirrel.squinnondation.no_emoji = True
|
|
squirrel.add_system_message("Warning: the emoji lib is not installed. The support will be disabled."
|
|
"To use them, please consider to install the emoji package.")
|
|
|
|
squirrel.refresh_history()
|
|
squirrel.refresh_input()
|
|
if not instance.no_emoji:
|
|
squirrel.refresh_emoji_pad()
|
|
|
|
if instance.args.client_address and instance.args.client_port:
|
|
hazelnut = Hazelnut(address=instance.args.client_address, port=instance.args.client_port)
|
|
hazelnut.potential = True
|
|
squirrel.update_hazelnut_table(hazelnut)
|
|
htlv = HelloTLV().construct(8, squirrel)
|
|
pkt = Packet().construct(htlv)
|
|
squirrel.send_packet(hazelnut, pkt)
|
|
|
|
squirrel.start_threads()
|
|
squirrel.wait_for_key()
|