Message per Jabber versenden

Aus MHC-Wiki

Wechseln zu: Navigation, Suche

Dieses Pythonscript dient mir um Nachrichten des SEP Sesam Backupsystems an Jabberuser zu schicken. Die Idee ist dabei, dass es einen Sesamuser im Jabber server gibt, auf dessen Roaster (Freundesliste) alle die eingetragen sind, die eine meldung bekommen sollen. Man ruft das ganze dann so aus den sm_notify und sm_alarm Script auf:

jabberSESAM.py -h jabber.mhcsoftware.de -u sesam -p geheim -m "SESAM Alarm: $message"

jabberSESAM.py

#! /usr/bin/env python

import getopt, sys, time

import jabber

class JabberClient:

   def __init__( self, hostname, username,
    password ):
       self.client = jabber.Client( hostname )

       self.client.connect()

       self.client.registerHandler('presence', self.handlePresence )

       if ( not self.client.auth( username, password, 'SESAM' ) ):
           print "Authorization failed."

       self.client.requestRoster()
       self.client.sendInitPresence()

   def __del__( self ):
       self.client.disconnect()

   def handlePresence( self, client, presence ):
       type = presence.getType()
       who = presence.getFrom().getStripped()

       if ( type == 'subscribe' ):
           self.client.send( jabber.Presence( to = who, type = 'subscribed' ) )
           self.client.send( jabber.Presence( to = who, type = 'subscribe' ) )

       elif ( type == 'unsubscribe' ):
           self.client.send( jabber.Presence( to = who, type = 'unsubscribed' ) )
           self.client.send( jabber.Presence( to = who, type = 'unsubscribe' ) )

   def sendMessageToAll( self, text ):
       roster = self.client.requestRoster()
       users = roster.getSummary()
       for user in users.keys():
           message = jabber.Message( user, text )
           self.client.send( message )
 
def usage():
   print "Usage: jabberSESAM.py"
   print " Options:"
   print "  -m, --message=Message"
   print "      --help"
   print "  -h, --host=HOSTNAME (default: localhost)"
   print "  -u, --user=USERNAME"
   print "  -p, --password=PASSWORD"
   print ""
   print "Sends message to all users on you roaster"
  
def main():
   options = "m:h:u:p:"
   long_options = [ "help", "message=", "host=", "user=", "password=" ]

   try:
       opts, args = getopt.getopt( sys.argv[1:], options, long_options )
   except getopt.GetoptError:
       usage()
       sys.exit( 2 )

   message = ""
   host = "localhost"
   user = ""
   password = ""

   for option, arg in opts:

       if ( option == "--help" ):
           usage()
           sys.exit()

       if ( option in ( "-m", "--message" ) ):
           message = arg

       if ( option in ( "-h", "--host" ) ):
           host = arg
       if ( option in ( "-i", "--interval" ) ):
           interval = int( arg )

       if ( option in ( "-u", "--user" ) ):
           user = arg

       if ( option in ( "-p", "--password" ) ):
           password = arg

   if ( ( host == "" ) or ( user == "" ) or ( password == "" ) or ( message == "" ) ):
       usage()
       sys.exit( 2 )
   else:
       available = True
       client = JabberClient( host, user, password )
       client.sendMessageToAll( message )

try:
   if __name__ == "__main__":
       main()
except KeyboardInterrupt:
   pass

Angelehnt an einen Artikel aus der IX 3/2004


Alternativ mit dem XMPP-Python Modul:

#! /usr/bin/env python

import warnings
warnings.filterwarnings("ignore")

import getopt, sys, time

import xmpp


class JabberClient:

   def __init__( self, hostname, username, password ):

       self.client = xmpp.Client( hostname ,debug=[])

       self.client.connect()

       if ( not self.client.auth( username, password, 'SESAM' ) ):
           print "Authorization failed."

       self.client.sendInitPresence()

   def __del__( self ):
       self.client.disconnect()

   def sendMessageToAll( self, text ):
       roster = self.client.getRoster()
       for user in roster.keys():
           message = xmpp.Message( user, text )
           self.client.send( message )
 
def usage():
   print "Usage: jabberSESAM.py"
   print " Options:"
   print "  -m, --message=Message"
   print "      --help"
   print "  -u, --user=USERNAME@server"
   print "  -p, --password=PASSWORD"
   print ""
   print "Sends message to all users on you roaster"
  
def main():
   warnings.filterwarnings("ignore")
   options = "m:u:p:"
   long_options = [ "help", "message=", "host=", "user=", "password=" ]

   try:
       opts, args = getopt.getopt( sys.argv[1:], options, long_options )
   except getopt.GetoptError:
       usage()
       sys.exit( 2 )

   message = ""
   host = "localhost"
   user = ""
   password = ""

   for option, arg in opts:

       if ( option == "--help" ):
           usage()
           sys.exit()

       if ( option in ( "-m", "--message" ) ):
           message = arg

       if ( option in ( "-i", "--interval" ) ):
           interval = int( arg )

       if ( option in ( "-u", "--user" ) ):
           user = arg

       if ( option in ( "-p", "--password" ) ):
           password = arg

   jid = xmpp.JID(user)
   user,host = jid.getNode(),jid.getDomain()

   if ( ( host == "" ) or ( user == "" ) or ( password == "" ) or ( message == "" ) ):
       usage()
       sys.exit( 2 )
   else:
       available = True
       client = JabberClient( host, user, password )
       client.sendMessageToAll( message )

try:
   if __name__ == "__main__":
       main()
except KeyboardInterrupt:
   pass
Persönliche Werkzeuge
Navigation