Welcome to mirror list, hosted at ThFree Co, Russian Federation.

irk.py « calm - cygwin.com/git/cygwin-apps/calm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4028dd78a184ca6153de24064ccdf8fefb43dd98 (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
#!/usr/bin/env python3
#
# Tell irked to send a message to an IRC channel
#
# First argument must be a channel URL. If it does not begin with "irc",
# the base URL for freenode is prepended.
#
# SPDX-License-Identifier: BSD-2-Clause

import json
import socket
import sys

DEFAULT_SERVER = ("localhost", 6659)
DEFAULT_TARGET = ['irc://irc.libera.chat/cygwin-bots']


def connect(server=DEFAULT_SERVER):
    return socket.create_connection(server)


def send(s, target, message):
    data = {"to": target, "privmsg": message}
    # print(json.dumps(data))
    s.sendall(bytes(json.dumps(data), "ascii"))


def irk(message, target=DEFAULT_TARGET, server=DEFAULT_SERVER):
    if not isinstance(target, list):
        target = [target]

    for t in target:
        try:
            s = connect(server)
            if "irc:" not in t and "ircs:" not in t:
                t = "irc://chat.freenode.net/{0}".format(t)

            send(s, t, message)

            s.close()
        except OSError:
            pass


def main():
    message = " ".join(sys.argv[1:])

    try:
        irk(message)
    except socket.error as e:
        sys.stderr.write("irk: write to server failed: %r\n" % e)
        sys.exit(1)


if __name__ == '__main__':
    main()