From 9020af538a5f5a80abebf7e1a25fc6713d93684d Mon Sep 17 00:00:00 2001 From: Konrad Krawiec Date: Sun, 24 Jun 2018 14:55:19 -0500 Subject: [PATCH] Improve the logic. Summary: Bot now connects to the IRC server only once, responds to ping requests, and reconnects in case of failure. Test Plan: Test in prod Reviewers: tsimonq2 Reviewed By: tsimonq2 Subscribers: tsimonq2 Differential Revision: https://phab.lubuntu.me/D1 --- .arcconfig | 3 +++ lugito | 65 ++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 44 insertions(+), 24 deletions(-) create mode 100644 .arcconfig diff --git a/.arcconfig b/.arcconfig new file mode 100644 index 0000000..3d90d92 --- /dev/null +++ b/.arcconfig @@ -0,0 +1,3 @@ +{ + "phabricator.uri" : "https://phab.lubuntu.me/" +} diff --git a/lugito b/lugito index 2515870..5bc6550 100755 --- a/lugito +++ b/lugito @@ -4,6 +4,7 @@ import http import hmac import json import socket +import threading from flask import Flask, request from pprint import pprint from hashlib import sha256 @@ -15,33 +16,17 @@ phab = Phabricator(host=website+"/api/", token="API KEY") app = Flask(__name__) -def isnewtask(task): - modified = None - - for data in task: - if modified != None: - if data["dateCreated"] == data["dateModified"] and data["dateCreated"] == modified: - modified = data["dateCreated"] - newtask = True - else: - newtask = False - break - else: - modified = data["dateCreated"] - - return newtask - -def sendnotice(message): - username = "lugito" - server = "irc.freenode.net" - port = 6667 - channel = "#lubuntu-devel" +global username +username = "lugito" +server = "irc.freenode.net" +port = 6667 +channel = "#lubuntu-devel" +def connecttoirc(): + global conn, username conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.connect((server, port)) - setup = False - usersuffix = 0 while setup == False: response = conn.recv(512).decode("utf-8") @@ -64,6 +49,22 @@ def sendnotice(message): if "366" in response: setup = True +def isnewtask(task): + modified = None + for data in task: + if modified != None: + if data["dateCreated"] == data["dateModified"] and data["dateCreated"] == modified: + modified = data["dateCreated"] + newtask = True + else: + newtask = False + break + else: + modified = data["dateCreated"] + + return newtask + +def sendnotice(message): conn.send("NOTICE {} :{}\r\n".format(channel, message).encode("utf-8")) def ircmessage(objectstr, who, body, link): @@ -80,6 +81,18 @@ def ircmessage(objectstr, who, body, link): # Aaaaand, send it off! sendnotice(message) +def listenirc(): + while 1: + ircmsg = conn.recv(512) + if len(ircmsg) == 0: + print("Connection lost, reconnecting!") + connecttoirc() + continue + ircmsg = ircmsg.decode("UTF-8").strip('\n\r') + print(ircmsg) + if ircmsg.find("PING :") != -1: + conn.send(bytes("PONG :pingis\n", "UTF-8")) + @app.route("/", methods=["POST"]) def main(): data = request.data @@ -173,4 +186,8 @@ def main(): return "OK" if __name__ == "__main__": - app.run(host="0.0.0.0", port=5000) + connecttoirc() + t = threading.Thread(target=listenirc) + t.daemon = True + t.start() + app.run(host="0.0.0.0", port=5000) \ No newline at end of file