summaryrefslogtreecommitdiff
blob: fc85733eca6f5478cdab467bc977161892fe11f1 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import discord
import asyncio
import argparse
import socket


################################################################################
## MAIN ########################################################################
################################################################################
parser = argparse.ArgumentParser(
    description = (
        "Discord Client for JabberHive"
    )
)

parser.add_argument(
    '-d',
    '--destination',
    type = str,
    help = 'UNIX socket this client connects to.',
)

parser.add_argument(
    '-t',
    '--token',
    type = str,
    help = 'Discord token.',
)

parser.add_argument(
    '-c',
    '--print-chat',
    dest='print_chat',
    help = 'Prints all messages',
    action="store_true",
)

args = parser.parse_args()
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

client = discord.Client()

def get_jh_reply ():
    is_done = False
    result = ""
    jh_reply = b""

    matched = 0

    while not is_done:
        c = b"\0"
        jh_reply = b""

        while (c != b"\n"):
            c = server.recv(1)
            jh_reply += c

        if ((jh_reply == b"!P \n") or (jh_reply == b"!N \n")):
            is_done = True
        else:
            jh_reply = jh_reply.decode("UTF-8")

            if (jh_reply.startswith("!GR ")):
                result = jh_reply[4:]
                result = result[:-1]

    return result

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
    server.connect(args.destination)

@client.event
async def on_message(message):
    if (message.author.id == client.user.id):
        return

    server.sendall(
        b"?RLR "
        + bytes(message.clean_content.replace('\n', ' '), "utf8")
        + b"\n"
    )

    result = get_jh_reply()

    if (args.print_chat):
        print(
            str(message.server)
            + "#"
            + str(message.channel.name)
            + " <"
            + str(message.author.name)
            + "> "
            + str(message.clean_content)
        )

        if (len(result) > 0):
            print("#" + str(message.channel.name) + " <- " + result)

    if (len(result) > 0):
        await client.send_message(message.channel, result)

client.run(args.token)