minchat.py :  » Network » Twisted » Twisted-1.0.3 » Twisted-1.0.3 » doc » examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Twisted 
Twisted » Twisted 1.0.3 » Twisted 1.0.3 » doc » examples » minchat.py
# Twisted, the Framework of Your Internet
# Copyright (C) 2001 Matthew W. Lefkowitz
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""
A very simple twisted.im-based logbot.
"""

from twisted.im import basechat,baseaccount

# A list of account objects. We might as well create them at runtime, this is
# supposed to be a Minimalist Implementation, after all.
from twisted.im import ircsupport
accounts = [
    ircsupport.IRCAccount("IRC", 1,
        "Tooty",            # nickname
        "",                 # passwd
        "irc.freenode.net", # irc server
        6667,               # port
        "#twisted",         # comma-seperated list of channels
    )
]


class AccountManager (baseaccount.AccountManager):
    """This class is a minimal implementation of the Acccount Manager.

    Most implementations will show some screen that lets the user add and
    remove accounts, but we're not quite that sophisticated.
    """

    def __init__(self):

        self.chatui = MinChat()

        if len(accounts) == 0:
            print "You have defined no accounts."
        for acct in accounts:
            acct.logOn(self.chatui)


class MinConversation(basechat.Conversation):
    """This class is a minimal implementation of the abstract Conversation class.

    This is all you need to override to receive one-on-one messages.
    """
    def show(self):
        """If you don't have a GUI, this is a no-op.
        """
        pass
    
    def hide(self):
        """If you don't have a GUI, this is a no-op.
        """
        pass
    
    def showMessage(self, text, metadata=None):
        print "<%s> %s" % (self.person.name, text)
        
    def contactChangedNick(self, person, newnick):
        basechat.Conversation.contactChangedNick(self, person, newnick)
        print "-!- %s is now known as %s" % (person.name, newnick)


class MinGroupConversation(basechat.GroupConversation):
    """This class is a minimal implementation of the abstract GroupConversation class.

    This is all you need to override to listen in on a group conversaion.
    """
    def show(self):
        """If you don't have a GUI, this is a no-op.
        """
        pass

    def hide(self):
        """If you don't have a GUI, this is a no-op.
        """
        pass

    def showGroupMessage(self, sender, text, metadata=None):
        print "<%s/%s> %s" % (sender, self.group.name, text)

    def setTopic(self, topic, author):
        print "-!- %s set the topic of %s to: %s" % (author, 
            self.group.name, topic)

    def memberJoined(self, member):
        basechat.GroupConversation.memberJoined(self, member)
        print "-!- %s joined %s" % (member, self.group.name)

    def memberChangedNick(self, oldnick, newnick):
        basechat.GroupConversation.memberChangedNick(self, oldnick, newnick)
        print "-!- %s is now known as %s in %s" % (oldnick, newnick,
            self.group.name)

    def memberLeft(self, member):
        basechat.GroupConversation.memberLeft(self, member)
        print "-!- %s left %s" % (member, self.group.name)
    
class MinChat(basechat.ChatUI):
    """This class is a minimal implementation of the abstract ChatUI class.

    There are only two methods that need overriding - and of those two, 
    the only change that needs to be made is the default value of the Class
    parameter.
    """

    def getGroupConversation(self, group, Class=MinGroupConversation, 
        stayHidden=0):

        return basechat.ChatUI.getGroupConversation(self, group, Class, 
            stayHidden)

    def getConversation(self, person, Class=MinConversation, 
        stayHidden=0):

        return basechat.ChatUI.getConversation(self, person, Class, stayHidden)

if __name__ == "__main__":
    from twisted.internet import reactor

    AccountManager()

    reactor.run()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.