You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

103 lines
3.3 KiB

import logging
import argparse
from sleekxmpp import ClientXMPP
from sleekxmpp.exceptions import IqError, IqTimeout
import os
import urllib
import datetime
import ssl
from PIL import Image
parser = argparse.ArgumentParser()
parser.add_argument("-j", "--jid", help="jabber identifier", type=str, required=True)
parser.add_argument("-p", "--password", help="password", type=str, required=True)
parser.add_argument("-m", "--muc", help="destination muc", type=str, required=True)
parser.add_argument("-n", "--nick", help="nickname of the bot", default="archivist", type=str)
parser.add_argument("-o", "--output", help="output folder", default="files/", type=str)
args = parser.parse_args()
class ArchivistBot(ClientXMPP):
def __init__(self, jid, password, room, nick, output):
ClientXMPP.__init__(self, jid, password)
self.datadir = output
self.room = room
self.nick = nick
self.add_event_handler("session_start", self.session_start)
self.add_event_handler("message", self.archive_msg) # by using 'message' instead of 'groupchat_message' every message received can be archived (also personal msgs)
# self.add_event_handler("groupchat_message", self.archive_msg2)
self.register_plugin('xep_0045')
self.register_plugin('xep_0030')
self.register_plugin('xep_0084')
self.register_plugin('xep_0096') # Transfer files..
self.register_plugin('xep_0066') # Transfer files..
def session_start(self, event):
self.get_roster()
self.send_presence()
self.plugin['xep_0045'].joinMUC(self.room, self.nick)
# XEP-0084 User Avatar
# Requires SleekXMPP 81b7b2c1908e0f6a5435ce67745b5f4dafb59816
with open('contact.png', 'rb') as avatar_file:
avatar = avatar_file.read()
avatar_id = self['xep_0084'].generate_id(avatar)
info = {
'id': avatar_id,
'type': 'image/jpeg',
'bytes': len(avatar)
}
self['xep_0084'].publish_avatar(avatar)
self['xep_0084'].publish_avatar_metadata(items=[info])
# XEP-0153: vCard-Based Avatars
# Not working ATM
self['xep_0153'].set_avatar(avatar=avatar, mtype='image/png')
def archive_msg(self, msg):
# Always check that a message is not from yourself, otherwise you will create an infinite loop responding to your own messages.
if 'mucnick' in msg and msg['mucnick'] == self.nick:
return
if msg['oob']['url']:
logging.getLogger().debug("received OOB from %s with %s" % (self.nick, msg['oob']['url']))
filename = os.path.basename(msg['oob']['url'])
targetDir = self.datadir
if not os.path.exists(targetDir):
os.mkdir( targetDir, 0755 )
targetFile = os.path.join(targetDir, filename)
#needed to disable certificate validation:
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
# save image to file
urllib.urlretrieve(msg['oob']['url'], targetFile, context=ctx)
logging.getLogger().debug("saved to %s" % targetFile)
# if msg['type'] in ('chat', 'normal'):
# msg.reply("Thanks for sending\n%(body)s" % msg).send()
if __name__ == '__main__':
# Ideally use optparse or argparse to get JID,
# password, and log level.
logging.basicConfig(level=logging.DEBUG, format='%(levelname)-8s %(message)s')
client = ArchivistBot(args.jid, args.password, args.muc, args.nick, args.output)
if client.connect():
client.process(block=True)
else:
logging.getLogger().error("Can't connect.")