123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- #!/usr/bin/env python3
- #
- # Internet Delay Chat server written in Python Trio.
- #
- # Written by: Andrew <https://www.andrewyu.org>
- # luk3yx <https://luk3yx.github.io>
- #
- # This is free and unencumbered software released into the public
- # domain.
- #
- # Anyone is free to copy, modify, publish, use, compile, sell, or
- # distribute this software, either in source code form or as a compiled
- # binary, for any purpose, commercial or non-commercial, and by any
- # means.
- #
- # In jurisdictions that recognize copyright laws, the author or authors
- # of this software dedicate any and all copyright interest in the
- # software to the public domain. We make this dedication for the benefit
- # of the public at large and to the detriment of our heirs and
- # successors. We intend this dedication to be an overt act of
- # relinquishment in perpetuity of all present and future rights to this
- # software under copyright law.
- #
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- # IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
- # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- # OTHER DEALINGS IN THE SOFTWARE.
- #
- # This program requires Python 3.9 or later due to its extensive use of
- # type annotations. Usage with an older version would likely cause
- # SyntaxErrors. If mypy has problems detecting types on the Trio
- # library, install trio-typing. Please mypy after every runnable edit.
- #
- from __future__ import annotations
- from typing import Awaitable, Callable
- import time
- from pprint import pprint
- import trio
- import ssl
- import traceback
- import exceptions
- import entities
- import minilog
- import utils
- import config
- starttime = time.time()
- PORT = 6835
- ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
- ctx.load_cert_chain(
- "/etc/letsencrypt/live/fcm.andrewyu.org/fullchain.pem",
- "/etc/letsencrypt/live/fcm.andrewyu.org/privkey.pem",
- )
- client_id_counter = -1
- local_users: dict[bytes, entities.User] = {}
- local_channels: dict[bytes, entities.Channel] = {}
- for username in config.users:
- local_users[username] = entities.User(
- username=username,
- password=config.users[username]["password"],
- options=config.users[username]["options"],
- )
- for channelname in config.channels:
- local_channels[channelname] = entities.Channel(
- channelname=channelname,
- broadcast_to=[
- local_users[username]
- for username in config.channels[channelname]["broadcast_to"]
- ],
- guild=None,
- )
- for u in local_channels[channelname].broadcast_to:
- u.in_channels.append(local_channels[channelname])
- _CMD_HANDLER = Callable[
- [entities.Client, "dict[str, bytes]"], Awaitable[None]
- ]
- _registered_commands: dict[bytes, _CMD_HANDLER] = {}
- def register_command(
- command: str,
- ) -> Callable[[_CMD_HANDLER], _CMD_HANDLER]:
- def register_inner(func: _CMD_HANDLER) -> _CMD_HANDLER:
- _registered_commands[command.encode("ascii")] = func
- return func
- return register_inner
- @register_command("HELP")
- async def _help_cmd(
- client: entities.Client, args: dict[str, bytes]
- ) -> None:
- await utils.send(
- client,
- b"HELP",
- AVAILABLE_COMMANDS=b" ".join(_registered_commands),
- )
- @register_command("LOGIN")
- async def _login_cmd(
- client: entities.Client, args: dict[str, bytes]
- ) -> None:
- if client.user:
- raise exceptions.AlreadyLoggedIn(
- b"You are already logged in as "
- + client.user.username
- + b"."
- )
- attempting_username = utils.carg(args, "USERNAME", b"LOGIN")
- attempting_password = utils.carg(args, "PASSWORD", b"LOGIN")
- try:
- if (
- local_users[attempting_username].password
- == attempting_password
- ):
- client.user = local_users[attempting_username]
- local_users[attempting_username].connected_clients.append(
- client
- )
- await utils.send(
- client,
- b"LOGIN_GOOD",
- USERNAME=attempting_username,
- COMMENT=b"Login is good.",
- )
- assert client.user is not None
- for c in client.user.in_channels:
- await utils.send(
- client,
- b"JOIN",
- CHANNEL=c.channelname,
- USERS=b" ".join(
- [u.username for u in c.broadcast_to]
- ),
- )
- await utils.send(
- client,
- b"END_BURST",
- COMMENT=b"I'm finished telling you the state you're in.",
- )
- if client.user.queue:
- for i in range(len(client.user.queue)):
- b = client.user.queue.pop(0)
- # Do not pop "i" here, because we're modifying the
- # iterated object within the iteration, so the
- # indexes change! Therefore pop 0.
- await utils.quote(client, b)
- await utils.send(
- client,
- b"END_OFFLINE_MESSAGES",
- COMMENT=b"I'm finished telling you your offline messages.",
- )
- else:
- raise exceptions.LoginFailed(
- b"Invalid password for " + attempting_username + b"."
- )
- except KeyError:
- raise exceptions.LoginFailed(
- attempting_username + b" is not a registered username."
- )
- @register_command("PING")
- async def _ping_cmd(
- client: entities.Client, args: dict[str, bytes]
- ) -> None:
- await utils.send(client, b"PONG", COOKIE=utils.carg(args, "COOKIE"))
- @register_command("EGG")
- async def _egg_cmd(
- client: entities.Client, args: dict[str, bytes]
- ) -> None:
- await utils.send(
- client,
- b"EASTER_EGG",
- YAY=b"Andrew: Never gonna give you up\nnever gonna let you down\nnever gonna run around and desert you\nnever gonna make you cry\nnever gonna say goodbye\nnever gonna tell a lie and hurt you",
- )
- @register_command("PRIVMSG")
- async def _privmsg_cmd(
- client: entities.Client, args: dict[str, bytes]
- ) -> None: # in the future this should return the raw line sent to the target client
- if not client.user:
- raise exceptions.NotLoggedIn(
- b"You can't use PRIVMSG before logging in!"
- )
- else:
- target_name = utils.carg(args, "TARGET")
- try:
- target_user = local_users[target_name]
- except KeyError:
- raise exceptions.NonexistantTargetError(
- b"The target " + target_name + b" is nonexistant."
- )
- else:
- await utils.send(
- target_user,
- b"PRIVMSG",
- SOURCE=client.user.username,
- TYPE=args.get("TYPE", b"NORMAL"),
- TARGET=utils.carg(args, "TARGET"),
- MESSAGE=utils.carg(args, "MESSAGE"),
- )
- if target_user is not client.user:
- await utils.send(
- client.user,
- b"PRIVMSG",
- SOURCE=client.user.username,
- TYPE=args.get("TYPE", b"NORMAL"),
- TARGET=utils.carg(args, "TARGET"),
- MESSAGE=utils.carg(args, "MESSAGE"),
- )
- # Do you think that we should put echo-message here, or in utils.send()?
- @register_command("CHANMSG")
- async def _chanmsg_cmd(
- client: entities.Client, args: dict[str, bytes]
- ) -> None:
- if not client.user:
- raise exceptions.NotLoggedIn(
- b"You can't use CHANMSG before logging in!"
- )
- else:
- target_channel_name = utils.carg(args, "TARGET")
- try:
- target_channel = local_channels[target_channel_name]
- except KeyError:
- raise exceptions.NonexistantTargetError(
- b"The target channel "
- + target_channel_name
- + b"is nonexistant."
- )
- else:
- await utils.send(
- target_channel,
- b"CHANMSG",
- SOURCE=client.user.username,
- TYPE=args.get("TYPE", b"NORMAL"),
- TARGET=target_channel_name,
- MESSAGE=utils.carg(args, "MESSAGE"),
- )
- # await utils.send(
- # client.user,
- # b"CHANMSG",
- # source=client.user.username,
- # target=target_channel_name,
- # message=utils.carg(args, "MESSAGE"),
- # )
- async def connection_loop(stream: trio.SSLStream) -> None:
- global client_id_counter
- client_id_counter += 1
- ident = str(client_id_counter).encode("ascii")
- minilog.note(f"Connection {str(ident)} has started.")
- client = entities.Client(cid=ident, stream=stream)
- await utils.send(client, b"MOTD", MESSAGE=config.motd)
- client.ccrt = stream.getpeercert()
- await utils.send(client, b"CLIENT_CERT", FINGERPRINT=repr(client.ccrt).encode("utf-8"))
- try:
- msg = b""
- async for newmsg in stream:
- msg += newmsg
- split_msg = msg.split(b"\n")
- if split_msg[-1] == b"\r":
- split_msg = split_msg[:-1]
- if len(split_msg) < 2:
- continue
- data = split_msg[0:-1]
- msg = split_msg[-1]
- minilog.debug(f"{ident.decode('ascii')} >>> {data!r}")
- for cmdline in data:
- try:
- cmd, args = utils.bytesToStd(cmdline)
- cmd = cmd.upper()
- if cmd in _registered_commands:
- await _registered_commands[cmd](client, args)
- else:
- raise exceptions.UnknownCommand(
- cmd + b" is an unknown command."
- )
- except exceptions.IDCUserCausedException as e:
- await utils.send(
- client,
- e.severity,
- PROBLEM=e.error_type,
- COMMENT=e.args[0],
- )
- except Exception as exc:
- traceback.print_exc()
- minilog.warning(f"{ident!r}: crashed: {exc!r}")
- finally:
- if client.user:
- client.user.connected_clients.remove(client)
- del client.stream
- del client
- minilog.note(f"Connection {str(ident)} has ended.")
- async def tls_wrapper(s: trio.SocketStream) -> None:
- try:
- await connection_loop(trio.SSLStream(s, ctx, server_side=True))
- except trio.BrokenResourceError:
- minilog.caution("Some client has messed-up TLS.")
- async def main() -> None:
- await trio.serve_tcp(tls_wrapper, PORT)
- def run_i_guess() -> None:
- trio.run(main)
- if __name__ == "__main__":
- try:
- minilog.note("Definitions complete. Establishing listener.")
- trio.run(main)
- except KeyboardInterrupt:
- minilog.error("KeyboardInterrupt!")
- finally:
- minilog.note(
- f"I've ran for {str(time.time() - starttime)} seconds!"
- )
|