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

cygwin.com/git/cygwin-apps/calm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Turney <jon.turney@dronecode.org.uk>2016-08-30 14:41:21 +0300
committerJon Turney <jon.turney@dronecode.org.uk>2016-09-04 14:40:51 +0300
commit2674ffdbbf22526b0d846bddd36c0d3a5a527f7f (patch)
tree6430c1f73ba0fdde387f3e36c65d45dffb9c13db
parented2f5db5b59dd6eef84c98adfbdc0fe27b2d3cb6 (diff)
Add module for sending mesages to IRC via irked
-rwxr-xr-xcalm/irk.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/calm/irk.py b/calm/irk.py
new file mode 100755
index 0000000..b9f8957
--- /dev/null
+++ b/calm/irk.py
@@ -0,0 +1,48 @@
+#!/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 = '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):
+ s = connect(server)
+ if "irc:" not in target and "ircs:" not in target:
+ target = "irc://chat.freenode.net/{0}".format(target)
+
+ send(s, target, message)
+
+ s.close()
+
+
+def main():
+ message = " ".join(sys.argv[:])
+
+ 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()