mirror of
https://github.com/balkian/XMPP-console-client.git
synced 2024-12-03 13:32:27 +00:00
it's alivegit add Sender.py Web40Agent.py
This commit is contained in:
commit
12e75ce64b
151
ConsoleClient.py
Normal file
151
ConsoleClient.py
Normal file
@ -0,0 +1,151 @@
|
||||
'''
|
||||
Created on Aug 26, 2011
|
||||
|
||||
@author: balkian
|
||||
'''
|
||||
import xmpp
|
||||
import time
|
||||
import logging
|
||||
import threading
|
||||
import sys
|
||||
import traceback
|
||||
import cmd
|
||||
|
||||
x = logging.getLogger("consoleClient")
|
||||
x.setLevel(logging.DEBUG)
|
||||
h = logging.StreamHandler()
|
||||
f = logging.Formatter("%(levelname)s %(asctime)s %(funcName)s %(lineno)d %(message)s")
|
||||
x.addHandler(h)
|
||||
|
||||
class jabberProcess(threading.Thread):
|
||||
|
||||
def __init__(self, socket):
|
||||
self.jabber = socket
|
||||
#self._alive = True
|
||||
self._forceKill = threading.Event()
|
||||
self._forceKill.clear()
|
||||
threading.Thread.__init__(self)
|
||||
self.setDaemon(False)
|
||||
|
||||
def _kill(self):
|
||||
try:
|
||||
self._forceKill.set()
|
||||
except:
|
||||
#Agent is already dead
|
||||
pass
|
||||
|
||||
def forceKill(self):
|
||||
return self._forceKill.isSet()
|
||||
|
||||
def run(self):
|
||||
"""
|
||||
periodic jabber update
|
||||
"""
|
||||
while not self.forceKill():
|
||||
try:
|
||||
err = self.jabber.Process(0.4)
|
||||
except Exception, e:
|
||||
_exception = sys.exc_info()
|
||||
if _exception[0]:
|
||||
x.debug( '\n'+''.join(traceback.format_exception(_exception[0], _exception[1], _exception[2])).rstrip(),"err")
|
||||
x.debug("Exception in jabber process: "+ str(e))
|
||||
x.debug("Jabber connection failed (dying)")
|
||||
self._kill()
|
||||
err = None
|
||||
|
||||
if err == None or err == 0: # None or zero the integer, socket closed
|
||||
x.debug("Client disconnected (dying)")
|
||||
self._kill()
|
||||
|
||||
class ConsoleClient(cmd.Cmd):
|
||||
def __init__(self):
|
||||
cmd.Cmd.__init__(self) # initialize the base class
|
||||
self.prompt = "Command> " # customize the prompt
|
||||
self.intro = 'Console client with basic functionalities'
|
||||
|
||||
def _add_local(self, jid):
|
||||
if not jid.find('@')+1:
|
||||
return "%s@127.0.0.1"%jid
|
||||
return jid
|
||||
|
||||
|
||||
def error_handler(self, type, value, traceback):
|
||||
print "Error:", type
|
||||
print "Value:", value
|
||||
|
||||
def messageHandler(self, conn, mess, raiseFlag=True):
|
||||
x.info('Got a message: %s' % mess)
|
||||
|
||||
def presenceHandler(self, conn, mess, raiseFlag=True):
|
||||
x.info('Got a presence: %s' % mess)
|
||||
x.info('Type: %s'%str(mess.getType()))
|
||||
if str(mess.getType()) == 'subscribe':
|
||||
x.info('Subscribe request')
|
||||
self.conn.sendPresence(mess.getFrom(), 'subscribed')
|
||||
|
||||
def iqHandler(self, conn, mess, raiseFlag=True):
|
||||
x.info('Got an iq: %s' % mess)
|
||||
|
||||
def auth(self,tjid,passwd,autoregister=False):
|
||||
jid=xmpp.JID(tjid)
|
||||
user,server=jid.getNode(),jid.getDomain()
|
||||
x.info('Using: %s @ %s : %s' % (user,server,passwd))
|
||||
self.conn=xmpp.Client(jid.getDomain(),5222,debug=[])
|
||||
tries=5
|
||||
while not self.conn.connect(use_srv=None) and tries >0:
|
||||
time.sleep(0.005)
|
||||
tries -=1
|
||||
if tries <=0 :
|
||||
x.info("There is no XMPP server at " + server + " . Agent dying...")
|
||||
sys.exit(2)
|
||||
|
||||
if (self.conn.auth(user,passwd) == None):
|
||||
|
||||
x.error( "First auth attempt failed. Trying to register")
|
||||
|
||||
if (autoregister):
|
||||
self.reg(jid,passwd)
|
||||
self.auth(tjid,passwd)
|
||||
return
|
||||
print "%s got authed"%(user)
|
||||
|
||||
self.conn.RegisterHandler('message',self.messageHandler)
|
||||
self.conn.RegisterHandler('presence',self.presenceHandler)
|
||||
self.conn.RegisterHandler('iq',self.iqHandler)
|
||||
self.conn.sendInitPresence(False)
|
||||
jabber_process = jabberProcess(self.conn)
|
||||
jabber_process.start()
|
||||
|
||||
|
||||
|
||||
def reg(self,jid,passwd):
|
||||
user,server=jid.getNode(),jid.getDomain()
|
||||
conn=xmpp.Client(server,5222,debug=[])
|
||||
conn.connect()
|
||||
x.info('user %s server %s' % (user,server))
|
||||
# xmpp.features.getRegInfo(conn,jid.getDomain())
|
||||
xmpp.features.register(conn,jid.getDomain(),\
|
||||
{'username':user, 'password':str(passwd), 'name':user})
|
||||
|
||||
def do_auth(self,line):
|
||||
jid=raw_input('Enter jid: ')
|
||||
passwd=raw_input('Enter password:')
|
||||
reg=raw_input('Autoregister? [Y/n]: ')
|
||||
autoregister=(reg.lower() in ['true','yes','y'])
|
||||
jid = self._add_local(jid)
|
||||
self.auth(jid,passwd,autoregister)
|
||||
|
||||
def do_send(self,line):
|
||||
jid=raw_input('To: ')
|
||||
body=raw_input('Body:')
|
||||
jid=self._add_local(jid)
|
||||
mess=xmpp.Message(jid, body)
|
||||
self.conn.send(mess)
|
||||
|
||||
def do_raw(self,line):
|
||||
self.conn.send(line)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
cl = ConsoleClient()
|
||||
cl.cmdloop()
|
87
Sender.py
Normal file
87
Sender.py
Normal file
@ -0,0 +1,87 @@
|
||||
#TODO Improve this class
|
||||
'''
|
||||
Basic agent for Web40
|
||||
'''
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
sys.path.append('..')
|
||||
|
||||
import spade
|
||||
from spade import Behaviour
|
||||
import xmpp
|
||||
|
||||
host = "127.0.0.1"
|
||||
|
||||
|
||||
class Sender(spade.Agent.Agent):
|
||||
|
||||
class SendMsgBehav(spade.Behaviour.OneShotBehaviour):
|
||||
"""
|
||||
#TODO
|
||||
add description
|
||||
"""
|
||||
|
||||
def _process(self):
|
||||
msg = xmpp.Message(to="a@127.0.0.1/spade")
|
||||
msg.setBody("testSendMsg")
|
||||
print ">Sending message in 3 . . ."
|
||||
time.sleep(1)
|
||||
print ">Sending message in 2 . . ."
|
||||
time.sleep(1)
|
||||
print ">Sending message in 1 . . ."
|
||||
time.sleep(1)
|
||||
|
||||
self.myAgent.send(msg)
|
||||
|
||||
print "I sent a message"
|
||||
#print str(msg)
|
||||
|
||||
class RecvMsgBehav(spade.Behaviour.EventBehaviour):
|
||||
"""
|
||||
This EventBehaviour gets launched when a message that matches its template arrives at the agent
|
||||
"""
|
||||
|
||||
def _process(self):
|
||||
print "This behaviour has been triggered by a message!"
|
||||
msg =self._receive(True)
|
||||
print msg
|
||||
print "%%Type:",type(msg)
|
||||
print "#########Bye"
|
||||
rep = msg.buildReply()
|
||||
rep.setBody(msg.getBody())
|
||||
self.myAgent.send(rep)
|
||||
print "Message sent"
|
||||
|
||||
|
||||
def _setup(self):
|
||||
# Create the template for the EventBehaviour: a message from myself
|
||||
msg = xmpp.Message(frm=".*@127.0.0.1/spade")
|
||||
# template.setSender(spade.AID.aid("prueba2@"+host,["xmpp://prueba2@"+host+"/spade"]))
|
||||
t = spade.Behaviour.MessageTemplate(msg,regex=True)
|
||||
print "Template:",t
|
||||
|
||||
# Add the EventBehaviour with its template
|
||||
self.addBehaviour(self.RecvMsgBehav(),t)
|
||||
|
||||
# Add the sender behaviour
|
||||
self.addBehaviour(self.SendMsgBehav())
|
||||
|
||||
|
||||
a = Sender("a@"+host,"secret")
|
||||
|
||||
time.sleep(1)
|
||||
a.setDebugToScreen()
|
||||
a.start()
|
||||
|
||||
alive = True
|
||||
import time
|
||||
while alive:
|
||||
try:
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
alive=False
|
||||
a.stop()
|
||||
sys.exit(0)
|
84
Web40Agent.py
Normal file
84
Web40Agent.py
Normal file
@ -0,0 +1,84 @@
|
||||
#TODO Improve this class
|
||||
'''
|
||||
Basic agent for Web40
|
||||
'''
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
sys.path.append('..')
|
||||
|
||||
import spade
|
||||
from spade import Behaviour
|
||||
from xmpp import Message,Protocol
|
||||
|
||||
host = "127.0.0.1"
|
||||
tags=["presence","message","Iq"]
|
||||
|
||||
class ConvBehav(spade.Behaviour.Behaviour):
|
||||
|
||||
def __init__(self):
|
||||
spade.Behaviour.Behaviour.__init__(self)
|
||||
self._state = 0
|
||||
|
||||
|
||||
def _process(self):
|
||||
msg = self._receive(True)
|
||||
print "--- Message Received: %s" %msg
|
||||
|
||||
|
||||
|
||||
class MainBehav(spade.Behaviour.Behaviour):
|
||||
"""
|
||||
#TODO
|
||||
add description
|
||||
"""
|
||||
|
||||
def _process(self):
|
||||
msg = self._receive(True)
|
||||
print "+Message received %s" % msg
|
||||
sndr = msg.getFrom().getStripped()
|
||||
if(not self.myAgent._conversations.has_key(sndr)):
|
||||
print "***Adding handler"
|
||||
templ=Behaviour.BehaviourTemplate(None)
|
||||
for i in tags:
|
||||
mess = Protocol(i)
|
||||
mess.setFrom(sndr)
|
||||
tmp = Behaviour.MessageTemplate(mess,regex=True)
|
||||
templ= templ or tmp
|
||||
b = ConvBehav()
|
||||
self.myAgent._conversations[sndr]=b
|
||||
self.myAgent.addBehaviour(b,templ)
|
||||
print "***Added "
|
||||
|
||||
|
||||
#print str(msg)
|
||||
|
||||
|
||||
class Web40Agent(spade.Agent.Agent):
|
||||
|
||||
def __init__(self, agentjid, password, port=5222, debug=[], p2p=False):
|
||||
spade.Agent.Agent.__init__(self, agentjid, password, port=port, debug=debug, p2p=p2p)
|
||||
self._conversations = {}
|
||||
def _setup(self):
|
||||
mb = MainBehav()
|
||||
self.setDefaultBehaviour(mb)
|
||||
print "Agent Web40 started"
|
||||
|
||||
|
||||
a = Web40Agent("a@"+host,"secret")
|
||||
|
||||
time.sleep(1)
|
||||
#a.setDebugToScreen()
|
||||
a.start()
|
||||
|
||||
alive = True
|
||||
import time
|
||||
while alive:
|
||||
try:
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
alive=False
|
||||
a.stop()
|
||||
sys.exit(0)
|
Loading…
Reference in New Issue
Block a user