/src/services/EntityTime.py

http://pyaimt.googlecode.com/ · Python · 40 lines · 32 code · 6 blank · 2 comment · 2 complexity · 60e022e3b5bad6f1e0c608651aa39ee2 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 config
  6. from debug import LogEvent, INFO, WARN, ERROR
  7. import globals
  8. import time
  9. class EntityTime:
  10. def __init__(self, pytrans):
  11. self.pytrans = pytrans
  12. self.pytrans.disco.addFeature(globals.IQTIME, self.incomingIq, config.jid)
  13. self.pytrans.disco.addFeature(globals.IQTIME, self.incomingIq, "USER")
  14. def incomingIq(self, el):
  15. eltype = el.getAttribute("type")
  16. if eltype != "get": return # Only answer "get" stanzas
  17. self.sendTime(el)
  18. def sendTime(self, el):
  19. LogEvent(INFO)
  20. iq = Element((None, "iq"))
  21. iq.attributes["type"] = "result"
  22. iq.attributes["from"] = el.getAttribute("to")
  23. iq.attributes["to"] = el.getAttribute("from")
  24. if el.getAttribute("id"):
  25. iq.attributes["id"] = el.getAttribute("id")
  26. query = iq.addElement("query")
  27. query.attributes["xmlns"] = globals.IQTIME
  28. utc = query.addElement("utc")
  29. utc.addContent(str(time.strftime("%Y%m%dT%H:%M:%S")))
  30. tz = query.addElement("tz")
  31. tz.addContent(str(time.tzname[1]))
  32. display = query.addElement("display")
  33. display.addContent(str(time.ctime()))
  34. self.pytrans.send(iq)