]> code.delx.au - pymsnt/blob - src/misciq.py
bbbde0fffea1c765e1c9f9d0709c1f4f0bd729d9
[pymsnt] / src / misciq.py
1 # Copyright 2004 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
3
4 import utils
5 if(utils.checkTwisted()):
6 from twisted.xish.domish import Element
7 from twisted.words.protocols.jabber import jid
8 else:
9 from tlib.domish import Element
10 from tlib.jabber import jid
11 from twisted.internet import reactor, task
12
13 import legacy
14 import config
15 import debug
16 import lang
17 import sys
18
19
20 class PingService:
21 def __init__(self, pytrans):
22 self.pytrans = pytrans
23 self.pingCounter = 0
24 self.pingCheckTask = task.LoopingCall(self.pingCheck)
25 reactor.callLater(10.0, self.start)
26
27 def start(self):
28 self.pingCheckTask.start(120.0)
29
30 def pingCheck(self):
31 if(self.pingCounter >= 2 and self.pytrans.xmlstream): # Two minutes of no response from the server
32 self.pytrans.xmlstream.transport.loseConnection()
33 elif(config.mainServerJID):
34 d = self.pytrans.discovery.sendIq(self.makePingPacket())
35 d.addCallback(self.pongReceived)
36 self.pingCounter += 1
37
38 def pongReceived(self, el):
39 self.pingCounter = 0
40
41 def makePingPacket(self):
42 iq = Element((None, "iq"))
43 iq.attributes["from"] = config.jid
44 iq.attributes["to"] = config.mainServerJID
45 iq.attributes["type"] = "get"
46 query = iq.addElement("query")
47 query.attributes["xmlns"] = "jabber:iq:version"
48 return iq
49
50 class GatewayTranslator:
51 def __init__(self, pytrans):
52 self.pytrans = pytrans
53 self.pytrans.discovery.addFeature("jabber:iq:gateway", self.incomingIq)
54
55 def incomingIq(self, el):
56 fro = el.getAttribute("from")
57 ID = el.getAttribute("id")
58 itype = el.getAttribute("type")
59 if(itype == "get"):
60 self.sendPrompt(fro, ID, utils.getLang(el))
61 elif(itype == "set"):
62 self.sendTranslation(fro, ID, el)
63
64
65 def sendPrompt(self, to, ID, ulang):
66 debug.log("GatewayTranslator: Sending translation details for jabber:iq:gateway - user %s %s" % (to, ID))
67
68 iq = Element((None, "iq"))
69
70 iq.attributes["type"] = "result"
71 iq.attributes["from"] = config.jid
72 iq.attributes["to"] = to
73 iq.attributes["id"] = ID
74 query = iq.addElement("query")
75 query.attributes["xmlns"] = "jabber:iq:gateway"
76 desc = query.addElement("desc")
77 desc.addContent(lang.get(ulang).gatewayTranslator)
78 prompt = query.addElement("prompt")
79
80 self.pytrans.send(iq)
81
82 def sendTranslation(self, to, ID, el):
83 debug.log("GatewayTranslator: Translating account for jabber:iq:gateway - user %s %s" % (to, ID))
84
85 # Find the user's legacy account
86 legacyaccount = None
87 for query in el.elements():
88 if(query.name == "query"):
89 for child in query.elements():
90 if(child.name == "prompt"):
91 legacyaccount = str(child)
92 break
93 break
94
95
96 if(legacyaccount and len(legacyaccount) > 0):
97 debug.log("GatewayTranslator: Sending translated account for jabber:iq:gateway - user %s %s" % (to, ID))
98 iq = Element((None, "iq"))
99 iq.attributes["type"] = "result"
100 iq.attributes["from"] = config.jid
101 iq.attributes["to"] = to
102 iq.attributes["id"] = ID
103 query = iq.addElement("query")
104 query.attributes["xmlns"] = "jabber:iq:gateway"
105 prompt = query.addElement("prompt")
106 prompt.addContent(legacy.translateAccount(legacyaccount))
107
108 self.pytrans.send(iq)
109
110 else:
111 self.pytrans.discovery.sendIqNotValid(to, ID, "jabber:iq:gateway")
112
113
114
115 class VersionTeller:
116 def __init__(self, pytrans):
117 self.pytrans = pytrans
118 self.pytrans.discovery.addFeature("jabber:iq:version", self.incomingIq)
119
120 def incomingIq(self, el):
121 eltype = el.getAttribute("type")
122 if(eltype != "get"): return # Only answer "get" stanzas
123
124 self.sendVersion(el)
125
126 def sendVersion(self, el):
127 debug.log("Discovery: Sending transport version information")
128 iq = Element((None, "iq"))
129 iq.attributes["type"] = "result"
130 iq.attributes["from"] = config.jid
131 iq.attributes["to"] = el.getAttribute("from")
132 if(el.getAttribute("id")):
133 iq.attributes["id"] = el.getAttribute("id")
134 query = iq.addElement("query")
135 query.attributes["xmlns"] = "jabber:iq:version"
136 name = query.addElement("name")
137 name.addContent(legacy.name)
138 version = query.addElement("version")
139 version.addContent(legacy.version)
140 os = query.addElement("os")
141 os.addContent("Python" + sys.version)
142
143 self.pytrans.send(iq)
144
145
146