hackbot/common.py

115 lines
3.2 KiB
Python
Raw Normal View History

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# file: common.py
# date: 24.07.2020
# desc: common functions related to hackbot.py
import slixmpp
def get_type_from_stanza(stanza):
'''
Returns the type of a stanza.
param 1: stanza object
returns: string or false
'''
if isinstance(stanza, slixmpp.Message):
return stanza.get_type()
return False
def get_body_from_stanza(stanza):
'''
Extracts the body from the given stanza.
returns: string or false
'''
message_type = get_type_from_stanza(stanza)
if message_type is not False:
if message_type == 'groupchat':
return stanza['body']
elif message_type in ('chat', 'normal', 'error', 'headline'):
print('\tTyp not supported yet: {}'.format(message_type))
return False
return False
def get_command_from_body(stanza):
'''
Checks if the given stanzas body starts with a command. Returns command
or false.
param 1: stanza object
returns: string or false
'''
body = get_body_from_stanza(stanza)
if body is not False:
if body.lstrip().startswith('!'):
word = body.split()[0]
if word[1:].isalpha():
return word[1:].lower()
return False
def get_arguments_from_body(stanza):
'''
Grabs all from body behind the leading word.
param 1: stanza object
returns: list or false
'''
body = get_body_from_stanza(stanza)
if body is not False:
behind = body.split()[1:]
if len(behind) != 0:
return behind
return False
def get_nick_from_stanza(stanza):
'''
'''
if isinstance(stanza, slixmpp.Message):
message_type = stanza.get_type()
if message_type == 'groupchat':
return stanza.get_mucnick()
elif isinstance(stanza, slixmpp.Presence):
jid = stanza.getFrom()
return jid.resource
else: print('Unhandled message: {}'.format(str(stanza)))
# Elements and functions at a message object:
# print('\nfrom: {}'.format(msg.get_from()))
# print('bare: {}'.format(msg.get_from().bare))
# print('node: {}'.format(msg.get_from().node))
# print('domain: {}'.format(msg.get_from().domain))
# print('resource: {}'.format(msg.get_from().resource))
# print('lang: {}'.format(msg.get_lang()))
# print('muc nick: {}'.format(msg.get_mucnick()))
# print('muc room: {}'.format(msg.get_mucroom()))
# print('parent thread: {}'.format(msg.get_parent_thread()))
# print('payload: {}'.format(msg.get_payload()))
# print('values: {}'.format(msg.get_stanza_values()))
# print('to: {}'.format(msg.get_to()))
# print('type: {}'.format(msg.get_type()))
# print('\npayload:')
# for i in msg.get_payload():
# print('keys: {}'.format(i.keys()))
# print('items: {}'.format(i.items()))
# print('tag: {}'.format(i.tag))
# print('text: {}'.format(i.text))
# print('\nvalues:')
# for i in msg.get_stanza_values():
# print('{}: {}'.format(i, msg[i]))
# print('\n:')
#
# mlang = msg['lang']
# mnick = msg['mucnick']
# mbody = msg['body']
# mroom = msg['mucroom']
# mfrom = msg['from']
# mtype = msg['type']
# mid = msg['id']
# mto = msg['to']