1111Uses the common mail iteration method from the lib file.
1212"""
1313import re
14+
1415from intelmq .lib .utils import unzip
15- from intelmq .lib .exceptions import InvalidArgument
16+ from intelmq .lib .exceptions import InvalidArgument , MissingDependencyError
1617
1718from ._lib import MailCollectorBot
1819
1920
2021class MailAttachCollectorBot (MailCollectorBot ):
2122 """Monitor IMAP mailboxes and retrieve mail attachments"""
23+
2224 attach_regex : str = "csv.zip"
2325 extract_files : bool = True
2426 folder : str = "INBOX"
@@ -28,11 +30,25 @@ class MailAttachCollectorBot(MailCollectorBot):
2830 mail_user : str = "<user>"
2931 rate_limit : int = 60
3032 subject_regex : str = "<subject>"
33+ decrypt_gpg : bool = False
34+ """ Decrypt the attachment with GPG """
35+
36+ gpg_passphrase : str = ""
37+ """ The private key passhrase """
38+
39+ gpg_home : str = ""
40+ """ Change the GPG home directory """
3141
3242 def init (self ):
3343 super ().init ()
3444 if self .attach_regex is None :
3545 raise InvalidArgument ('attach_regex' , expected = 'string' )
46+ if self .decrypt_gpg :
47+ try :
48+ from gnupg import GPG
49+ except ImportError :
50+ raise MissingDependencyError ("python-gnupg" , ">=0.5" )
51+ self ._gpg = GPG (gnupghome = self .gpg_home )
3652
3753 def process_message (self , uid , message ):
3854 seen = False
@@ -63,6 +79,15 @@ def process_message(self, uid, message):
6379 raw_reports = ((attach_filename , attach ['content' ].read ()), )
6480
6581 for file_name , raw_report in raw_reports :
82+ if self .decrypt_gpg :
83+ gpg = self ._gpg .decrypt (
84+ raw_report , passphrase = self .gpg_passphrase
85+ )
86+ if gpg .ok :
87+ raw_report = gpg .data
88+ else :
89+ self .logger .error ('Could not decrypt attachment %s: %s.' , file_name , gpg .status )
90+ continue
6691 report = self .new_report ()
6792 report .add ("raw" , raw_report )
6893 if file_name :
0 commit comments