/src/services/GatewayTranslator.py

http://pyaimt.googlecode.com/ · Python · 75 lines · 59 code · 13 blank · 3 comment · 9 complexity · 98573ee7228b1188e7abd5ffab4e4c23 MD5 · raw file

  1. # Copyright 2004-2006 Daniel Henninger <jadestorm@nc.rr.com>
  2. # Licensed for distribution under the GPL version 2, check COPYING for details
  3. import utils
  4. from twisted.words.xish.domish import Element
  5. import legacy
  6. import config
  7. import lang
  8. from debug import LogEvent, INFO, WARN, ERROR
  9. import globals
  10. class GatewayTranslator:
  11. def __init__(self, pytrans):
  12. self.pytrans = pytrans
  13. self.pytrans.disco.addFeature(globals.IQGATEWAY, self.incomingIq, config.jid)
  14. def incomingIq(self, el):
  15. fro = el.getAttribute("from")
  16. ID = el.getAttribute("id")
  17. itype = el.getAttribute("type")
  18. if itype == "get":
  19. self.sendPrompt(fro, ID, utils.getLang(el))
  20. elif itype == "set":
  21. self.sendTranslation(fro, ID, el)
  22. def sendPrompt(self, to, ID, ulang):
  23. LogEvent(INFO)
  24. iq = Element((None, "iq"))
  25. iq.attributes["type"] = "result"
  26. iq.attributes["from"] = config.jid
  27. iq.attributes["to"] = to
  28. iq.attributes["id"] = ID
  29. query = iq.addElement("query")
  30. query.attributes["xmlns"] = globals.IQGATEWAY
  31. desc = query.addElement("desc")
  32. desc.addContent(lang.get("gatewaytranslator", ulang))
  33. prompt = query.addElement("prompt")
  34. self.pytrans.send(iq)
  35. def sendTranslation(self, to, ID, el):
  36. LogEvent(INFO)
  37. # Find the user's legacy account
  38. legacyaccount = None
  39. for query in el.elements():
  40. if query.name == "query":
  41. for child in query.elements():
  42. if child.name == "prompt":
  43. legacyaccount = str(child)
  44. break
  45. break
  46. if legacyaccount and len(legacyaccount) > 0:
  47. LogEvent(INFO, msg="Sending translated account")
  48. iq = Element((None, "iq"))
  49. iq.attributes["type"] = "result"
  50. iq.attributes["from"] = config.jid
  51. iq.attributes["to"] = to
  52. iq.attributes["id"] = ID
  53. query = iq.addElement("query")
  54. query.attributes["xmlns"] = globals.IQGATEWAY
  55. prompt = query.addElement("prompt")
  56. prompt.addContent(legacy.translateAccount(legacyaccount))
  57. jid = query.addElement("jid")
  58. jid.addContent(legacy.translateAccount(legacyaccount))
  59. self.pytrans.send(iq)
  60. else:
  61. self.pytrans.iq.sendIqError(to, ID, globals.IQGATEWAY)
  62. self.pytrans.iq.sendIqError(to=to, fro=config.jid, ID=ID, xmlns="jabber:iq:gateway", etype="retry", condition="bad-request")