Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/60972


Change subject: Add simple TCP->IRC forwarding bot
......................................................................

Add simple TCP->IRC forwarding bot

This patch adds a simple Python IRC bot which reads data from a TCP socket and
writes it to an IRC channel.

Change-Id: I16aed61ba1ac9b98820796aa1189e775c46f0c63
---
A tcpirc.py
1 file changed, 87 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/debs/adminbot 
refs/changes/72/60972/1

diff --git a/tcpirc.py b/tcpirc.py
new file mode 100755
index 0000000..91d398f
--- /dev/null
+++ b/tcpirc.py
@@ -0,0 +1,87 @@
+#!/usr/bin/env python
+# -*- coding: utf8 -*-
+"""
+  Simple TCP -> IRC forwarder bot
+
+  Takes a single argument which is the path a JSON configuration file. The file
+  should have the following format:
+
+    {
+        "irc": {
+            "channel": "#wikimedia-operations",
+            "network": ["irc.freenode.net", 6667],
+            "nickname": "tcpircbot",
+            "realname": "tcp-irc bot"
+        },
+        "tcp": {
+            "maxclients": 5,
+            "port": 9125
+        }
+    }
+
+"""
+import sys
+reload(sys)
+sys.setdefaultencoding('utf8')
+
+import json
+import os
+import select
+import socket
+
+try:
+    import irc.bot as ircbot
+except ImportError:
+    import ircbot
+
+
+# Max bytes to read from socket
+BUFSIZE = 4096
+
+
+class ForwarderBot(ircbot.SingleServerIRCBot):
+    """Minimal IRC bot; joins a channel."""
+
+    def __init__(self, network, nickname, realname, channel):
+        ircbot.SingleServerIRCBot.__init__(self, [network], nickname, realname)
+        self.channel = channel
+
+    def on_welcome(self, connection, event):
+        connection.join(self.channel)
+
+
+if len(sys.argv) < 2:
+    sys.exit('Usage: %s CONFIGFILE' % __file__)
+
+with open(sys.argv[1]) as f:
+    config = json.load(f)
+
+# Create a TCP server socket
+server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+server.bind(('', config['tcp']['port']))
+server.listen(config['tcp']['maxclients'])
+
+# Create a bot and connect to IRC
+bot = ForwarderBot(**config['irc'])
+bot._connect()
+
+sockets = [server, bot.connection.socket]
+
+while 1:
+    readable, writable, exceptional = select.select(sockets, [], [])
+    for sock in readable:
+        if sock is server:
+            conn, addr = server.accept()
+            sockets.append(conn)
+        elif sock is bot.connection.socket:
+            bot.connection.process_data()
+        else:
+            data = sock.recv(BUFSIZE)
+            if data:
+                try:
+                    bot.connection.privmsg(bot.channel, data)
+                except UnicodeDecodeError:
+                    pass
+            else:
+                sock.close()
+                sockets.remove(sock)

-- 
To view, visit https://gerrit.wikimedia.org/r/60972
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I16aed61ba1ac9b98820796aa1189e775c46f0c63
Gerrit-PatchSet: 1
Gerrit-Project: operations/debs/adminbot
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <o...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to