class XmpppyTransport(Transport): """ Implements the XMPP transport (xmpppy). """ def __init__(self): settings = core.server.server.settings self.domain = settings.get('xmpp_domain') self.host = settings.get('xmpp_host') self.port = settings.get('xmpp_port') self.user = settings.get('xmpp_user') self.password = settings.get('xmpp_password') self.presence = settings.get('xmpp_presence') self.connected = False @classmethod def isIdentifiedBy(cls, type): return type == 'xmpp' def connect(self): """ Connects to an XMPP server. """ self.client = Client(self.domain) if not self.client.connect(server=(self.host, self.port)): raise IOError('Cannot connect to server.') if not self.client.auth(self.user, self.password, self.presence): raise IOError('Cannot auth with server.') self.client.RegisterHandler('message', message_handler) self.client.sendInitPresence() self.connected = True while True: self.client.Process(1) def disconnect(self): """ Disconnects from the current XMPP server. """ if connected: self.client.sendPresence(typ='unavailable') self.client.disconnect() self.client = None self.connected = False def send(self, destination, action): self.client.send(Message('tim@localhost', str(action)))
def send_psto(jid, password, message): jid = JID(jid) conn = Client(jid.getDomain(), debug=[]) if not conn.connect(): logger.error("not connect to jabber") if not conn.auth(jid.getNode(), password, "Lastfm2Jabber"): logger.error("error auth to jabber") try: conn.send(Message(to="*****@*****.**", body="* bot, /mu/ // %s" % message)) except: logger.error("error sending message")
class JabberJob: def __init__(self, login, password, server): self.client = Client(server, debug=[]) self.client.connect() if not self.client.auth(login, password): self.client.disconnect() raise JabberAuthException() def send_message(self, to, message): self.client.send(protocol.Message(to, message)) def disconnect(self): self.client.disconnect()
def send(self, recipients, message): """Send message to recipients via xmpp.""" jid = JID(self.user) if self.server: server = self.server else: server = jid.getDomain() cl = Client(server, port=self.port, debug=[]) if not cl.connect(): raise IOError("Couldn't connect to xmpp server %s" % server) if not cl.auth(jid.getNode(), self.password, resource=self.resource): cl.Connection.disconnect() raise IOError("Xmpp auth erro using %s to %s", jid, server) for recip in recipients: cl.send(Message(recip[2], message))
def test(): client = Client('localhost') def message_handler(conn, mess_node): client.send(Message('tim@localhost', 'reponse!')) if not client.connect(server=('127.0.0.1', 5222)): raise IOError('Can not connect to server.') if not client.auth('abitbol', 'abitbol', 'abitbol'): raise IOError('Can not auth with server.') client.RegisterHandler('message', message_handler) client.sendInitPresence() client.send(Message('tim@localhost', 'Test message')) while 1: client.Process(1)
#!/usr/bin/env python import os from xmpp import Client,Iq,Node import sys username = "******" password = "******" domain = "gmail.com" import warnings warnings.filterwarnings("ignore") from xmpp import * cl=Client(server='gmail.com',debug=[]) if not cl.connect(server=('talk.google.com',5222)): raise IOError('Can not connect to server.') if not cl.auth(username, password, domain): raise IOError('Can not auth with server.') cl.send(Iq('set','google:shared-status', payload=[ Node('show',payload=[sys.argv[1]]), Node('status',payload=[sys.argv[2] if len(sys.argv)>2 else ""]) ])) cl.disconnect()
class OutputXMPP: rooms = [] def validator(self): from flexget import validator config = validator.factory('dict') config.accept('text', key='jid', required=True) config.accept('text', key='password', required=True) config.accept('text', key='to', required=True) config.accept('text', key='nickname') config.accept('text', key='connection_host') config.accept('integer', key='connection_port') config.accept('text', key='message_format') config.accept('text', key='message_type') return config def get_config(self, feed): config = feed.config['xmpp'] config.setdefault('message_format', '{{title}} has started downloading') config.setdefault('message_type', 'headline') config.setdefault('nickname', 'FlexGet') return config def on_process_start(self, feed): try: from xmpp import Client, Message, JID, Presence except ImportError: raise PluginError("output_xmpp requires xmppy, either `pip install xmpppy` or `apt-get install python-xmpp`") config = self.get_config(feed) debug = [] self.jid = JID(config.get('jid')) self.client = Client(self.jid, debug=debug) if self.client.connect() is None or self.client.auth(self.jid.getNode(), config['password'], uuid4()): self.error('Unable to connect to XMPP, disabling plugin') config['enabled'] = None return self.client.SendInitPresence() log.debug('Connected to XMPP server on JID %s' % self.jid.getNode()) def on_process_end(self, feed): if hasattr(self, 'client') and self.client.isConnected(): self.client.disconnect() self.client = None def on_feed_output(self, feed): from xmpp import Client, Message, JID, Presence config = self.get_config(feed) if config['enabled'] is None or feed.manager.options.learn: log.debug('XMPP plugin disabled or in learning mode, skipping.') return for entry in feed.accepted: body = entry.render(config.get('message_format')) if feed.manager.options.test: log.info("XMPP message: %s", body) continue msg = Message(body=body) for dest in [x.strip() for x in config['to'].split(',')]: if dest[0] == '@': dest = dest[1:] if not dest in self.rooms: self.client.send(Presence("%s/%s" % (dest, config['nickname']))) msg.setAttr('to', dest) msg.setType('groupchat') else: msg.setAttr('to', dest) msg.setType(config['message_type']) self.client.send(msg)