diff options
author | Micah Anderson <micah@riseup.net> | 2013-08-13 15:49:55 -0400 |
---|---|---|
committer | Micah Anderson <micah@riseup.net> | 2013-08-13 15:49:55 -0400 |
commit | 327dac28be41ac5204a777025e212b78a268c9d4 (patch) | |
tree | dd62cac222f1311f9df26ca8f2e90133c43dd937 /src/leap/mx/alias_resolver.py | |
parent | 54c3fe7a1240dbd2875c21fda9949fb8e91ecdef (diff) | |
parent | 5ae019af6e29697dd43700c614e06298d719ff96 (diff) |
update to 0.3.0
Diffstat (limited to 'src/leap/mx/alias_resolver.py')
-rw-r--r-- | src/leap/mx/alias_resolver.py | 50 |
1 files changed, 41 insertions, 9 deletions
diff --git a/src/leap/mx/alias_resolver.py b/src/leap/mx/alias_resolver.py index 2074ee5..08ebb73 100644 --- a/src/leap/mx/alias_resolver.py +++ b/src/leap/mx/alias_resolver.py @@ -19,6 +19,8 @@ """ Classes for resolving postfix aliases. +Test this with postmap -v -q "foo" tcp:localhost:4242 + TODO: o Look into using twisted.protocols.postfix.policies classes for controlling concurrent connections and throttling resource consumption. @@ -29,39 +31,69 @@ try: # from twisted.mail import alias from twisted.protocols import postfix from twisted.python import log + from twisted.internet import defer except ImportError: print "This software requires Twisted. Please see the README file" print "for instructions on getting required dependencies." +class LEAPPostFixTCPMapserver(postfix.PostfixTCPMapServer): + def _cbGot(self, value): + if value is None: + self.sendCode(500, postfix.quote("NOT FOUND SRY")) + else: + self.sendCode(200, postfix.quote(value)) + + class AliasResolverFactory(postfix.PostfixTCPMapDeferringDictServerFactory): + + protocol = LEAPPostFixTCPMapserver + def __init__(self, couchdb, *args, **kwargs): postfix.PostfixTCPMapDeferringDictServerFactory.__init__( self, *args, **kwargs) self._cdb = couchdb def _to_str(self, result): + """ + Properly encodes the result string if any. + """ if isinstance(result, unicode): result = result.encode("utf8") if result is None: log.msg("Result not found") return result + def spit_result(self, result): + """ + Formats the return codes in a postfix friendly format. + """ + if result is None: + return None + else: + return defer.succeed(result) + def get(self, key): + """ + Looks up the passed key, but only up to the username id of the key. + + At some point we will have to consider the domain part too. + """ try: log.msg("Processing key: %s" % (key,)) if key.find("@") == -1: - log.msg("Ignoring key since it's not an email address") - return None - - key = key.split("@")[0] - key = key.split("+")[0] + # No proper email address, but we need to continue processing + # the query so postmap is happy. + log.msg("Key it's not an email address") + else: + key = key.split("@")[0] + key = key.split("+")[0] log.msg("Final key to query: %s" % (key,)) d = self._cdb.queryByLoginOrAlias(key) + d.addCallback(self._to_str) + d.addCallback(self.spit_result) d.addErrback(log.err) return d - except: - log.err() - - return None + except Exception as e: + log.err('exception in get: %r' % e) |