postfix.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # -*- test-case-name: twisted.test.test_postfix -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Postfix mail transport agent related protocols.
  6. """
  7. from __future__ import annotations
  8. import sys
  9. from collections import UserDict
  10. from typing import TYPE_CHECKING, Union
  11. from urllib.parse import quote as _quote, unquote as _unquote
  12. from twisted.internet import defer, protocol
  13. from twisted.protocols import basic, policies
  14. from twisted.python import log
  15. # urllib's quote functions just happen to match
  16. # the postfix semantics.
  17. def quote(s):
  18. quoted = _quote(s)
  19. if isinstance(quoted, str):
  20. quoted = quoted.encode("ascii")
  21. return quoted
  22. def unquote(s):
  23. if isinstance(s, bytes):
  24. s = s.decode("ascii")
  25. quoted = _unquote(s)
  26. return quoted.encode("ascii")
  27. class PostfixTCPMapServer(basic.LineReceiver, policies.TimeoutMixin):
  28. """
  29. Postfix mail transport agent TCP map protocol implementation.
  30. Receive requests for data matching given key via lineReceived,
  31. asks it's factory for the data with self.factory.get(key), and
  32. returns the data to the requester. None means no entry found.
  33. You can use postfix's postmap to test the map service::
  34. /usr/sbin/postmap -q KEY tcp:localhost:4242
  35. """
  36. timeout = 600
  37. delimiter = b"\n"
  38. def connectionMade(self):
  39. self.setTimeout(self.timeout)
  40. def sendCode(self, code, message=b""):
  41. """
  42. Send an SMTP-like code with a message.
  43. """
  44. self.sendLine(str(code).encode("ascii") + b" " + message)
  45. def lineReceived(self, line):
  46. self.resetTimeout()
  47. try:
  48. request, params = line.split(None, 1)
  49. except ValueError:
  50. request = line
  51. params = None
  52. try:
  53. f = getattr(self, "do_" + request.decode("ascii"))
  54. except AttributeError:
  55. self.sendCode(400, b"unknown command")
  56. else:
  57. try:
  58. f(params)
  59. except BaseException:
  60. excInfo = str(sys.exc_info()[1]).encode("ascii")
  61. self.sendCode(400, b"Command " + request + b" failed: " + excInfo)
  62. def do_get(self, key):
  63. if key is None:
  64. self.sendCode(400, b"Command 'get' takes 1 parameters.")
  65. else:
  66. d = defer.maybeDeferred(self.factory.get, key)
  67. d.addCallbacks(self._cbGot, self._cbNot)
  68. d.addErrback(log.err)
  69. def _cbNot(self, fail):
  70. msg = fail.getErrorMessage().encode("ascii")
  71. self.sendCode(400, msg)
  72. def _cbGot(self, value):
  73. if value is None:
  74. self.sendCode(500)
  75. else:
  76. self.sendCode(200, quote(value))
  77. def do_put(self, keyAndValue):
  78. if keyAndValue is None:
  79. self.sendCode(400, b"Command 'put' takes 2 parameters.")
  80. else:
  81. try:
  82. key, value = keyAndValue.split(None, 1)
  83. except ValueError:
  84. self.sendCode(400, b"Command 'put' takes 2 parameters.")
  85. else:
  86. self.sendCode(500, b"put is not implemented yet.")
  87. if TYPE_CHECKING or sys.version_info >= (3, 9):
  88. _PostfixTCPMapDict = UserDict[bytes, Union[str, bytes]]
  89. else:
  90. _PostfixTCPMapDict = UserDict
  91. class PostfixTCPMapDictServerFactory(_PostfixTCPMapDict, protocol.ServerFactory):
  92. """
  93. An in-memory dictionary factory for PostfixTCPMapServer.
  94. """
  95. protocol = PostfixTCPMapServer
  96. class PostfixTCPMapDeferringDictServerFactory(protocol.ServerFactory):
  97. """
  98. An in-memory dictionary factory for PostfixTCPMapServer.
  99. """
  100. protocol = PostfixTCPMapServer
  101. def __init__(self, data=None):
  102. self.data = {}
  103. if data is not None:
  104. self.data.update(data)
  105. def get(self, key):
  106. return defer.succeed(self.data.get(key))