Formatting changes.
[jabberbot/examples.git] / examples / broadcast.py
blob30a1863db489ae780c6eecc3d2e5623a06be9a67
1 #!/usr/bin/python
3 # JabberBot: A simple jabber/xmpp bot framework
4 # Copyright (c) 2007-2011 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
31 import logging
33 # Fill in the JID + Password of your JabberBot here...
34 (JID, PASSWORD) = ('my-jabber-id@jabberserver.example.org','my-password')
36 class BroadcastingJabberBot(JabberBot):
37 """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."""
39 def __init__( self, jid, password, res = None):
40 super( BroadcastingJabberBot, self).__init__( jid, password, res)
41 # create console handler
42 chandler = logging.StreamHandler()
43 # create formatter
44 formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
45 # add formatter to handler
46 chandler.setFormatter(formatter)
47 # add handler to logger
48 self.log.addHandler(chandler)
49 # set level to INFO
50 self.log.setLevel(logging.INFO)
52 self.users = []
53 self.message_queue = []
54 self.thread_killed = False
56 @botcmd
57 def subscribe( self, mess, args):
58 """Subscribe to the broadcast list"""
59 user = mess.getFrom()
60 if user in self.users:
61 return 'You are already subscribed.'
62 else:
63 self.users.append( user)
64 self.log.info( '%s subscribed to the broadcast.' % user)
65 return 'You are now subscribed.'
67 @botcmd
68 def unsubscribe( self, mess, args):
69 """Unsubscribe from the broadcast list"""
70 user = mess.getFrom()
71 if not user in self.users:
72 return 'You are not subscribed!'
73 else:
74 self.users.remove( user)
75 self.log.info( '%s unsubscribed from the broadcast.' % user)
76 return 'You are now unsubscribed.'
78 # You can use the "hidden" parameter to hide the
79 # command from JabberBot's 'help' list
80 @botcmd(hidden=True)
81 def broadcast( self, mess, args):
82 """Sends out a broadcast, supply message as arguments (e.g. broadcast hello)"""
83 self.message_queue.append( 'broadcast: %s (from %s)' % ( args, str(mess.getFrom()), ))
84 self.log.info( '%s sent out a message to %d users.' % ( str(mess.getFrom()), len(self.users),))
86 def idle_proc( self):
87 if not len(self.message_queue):
88 return
90 # copy the message queue, then empty it
91 messages = self.message_queue
92 self.message_queue = []
94 for message in messages:
95 if len(self.users):
96 self.log.info('sending "%s" to %d user(s).' % ( message, len(self.users), ))
97 for user in self.users:
98 self.send( user, message)
100 def thread_proc( self):
101 while not self.thread_killed:
102 self.message_queue.append('this is an automatic message, sent all 60 seconds :)')
103 for i in range(60):
104 time.sleep( 1)
105 if self.thread_killed:
106 return
109 bc = BroadcastingJabberBot( JID, PASSWORD)
111 th = threading.Thread( target = bc.thread_proc)
112 bc.serve_forever( connect_callback = lambda: th.start())
113 bc.thread_killed = True