aboutsummaryrefslogtreecommitdiff
path: root/b4/__init__.py
diff options
context:
space:
mode:
authorKonstantin Ryabitsev <konstantin@linuxfoundation.org>2021-10-19 17:34:17 -0400
committerKonstantin Ryabitsev <konstantin@linuxfoundation.org>2021-10-19 17:34:17 -0400
commit6a212b5524903c170d420763c9c08a5444745281 (patch)
tree305c879817044b12b43bf916c20b50ff28418ef8 /b4/__init__.py
parenta57ec40ef6d57b5aea62fbd1c814175d1da3fd23 (diff)
downloadb4-6a212b5524903c170d420763c9c08a5444745281.tar.gz
Initial implementation of native mail sending
I'm felling comfortable that "b4 ty" is sufficiently mature at this point to implement sending thank-yous directly. This is only the initial implementation that covers only the very basic parts of git's sendemail configuration options, but this should actually cover 90% of cases if not more. One important caveat -- I moved the "b4 ty -s" flag to be "b4 ty -t" in order to disambiguate it from the capital -S (that actually does the sending). Since "b4 ty" is still marked as an experimental feature, I feel we can do this without much impact. Signed-off-by: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
Diffstat (limited to 'b4/__init__.py')
-rw-r--r--b4/__init__.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/b4/__init__.py b/b4/__init__.py
index f8766d5..adbcf7d 100644
--- a/b4/__init__.py
+++ b/b4/__init__.py
@@ -130,6 +130,8 @@ DEFAULT_CONFIG = {
# git-config for gpg.program, and if that's not set,
# we'll use "gpg" and hope for the better
'gpgbin': None,
+ # When sending mail, use this sendemail identity configuration
+ 'sendemail-identity': None,
}
# This is where we store actual config
@@ -2480,3 +2482,54 @@ def read_template(tptfile):
continue
tpt += line
return tpt
+
+
+def get_smtp(identity: Optional[str] = None):
+ import smtplib
+ if identity:
+ sconfig = get_config_from_git(rf'sendemail\.{identity}\..*')
+ sectname = f'sendemail.{identity}'
+ else:
+ sconfig = get_config_from_git(rf'sendemail\..*')
+ sectname = 'sendemail'
+ if not len(sconfig):
+ raise smtplib.SMTPException('Unable to find %s settings in any applicable git config' % sectname)
+
+ # Limited support for smtp settings to begin with, but should cover the vast majority of cases
+ fromaddr = sconfig.get('from')
+ server = sconfig.get('smtpserver', 'localhost')
+ port = sconfig.get('smtpserverport', 0)
+ try:
+ port = int(port)
+ except ValueError:
+ raise smtplib.SMTPException('Invalid smtpport entry in %s' % sectname)
+
+ encryption = sconfig.get('smtpencryption')
+ # We only authenticate if we have encryption
+ if encryption:
+ if encryption in ('tls', 'starttls'):
+ # We do startssl
+ smtp = smtplib.SMTP(server, port)
+ # Introduce ourselves
+ smtp.ehlo()
+ # Start encryption
+ smtp.starttls()
+ # Introduce ourselves again to get new criteria
+ smtp.ehlo()
+ elif encryption in ('ssl', 'smtps'):
+ # We do TLS from the get-go
+ smtp = smtplib.SMTP_SSL(server, port)
+ else:
+ raise smtplib.SMTPException('Unclear what to do with smtpencryption=%s' % encryption)
+
+ # If we got to this point, we should do authentication.
+ auser = sconfig.get('smtpuser')
+ apass = sconfig.get('smtppass')
+ if auser and apass:
+ # Let any exceptions bubble up
+ smtp.login(auser, apass)
+ else:
+ # We assume you know what you're doing if you don't need encryption
+ smtp = smtplib.SMTP(server, port)
+
+ return smtp, fromaddr