11from .message import Message
22
3+
34class Handler :
45 """Handle request"""
56
@@ -14,21 +15,24 @@ def __init__(self, conn: object, addr: str, conf: object, logger: object, db_poo
1415 self .db_pool = db_pool
1516 try :
1617 self .handle ()
17- except Exception as e : # pragma: no cover
18+ except Exception as e : # pragma: no cover
1819 self .logger .exception ('Unhandled Exception: %s' , e )
1920 self .logger .warning ('Received DATA: %s' , self .data )
20- self .send_response ('DUNNO' ) # use DUNNO as accept action, just to distinguish between OK and unhandled exception
21+ self .send_response ('DUNNO' ) # use DUNNO as accept action, just to distinguish between OK and unhandled exception
2122
2223 def handle (self ) -> None :
2324 """Handle request"""
2425 # Read data
25- self .data = self .conn .recv (2048 ).decode ('utf-8' ) # Attention: We only read first 2048 bytes, which is sufficient for our needs
26+ # Attention: We only read first 2048 bytes, which is sufficient for our needs
27+ self .data = self .conn .recv (2048 ).decode ('utf-8' )
2628 if not self .data :
2729 raise Exception ('No data received' )
2830 self .logger .debug ('Received data: %s' , self .data )
2931
3032 # Parse data using a dictionary comprehension
31- request = { key : value for line in self .data .strip ().split ("\n " ) for key , value in [line .strip ().split ('=' , 1 )] if value }
33+ request = {
34+ key : value for line in self .data .strip ().split ("\n " ) for key , value in [line .strip ().split ('=' , 1 )] if value
35+ }
3236 self .logger .debug ('Parsed request: %s' , request )
3337
3438 # Add msgid to logger
@@ -60,17 +64,18 @@ def handle(self) -> None:
6064 )
6165 message .get_ratelimit ()
6266 was_blocked = message .is_blocked ()
63- message .update_ratelimit () # Always update counter, even if quota limit was already reached before (was_blocked)!
64- blocked = message .is_blocked () # ... and make sure we check for blocked after having raised the counter.
67+ message .update_ratelimit () # Always update counter, even if quota limit was already reached before (was_blocked)!
68+ blocked = message .is_blocked () # ... and make sure we check for blocked after having raised the counter.
6569 message .store ()
6670
6771 # Detailed log message in the following format:
68- # TEST1234567: client=unknown[8.8.8.8], helo=myCLIENTPC, sasl_method=PLAIN, sasl_username=test@example.com, recipient_count=1, curr_count=2/1000, status=ACCEPTED
69- log_message = 'client={}[{}], helo={}, sasl_method={}, sasl_username={}, from={}, to={}, recipient_count={}, curr_count={}/{}, status={}{}' .format (
72+ # TEST1234567: client=unknown[8.8.8.8], helo=myCLIENTPC, sasl_method=PLAIN, sasl_username=test@example.com,
73+ # recipient_count=1, curr_count=2/1000, status=ACCEPTED
74+ log_msg = 'client={}[{}], helo={}, sasl_method={}, sasl_username={}, from={}, to={}, recipient_count={}, curr_count={}/{}, status={}{}' .format ( # noqa: E501
7075 message .client_name ,
7176 message .client_address ,
72- request .get ('helo_name' ), # currently not stored in Message object or `messages` table
73- request ['sasl_method' ], # currently not stored in Message object or `messages` table
77+ request .get ('helo_name' ), # currently not stored in Message object or `messages` table
78+ request ['sasl_method' ], # currently not stored in Message object or `messages` table
7479 message .sender ,
7580 message .from_addr ,
7681 message .to_addr ,
@@ -89,11 +94,11 @@ def handle(self) -> None:
8994 self .logger .debug ('Rate limit reached for %s, notifying sender via webhook!' , message .sender )
9095 from .webhook import Webhook
9196 Webhook (self .conf , self .logger , message ).call ()
92- self .logger .warning (log_message )
97+ self .logger .warning (log_msg )
9398 self .send_response ('DEFER_IF_PERMIT ' + self .conf .get ('ACTION_TEXT_BLOCKED' , 'Rate limit reached, retry later' ))
9499 else :
95100 self .logger .debug ('Message ACCEPTED: %s' , message .get_props_description ())
96- self .logger .info (log_message )
101+ self .logger .info (log_msg )
97102 self .send_response ('OK' )
98103
99104 def send_response (self , message : str = 'OK' ) -> None :
@@ -106,9 +111,9 @@ def send_response(self, message: str = 'OK') -> None:
106111
107112 def __del__ (self ):
108113 """Destructor"""
109- self .logger .msgid = None # Reset msgid in logger
114+ self .logger .msgid = None # Reset msgid in logger
110115 # TODO: Do we need to close the cursor as well? (prior to closing the connection)
111116 if self .db is not None :
112- self .db .close () # Close database connection
117+ self .db .close () # Close database connection
113118 self .db = None
114119 self .logger .debug ('Handler destroyed' )
0 commit comments