sync
authorAlan <alan@roojs.com>
Thu, 2 Nov 2023 08:02:03 +0000 (16:02 +0800)
committerAlan <alan@roojs.com>
Thu, 2 Nov 2023 08:02:03 +0000 (16:02 +0800)
DataObjects/Core_notify_sender.php [new file with mode: 0644]
sql/core_notify_sender.sql [new file with mode: 0644]

diff --git a/DataObjects/Core_notify_sender.php b/DataObjects/Core_notify_sender.php
new file mode 100644 (file)
index 0000000..e40e45b
--- /dev/null
@@ -0,0 +1,124 @@
+<?php
+/**
+ * Table Definition for core_notify_sender
+ */
+class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
+
+class Pman_Core_DataObjects_Core_notify_sender extends DB_DataObject 
+{
+    ###START_AUTOCODE
+    /* the code below is auto generated do not remove the above tag */
+
+    public $__table = 'core_notify_sender';    // table name
+    public $id;                              // int(11)  not_null primary_key auto_increment
+    public $email;
+    public $poolname;
+    public $is_active;
+    public $priority;
+    
+    
+    
+    function emailToSender($email , $notify)
+    {
+        require_once 'Mail/RFC822.php';
+        $parser = new Mail_RFC822();
+        $addresses = $parser->parseAddressList( $email['headers']['From'], 'localhost', false);
+        if (is_a($addresses, 'PEAR_Error')) {
+            return false;
+        }
+        $from_addr = $addresses[0];
+        //print_r($email['headers']['From']);         print_R($from_addr);exit;
+
+        $from = $from_addr->mailbox . '@' . $from_addr->host;
+
+        $ns = DB_DataObject::Factory($this->tableName());
+        $ns->setFrom(array(
+            'email' => $from,
+            'poolname' => $notify->tableName()
+        ));
+        if (!$ns->find(true)) {
+            $ns->is_active = 1;
+            $ns->priority = 1;
+            $ns->insert();
+            return false;
+        }
+        return $ns;
+    }
+    
+    
+    function filterEmail($email, $notify)
+    {
+        //
+        if (empty($email['headers']['From']) || empty($notify)) {
+            return $email;
+        }
+        $bits = explode('@', $notify->to_email);
+        $to_dom = DB_DataObject::factory('core_domain')->loadOrCreate($bits[1]);
+        
+        
+        $ns = $this->emailToSender($email, $notify);
+        if ($ns == false) {
+            return $email;
+        }
+        // is it blacklisted?
+        $bl = DB_DAtaObject::Factory('core_notify_sender_blacklist');
+        $bl->setFrom(array(
+                'sender_id'=> $ns->id,
+                'domain_id' => $to_dom->id
+        ));
+        if (!$bl->count()) {
+            return $email;
+        }
+        // it's blacklisted..
+        // try finding alternative.
+        $bl = DB_DAtaObject::Factory('core_notify_sender_blacklist');
+        $bl->domain_id = $to_dom->id;
+        $bad_ids = $bl->fetchAll('sender_id');
+        
+        $ns = DB_DataObject::Factory($this->tableName());
+        $ns->setFrom(array(
+            'email' => $from,
+            'is_active' => 1,
+        ));
+        $ns->whereAddIn('!id', $bad_ids, 'int');
+        if (!$ns->count()){
+            return $email; // no alternative available
+        }
+        $ns->limit(1);
+        $ns->find(true);
+        $mail['headers']['From'] = $from_addr->personal . ' <' . $ns->email .'>';
+        
+        return $mail;
+        
+        //check blacklist for 
+        
+         
+        
+    }
+    
+    function checkSmtpResponse($email, $notify, $errmsg)
+    {
+        $bl = DB_DataObject::factory('core_notify_sender_blacklist');
+        if (!$bl->messageIsBlacklisted($errmsg)) {
+            return; // ok.
+        }
+        // create a record.
+        
+        $bits = explode('@', $notify->to_email);
+        $to_dom = DB_DataObject::factory('core_domain')->loadOrCreate($bits[1]);
+        
+        $ns = $this->emailToSender($email);
+        $bl->setFrom(array(
+            'sender_id' => $ns->id,
+            'domain_id' => $to_dom->id,
+        ));
+        if ($bl->count()) {
+            return;
+        }
+        $bl->error_msg = $errmsg;
+        $bl->added_dt = $bl->sqlValue('NOW()');
+        $bl->insert();
+        
+    }
+    
+}
\ No newline at end of file
diff --git a/sql/core_notify_sender.sql b/sql/core_notify_sender.sql
new file mode 100644 (file)
index 0000000..4d84c45
--- /dev/null
@@ -0,0 +1,10 @@
+CREATE  TABLE core_notify_sender (
+    id INT(11) NOT NULL AUTO_INCREMENT ,
+    email VARCHAR(254) NOT NULL DEFAULT '',
+    is_active INT(4) NOT NULL DEFAULT 0,
+    poolname VARCHAR(128) NOT NULL DEFAULT '',
+    priority INT NOT NULL DEFAULT 0,
+    PRIMARY KEY (id)
+) ENGINE=InnoDB;;
+
+ALTER TABLE core_notify_sender ADD INDEX lookup (email,poolname,is_active);