Update website and e-mail info
[jabberbot/examples.git] / examples / broadcast.py
blob9767b5c235be834fb5a99f659679b636d4226c7f
1 #!/usr/bin/python
3 # JabberBot: A simple jabber/xmpp bot framework
4 # Copyright (c) 2007 Thomas Perl <thp.io/about>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # This is an example JabberBot that serves as broadcasting server.
21 # Users can subscribe/unsubscribe to messages and send messages
22 # by using "broadcast". It also shows how to send message from
23 # outside the main loop, so you can inject messages into the bot
24 # from other threads or processes, too.
27 from jabberbot import JabberBot, botcmd
29 import threading
30 import time
32 # Fill in the JID + Password of your JabberBot here...
33 (JID, PASSWORD) = ('my-jabber-id@jabberserver.example.org','my-password')
35 class BroadcastingJabberBot(JabberBot):
36 """This is a simple broadcasting client. Use "subscribe" to subscribe to broadcasts, "unsubscribe" to unsubscribe and "broadcast" + message to send out a broadcast message. Automatic messages will be sent out all 60 seconds."""
38 def __init__( self, jid, password, res = None):
39 super( BroadcastingJabberBot, self).__init__( jid, password, res)
41 self.users = []
42 self.message_queue = []
43 self.thread_killed = False
45 @botcmd
46 def subscribe( self, mess, args):
47 """Subscribe to the broadcast list"""
48 user = mess.getFrom()
49 if user in self.users:
50 return 'You are already subscribed.'
51 else:
52 self.users.append( user)
53 self.log( '%s subscribed to the broadcast.' % user)
54 return 'You are now subscribed.'
56 @botcmd
57 def unsubscribe( self, mess, args):
58 """Unsubscribe from the broadcast list"""
59 user = mess.getFrom()
60 if not user in self.users:
61 return 'You are not subscribed!'
62 else:
63 self.users.remove( user)
64 self.log( '%s unsubscribed from the broadcast.' % user)
65 return 'You are now unsubscribed.'
67 # You can use the "hidden" parameter to hide the
68 # command from JabberBot's 'help' list
69 @botcmd(hidden=True)
70 def broadcast( self, mess, args):
71 """Sends out a broadcast, supply message as arguments (e.g. broadcast hello)"""
72 self.message_queue.append( 'broadcast: %s (from %s)' % ( args, str(mess.getFrom()), ))
73 self.log( '%s sent out a message to %d users.' % ( str(mess.getFrom()), len(self.users),))
75 def idle_proc( self):
76 if not len(self.message_queue):
77 return
79 # copy the message queue, then empty it
80 messages = self.message_queue
81 self.message_queue = []
83 for message in messages:
84 if len(self.users):
85 self.log('sending "%s" to %d user(s).' % ( message, len(self.users), ))
86 for user in self.users:
87 self.send( user, message)
89 def thread_proc( self):
90 while not self.thread_killed:
91 self.message_queue.append('this is an automatic message, sent all 60 seconds :)')
92 for i in range(60):
93 time.sleep( 1)
94 if self.thread_killed:
95 return
98 bc = BroadcastingJabberBot( JID, PASSWORD)
100 th = threading.Thread( target = bc.thread_proc)
101 bc.serve_forever( connect_callback = lambda: th.start())
102 bc.thread_killed = True