Browse Source

Move out from remailer modules common to client

master
juga 2 years ago
parent
commit
76eb7eb433
7 changed files with 94 additions and 80 deletions
  1. +5
    -48
      mixnet/common.py
  2. +0
    -0
      mixnet/defaults.py
  3. +0
    -0
      mixnet/exceptions.py
  4. +9
    -8
      mixnet/remailer/cli.py
  5. +51
    -0
      mixnet/remailer/remailer.py
  6. +2
    -2
      tests/conftest.py
  7. +27
    -22
      tests/test_remailer.py

mixnet/remailer/lib.py → mixnet/common.py View File


mixnet/remailer/constants.py → mixnet/defaults.py View File


mixnet/remailer/exceptions.py → mixnet/exceptions.py View File


+ 9
- 8
mixnet/remailer/cli.py View File

@ -6,9 +6,10 @@ import logging.config
import os
import sys
from . import __version__, constants, exceptions, lib
from mixnet import __version__, common, defaults, exceptions
from mixnet.remailer import remailer
logging.config.dictConfig(constants.LOGGING)
logging.config.dictConfig(defaults.LOGGING)
logger = logging.getLogger(__name__)
# coloredlogs.install(level=logger.level)
@ -66,9 +67,9 @@ def main():
logger.debug("Arguments: %s", args)
if args.apphome:
lib.set_env(args.apphome, True)
common.set_env(args.apphome, True)
else:
lib.set_env()
common.set_env()
if args.gnupghome:
os.environ["GNUPGHOME"] = args.gnupghome
@ -76,7 +77,7 @@ def main():
if args.keyring:
try:
lib.import_keys(args.keyring.read())
common.import_keys(args.keyring.read())
except (exceptions.PepKeyNotFound, Exception) as e:
logger.error(e)
sys.exit(1)
@ -88,7 +89,7 @@ def main():
logger.error("Please, provides also `mynameaddress` argument.")
sys.exit(1)
try:
lib.set_my_identity(args.mynameaddress, args.myfpr)
common.set_my_identity(args.mynameaddress, args.myfpr)
except (exceptions.NoEmailAddress, exceptions.PepKeyNotFound) as e:
logger.error(e)
sys.exit(1)
@ -96,7 +97,7 @@ def main():
if args.mynameaddress:
try:
lib.set_my_identity(args.mynameaddress)
common.set_my_identity(args.mynameaddress)
except exceptions.NoEmailAddress as e:
logger.error(e)
sys.exit(1)
@ -110,7 +111,7 @@ def main():
mail_str = fd.read()
logger.debug("Received Email:\n%s", mail_str)
try:
lib.process_email(mail_str)
remailer.process_email(mail_str)
except (exceptions.EmailNotDecrypted, exceptions.EmailNotTrusted) as e:
logger.error(e)
sys.exit(1)


+ 51
- 0
mixnet/remailer/remailer.py View File

@ -0,0 +1,51 @@
"""Mixnet remailer library."""
import email
import logging
import os
from mixnet import common, defaults, exceptions
logger = logging.getLogger(__name__)
def decrypt_email(email_str):
"""Decrypt an incoming Email and forward it to the next hop.
There's not need to check that the recipient of the Email is this
remailer, if it can be decrypted, it'll be forwarded.
The decrypted Email, the one to forward, won't probably have this
remailer as sender and does not need to.
:param str email_str: incoming Email
:returns email.message.Message: the Email to forward
"""
import pEp
# XXX: This returns ValueError
# msg = pEp.incoming_message(email_str)
msg = pEp.Message(email_str)
# Set direction
msg.dir = defaults.INCOMING_MSG
# XXX: Why doesn't detect is encrypted?
# if msg.enc_format == 0:
dec_msg, key_list, rating, _r = msg.decrypt()
# XXX: r is always 0?
if not key_list:
raise exceptions.EmailNotDecrypted
# XXX: Add defaults to peppythonadapter
# XXX: is it yellow >= 0?
if pEp.color(rating) < 0:
raise exceptions.EmailNotTrusted
email_to_send = email.message_from_string(dec_msg.longmsg)
logger.info("Next hop: %s", email_to_send["To"])
return email_to_send
def process_email(email_str):
logger.debug("$HOME=%s", os.environ["HOME"])
gnupg_home = os.environ.get("GNUPGHOME", None)
if gnupg_home:
logger.debug("$GNUPGHOME=%s", os.environ["GNUPGHOME"])
dec_mail = decrypt_email(email_str)
common.send_email(dec_mail)

+ 2
- 2
tests/conftest.py View File

@ -1,7 +1,7 @@
"""pytest configuration for the unit tests."""
import pytest
from mixnet.remailer import lib
from mixnet import common
@pytest.fixture()
@ -75,7 +75,7 @@ def email_from_bob_to_carol(datadir):
@pytest.fixture(scope="function")
def set_env(tmpdir):
lib.set_env(str(tmpdir))
common.set_env(str(tmpdir))
@pytest.fixture(scope="function")


tests/test_lib.py → tests/test_remailer.py View File


Loading…
Cancel
Save