xmpp-chatbot/classes/functions.py
nico 825167b8aa
Squashed commit of the following:
commit 91cf4b74105f69f16a80825581a0d2d34d15d155
Merge: 30ceaea 90b42ed
Author: nico <nico.wellpott@uni-oldenburg.de>
Date:   Wed Oct 3 22:34:42 2018 +0200

    Merge remote-tracking branch 'origin/xep' into xep

commit 30ceaea56a77ed95deba30c4fe65e238ea5960ac
Author: nico <nico.wellpott@uni-oldenburg.de>
Date:   Wed Oct 3 22:34:33 2018 +0200

    Initial Version XEP query

    + added initial version of xep query class

    Init Implementation

    + added xep plugin to bot class

    * reworked validation function
    * updated .gitignore file
    + added xep plugin

commit 90b42edb9b8e92eba3bb67030d5f919b1e71d0bc
Author: nico <nico.wellpott@uni-oldenburg.de>
Date:   Wed Oct 3 22:34:21 2018 +0200

    * reworked validation function
    * updated .gitignore file
    + added xep plugin

commit 25c78807731417867840d6fe4abf598e64aded28
Author: nico <nico@magicbroccoli.de>
Date:   Wed Oct 3 10:54:02 2018 +0200

    Init Implementation

    + added xep plugin to bot class

commit fe711f44d40671d927e9b946fb66679b297272c8
Author: nico <nico@magicbroccoli.de>
Date:   Tue Oct 2 21:20:08 2018 +0200

    Initial Version XEP query

    + added initial version of xep query class
2018-10-03 23:24:36 +02:00

108 lines
3 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# XEP-0072: Server Version
class Version:
def __init__(self, version, msg, target):
self.version = version['software_version']['version']
self.os = version['software_version']['os']
self.name = version['software_version']['name']
self.nick = msg['mucnick']
self.message_type = msg['type']
self.target = target
def format_version(self):
if self.message_type == "groupchat":
text = "%s: %s is running %s version %s on %s" % (self.nick, self.target, self.name, self.version, self.os)
else:
text = "%s is running %s version %s on %s" % (self.target, self.name, self.version, self.os)
return text
# XEP-0012: Last Activity
class LastActivity:
""" query the server uptime of the specified domain, defined by XEP-0012 """
def __init__(self, last_activity, msg, target):
self.last_activity = last_activity
self.nick = msg['mucnick']
self.message_type = msg['type']
self.target = target
def format_values(self, granularity=4):
seconds = self.last_activity['last_activity']['seconds']
uptime = []
intervals = (
('years', 31536000), # 60 * 60 * 24 * 365
('weeks', 604800), # 60 * 60 * 24 * 7
('days', 86400), # 60 * 60 * 24
('hours', 3600), # 60 * 60
('minutes', 60),
('seconds', 1)
)
for name, count in intervals:
value = seconds // count
if value:
seconds -= value * count
if value == 1:
name = name.rstrip('s')
uptime.append("{} {}".format(value, name))
result = ' '.join(uptime[:granularity])
if self.message_type == "groupchat":
text = "%s: %s is running since %s" % (self.nick, self.target, result)
else:
text = "%s is running since %s" % (self.target, result)
return text
# XEP-0157: Contact Addresses for XMPP Services
class ContactInfo:
def __init__(self, contact, msg, target):
self.contact = contact
self.message = msg
self.target = target
def format_contact(self):
server_info = []
sep = ' , '
possible_vars = ['abuse-addresses',
'admin-addresses',
'feedback-addresses',
'sales-addresses',
'security-addresses',
'support-addresses']
for field in self.contact['disco_info']['form']:
var = field['var']
if var in possible_vars:
field_value = field.get_value(convert=False)
value = sep.join(field_value) if isinstance(field_value, list) else field_value
server_info.append(' - %s: %s' % (var, value))
if server_info:
text = "contact addresses for %s are" % self.target
for count in range(server_info.__len__()):
text += "\n" + server_info[count]
else:
text = "%s has no contact addresses configured." % self.target
return text
# class handeling XMPPError exeptions
class HandleError:
def __init__(self, error, msg, key, target="target missing"):
self.error = error
self.message = msg
self.key = key
self.target = target
def build_report(self):
condition = self.error.condition
keyword = self.key[1:]
text = "There was an error requesting " + self.target + '\'s ' + keyword + " : " + condition
return text