[Twisted-Python] Beginning of jabber client protocol
Neil Blakey-Milner
nbm at mithrandr.moria.org
Tue Feb 25 16:03:33 EST 2003
On Tue 2003-02-25 (21:30), Neil Blakey-Milner wrote:
> That's about it, but I'm unlikely to have much time to spend on this for
> some time, so I'm looking for comments on how to do it better for when I
> do get time, and/or support for other functionality.
I've hacked together the attached Tk client to help anyone who wishes to
play with this code.
Neil
--
Neil Blakey-Milner
nbm at mithrandr.moria.org
-------------- next part --------------
#!/usr/local/bin/python
#
# Copyright (c) 2002 Neil Blakey-Milner
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
import sys
import Tkinter
from twisted.internet import reactor, tksupport
from twisted.internet.protocol import ClientFactory
from twisted.python import log, logfile
import tpjabber
class TkJabber(tpjabber.Jabber):
def jabberGotAuthenticationTypes(self, types, document):
line = "Available authentication types: %s" % (", ".join(types))
self.factory.screenObj.addLine(line)
def jabberLoggedIn(self, document):
line = "Successfully logged in as %s on %s" % (self.factory.jid,
self.factory.host)
self.factory.screenObj.addLine(line)
self.factory.screenObj.status("Logged in as %s on %s" %
(self.factory.jid, self.factory.host))
def jabberGotMessage(self, sender, recipient, body, document):
line = "Message from %s: %s" % (sender, body)
self.factory.screenObj.addLine(line)
#self.jabberSendMessage(sender, "I got your message: %s" % (body))
def jabberFailedLogIn(self, error, code, reason, document):
line = "Login failed (code %s), reason: %s" % (code, reason)
self.factory.screenObj.addLine(line)
def jabberGotRosterStart(self, document):
line = "Roster:"
self.factory.screenObj.addLine(line)
line = "--------------------"
self.factory.screenObj.addLine(line)
def jabberGotRosterEntry(self, jid, ask, subscription, document):
line = "%s: " % (jid)
if subscription:
line = line + "%s" % (subscription)
if ask:
line = line + "%s-request" % (ask)
self.factory.screenObj.addLine(line)
def jabberGotRosterEnd(self, document):
line = "--------------------"
self.factory.screenObj.addLine(line)
def jabberSubscribeRequest(self, sender, document):
line = "Subscribe request from %s" % (sender)
self.factory.screenObj.addLine(line)
def jabberSendMessage(self, recipient, body):
line = "-> *%s* %s" % (recipient, body)
self.factory.screenObj.addLine(line)
class TkJabberClientFactory(tpjabber.JabberClientFactory):
protocol = TkJabber
def __init__(self, window, host, user, password):
tpjabber.JabberClientFactory.__init__(self, host, user, password)
self.screenObj = window
window.factory = self
class win(Tkinter.Tk):
# some constants to calculate window sizes
CHARHEIGHT=15
CHARWIDTH=8
MARGIN=1
# color codes
color=['#000000', '#000080', '#008000', '#008080', '#800000', '#800080',
# black blue green cyan red purple
'#808000', '#CCCCCC', '#808080', '#0000FF', '#00FF00', '#00FFFF',
# brown lt gray gray lt blue lt green lt cyan
'#FF0000', '#FF00FF', '#FFFF00', '#FFFFFF']
# lt red pink yellow white
def __init__(self, title='', width=80, height=24):
Tkinter.Tk.__init__(self)
self.textarea = Tkinter.Text(self, width=width, height=height,
state='disabled', wrap='word')
self.sb = Tkinter.Scrollbar(self, command=self.textarea.yview)
self.textarea.configure(yscrollcommand=self.sb.set)
self.cmdlinevar = Tkinter.StringVar()
self.cmdlinevar.set('')
self.commandline = Tkinter.Entry(self, width=80, textvariable=self.cmdlinevar)
self.commandline.bind('<Return>', self.gotLineInput)
self.statusbar = Tkinter.Label(self, bd=1, relief='sunken')
self.textarea.grid(row=0, column=0, sticky='nsew')
self.sb.grid(row=0, column=1, sticky='ns')
self.statusbar.grid(row=2, columnspan=2, sticky='ew')
self.commandline.grid(row=1, columnspan=2, sticky='ew')
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.status("Ready...")
self.title(title)
def close(self):
self.withdraw()
self.update_idletasks()
self.destroy()
def addLine(self, line):
self.textarea.config(state='normal')
self.textarea.insert('end', "\n%s" % (line))
self.textarea.config(state='disabled')
self.textarea.see('end')
def status(self, msg):
self.statusbar.config(text=msg)
self.statusbar.update_idletasks()
def gotLineInput(self, text):
c = self.cmdlinevar.get()
self.commandline.delete(0, 'end')
split = c.split()
if len(split):
func = getattr(self, "input_%s" % (split[0]), self.input_UNHANDLED)
func(split[0], " ".join(split[1:]))
def input_UNHANDLED(self, command, text):
self.addLine("Unknown command: %s" % (command))
def input_echo(self, command, text):
self.addLine(text)
def input_message(self, command, text):
split = text.split()
self.factory.protocolobj.jabberSendMessage(split[0], " ".join(split[1:]))
#self.addLine(str(dir(self.factory)))
def input_exit(self, command, text):
self.addLine("Shutting down...")
#self.close()
reactor.stop()
def main():
host = "jabber.moria.org"
user = "test"
password = "test"
#log.startLogging(logfile.LogFile("jabclient.log", "."))
log.startLogging(sys.stdout)
screen = win("tpjabber/Tk")
screen.protocol('WM_DELETE_WINDOW', reactor.stop)
tksupport.install(screen)
factory = TkJabberClientFactory(screen, host, user, password)
reactor.connectTCP(host, 5222, factory)
reactor.run()
if __name__ == "__main__":
main()
More information about the Twisted-Python
mailing list