Ticket #5725: publicstate.diff

File publicstate.diff, 4.6 KB (added by itamar, 4 years ago)

Patch to twisted.mail.smtp

  • smtp.py

     
    10091009        self.code = -1
    10101010        self.log = util.LineLog(logsize)
    10111011
     1012    def setResponseHandlers(self, expected, succeeded, failed):
     1013        """
     1014        Set the functions that will handle successful and failed responses.
     1015
     1016        @param expected: A C{list} of C{int}, response codes indicating success.
     1017
     1018        @param succeeded: A callable that takes two arguments, the response
     1019            code (an C{int}) and a C{bytes} description, which will be called
     1020            if the response code matches the codes in the expected list.
     1021
     1022        @param failed: A callable that takes two arguments, the response code
     1023            (an C{int}) and a C{bytes} description, which will be called if
     1024            the response code does not matche the expected list.
     1025        """
     1026        self._expected = expected
     1027        self._okresponse = succeeded
     1028        self._failresponse = failed
     1029
    10121030    def sendLine(self, line):
    10131031        # Log sendLine only if you are in debug mode for performance
    10141032        if self.debug:
     
    10181036
    10191037    def connectionMade(self):
    10201038        self.setTimeout(self.timeout)
     1039        self.setResponseHandlers([220], self.smtpState_helo,
     1040                                 self.smtpConnectionFailed)
    10211041
    1022         self._expected = [ 220 ]
    1023         self._okresponse = self.smtpState_helo
    1024         self._failresponse = self.smtpConnectionFailed
    1025 
    10261042    def connectionLost(self, reason=protocol.connectionDone):
    10271043        """We are no longer connected"""
    10281044        self.setTimeout(None)
     
    10801096
    10811097    def smtpState_helo(self, code, resp):
    10821098        self.sendLine('HELO ' + self.identity)
    1083         self._expected = SUCCESS
    1084         self._okresponse = self.smtpState_from
     1099        self.setResponseHandlers(SUCCESS, self.smtpState_from,
     1100                                 self.smtpConnectionFailed)
    10851101
    10861102    def smtpState_from(self, code, resp):
    10871103        self._from = self.getMailFrom()
    1088         self._failresponse = self.smtpTransferFailed
    10891104        if self._from is not None:
    10901105            self.sendLine('MAIL FROM:%s' % quoteaddr(self._from))
    1091             self._expected = [250]
    1092             self._okresponse = self.smtpState_to
     1106            self.setResponseHandlers([250], self.smtpState_to,
     1107                                     self.smtpTransferFailed)
    10931108        else:
    10941109            # All messages have been sent, disconnect
    10951110            self._disconnectFromServer()
     
    11011116        self.toAddresses = iter(self.getMailTo())
    11021117        self.toAddressesResult = []
    11031118        self.successAddresses = []
    1104         self._okresponse = self.smtpState_toOrData
    1105         self._expected = xrange(0,1000)
     1119        self.setResponseHandlers(xrange(0,1000), self.smtpState_toOrData,
     1120                                 self.smtpTransferFailed)
    11061121        self.lastAddress = None
    11071122        return self.smtpState_toOrData(0, '')
    11081123
     
    11161131        except StopIteration:
    11171132            if self.successAddresses:
    11181133                self.sendLine('DATA')
    1119                 self._expected = [ 354 ]
    1120                 self._okresponse = self.smtpState_data
     1134                self.setResponseHandlers([354], self.smtpState_data,
     1135                                         self.smtpTransferFailed)
    11211136            else:
    11221137                return self.smtpState_msgSent(code,'No recipients accepted')
    11231138        else:
     
    11301145        def ebTransfer(err):
    11311146            self.sendError(err.value)
    11321147        d.addCallbacks(self.finishedFileTransfer, ebTransfer)
    1133         self._expected = SUCCESS
    1134         self._okresponse = self.smtpState_msgSent
     1148        self.setResponseHandlers(SUCCESS, self.smtpState_msgSent,
     1149                                 self.smtpTransferFailed)
    11351150
    1136 
    11371151    def smtpState_msgSent(self, code, resp):
    11381152        if self._from is not None:
    11391153            self.sentMail(code, resp, len(self.successAddresses),
     
    11421156        self.toAddressesResult = []
    11431157        self._from = None
    11441158        self.sendLine('RSET')
    1145         self._expected = SUCCESS
    1146         self._okresponse = self.smtpState_from
     1159        self.setResponseHandlers(SUCCESS, self.smtpState_from,
     1160                                 self.smtpConnectionFailed)
    11471161
    11481162    ##
    11491163    ## Helpers for FileSender
     
    12171231        raise NotImplementedError
    12181232
    12191233    def _disconnectFromServer(self):
    1220         self._expected = xrange(0, 1000)
    1221         self._okresponse = self.smtpState_disconnect
     1234        self.setResponseHandlers(xrange(0, 1000), self.smtpState_disconnect,
     1235                                 self.smtpState_disconnect)
    12221236        self.sendLine('QUIT')
    12231237
    12241238