b9e3e47
--- urlwatch	2009-08-14 01:47:46.457960865 +0200
b9e3e47
+++ urlwatch_xmpp	2009-08-14 15:46:25.881464617 +0200
b9e3e47
@@ -55,6 +55,7 @@
b9e3e47
 cache_dir = os.path.join(urlwatch_dir, 'cache')
b9e3e47
 scripts_dir = os.path.join(urlwatch_dir, 'lib')
b9e3e47
 hooks_py = os.path.join(scripts_dir, 'hooks.py')
b9e3e47
+xmpp_txt = os.path.join(urlwatch_dir, 'xmpp.txt')
b9e3e47
 
b9e3e47
 # Check if we are installed in the system already
b9e3e47
 (prefix, bindir) = os.path.split(os.path.dirname(os.path.abspath(sys.argv[0])))
b9e3e47
@@ -141,8 +142,10 @@
b9e3e47
     parser.add_option('', '--urls', dest='urls', metavar='FILE', help='Read URLs from the specified file')
b9e3e47
     parser.add_option('', '--hooks', dest='hooks', metavar='FILE', help='Use specified file as hooks.py module')
b9e3e47
     parser.add_option('-e', '--display-errors', action='store_true', dest='display_errors', help='Include HTTP errors (404, etc..) in the output')
b9e3e47
+    parser.add_option('-x', '--xmpp', action='store_true', dest='xmpp', help='Send a message to XMPP clients')
b9e3e47
+    parser.add_option('', '--xmpp-file', dest='xmpp_file', metavar='FILE', help='Read XMPP login data and recipients from [FILE] instead')
b9e3e47
 
b9e3e47
-    parser.set_defaults(verbose=False, display_errors=False)
b9e3e47
+    parser.set_defaults(verbose=False, display_errors=False, xmpp=False)
b9e3e47
 
b9e3e47
     (options, args) = parser.parse_args(sys.argv)
b9e3e47
 
b9e3e47
@@ -176,6 +179,9 @@
b9e3e47
             log.error('%s is not a file' % options.hooks)
b9e3e47
             print 'Error: %s is not a file' % options.hooks
b9e3e47
             sys.exit(1)
b9e3e47
+    
b9e3e47
+    if options.xmpp_file:
b9e3e47
+        options.xmpp = True
b9e3e47
 
b9e3e47
     # Created all needed folders
b9e3e47
     for needed_dir in (urlwatch_dir, cache_dir, scripts_dir):
b9e3e47
@@ -201,6 +207,52 @@
b9e3e47
         if not options.hooks and os.path.exists(hooks_py_example) and not os.path.exists(hooks_py_fn):
b9e3e47
             shutil.copy(hooks_py_example, hooks_py_fn)
b9e3e47
         sys.exit(1)
b9e3e47
+    
b9e3e47
+    if options.xmpp:
b9e3e47
+        try:
b9e3e47
+            import xmpp
b9e3e47
+        except ImportError:
b9e3e47
+            log.error('python-xmpp not installed')
b9e3e47
+            print 'Error: You need python-xmpp installed to use the -x switch'
b9e3e47
+            sys.exit(1)
b9e3e47
+        
b9e3e47
+        if options.xmpp_file:
b9e3e47
+            if os.path.isfile(options.xmpp_file):
b9e3e47
+                xmpp_txt = options.xmpp_file
b9e3e47
+                log.info('using %s as xmpp.txt' % xmpp_txt)
b9e3e47
+            else:
b9e3e47
+                log.error('no such file: %s' % options.xmpp_file)
b9e3e47
+                print 'Error: There is no such file: %s' % options.xmpp_file
b9e3e47
+                sys.exit(1)
b9e3e47
+        
b9e3e47
+        try:
b9e3e47
+            xmpp_filehandle = open(xmpp_txt, 'r')            
b9e3e47
+        except IOError:
b9e3e47
+            log.error('No xmpp.txt file')
b9e3e47
+            print 'Error: You need to create a xmpp.txt file first.'
b9e3e47
+            print ''
b9e3e47
+            print 'Place it in %s' % os.path.dirname(xmpp_txt)
b9e3e47
+            print ''
b9e3e47
+            print ''
b9e3e47
+            print 'Syntax is:'
b9e3e47
+            print ''
b9e3e47
+            print '\tsender-username@server.tld senderpassword'
b9e3e47
+            print '\tfirst-recipient@jabberserver1.tld'
b9e3e47
+            print '\tsecond-recipient@jabberserver2.tld'
b9e3e47
+            print '\tthird-recipient@foobarserver.tld'
b9e3e47
+            print '\t...'
b9e3e47
+            print ''
b9e3e47
+            sys.exit(1)
b9e3e47
+        
b9e3e47
+        xmpp_lines = xmpp_filehandle.readlines()
b9e3e47
+        xmpp_filehandle.close()
b9e3e47
+        for i in xrange(len(xmpp_lines)):
b9e3e47
+            xmpp_lines[i] = xmpp_lines[i].strip("\n")
b9e3e47
+        (xmpp_from, xmpp_pass) = xmpp_lines[0].split(' ')
b9e3e47
+        xmpp_to = []
b9e3e47
+        for i in xrange(1, len(xmpp_lines)):
b9e3e47
+            xmpp_to.append(xmpp_lines[i])
b9e3e47
+        log.info('using %s to send status messages to %s' % (xmpp_from, xmpp_to))            
b9e3e47
 
b9e3e47
     headers = {
b9e3e47
             'User-agent': user_agent,
b9e3e47
@@ -277,18 +329,36 @@
b9e3e47
     end = datetime.datetime.now()
b9e3e47
 
b9e3e47
     # Output everything
b9e3e47
-    if len(summary) > 1:
b9e3e47
+    if len(summary) > 0:
b9e3e47
         log.info('printing summary with %d items' % len(summary))
b9e3e47
         print '-'*line_length
b9e3e47
         print 'summary: %d changes' % (len(summary),)
b9e3e47
         print ''
b9e3e47
+        msg = ''
b9e3e47
         for id, line in enumerate(summary):
b9e3e47
             print '%02d. %s' % (id+1, line)
b9e3e47
+            msg += "\n" + '%02d. %s' % (id+1, line)
b9e3e47
         print '-'*line_length
b9e3e47
         print '\n\n\n'
b9e3e47
+        
b9e3e47
+        if (options.xmpp):
b9e3e47
+            try:
b9e3e47
+                (xmpp_user, xmpp_server) = xmpp_from.split('@')
b9e3e47
+                client = xmpp.Client(xmpp_server)
b9e3e47
+                client.connect(server=(xmpp_server,5223))
b9e3e47
+                client.auth(xmpp_user, xmpp_pass, 'urlwatch')
b9e3e47
+                client.sendInitPresence()
b9e3e47
+                for to in xmpp_to:
b9e3e47
+                    message = xmpp.Message(to, msg)
b9e3e47
+                    message.setAttr('type', 'chat')
b9e3e47
+                    client.send(message)
b9e3e47
+            except:
b9e3e47
+                log.error('Could not send message to %s' % xmpp_to)
b9e3e47
+                print 'Error: Could not send message to %s' % xmpp_to
b9e3e47
+            
b9e3e47
     else:
b9e3e47
         log.info('summary is too short - not printing')
b9e3e47
-    if len(details) > 1:
b9e3e47
+    if len(details) > 1 and not options.xmpp:
b9e3e47
         log.info('printing details with %d items' % len(details))
b9e3e47
         print '\n'.join(details)
b9e3e47
         print '-- '