Initial commit.

pull/1/head
Simon Quigley 7 years ago
commit 5f3f2e083a

@ -0,0 +1,11 @@
Copyright 2018 Lubuntu Developers <lubuntu-devel@lists.ubuntu.com>
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

@ -0,0 +1,5 @@
# Lugito
This is Lubuntu's friendly IRC notifications bot, hooked up to our Phabricator instance at phab.lubuntu.me
The code is licensed under the 3-clause BSD license, and is copyrighted by the Lubuntu team. More info available in LICENSE.

134
lugito

@ -0,0 +1,134 @@
#!/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 = "##tsimonq2-thing-testing"
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:
# Let's get the title of the task
titles = []
for i in tasksearch:
if i["type"] == "title":
titles.append(i)
# We need some special casing in case this also renames the task title (to choose the new one, i.e. with a higher ID)
if len(titles) == 1:
title = titles[0]
elif len(titles) == 2:
if titles[0]["id"] > titles[1]["id"]:
title = titles[0]["fields"]["new"]
else:
title = titles[1]["fields"]["new"]
commentid = None
# Let's see if this was a comment and if it's just an edit
for task in tasksearch:
dataepoch = data["action"]["epoch"]
datemodified = task["dateModified"]
if datemodified >= (dataepoch - 10) and datemodified <= (dataepoch + 10) and task["comments"] != []:
comment = True
commentid = task["id"]
if datemodified != task["dateCreated"]:
edited = True
else:
edited = False
else:
comment = False
# We should also know who did this thing
userlookup = tasksearch[0]["authorPHID"]
who = dict(phab.phid.query(phids=[userlookup]))[userlookup]["fullName"]
if isnewtask(tasksearch) is False and commentid is not None:
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"] + "#" + str(commentid) + "\x03"
message = "\x033[\x03\x0313"+ fulltaskname +"\x03\x033]\x03 \x0315" + str(who) + "\x03 "
if edited == False:
message = message + "commented on the task: " + link
elif edited == True:
message = message + "edited a message on the task: " + link
sendnotice(message)
return "OK"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
Loading…
Cancel
Save