squinnondation/squinnondation/squinnondation.py

89 lines
3.5 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 .peers import Peer, User
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
multicast: 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.")
parser.add_argument('--multicast', '-mc', action='store_true', help="Use multicast?")
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
self.multicast = self.args.multicast
@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()
user = User(instance, nickname)
if not user.squinnondation.no_emoji:
# Check that the emoji lib is installed
try:
import emoji
_ = emoji
except ImportError:
user.squinnondation.no_emoji = True
user.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.")
user.refresh_history()
user.refresh_input()
user.refresh_emoji_pad()
if instance.args.client_address and instance.args.client_port:
peer = Peer(address=instance.args.client_address, port=instance.args.client_port)
htlv = HelloTLV().construct(8, user)
pkt = Packet().construct(htlv)
user.send_packet(peer, pkt)
user.start_threads()
user.wait_for_key()