Skip to content

Instantly share code, notes, and snippets.

@lviana
Created March 27, 2015 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lviana/a729adfdf8dc94c2e007 to your computer and use it in GitHub Desktop.
Save lviana/a729adfdf8dc94c2e007 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import xmpp
from bottle import get, post, abort, request, run
class notifier(object):
def __init__(self, server='include.io'):
self.client = xmpp.Client(server, debug='')
self.connect()
self.setPresence('Idiota')
def connect(self, username='idiota', passwd='ler0ler0'):
self.client.connect()
self.client.auth(username, passwd, 'notifier')
self.client.getRoster()
def setPresence(self, nickname=''):
self.client.sendInitPresence()
if nickname:
iqVcard = xmpp.Iq(typ='set')
vcard = iqVcard.addChild(name='vCard', namespace=xmpp.NS_VCARD)
vcard.addChild(name='NICKNAME', payload=[nickname])
self.client.send(iqVcard)
def setStatus(self, message):
status = xmpp.Presence(status=message, priority=5, show='chat')
self.client.send(status)
def sendMessage(self, msg, to):
message = xmpp.Message(to, msg)
message.setAttr('type', 'chat')
self.client.send(message)
def getContacts(self):
contacts = self.client.Roster.getItems()
return contacts
def addContact(self, contact):
# Fix-me!
self.client.Roster.Subscribe(contact)
self.client.Roster.Authorize(contact)
self.client.Roster.setItem(contact)
self.client.getRoster()
def disconnect(self):
self.client.disconnect()
event = notifier()
event.setStatus('Notification service online!')
@post('/message/:recipient')
def postMessage(recipient):
try:
entity = request.json
message = str(entity['message'])
except KeyError:
abort(400, 'Malformed json object')
try:
event.sendMessage(message, recipient)
except IOError:
event.connect()
event.sendMessage(message, recipient)
return 'Message sent!'
@post('/contacts/:newcontact')
def postContact(newcontact):
# Fix-me!
return newcontact
output = event.addContact()
return output
@get('/contacts.json')
def getJsonContactList():
output = {'contacts': event.getContacts()}
return output
@get('/contacts')
def getRawContactList():
output = ''
for contact in event.getContacts():
output = '%s%s\n' % (output, contact)
return output.strip()
run(host='127.0.0.1', port=8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment