removed ClientXMPP.server in favor of ClientXMPP.domain

This commit is contained in:
Tom Nichols 2010-07-09 17:25:11 -04:00
parent d09cbef9a7
commit 9c5285987d
6 changed files with 13 additions and 26 deletions

View File

@ -41,25 +41,16 @@ except ImportError:
srvsupport = False
#class PresenceStanzaType(object):
#
# def fromXML(self, xml):
# self.ptype = xml.get('type')
class ClientXMPP(basexmpp, XMLStream):
"""SleekXMPP's client class. Use only for good, not evil."""
def __init__(self, jid, password, ssl=False, plugin_config = {}, plugin_whitelist=[], escape_quotes=True):
global srvsupport
XMLStream.__init__(self)
self.default_ns = 'jabber:client'
basexmpp.__init__(self)
self.plugin_config = plugin_config
self.escape_quotes = escape_quotes
self.set_jid(jid)
self.server = None
self.port = 5222 # not used if DNS SRV is used
self.plugin_whitelist = plugin_whitelist
self.auto_reconnect = True
@ -105,25 +96,25 @@ class ClientXMPP(basexmpp, XMLStream):
def connect(self, host=None, port=None):
"""Connect to the Jabber Server. Attempts SRV lookup, and if it fails, uses
the JID server."""
the JID server. You can optionally specify a host/port if you're not using
DNS and want to connect to a server address that is different from the XMPP domain."""
if self.state['connected']: return True
if host:
self.server = host
if host: # if a host was specified, don't attempt a DNS lookup.
if port is None: port = self.port
else:
if not self.srvsupport:
logging.debug("Did not supply (address, port) to connect to and no SRV support is installed (http://www.dnspython.org). Continuing to attempt connection, using domain from JID.")
logging.warn("Did not supply (address, port) to connect to and no SRV support is installed (http://www.dnspython.org). Continuing to attempt connection, using domain from JID.")
else:
logging.debug("Since no address is supplied, attempting SRV lookup.")
try:
answers = dns.resolver.query("_xmpp-client._tcp.%s" % self.server, dns.rdatatype.SRV)
answers = dns.resolver.query("_xmpp-client._tcp.%s" % self.domain, dns.rdatatype.SRV)
except dns.resolver.NXDOMAIN:
logging.debug("No appropriate SRV record found. Using JID server name.")
logging.info("No appropriate SRV record found for %s. Using domain as server address.", self.domain)
except dns.exception.DNSException:
# this could be a timeout or other DNS error. Worth retrying?
logging.exception("DNS error during SRV query for %s. Using JID server name.", self.server)
logging.exception("DNS error during SRV query for %s. Using domain as server address.", self.domain)
else:
# pick a random answer, weighted by priority
# there are less verbose ways of doing this (random.choice() with answer * priority), but I chose this way anyway
@ -140,16 +131,12 @@ class ClientXMPP(basexmpp, XMLStream):
if picked <= priority:
(host,port) = addresses[priority]
break
# if SRV lookup was successful, we aren't using a particular server.
self.server = None
if not host:
# if all else fails take server from JID.
(host,port) = (self.domain, self.port)
self.server = None
logging.debug('Attempting connection to %s:%d', host, port )
#TODO option to not use TLS?
result = XMLStream.connect(self, host, port)
if result:
self.event("connected")

View File

@ -33,7 +33,7 @@ class gmail_notify(base.base_plugin):
def handler_gmailcheck(self, payload):
#TODO XEP 30 should cache results and have getFeature
result = self.xmpp['xep_0030'].getInfo(self.xmpp.server)
result = self.xmpp['xep_0030'].getInfo(self.xmpp.domain)
features = []
for feature in result.findall('{http://jabber.org/protocol/disco#info}query/{http://jabber.org/protocol/disco#info}feature'):
features.append(feature.get('var'))

View File

@ -45,7 +45,7 @@ class xep_0078(base.base_plugin):
logging.debug("Starting jabber:iq:auth Authentication")
auth_request = self.xmpp.makeIqGet()
auth_request_query = ET.Element('{jabber:iq:auth}query')
auth_request.attrib['to'] = self.xmpp.server
auth_request.attrib['to'] = self.xmpp.domain
username = ET.Element('username')
username.text = self.xmpp.username
auth_request_query.append(username)

View File

@ -38,7 +38,7 @@ class xep_0092(base.base_plugin):
def report_version(self, xml):
iq = self.xmpp.makeIqResult(xml.get('id', 'unknown'))
iq.attrib['to'] = xml.get('from', self.xmpp.server)
iq.attrib['to'] = xml.get('from', self.xmpp.domain)
query = ET.Element('{jabber:iq:version}query')
name = ET.Element('name')
name.text = self.name

View File

@ -41,14 +41,14 @@ class xep_0199(base.base_plugin):
def handler_pingserver(self, xml):
if not self.running:
time.sleep(self.config.get('frequency', 300))
while self.sendPing(self.xmpp.server, self.config.get('timeout', 30)) is not False:
while self.sendPing(self.xmpp.domain, self.config.get('timeout', 30)) is not False:
time.sleep(self.config.get('frequency', 300))
logging.debug("Did not recieve ping back in time. Requesting Reconnect.")
self.xmpp.disconnect(reconnect=True)
def handler_ping(self, xml):
iq = self.xmpp.makeIqResult(xml.get('id', 'unknown'))
iq.attrib['to'] = xml.get('from', self.xmpp.server)
iq.attrib['to'] = xml.get('from', self.xmpp.domain)
self.xmpp.send(iq)
def sendPing(self, jid, timeout = 30):

View File

@ -43,7 +43,7 @@ class testps(sleekxmpp.ClientXMPP):
self.node = "pstestnode_%s"
self.pshost = pshost
if pshost is None:
self.pshost = self.server
self.pshost = self.domain
self.nodenum = int(nodenum)
self.leafnode = self.nodenum + 1
self.collectnode = self.nodenum + 2