]>
code.delx.au - pymsnt/blob - src/misciq.py
bbbde0fffea1c765e1c9f9d0709c1f4f0bd729d9
1 # Copyright 2004 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
5 if(utils
.checkTwisted()):
6 from twisted
.xish
.domish
import Element
7 from twisted
.words
.protocols
.jabber
import jid
9 from tlib
.domish
import Element
10 from tlib
.jabber
import jid
11 from twisted
.internet
import reactor
, task
21 def __init__(self
, pytrans
):
22 self
.pytrans
= pytrans
24 self
.pingCheckTask
= task
.LoopingCall(self
.pingCheck
)
25 reactor
.callLater(10.0, self
.start
)
28 self
.pingCheckTask
.start(120.0)
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
)
38 def pongReceived(self
, el
):
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"
50 class GatewayTranslator
:
51 def __init__(self
, pytrans
):
52 self
.pytrans
= pytrans
53 self
.pytrans
.discovery
.addFeature("jabber:iq:gateway", self
.incomingIq
)
55 def incomingIq(self
, el
):
56 fro
= el
.getAttribute("from")
57 ID
= el
.getAttribute("id")
58 itype
= el
.getAttribute("type")
60 self
.sendPrompt(fro
, ID
, utils
.getLang(el
))
62 self
.sendTranslation(fro
, ID
, el
)
65 def sendPrompt(self
, to
, ID
, ulang
):
66 debug
.log("GatewayTranslator: Sending translation details for jabber:iq:gateway - user %s %s" % (to
, ID
))
68 iq
= Element((None, "iq"))
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")
82 def sendTranslation(self
, to
, ID
, el
):
83 debug
.log("GatewayTranslator: Translating account for jabber:iq:gateway - user %s %s" % (to
, ID
))
85 # Find the user's legacy account
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
)
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
))
108 self
.pytrans
.send(iq
)
111 self
.pytrans
.discovery
.sendIqNotValid(to
, ID
, "jabber:iq:gateway")
116 def __init__(self
, pytrans
):
117 self
.pytrans
= pytrans
118 self
.pytrans
.discovery
.addFeature("jabber:iq:version", self
.incomingIq
)
120 def incomingIq(self
, el
):
121 eltype
= el
.getAttribute("type")
122 if(eltype
!= "get"): return # Only answer "get" stanzas
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
)
143 self
.pytrans
.send(iq
)