]> jfr.im git - irc/quakenet/qwebirc.git/blobdiff - qwebirc/ircclient.py
add dynamic configuration support
[irc/quakenet/qwebirc.git] / qwebirc / ircclient.py
index 8b9b3e59ef0731e586ebd93ee514ac7dccb28eb1..8e06eaed64799658e78cf3fa9c5b9612e95db2ee 100644 (file)
@@ -1,13 +1,18 @@
 import twisted, sys, codecs, traceback
 from twisted.words.protocols import irc
-from twisted.internet import reactor, protocol
+from twisted.internet import reactor, protocol, abstract
 from twisted.web import resource, server
 from twisted.protocols import basic
-
-import hmac, time, config
+from twisted.names.client import Resolver
+import hmac, time, config, random, qwebirc.config_options as config_options
 from config import HMACTEMPORAL
 
-if config.WEBIRC_MODE == "hmac":
+if config.get("CONNECTION_RESOLVER"):
+  CONNECTION_RESOLVER = Resolver(servers=config.get("CONNECTION_RESOLVER"))
+else:
+  CONNECTION_RESOLVER = None
+
+if hasattr(config, "WEBIRC_MODE") and config.WEBIRC_MODE == "hmac":
   HMACKEY = hmac.HMAC(key=config.HMACKEY)
 
 def hmacfn(*args):
@@ -71,11 +76,13 @@ class QWebIRCClient(basic.LineReceiver):
     
     self.lastError = None
     f = self.factory.ircinit
-    nick, ident, ip, realname, hostname = f["nick"], f["ident"], f["ip"], f["realname"], f["hostname"]
+    nick, ident, ip, realname, hostname, pass_ = f["nick"], f["ident"], f["ip"], f["realname"], f["hostname"], f.get("password")
     self.__nickname = nick
     self.__perform = f.get("perform")
 
-    if config.WEBIRC_MODE == "hmac":
+    if not hasattr(config, "WEBIRC_MODE"):
+      self.write("USER %s bleh bleh %s :%s" % (ident, ip, realname))
+    elif config.WEBIRC_MODE == "hmac":
       hmac = hmacfn(ident, ip)
       self.write("USER %s bleh bleh %s %s :%s" % (ident, ip, hmac, realname))
     elif config.WEBIRC_MODE == "webirc":
@@ -84,7 +91,7 @@ class QWebIRCClient(basic.LineReceiver):
     elif config.WEBIRC_MODE == "cgiirc":
       self.write("PASS %s_%s_%s" % (config.CGIIRC_STRING, ip, hostname))
       self.write("USER %s bleh %s :%s" % (ident, ip, realname))
-    else:
+    elif config.WEBIRC_MODE == config_options.WEBIRC_REALNAME or config.WEBIRC_MODE is None: # last bit is legacy
       if ip == hostname:
         dispip = ip
       else:
@@ -92,6 +99,8 @@ class QWebIRCClient(basic.LineReceiver):
 
       self.write("USER %s bleh bleh :%s - %s" % (ident, dispip, realname))
 
+    if pass_ is not None:
+      self.write("PASS :%s" % pass_)
     self.write("NICK %s" % nick)
     
     self.factory.client = self
@@ -137,7 +146,27 @@ class QWebIRCFactory(protocol.ClientFactory):
 
 def createIRC(*args, **kwargs):
   f = QWebIRCFactory(*args, **kwargs)
-  reactor.connectTCP(config.IRCSERVER, config.IRCPORT, f)
+  
+  tcpkwargs = {}
+  if hasattr(config, "OUTGOING_IP"):
+    tcpkwargs["bindAddress"] = (config.OUTGOING_IP, 0)
+  
+  if CONNECTION_RESOLVER is None:
+    if hasattr(config, "SSLPORT"):
+      from twisted.internet import ssl
+      reactor.connectSSL(config.IRCSERVER, config.SSLPORT, f, ssl.ClientContextFactory(), **tcpkwargs)
+    else:
+      reactor.connectTCP(config.IRCSERVER, config.IRCPORT, f, **tcpkwargs)
+    return f
+
+  def callback(result):
+    name, port = random.choice(sorted((str(x.payload.target), x.payload.port) for x in result[0]))
+    reactor.connectTCP(name, port, f, **tcpkwargs)
+  def errback(err):
+    f.clientConnectionFailed(None, err) # None?!
+
+  d = CONNECTION_RESOLVER.lookupService(config.IRCSERVER, (1, 3, 11))
+  d.addCallbacks(callback, errback)
   return f
 
 if __name__ == "__main__":