Changeset View
Changeset View
Standalone View
Standalone View
src/app/Policy/Greylist/Request.php
Show First 20 Lines • Show All 107 Lines • ▼ Show 20 Lines | public function shouldDefer() | ||||
$enabled = true; | $enabled = true; | ||||
if ($recipient) { | if ($recipient) { | ||||
$enabled = $recipient->getSetting('greylist_enabled') !== 'false'; | $enabled = $recipient->getSetting('greylist_enabled') !== 'false'; | ||||
} | } | ||||
// FIXME: Shouldn't we bail-out (return early) if there's no $recipient? | // FIXME: Shouldn't we bail-out (return early) if there's no $recipient? | ||||
// the following block is to maintain statistics and state ... | // the following block is to maintain statistics and state ... | ||||
$entries = Connect::where( | |||||
[ | |||||
'sender_domain' => $this->senderDomain, | |||||
'net_id' => $this->netID, | |||||
'net_type' => $this->netType | |||||
] | |||||
) | |||||
->whereDate('updated_at', '>=', $this->timestamp->copy()->subDays(7)); | |||||
// determine if the sender domain is a whitelist from this network | // determine if the sender domain is a whitelist from this network | ||||
$this->whitelist = Whitelist::where( | $this->whitelist = Whitelist::where( | ||||
[ | [ | ||||
'sender_domain' => $this->senderDomain, | 'sender_domain' => $this->senderDomain, | ||||
'net_id' => $this->netID, | 'net_id' => $this->netID, | ||||
'net_type' => $this->netType | 'net_type' => $this->netType | ||||
] | ] | ||||
)->first(); | )->first(); | ||||
$cutoffDate = $this->timestamp->copy()->subDays(7); | |||||
if ($this->whitelist) { | if ($this->whitelist) { | ||||
if ($this->whitelist->updated_at < $this->timestamp->copy()->subMonthsWithoutOverflow(1)) { | if ($this->whitelist->updated_at < $this->timestamp->copy()->subMonthsWithoutOverflow(1)) { | ||||
$this->whitelist->delete(); | $this->whitelist->delete(); | ||||
} else { | } else { | ||||
$this->whitelist->updated_at = $this->timestamp; | $this->whitelist->updated_at = $this->timestamp; | ||||
$this->whitelist->save(['timestamps' => false]); | $this->whitelist->save(['timestamps' => false]); | ||||
$entries->update( | Connect::where( | ||||
[ | |||||
'sender_domain' => $this->senderDomain, | |||||
'net_id' => $this->netID, | |||||
'net_type' => $this->netType, | |||||
'greylisting' => true | |||||
] | |||||
) | |||||
->whereDate('updated_at', '>=', $cutoffDate) | |||||
->update( | |||||
[ | [ | ||||
'greylisting' => false, | 'greylisting' => false, | ||||
'updated_at' => $this->timestamp | 'updated_at' => $this->timestamp | ||||
] | ] | ||||
); | ); | ||||
return false; | return false; | ||||
} | } | ||||
} else { | } else { | ||||
if ($entries->count() >= 5) { | $count = Connect::where( | ||||
[ | |||||
'sender_domain' => $this->senderDomain, | |||||
'net_id' => $this->netID, | |||||
'net_type' => $this->netType | |||||
] | |||||
) | |||||
->whereDate('updated_at', '>=', $cutoffDate) | |||||
->limit(5)->count(); | |||||
// Automatically create a whitelist if we have at least 5 messages from the sender | |||||
if ($count >= 5) { | |||||
$this->whitelist = Whitelist::create( | $this->whitelist = Whitelist::create( | ||||
[ | [ | ||||
'sender_domain' => $this->senderDomain, | 'sender_domain' => $this->senderDomain, | ||||
'net_id' => $this->netID, | 'net_id' => $this->netID, | ||||
'net_type' => $this->netType, | 'net_type' => $this->netType, | ||||
'created_at' => $this->timestamp, | 'created_at' => $this->timestamp, | ||||
'updated_at' => $this->timestamp | 'updated_at' => $this->timestamp | ||||
] | ] | ||||
); | ); | ||||
$entries->update( | Connect::where( | ||||
[ | |||||
'sender_domain' => $this->senderDomain, | |||||
'net_id' => $this->netID, | |||||
'net_type' => $this->netType, | |||||
'greylisting' => true | |||||
] | |||||
) | |||||
->whereDate('updated_at', '>=', $cutoffDate) | |||||
->update( | |||||
[ | [ | ||||
'greylisting' => false, | 'greylisting' => false, | ||||
'updated_at' => $this->timestamp | 'updated_at' => $this->timestamp | ||||
] | ] | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
// TODO: determine if the sender (individual) is a whitelist | // TODO: determine if the sender (individual) is a whitelist | ||||
// TODO: determine if the sender is a penpal of any of the recipients. First recipient wins. | // TODO: determine if the sender is a penpal of any of the recipients. First recipient wins. | ||||
if (!$enabled) { | if (!$enabled) { | ||||
▲ Show 20 Lines • Show All 103 Lines • Show Last 20 Lines |