Guest User

Untitled

a guest
Jun 16th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #!/usr/local/bin/python
  2. # -*- coding: utf-8 -*-
  3. import sys, xmpp, string, re
  4. from gtranslate import translate
  5.  
  6. config = {
  7. 'jid':'translator55429@jabber.ru',
  8. 'pass':'password'
  9. }
  10.  
  11. def message_handler(conn, mess):
  12. text = mess.getBody()
  13. user = mess.getFrom()
  14. langpair = 'en|ru'
  15. if text:
  16. lines = text.splitlines()
  17. regexp = re.compile('^\w{2}\|\w{2}$', re.IGNORECASE)
  18. if regexp.match(lines[0]):
  19. langpair = lines[0]
  20. del lines[0]
  21.  
  22. text = string.join(lines)
  23. reply = translate(text, langpair)
  24. conn.send(xmpp.Message(mess.getFrom(),reply))
  25.  
  26. def step_on(conn):
  27. try:
  28. conn.Process(1)
  29. except KeyboardInterrupt: return 0
  30. return 1
  31.  
  32. def go_on(conn):
  33. while step_on(conn): pass
  34.  
  35. jid = xmpp.JID(config['jid'])
  36. user, server, password = jid.getNode(), jid.getDomain(), config['pass']
  37.  
  38. conn = xmpp.Client(server)#,debug=[])
  39. conres = conn.connect()
  40.  
  41. if not conres:
  42. print "Unable to connect to server %s!"%server
  43. sys.exit(1)
  44.  
  45. if conres<>'tls':
  46. print "Warning: unable to estabilish secure connection - TLS failed!"
  47.  
  48. authres = conn.auth(user,password)
  49.  
  50. if not authres:
  51. print "Unable to authorize on %s - check login/password."%server
  52. sys.exit(1)
  53.  
  54. if authres<>'sasl':
  55. print "Warning: unable to perform SASL auth os %s. Old authentication method used!"%server
  56.  
  57. conn.RegisterHandler('message', message_handler)
  58. conn.sendInitPresence(requestRoster = 1)
  59. print "Bot started."
  60. go_on(conn)
Add Comment
Please, Sign In to add comment