Permalink
Browse files

add edit_headers driver

  • Loading branch information...
1 parent 05b0644 commit 219160cff3c9c3be31a82124611d5a56b74bfab1 @JohnDoh committed Jul 28, 2012
Showing with 98 additions and 1 deletion.
  1. +23 −1 README
  2. +22 −0 config.inc.php.dist
  3. +53 −0 drivers/edit_headers.php
View
24 README
@@ -69,6 +69,12 @@ sa_detach: If the message is a Spamassassin spam report with the original email
attached then this is detached and saved in the Inbox, the spam
report is deleted
+edit_headers: Edit the message headers. Headers are edited using preg_replace.
+ WARNING: Be sure to match the entire header line, including the
+ name of the header, and include the ^ and $ and test carefully
+ before use on real messages. This driver alters the message
+ source
+
Spam learning commands
======================
Spamassassin:
@@ -79,4 +85,20 @@ Ham learning commands
=====================
Spamassassin:
'sa-learn --ham --username=%u %f' or
- 'sa-learn --ham --prefs-file=/var/mail/%d/%l/.spamassassin/user_prefs %f'
+ 'sa-learn --ham --prefs-file=/var/mail/%d/%l/.spamassassin/user_prefs %f'
+
+
+edit_headers example config
+===========================
+WARNING: These are simple examples of how to configure the driver options, use
+at your own risk
+
+$rcmail_config['markasjunk2_spam_patterns'] = array(
+ 'patterns' => array('/^(Subject:\s*)(.*)$/m'),
+ 'replacements' => array('$1[SPAM] $2')
+);
+
+$rcmail_config['markasjunk2_ham_patterns'] = array(
+ 'patterns' => array('/^(Subject:\s*)\[SPAM\](.*)$/m'),
+ 'replacements' => array('$1$2')
+);
View
@@ -117,4 +117,26 @@ $rcmail_config['markasjunk2_email_subject'] = 'learn this message as %t';
// Path to SAUserPrefs config file
$rcmail_config['markasjunk2_sauserprefs_config'] = '../sauserprefs/config.inc.php';
+// edit_headers Driver options
+// ---------------------------
+// Patterns to match and replace headers for spam messages
+// Replacement method uses preg_replace - http://www.php.net/manual/function.preg-replace.php
+// WARNING: Be sure to match the entire header line, including the name of the header, also use ^ and $ and the 'm' flag
+// see the README for an example
+// TEST CAREFULLY BEFORE USE ON REAL MESSAGES
+$rcmail_config['markasjunk2_spam_patterns'] = array(
+ 'patterns' => array(),
+ 'replacements' => array()
+ );
+
+// Patterns to match and replace headers for spam messages
+// Replacement method uses preg_replace - http://www.php.net/manual/function.preg-replace.php
+// WARNING: Be sure to match the entire header line, including the name of the header, also use ^ and $ and the 'm' flag
+// see the README for an example
+// TEST CAREFULLY BEFORE USE ON REAL MESSAGES
+$rcmail_config['markasjunk2_ham_patterns'] = array(
+ 'patterns' => array(),
+ 'replacements' => array()
+ );
+
?>
View
@@ -0,0 +1,53 @@
+<?php
+
+/**
+ * Edit headers
+ * @version 1.0
+ * @author Philip Weir
+ */
+
+class markasjunk2_edit_headers
+{
+ public function spam(&$uids)
+ {
+ $this->_edit_headers($uids, true);
+ }
+
+ public function ham(&$uids)
+ {
+ $this->_edit_headers($uids, false);
+ }
+
+ private function _edit_headers(&$uids, $spam)
+ {
+ $rcmail = rcube::get_instance();
+ $args = $spam ? $rcmail->config->get('markasjunk2_spam_patterns') : $rcmail->config->get('markasjunk2_ham_patterns');
+
+ if (sizeof($args['patterns']) == 0)
+ return;
+
+ $mbox = rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_POST);
+
+ $new_uids = array();
+ foreach (explode(",", $uids) as $uid) {
+ $raw_message = $rcmail->storage->get_raw_body($uid);
+ $raw_headers = $rcmail->storage->get_raw_headers($uid);
+
+ $updated_headers = preg_replace($args['patterns'], $args['replacements'], $raw_headers);
+ $raw_message = str_replace($raw_headers, $updated_headers, $raw_message);
+
+ $saved = $rcmail->storage->save_message($mbox, $raw_message);
+
+ if ($saved !== false) {
+ $rcmail->output->command('rcmail_markasjunk2_move', null, $uid);
+ array_push($new_uids, $saved);
+ }
+
+ }
+
+ if (sizeof($new_uids) > 0)
+ $uids = implode(',', $new_uids);
+ }
+}
+
+?>

0 comments on commit 219160c

Please sign in to comment.