Skip to content

Instantly share code, notes, and snippets.

@shashi
Created July 15, 2009 05:48
Show Gist options
  • Save shashi/147481 to your computer and use it in GitHub Desktop.
Save shashi/147481 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
## MegaHAL XMPP bot version 0.1
##
## Copyright (C) 2009 Shashi Gowda <[email protected]>
##
## (Most part of the code in this file is derived from jabber-logger.py)
## This program is free software; you can redistribute it and/or modify
## it under the terms of the MIT Lisence
teachers=[ '[email protected]', '[email protected]', '[email protected]', '[email protected]' ]
users=['[email protected]', '[email protected]']
account={'jid':'[email protected]/use','password':'password'}
import warnings
warnings.simplefilter("ignore",DeprecationWarning)
#TODO: Fix this ^
import pyxmpp
import sys
import logging
import locale
import codecs
import mh_python as bot
bot.initbrain()
from pyxmpp.all import JID,Iq,Presence,Message,StreamError
from pyxmpp.jabber.client import JabberClient
def reply(str,usr):
user = usr.split('/')[0]
if not (user in users or user in teachers):
return 'go save the polar bears!'
if user in teachers:
bot.learn(str)
bot.cleanup()
return bot.doreply(str)
# Command Logger
class Client(JabberClient):
"""Simple bot (client) example. Uses `pyxmpp.jabber.client.JabberClient`
class as base. That class provides basic stream setup (including
authentication) and Service Discovery server. It also does server address
and port discovery based on the JID provided."""
def __init__(self, jid, password):
# if bare JID is provided add a resource -- it is required
if not jid.resource:
jid=JID(jid.node, jid.domain, "Logger")
# setup client with provided connection information
# and identity data
JabberClient.__init__(self, jid, password,
disco_name="megahald", disco_type="bot")
# register features to be announced via Service Discovery
self.disco_info.add_feature("jabber:iq:version")
def stream_state_changed(self,state,arg):
"""This one is called when the state of stream connecting the component
to a server changes. This will usually be used to let the user
know what is going on."""
#print "*** State changed: %s %r ***" % (state,arg)
def session_started(self):
"""This is called when the IM session is successfully started
(after all the neccessery negotiations, authentication and
authorizasion).
That is the best place to setup various handlers for the stream.
Do not forget about calling the session_started() method of the base
class!"""
JabberClient.session_started(self)
# set up handlers for supported <iq/> queries
self.stream.set_iq_get_handler("query","jabber:iq:version",self.get_version)
# set up handlers for <presence/> stanzas
self.stream.set_presence_handler("available",self.presence)
self.stream.set_presence_handler("subscribe",self.presence_control)
self.stream.set_presence_handler("subscribed",self.presence_control)
self.stream.set_presence_handler("unsubscribe",self.presence_control)
self.stream.set_presence_handler("unsubscribed",self.presence_control)
# set up handler for <message stanza>
self.stream.set_message_handler("normal",self.message)
def get_version(self,iq):
"""Handler for jabber:iq:version queries.
jabber:iq:version queries are not supported directly by PyXMPP, so the
XML node is accessed directly through the libxml2 API. This should be
used very carefully!"""
iq=iq.make_result_response()
q=iq.new_query("jabber:iq:version")
q.newTextChild(q.ns(),"name","Echo component")
q.newTextChild(q.ns(),"version","1.0")
self.stream.send(iq)
return True
def message(self,stanza):
"""Message handler for the component.
Echoes the message back if its type is not 'error' or
'headline', also sets own presence status to the message body. Please
note that all message types but 'error' will be passed to the handler
for 'normal' message unless some dedicated handler process them.
:returns: `True` to indicate, that the stanza should not be processed
any further."""
subject=stanza.get_subject()
body=stanza.get_body()
t=stanza.get_type()
#print u'Message from %s received.' % (unicode(stanza.get_from(),)),
if (body and t=="chat"):
body = reply(body,stanza.get_from().__str__())
if stanza.get_type()=="headline":
# 'headline' messages should never be replied to
return True
if subject:
subject=u"Re: "+subject
m=Message(
to_jid=stanza.get_from(),
from_jid=stanza.get_to(),
stanza_type=stanza.get_type(),
subject=subject,
body=body)
self.stream.send(m)
#if body:
# p=Presence(status=body)
# self.stream.send(p)
return True
def presence(self,stanza):
"""Handle 'available' (without 'type') and 'unavailable' <presence/>."""
msg=u"%s has become " % (stanza.get_from())
t=stanza.get_type()
if t=="unavailable":
msg+=u"unavailable"
else:
msg+=u"available"
show=stanza.get_show()
if show:
msg+=u"(%s)" % (show,)
status=stanza.get_status()
if status:
msg+=u": "+status
print msg
def presence_control(self,stanza):
"""Handle subscription control <presence/> stanzas -- acknowledge
them."""
t=stanza.get_type()
p=stanza.make_accept_response()
self.stream.send(p)
return True
def print_roster_item(self,item):
if item.name:
name=item.name
else:
name=u""
print (u'%s "%s" subscription=%s groups=%s'
% (unicode(item.jid), name, item.subscription,
u",".join(item.groups)) )
def roster_updated(self,item=None):
if not item:
print u"My roster:"
for item in self.roster.get_items():
self.print_roster_item(item)
return
print u"Roster item updated:"
self.print_roster_item(item)
def logIn():
c=Client(JID(account['jid']) ,account['password'])
try:
print u"connecting..."
c.connect()
# Component class provides basic "main loop" for the applitation
# Though, most applications would need to have their own loop and call
# component.stream.loop_iter() from it whenever an event on
# component.stream.fileno() occurs.
print u"looping..."
c.loop(1)
except KeyboardInterrupt:
print u"\nBye..."
bot.cleanup()
c.disconnect()
except:
bot.cleanup()
c.disconnect()
print 'Reconnecting...'
logIn()
# XMPP protocol is Unicode-based to properly display data received
# _must_ convert it to local encoding or UnicodeException may be raised
locale.setlocale(locale.LC_CTYPE,"")
encoding=locale.getlocale()[1]
if not encoding:
encoding="us-ascii"
sys.stdout=codecs.getwriter(encoding)(sys.stdout,errors="replace")
sys.stderr=codecs.getwriter(encoding)(sys.stderr,errors="replace")
# PyXMPP uses `logging` module for its debug output
# applications should set it up as needed
def authUser(user):
return user in users
print u"creating client..."
logIn()
print u"exiting..."
# vi: sts=4 et sw=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment