This repository has been archived by the owner on Dec 14, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_xmpp.py
86 lines (68 loc) · 3 KB
/
output_xmpp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import logging
from uuid import uuid4
from flexget.plugin import *
__version__ = (0, 1)
log = logging.getLogger('xmpp')
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)
register_plugin(OutputXMPP, 'xmpp')