PageRenderTime 80ms CodeModel.GetById 76ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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