File: control.py

package info (click to toggle)
localslackirc 1.36-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 364 kB
  • sloc: python: 2,664; makefile: 78
file content (55 lines) | stat: -rw-r--r-- 2,001 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# localslackirc
# Copyright (C) 2023 Salvo "LtWorf" Tomaselli
#
# localslackirc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>


import asyncio
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from irc import Client as IrcClient

from log import *

async def handle_write(ircclient: "IrcClient", reader, writer) -> None:
    dest = (await reader.readline()).strip()
    msg = await reader.read()

    await ircclient.send_slack_message(dest, msg, action=False, re_send_to_irc=True)

async def handle_sendfile(ircclient: "IrcClient", reader, writer) -> None:
    dest = (await reader.readline()).strip()
    filename = (await reader.readline()).strip().decode('utf8')
    content = await reader.read()

    if await ircclient.send_file(dest, content, filename=filename):
        writer.write(b"ok")
    else:
        writer.write(b"fail")

async def handle_client(ircclient: "IrcClient", reader, writer) -> None:
    command = (await reader.readline()).strip()

    if command == b"sendfile":
        await handle_sendfile(ircclient, reader, writer)
    elif command == b"write":
        await handle_write(ircclient, reader, writer)

async def listen(socket_path: str, ircclient: "IrcClient") -> None:
    server = await asyncio.start_unix_server(lambda r,w: handle_client(ircclient, r, w), socket_path)
    async with server:
        await server.serve_forever()