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
pull/1/head
Konrad Krawiec 6 years ago committed by Simon Quigley
parent 1c1cc3f828
commit 9020af538a

@ -0,0 +1,3 @@
{
"phabricator.uri" : "https://phab.lubuntu.me/"
}

@ -4,6 +4,7 @@ import http
import hmac import hmac
import json import json
import socket import socket
import threading
from flask import Flask, request from flask import Flask, request
from pprint import pprint from pprint import pprint
from hashlib import sha256 from hashlib import sha256
@ -15,33 +16,17 @@ phab = Phabricator(host=website+"/api/", token="API KEY")
app = Flask(__name__) app = Flask(__name__)
def isnewtask(task): global username
modified = None username = "lugito"
server = "irc.freenode.net"
for data in task: port = 6667
if modified != None: channel = "#lubuntu-devel"
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"
def connecttoirc():
global conn, username
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect((server, port)) conn.connect((server, port))
setup = False setup = False
usersuffix = 0 usersuffix = 0
while setup == False: while setup == False:
response = conn.recv(512).decode("utf-8") response = conn.recv(512).decode("utf-8")
@ -64,6 +49,22 @@ def sendnotice(message):
if "366" in response: if "366" in response:
setup = True 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")) conn.send("NOTICE {} :{}\r\n".format(channel, message).encode("utf-8"))
def ircmessage(objectstr, who, body, link): def ircmessage(objectstr, who, body, link):
@ -80,6 +81,18 @@ def ircmessage(objectstr, who, body, link):
# Aaaaand, send it off! # Aaaaand, send it off!
sendnotice(message) 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"]) @app.route("/", methods=["POST"])
def main(): def main():
data = request.data data = request.data
@ -173,4 +186,8 @@ def main():
return "OK" return "OK"
if __name__ == "__main__": 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)
Loading…
Cancel
Save