77from email .mime .text import MIMEText
88
99from intelmq .lib .bot import Bot
10+ from typing import Optional
1011
1112
1213class SMTPOutputBot (Bot ):
@@ -15,23 +16,26 @@ class SMTPOutputBot(Bot):
1516 mail_from : str = "cert@localhost"
1617 mail_to : str = "{ev[source.abuse_contact]}"
1718 smtp_host : str = "localhost"
18- smtp_password : str = None
19- smtp_port : int = None
20- smtp_username : str = None
19+ smtp_password : Optional [ str ] = None
20+ smtp_port : int = 25
21+ smtp_username : Optional [ str ] = None
2122 ssl : bool = False
2223 starttls : bool = True
2324 subject : str = "Incident in your AS {ev[source.asn]}"
2425 text : str = "Dear network owner,\\ n\\ nWe have been informed that the following device might have security problems.\\ n\\ nYour localhost CERT"
2526
26- username = None
27- password = None
2827 http_verify_cert = True
2928
3029 def init (self ):
3130 if self .ssl :
3231 self .smtp_class = smtplib .SMTP_SSL
3332 else :
3433 self .smtp_class = smtplib .SMTP
34+ if self .http_verify_cert and self .smtp_class is smtplib .SMTP_SSL :
35+ self .kwargs = {'context' : ssl .create_default_context ()}
36+ else :
37+ self .kwargs = {}
38+
3539 if isinstance (self .fieldnames , str ):
3640 self .fieldnames = self .fieldnames .split (',' )
3741
@@ -41,25 +45,20 @@ def process(self):
4145
4246 csvfile = io .StringIO ()
4347 writer = csv .DictWriter (csvfile , fieldnames = self .fieldnames ,
44- quoting = csv .QUOTE_MINIMAL , delimiter = str ( ";" ) ,
48+ quoting = csv .QUOTE_MINIMAL , delimiter = ";" ,
4549 extrasaction = 'ignore' , lineterminator = '\n ' )
4650 writer .writeheader ()
4751 writer .writerow (event )
4852 attachment = csvfile .getvalue ()
4953
50- if self .http_verify_cert and self .smtp_class == smtplib .SMTP_SSL :
51- kwargs = {'context' : ssl .create_default_context ()}
52- else :
53- kwargs = {}
54-
55- with self .smtp_class (self .smtp_host , self .smtp_port , ** kwargs ) as smtp :
54+ with self .smtp_class (self .smtp_host , self .smtp_port , ** self .kwargs ) as smtp :
5655 if self .starttls :
5756 if self .http_verify_cert :
5857 smtp .starttls (context = ssl .create_default_context ())
5958 else :
6059 smtp .starttls ()
61- if self .username and self .password :
62- smtp .login (user = self .username , password = self .password )
60+ if self .smtp_username and self .smtp_password :
61+ smtp .login (user = self .smtp_username , password = self .smtp_password )
6362 msg = MIMEMultipart ()
6463 if self .text is not None :
6564 msg .attach (MIMEText (self .text .format (ev = event )))
0 commit comments