diff --git a/pyquarantine/cli.py b/pyquarantine/cli.py index 6e6388b..9c21b07 100644 --- a/pyquarantine/cli.py +++ b/pyquarantine/cli.py @@ -271,6 +271,13 @@ def release(quarantines, args): f"{args.quarantine}: released message with id {args.quarantine_id} " f"for {rcpts}") +def copy(quarantines, args): + logger = logging.getLogger(__name__) + quarantine = _get_quarantine(quarantines, args.quarantine, args.debug) + quarantine.copy(args.quarantine_id, args.recipient) + logger.info( + f"{args.quarantine}: sent a copy of message with id {args.quarantine_id} " + f"to {args.recipient}") def delete(quarantines, args): storage = _get_quarantine(quarantines, args.quarantine, args.debug).storage @@ -432,6 +439,27 @@ def main(): help="Release email for all recipients.", action="store_true") quar_release_parser.set_defaults(func=release) + # quarantine copy command + quar_copy_parser = quar_subparsers.add_parser( + "copy", + description="Send a copy of email to another recipient.", + help="Send a copy of email to another recipient.", + formatter_class=formatter_class) + quar_copy_parser.add_argument( + "quarantine_id", + metavar="ID", + help="Quarantine ID.") + quar_copy_parser.add_argument( + "-n", + "--disable-syslog", + dest="syslog", + help="Disable syslog messages.", + action="store_false") + quar_copy_parser_grp.add_argument( + "-t", "--to", + dest="recipient", + help="Release email for one recipient address.") + quar_copy_parser.set_defaults(func=copy) # quarantine delete command quar_delete_parser = quar_subparsers.add_parser( "delete", diff --git a/pyquarantine/storage.py b/pyquarantine/storage.py index a3b45db..7c18020 100644 --- a/pyquarantine/storage.py +++ b/pyquarantine/storage.py @@ -535,6 +535,20 @@ class Quarantine: return recipients + def copy(self, storage_id, recipient): + metadata, msg = self.storage.get_mail(storage_id) + try: + mailer.smtp_send( + self.smtp_host, + self.smtp_port, + metadata["mailfrom"], + recipient, + msg.as_string()) + + except Exception as e: + raise RuntimeError( + f"error while sending message to '{recipient}': {e}") + def execute(self, milter): logger = CustomLogger( self.logger, {"name": self.cfg["name"], "qid": milter.qid})