#!/usr/bin/env python # -*- encoding: utf-8 -*- import xmpp import getopt, sys def usage(): print '''Usage: sendgt [OPTION...] MESSAGE Example: sendgt -f solrex -p PASSWD -t solrex Hello! Options: -f, --from=SENDER The sender's google account name -p, --passwd=PASSWD The sender's password -t, --To=RECEIVERS The receivers' account name, separated by ',' -v, --verbose Print verbose information -?, --help Give this help list --usage Give a short usage message -V, --version Print program version Mandatory or optional arguments to long options are also mandatory or optional for any corresponding short options. Report bugs to .''' sys.exit() def main(): From = '' Passwd = '' To = '' Message = '' verbose = False try: opts, args = getopt.getopt(sys.argv[1:], "hf:p:t:v", ["help"]) except getopt.GetoptError, err: # print help information and exit: print str(err) # will print something like "option -a not recognized" usage() sys.exit(2) for o, a in opts: if o == "-v": verbose = True elif o in ("-h", "--help"): usage() elif o in ("-f", "--from"): From = a elif o in ("-p", "--passwd"): Passwd = a elif o in ("-t", "--to"): To = a.split(',') else: assert False, "Unkown option." usage() if args == []: args = sys.stdin.readlines() for a in args: Message += a if From == '' or Passwd == '' or Message == '': print 'ERROR: SENDER, PASSWD and MESSAGE is needed.' usage() From, a, b = From.partition('@') cnx = xmpp.Client('gmail.com', debug=[]) cnx.connect( server=('talk.google.com', 5223) ) cnx.auth(From, Passwd, 'python') for a in To: if a.find('@') == -1: a += '@gmail.com' cnx.send(xmpp.Message(a, Message)) if __name__ == "__main__": main()