summaryrefslogtreecommitdiffstats
path: root/classes
diff options
context:
space:
mode:
authornico <nico.wellpott@uni-oldenburg.de>2018-10-03 23:40:12 +0200
committerGitHub <noreply@github.com>2018-10-03 23:40:12 +0200
commit1987753e3fb2bd8328efd99935bcceb72efedb3e (patch)
tree880e58b10bf08a623f7b559afe4c0526b5ff1c51 /classes
parent67f9eb9ed3642f4430aabdd8c33aaedecf25d316 (diff)
parent6c6c14b55fdbf3f79e21f961354952190b443a6b (diff)
Merge pull request #4 from mightyBroccoli/dev
+ added !xep feature
Diffstat (limited to 'classes')
-rw-r--r--classes/functions.py2
-rw-r--r--classes/strings.py9
-rw-r--r--classes/xep.py87
3 files changed, 94 insertions, 4 deletions
diff --git a/classes/functions.py b/classes/functions.py
index 08d6146..a8ed356 100644
--- a/classes/functions.py
+++ b/classes/functions.py
@@ -93,7 +93,7 @@ class ContactInfo:
# class handeling XMPPError exeptions
class HandleError:
- def __init__(self, error, msg, key, target):
+ def __init__(self, error, msg, key, target="target missing"):
self.error = error
self.message = msg
self.key = key
diff --git a/classes/strings.py b/classes/strings.py
index ade0520..6866a31 100644
--- a/classes/strings.py
+++ b/classes/strings.py
@@ -12,7 +12,8 @@ class StaticAnswers:
'help': '!help -- display this text',
'version': '!version domain.tld -- receive XMPP server version',
'uptime': '!uptime domain.tld -- receive XMPP server uptime',
- 'contact': '!contact domain.tld -- receive XMPP server contact address info'}
+ 'contact': '!contact domain.tld -- receive XMPP server contact address info',
+ 'xep': '!xep XEP Number -- recieve information about the specified XEP'}
self.possible_answers = {
'1': 'I heard that, %s.',
'2': 'I am sorry for that %s.',
@@ -22,8 +23,10 @@ class StaticAnswers:
'2': 'not a valid target'
}
self.keywords = {
- "keywords": ["!help", "!uptime", "!version", "!contact"],
- "no_arg_keywords": ["!help"]
+ "keywords": ["!help", "!uptime", "!version", "!contact", "!xep"],
+ "domain_keywords": ["!uptime", "!version", "!contact"],
+ "no_arg_keywords": ["!help"],
+ "number_keywords": ["!xep"]
}
def keys(self, arg="", keyword='keywords'):
diff --git a/classes/xep.py b/classes/xep.py
new file mode 100644
index 0000000..9e4f61f
--- /dev/null
+++ b/classes/xep.py
@@ -0,0 +1,87 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+import requests
+import defusedxml.ElementTree as ET
+
+
+class XEPRequest:
+ def __init__(self, msg, xepnumber):
+ """
+ class which requests the header of the referenced xep
+ :param xepnumber: number int or str to request the xep for
+ """
+ self.message_type = msg['type']
+ self.muc_nick = msg['mucnick']
+
+ self.reqxep = str(xepnumber)
+ self.xeplist = None
+ self.acceptedxeps = list()
+
+ def req_xeplist(self):
+ """
+ query and save the current xep list to reduce network bandwidth
+ """
+ try:
+ with open(".etag") as file:
+ local_etag = file.read()
+ except FileNotFoundError:
+ local_etag = ""
+
+ with requests.Session() as s:
+ s.headers.update({'Accept': 'application/xml'})
+ head = s.head("https://xmpp.org/extensions/xeplist.xml")
+ etag = head.headers['etag']
+
+ if local_etag == etag:
+ with open("xeplist.xml", "r") as file:
+ self.xeplist = ET.fromstring(file.read())
+ else:
+ r = s.get("https://xmpp.org/extensions/xeplist.xml")
+ r.encoding = 'utf-8'
+ local_etag = head.headers['etag']
+
+ with open("xeplist.xml", "w") as file:
+ file.write(r.content.decode())
+ self.xeplist = ET.fromstring(r.content.decode())
+
+ with open('.etag', 'w') as string:
+ string.write(local_etag)
+
+ # populate xep comparison list
+ for xep in self.xeplist.findall(".//*[@accepted='true']/number"):
+ self.acceptedxeps.append(xep.text)
+
+ def get(self):
+ """
+ function to query the xep entry if xepnumber is present in xeplist
+ :return: nicely formatted xep header information
+ """
+ # check if xeplist is accurate
+ self.req_xeplist()
+
+ result = list()
+ # if requested number is inside acceptedxeps continou
+ if self.reqxep in self.acceptedxeps:
+ searchstring = ".//*[@accepted='true']/[number='%s']" % self.reqxep
+
+ for item in self.xeplist.findall(searchstring):
+ for x in range(1,5):
+ result.append(item[x].tag + " : " + item[x].text)
+
+ else:
+ if self.message_type == "groupchat":
+ result.append(self.muc_nick + " : " + "XEP-" + str(self.reqxep) + " : is not available.")
+ else:
+ result.append("XEP-" + str(self.reqxep) + " : is not available.")
+
+ return result
+
+ def format(self):
+ reply = self.get()
+ if self.message_type == "groupchat":
+ text = "%s: " % self.muc_nick
+ reply[0] = text + reply[0]
+
+ text = '\n'.join(reply)
+
+ return text