#!/usr/bin/env python3 import http import hmac import json import socket from flask import Flask, request from pprint import pprint from hashlib import sha256 from phabricator import Phabricator website = "https://phab.lubuntu.me" 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" 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") if "No Ident response" in response: conn.send("NICK {}\r\n".format(username).encode("utf-8")) conn.send("USER {} * * :{}\r\n".format(username, username).encode("utf-8")) if "376" in response: conn.send("JOIN {}\r\n".format(channel).encode("utf-8")) if "433" in response: usersuffix = usersuffix + 1 username = username + str(usersuffix) conn.send("NICK {}\r\n".format(username).encode("utf-8")) conn.send("USER {} * * :{}\r\n".format(username, username).encode("utf-8")) if "PING" in response: conn.send("PONG :{}\r\n".format(response.split(":")[1]).encode("utf-8")) if "366" in response: setup = True conn.send("NOTICE {} :{}\r\n".format(channel, message).encode("utf-8")) @app.route("/", methods=["POST"]) def main(): data = request.data hash = hmac.new(bytes(u"HMAC KEY", "utf-8"), data, sha256) # We MUST ensure that the request came from Phab if hash.hexdigest() == request.headers["X-Phabricator-Webhook-Signature"]: data = json.loads(data) print(data) print taskexists = True try: tasksearch = phab.transaction.search(objectIdentifier=data["object"]["phid"])["data"] except http.client.HTTPException: taskexists = False if taskexists: print("Task exists, checking to see if it's new.") newtask = isnewtask(tasksearch) if newtask: print("Yes, it's a new task.") else: print("No, it's not a new task.") # If it's not a new task, let's see if it's a comment, and if it's just an edit comment = None commentid = None edited = None if not newtask: commentid = None edited = False for task in tasksearch: dataepoch = data["action"]["epoch"] datemodified = task["dateModified"] if datemodified >= (dataepoch - 10) and datemodified <= (dataepoch + 10) and task["comments"] != []: print("It's a comment, yes.") comment = True commentid = task["id"] if datemodified != task["dateCreated"]: print("The comment was edited.") edited = True else: print("The comment was NOT edited.") edited = False break else: comment = False if comment or edited or newtask: # We should also know who did this thing userlookup = tasksearch[0]["authorPHID"] who = dict(phab.phid.query(phids=[userlookup]))[userlookup]["fullName"] fulltaskname = phab.phid.query(phids=[data["object"]["phid"]])[data["object"]["phid"]]["fullName"] link = "\x032" + phab.phid.query(phids=[data["object"]["phid"]])[data["object"]["phid"]]["uri"] if commentid: link = link + "#" + str(commentid) + "\x03" else: link = link + "\x03" message = "\x033[\x03\x0313"+ fulltaskname +"\x03\x033]\x03 \x0315" + str(who) + "\x03 " if comment: message = message + "commented on the task: " + link elif edited: message = message + "edited a message on the task: " + link elif newtask: message = message + "just created this task: " + link print(message) sendnotice(message) return "OK" if __name__ == "__main__": app.run(host="0.0.0.0", port=5000)