2018-10-10 17:43:01 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import defusedxml.ElementTree as Et
|
|
|
|
|
|
|
|
|
2018-11-06 23:43:11 +01:00
|
|
|
# XEP-0157: Contact Addresses for XMPP Services
|
2018-10-10 17:43:01 +02:00
|
|
|
class ServerContact:
|
2018-11-06 23:43:11 +01:00
|
|
|
"""
|
|
|
|
plugin to process the server contact addresses from a disco query
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
# init all necessary variables
|
2018-10-10 17:43:01 +02:00
|
|
|
self.possible_vars = ['abuse-addresses',
|
|
|
|
'admin-addresses',
|
|
|
|
'feedback-addresses',
|
|
|
|
'sales-addresses',
|
|
|
|
'security-addresses',
|
|
|
|
'support-addresses']
|
|
|
|
|
2018-11-06 23:43:11 +01:00
|
|
|
self.contact = None
|
|
|
|
self.target, self.opt_arg = None, None
|
|
|
|
|
2018-10-10 17:43:01 +02:00
|
|
|
def process(self):
|
|
|
|
# get etree from base xml
|
|
|
|
iq = Et.fromstring(str(self.contact))
|
|
|
|
|
|
|
|
# check if query is a valid result query
|
|
|
|
if iq.find('{http://jabber.org/protocol/disco#info}query'):
|
|
|
|
# only init result dict if result query is present
|
|
|
|
result = dict()
|
|
|
|
|
|
|
|
# extract query from iq
|
|
|
|
query = iq.find('{http://jabber.org/protocol/disco#info}query')
|
|
|
|
|
|
|
|
# extract jabber:x:data from query
|
|
|
|
xdata = query.findall('{jabber:x:data}x')
|
|
|
|
|
2018-11-09 19:42:21 +01:00
|
|
|
# iterate over all nodes with the xdata tag
|
|
|
|
for node in xdata:
|
2018-10-10 17:43:01 +02:00
|
|
|
|
2018-11-09 19:42:21 +01:00
|
|
|
# iterate over all child elements in node
|
|
|
|
for child in node:
|
2018-10-10 17:43:01 +02:00
|
|
|
|
2018-11-06 23:43:11 +01:00
|
|
|
# if one opt_arg is defined return just that one
|
|
|
|
if self.opt_arg in self.possible_vars:
|
|
|
|
if child.attrib['var'] == self.opt_arg:
|
|
|
|
# add section to result dict and append info
|
|
|
|
result[child.attrib['var']] = list()
|
|
|
|
for value in child:
|
|
|
|
result[child.attrib['var']].append(value.text)
|
|
|
|
|
2018-10-10 17:43:01 +02:00
|
|
|
# if node has a var attribute that matches our list process
|
2018-11-06 23:43:11 +01:00
|
|
|
elif child.attrib['var'] in self.possible_vars:
|
2018-10-10 17:43:01 +02:00
|
|
|
# add section to result dict and append info
|
|
|
|
result[child.attrib['var']] = list()
|
|
|
|
for value in child:
|
|
|
|
result[child.attrib['var']].append(value.text)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2018-11-06 23:43:11 +01:00
|
|
|
def format(self, query, target, opt_arg):
|
|
|
|
self.contact = query
|
|
|
|
|
|
|
|
self.target = target
|
|
|
|
self.opt_arg = opt_arg
|
|
|
|
|
2018-10-10 17:43:01 +02:00
|
|
|
result = self.process()
|
|
|
|
|
2018-11-06 23:43:11 +01:00
|
|
|
# if result is present continue
|
2018-10-10 17:43:01 +02:00
|
|
|
if result:
|
|
|
|
text = "contact addresses for %s are\n" % self.target
|
|
|
|
|
2018-11-06 23:43:11 +01:00
|
|
|
# if opt_arg is present and member of possible_vars change text line
|
|
|
|
if opt_arg in self.possible_vars:
|
|
|
|
text = "%s for %s are\n" % (self.opt_arg, self.target)
|
|
|
|
|
2018-10-10 17:43:01 +02:00
|
|
|
for key in result.keys():
|
2018-11-06 23:43:11 +01:00
|
|
|
addr = ' , '.join(result[key])
|
|
|
|
text += "- %s : %s\n" % (key, addr)
|
2018-10-10 17:43:01 +02:00
|
|
|
else:
|
|
|
|
text = "%s has no contact addresses configured." % self.target
|
|
|
|
|
2018-11-06 23:43:11 +01:00
|
|
|
# if opt_arg is present and member of possible_vars but the key is empty change text line
|
|
|
|
if opt_arg in self.possible_vars:
|
|
|
|
text = "%s for %s are not defined." % (self.opt_arg, self.target)
|
|
|
|
|
2018-10-10 17:43:01 +02:00
|
|
|
return text
|