Skip to content

Commit

Permalink
Changes for BIMI-Selector #1394
Browse files Browse the repository at this point in the history
  • Loading branch information
the-djmaze committed Jan 21, 2024
1 parent 8292c47 commit 58955f7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
9 changes: 8 additions & 1 deletion plugins/avatars/avatars.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@
avatars = new Map,
ncAvatars = new Map,
templateId = 'MailMessageView',
getBimiSelector = msg => {
// Get 's' value out of 'v=BIMI1; s=foo;'
let bimiSelector = msg.headers().valueByName('BIMI-Selector');
bimiSelector = bimiSelector ? bimiSelector.match(/;.*s=([^\s;]+)/)[1] : '';
return bimiSelector || '';
},
getAvatarUid = msg => {
let from = msg.from[0],
bimi = 'pass' == from.dkimStatus ? 1 : 0;
bimi = ('pass' == from.dkimStatus ? 1 : 0) + '-' + getBimiSelector(msg);
return `${bimi}/${from.email.toLowerCase()}`;
},
getAvatar = msg => ncAvatars.get(msg.from[0].email.toLowerCase()) || avatars.get(getAvatarUid(msg)),
Expand Down Expand Up @@ -97,6 +103,7 @@
runQueue();
}, 'Avatar', {
bimi: 'pass' == from.dkimStatus ? 1 : 0,
bimiSelector: getBimiSelector(item[0]),
email: from.email
});
break;
Expand Down
11 changes: 7 additions & 4 deletions plugins/avatars/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ private function JsonAvatar($message) : ?string
public function DoAvatar() : array
{
$bBimi = !empty($this->jsonParam('bimi'));
$sBimiSelector = $this->jsonParam('bimiSelector') ?: '';
$sEmail = $this->jsonParam('email');
$aResult = $this->getAvatar($sEmail, !empty($bBimi));
$aResult = $this->getAvatar($sEmail, $bBimi, $sBimiSelector);
if ($aResult) {
$aResult = [
'type' => $aResult[0],
Expand All @@ -103,7 +104,9 @@ public function DoAvatar() : array
public function ServiceAvatar(string $sServiceName, string $sBimi, string $sEmail)
{
$sEmail = \SnappyMail\Crypt::DecryptUrlSafe($sEmail);
if ($sEmail && ($aResult = $this->getAvatar($sEmail, !empty($sBimi)))) {
$aBimi = \explode('-', $sBimi, 2);
$sBimiSelector = isset($aBimi[1]) ? $aBimi[1] : 'default';
if ($sEmail && ($aResult = $this->getAvatar($sEmail, !empty($aBimi[0]), $sBimiSelector))) {
\header('Content-Type: '.$aResult[0]);
echo $aResult[1];
} else {
Expand Down Expand Up @@ -190,7 +193,7 @@ private static function getServiceIcon(string $sEmail) : ?string
return null;
}

private function getAvatar(string $sEmail, bool $bBimi) : ?array
private function getAvatar(string $sEmail, bool $bBimi, string $sBimiSelector = '') : ?array
{
if (!\strpos($sEmail, '@')) {
return null;
Expand Down Expand Up @@ -235,7 +238,7 @@ private function getAvatar(string $sEmail, bool $bBimi) : ?array
$aUrls = [];

if ($this->Config()->Get('plugin', 'bimi', false)) {
$BIMI = $bBimi ? \SnappyMail\DNS::BIMI($sDomain) : null;
$BIMI = $bBimi ? \SnappyMail\DNS::BIMI($sDomain, $sBimiSelector) : null;
if ($BIMI) {
$aUrls[] = $BIMI;
// $aResult = ['text/uri-list', $BIMI];
Expand Down
2 changes: 2 additions & 0 deletions snappymail/v/0.0.0/app/libraries/MailSo/Mail/MailClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ private function getEnvelopeOrHeadersRequestString() : string
MimeHeader::LIST_UNSUBSCRIBE,
// https://autocrypt.org/level1.html#the-autocrypt-header
MimeHeader::AUTOCRYPT,
// https://www.ietf.org/archive/id/draft-brand-indicators-for-message-identification-04.html#bimi-selector
MimeHeader::BIMI_SELECTOR,
// SPAM
MimeHeader::X_SPAM_STATUS,
MimeHeader::X_SPAM_FLAG,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,8 @@ abstract class Header
// https://autocrypt.org/level1.html#the-autocrypt-header
AUTOCRYPT = 'Autocrypt',

// https://www.ietf.org/archive/id/draft-brand-indicators-for-message-identification-04.html#bimi-selector
BIMI_SELECTOR = 'BIMI-Selector',

LIST_UNSUBSCRIBE = 'List-Unsubscribe';
}
13 changes: 8 additions & 5 deletions snappymail/v/0.0.0/app/libraries/snappymail/dns.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ abstract class DNS
{
/**
* $domain = 'bimigroup.org'
* $selector = 'default'
* Then a TXT lookup is done on 'default._bimi.bimigroup.org'
*/
public static function BIMI(string $domain) : string
public static function BIMI(string $domain, string $selector = 'default') : string
{
$selector = \trim($selector) ?: 'default';
$oCache = \RainLoop\Api::Actions()->Cacher();
$BIMI = $oCache->Get("dns-bimi-{$domain}") ?: null;
$sCacheKey = "dns-bimi-{$domain}-{$selector}";
$BIMI = $oCache->Get($sCacheKey) ?: null;
if ($BIMI) {
$BIMI = \json_decode($BIMI);
if ($BIMI[1] < \time()) {
Expand All @@ -22,12 +25,12 @@ public static function BIMI(string $domain) : string
}
if (null === $BIMI) {
$BIMI = '';
$values = \dns_get_record("default._bimi.{$domain}", \DNS_TXT);
$values = \dns_get_record("{$selector}._bimi.{$domain}", \DNS_TXT);
if ($values) {
foreach ($values as $value) {
if (\str_starts_with($value['txt'], 'v=BIMI1')) {
$BIMI = \preg_replace('/^.+l=([^;]+)(;.*)?$/D', '$1', $value['txt']);
$oCache->Set("dns-bimi-{$domain}", \json_encode([
$oCache->Set($sCacheKey, \json_encode([
$BIMI,
time() + $value['ttl']
]));
Expand All @@ -37,7 +40,7 @@ public static function BIMI(string $domain) : string
}
if (!$BIMI) {
// Don't lookup for 24 hours
$oCache->Set("dns-bimi-{$domain}", \json_encode([
$oCache->Set($sCacheKey, \json_encode([
$BIMI,
time() + 86400
]));
Expand Down

0 comments on commit 58955f7

Please sign in to comment.