Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python
- # -*- coding: utf-8 -*-
- import sys, xmpp, string, re
- from gtranslate import translate
- config = {
- 'jid':'translator55429@jabber.ru',
- 'pass':'password'
- }
- def message_handler(conn, mess):
- text = mess.getBody()
- user = mess.getFrom()
- langpair = 'en|ru'
- if text:
- lines = text.splitlines()
- regexp = re.compile('^\w{2}\|\w{2}$', re.IGNORECASE)
- if regexp.match(lines[0]):
- langpair = lines[0]
- del lines[0]
- text = string.join(lines)
- reply = translate(text, langpair)
- conn.send(xmpp.Message(mess.getFrom(),reply))
- def step_on(conn):
- try:
- conn.Process(1)
- except KeyboardInterrupt: return 0
- return 1
- def go_on(conn):
- while step_on(conn): pass
- jid = xmpp.JID(config['jid'])
- user, server, password = jid.getNode(), jid.getDomain(), config['pass']
- conn = xmpp.Client(server)#,debug=[])
- conres = conn.connect()
- if not conres:
- print "Unable to connect to server %s!"%server
- sys.exit(1)
- if conres<>'tls':
- print "Warning: unable to estabilish secure connection - TLS failed!"
- authres = conn.auth(user,password)
- if not authres:
- print "Unable to authorize on %s - check login/password."%server
- sys.exit(1)
- if authres<>'sasl':
- print "Warning: unable to perform SASL auth os %s. Old authentication method used!"%server
- conn.RegisterHandler('message', message_handler)
- conn.sendInitPresence(requestRoster = 1)
- print "Bot started."
- go_on(conn)
Add Comment
Please, Sign In to add comment