Import Ruty
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AmavisD Blacklist driver
|
||||
*
|
||||
* @version 1.0
|
||||
* @requires Amacube plugin
|
||||
*
|
||||
* @author Der-Jan
|
||||
*
|
||||
* Copyright (C) 2014 Der-Jan
|
||||
*
|
||||
* This driver is part of the MarkASJunk plugin for Roundcube.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Roundcube. If not, see https://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
/* # 'SELECT wb'.
|
||||
* # ' FROM wblist JOIN mailaddr ON wblist.sid=mailaddr.id'.
|
||||
* # ' WHERE wblist.rid=? AND mailaddr.email IN (%k)'.
|
||||
* # ' ORDER BY mailaddr.priority DESC';
|
||||
*/
|
||||
|
||||
class markasjunk_amavis_blacklist
|
||||
{
|
||||
private $user_email = '';
|
||||
|
||||
public function spam($uids, $src_mbox, $dst_mbox)
|
||||
{
|
||||
$this->_do_list($uids, true);
|
||||
}
|
||||
|
||||
public function ham($uids, $src_mbox, $dst_mbox)
|
||||
{
|
||||
$this->_do_list($uids, false);
|
||||
}
|
||||
|
||||
private function _do_list($uids, $spam)
|
||||
{
|
||||
$rcube = rcube::get_instance();
|
||||
$this->user_email = $rcube->user->data['username'];
|
||||
|
||||
$config_file = $rcube->config->get('markasjunk_amacube_config');
|
||||
|
||||
if (is_file($config_file) && !$rcube->config->load_from_file($config_file)) {
|
||||
rcube::raise_error([
|
||||
'code' => 527, 'file' => __FILE__, 'line' => __LINE__,
|
||||
'message' => "Failed to load config from $config_file"
|
||||
], true, false
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$db = rcube_db::factory($rcube->config->get('amacube_db_dsn'), '', true);
|
||||
$db->set_debug((bool) $rcube->config->get('sql_debug'));
|
||||
$db->db_connect('w');
|
||||
|
||||
$debug = $rcube->config->get('markasjunk_debug');
|
||||
|
||||
// check DB connections and exit on failure
|
||||
if ($err_str = $db->is_error()) {
|
||||
rcube::raise_error([
|
||||
'code' => 603,
|
||||
'type' => 'db',
|
||||
'message' => $err_str
|
||||
], false, true
|
||||
);
|
||||
}
|
||||
|
||||
$sql_result = $db->query("SELECT `id` FROM `users` WHERE `email` = ?", $this->user_email);
|
||||
if ($sql_result && ($res_array = $db->fetch_assoc($sql_result))) {
|
||||
$rid = $res_array['id'];
|
||||
}
|
||||
else {
|
||||
if ($debug) {
|
||||
rcube::write_log('markasjunk', $this->user_email . ' not found in users table');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($uids as $uid) {
|
||||
$message = new rcube_message($uid);
|
||||
$email = $message->sender['mailto'];
|
||||
|
||||
// skip invalid emails
|
||||
if (!rcube_utils::check_email($email, false)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$sql_result = $db->query("SELECT `id` FROM `mailaddr` WHERE `email` = ? ORDER BY `priority` DESC", $email);
|
||||
|
||||
if ($sql_result && ($res_array = $db->fetch_assoc($sql_result))) {
|
||||
$sid = $res_array['id'];
|
||||
}
|
||||
else {
|
||||
if ($debug) {
|
||||
rcube::write_log('markasjunk', "$email not found in mailaddr table - add it");
|
||||
}
|
||||
|
||||
$sql_result = $db->query("INSERT INTO `mailaddr` ( `priority`, `email` ) VALUES ( 20, ? )", $email);
|
||||
|
||||
if ($sql_result) {
|
||||
$sid = $db->insert_id();
|
||||
}
|
||||
else {
|
||||
if ($debug) {
|
||||
rcube::write_log('markasjunk', "Cannot add $email to mailaddr table: " . $db->is_error($sql_result));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$wb = '';
|
||||
$sql_result = $db->query("SELECT `wb` FROM `wblist` WHERE `sid` = ? AND `rid` =?", $sid, $rid);
|
||||
|
||||
if ($sql_result && ($res_array = $db->fetch_assoc($sql_result))) {
|
||||
$wb = $res_array['wb'];
|
||||
}
|
||||
|
||||
if (!$wb || (!$spam && preg_match('/^([BbNnFf])[\s]*\z/', $wb)) || ($spam && preg_match('/^([WwYyTt])[\s]*\z/', $wb))) {
|
||||
$newwb = 'w';
|
||||
|
||||
if ($spam) {
|
||||
$newwb = 'b';
|
||||
}
|
||||
|
||||
if ($wb) {
|
||||
$sql_result = $db->query("UPDATE `wblist` SET `wb` = ? WHERE `sid` = ? AND `rid` = ?",
|
||||
$newwb, $sid, $rid);
|
||||
}
|
||||
else {
|
||||
$sql_result = $db->query("INSERT INTO `wblist` (`sid`, `rid`, `wb`) VALUES (?, ?, ?)",
|
||||
$sid, $rid, $newwb);
|
||||
}
|
||||
|
||||
if (!$sql_result) {
|
||||
if ($debug) {
|
||||
rcube::write_log('markasjunk', "Cannot update wblist for user {$this->user_email} with $email");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Command line learn driver
|
||||
*
|
||||
* @version 3.1
|
||||
*
|
||||
* @author Philip Weir
|
||||
* Patched by Julien Vehent to support DSPAM
|
||||
* Enhanced support for DSPAM by Stevan Bajic <stevan@bajic.ch>
|
||||
*
|
||||
* Copyright (C) 2009-2018 Philip Weir
|
||||
*
|
||||
* This driver is part of the MarkASJunk plugin for Roundcube.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Roundcube. If not, see https://www.gnu.org/licenses/.
|
||||
*/
|
||||
class markasjunk_cmd_learn
|
||||
{
|
||||
public function spam($uids, $src_mbox, $dst_mbox)
|
||||
{
|
||||
$this->_do_salearn($uids, true, $src_mbox);
|
||||
}
|
||||
|
||||
public function ham($uids, $src_mbox, $dst_mbox)
|
||||
{
|
||||
$this->_do_salearn($uids, false, $src_mbox);
|
||||
}
|
||||
|
||||
private function _do_salearn($uids, $spam, $src_mbox)
|
||||
{
|
||||
$rcube = rcube::get_instance();
|
||||
$temp_dir = realpath($rcube->config->get('temp_dir'));
|
||||
$command = $rcube->config->get($spam ? 'markasjunk_spam_cmd' : 'markasjunk_ham_cmd');
|
||||
$debug = $rcube->config->get('markasjunk_debug');
|
||||
|
||||
if (!$command) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (strpos($command, '%h') !== false) {
|
||||
preg_match_all('/%h:([\w_-]+)/', $command, $header_names, PREG_SET_ORDER);
|
||||
$header_names = array_column($header_names, 1);
|
||||
}
|
||||
|
||||
// backwards compatibility %xds removed in markasjunk v1.12
|
||||
$command = str_replace('%xds', '%h:x-dspam-signature', $command);
|
||||
$command = str_replace('%u', escapeshellarg($_SESSION['username']), $command);
|
||||
$command = str_replace('%l', escapeshellarg($rcube->user->get_username('local')), $command);
|
||||
$command = str_replace('%d', escapeshellarg($rcube->user->get_username('domain')), $command);
|
||||
if (strpos($command, '%i') !== false) {
|
||||
$identity = $rcube->user->get_identity();
|
||||
$command = str_replace('%i', escapeshellarg($identity['email']), $command);
|
||||
}
|
||||
|
||||
foreach ($uids as $uid) {
|
||||
// reset command for next message
|
||||
$tmp_command = $command;
|
||||
|
||||
if (strpos($tmp_command, '%s') !== false) {
|
||||
$message = new rcube_message($uid);
|
||||
$tmp_command = str_replace('%s', escapeshellarg($message->sender['mailto']), $tmp_command);
|
||||
}
|
||||
|
||||
if (!empty($header_names)) {
|
||||
$storage = $rcube->get_storage();
|
||||
$storage->check_connection();
|
||||
$headers = $storage->conn->fetchHeader($src_mbox, $uid, true, false, $header_names);
|
||||
|
||||
foreach ($header_names as $header) {
|
||||
$val = null;
|
||||
if ($headers) {
|
||||
$val = $headers->get($header);
|
||||
$val = is_array($val) ? array_first($val) : $val;
|
||||
}
|
||||
|
||||
if (!empty($val)) {
|
||||
$tmp_command = str_replace('%h:' . $header, escapeshellarg($val), $tmp_command);
|
||||
}
|
||||
else {
|
||||
if ($debug) {
|
||||
rcube::write_log('markasjunk', "header {$header} not found in message {$src_mbox}/{$uid}");
|
||||
}
|
||||
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (strpos($command, '%f') !== false) {
|
||||
$tmpfname = tempnam($temp_dir, 'rcmSALearn');
|
||||
file_put_contents($tmpfname, $rcube->storage->get_raw_body($uid));
|
||||
$tmp_command = str_replace('%f', escapeshellarg($tmpfname), $tmp_command);
|
||||
}
|
||||
|
||||
$output = shell_exec($tmp_command);
|
||||
|
||||
if ($debug) {
|
||||
if ($output) {
|
||||
$tmp_command .= "\n$output";
|
||||
}
|
||||
|
||||
rcube::write_log('markasjunk', $tmp_command);
|
||||
}
|
||||
|
||||
if (isset($tmpfname)) {
|
||||
unlink($tmpfname);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Copy spam/ham messages to a directory for learning later
|
||||
*
|
||||
* @version 2.0
|
||||
*
|
||||
* @author Philip Weir
|
||||
*
|
||||
* Copyright (C) 2009-2014 Philip Weir
|
||||
*
|
||||
* This driver is part of the MarkASJunk plugin for Roundcube.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Roundcube. If not, see https://www.gnu.org/licenses/.
|
||||
*/
|
||||
class markasjunk_dir_learn
|
||||
{
|
||||
public function spam($uids, $src_mbox, $dst_mbox)
|
||||
{
|
||||
$this->_do_messagemove($uids, true);
|
||||
}
|
||||
|
||||
public function ham($uids, $src_mbox, $dst_mbox)
|
||||
{
|
||||
$this->_do_messagemove($uids, false);
|
||||
}
|
||||
|
||||
private function _do_messagemove($uids, $spam)
|
||||
{
|
||||
$rcube = rcube::get_instance();
|
||||
$dest_dir = unslashify($rcube->config->get($spam ? 'markasjunk_spam_dir' : 'markasjunk_ham_dir'));
|
||||
|
||||
if (!$dest_dir) {
|
||||
return;
|
||||
}
|
||||
|
||||
$debug = $rcube->config->get('markasjunk_debug');
|
||||
$filename = $rcube->config->get('markasjunk_filename');
|
||||
$filename = str_replace('%u', $_SESSION['username'], $filename);
|
||||
$filename = str_replace('%t', ($spam) ? 'spam' : 'ham', $filename);
|
||||
$filename = str_replace('%l', $rcube->user->get_username('local'), $filename);
|
||||
$filename = str_replace('%d', $rcube->user->get_username('domain'), $filename);
|
||||
|
||||
foreach ($uids as $uid) {
|
||||
$tmpfname = tempnam($dest_dir, $filename);
|
||||
file_put_contents($tmpfname, $rcube->storage->get_raw_body($uid));
|
||||
|
||||
if ($debug) {
|
||||
rcube::write_log('markasjunk', $tmpfname);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Edit headers
|
||||
*
|
||||
* @version 1.0
|
||||
*
|
||||
* @author Philip Weir
|
||||
*
|
||||
* Copyright (C) 2012-2014 Philip Weir
|
||||
*
|
||||
* This driver is part of the MarkASJunk plugin for Roundcube.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Roundcube. If not, see https://www.gnu.org/licenses/.
|
||||
*/
|
||||
class markasjunk_edit_headers
|
||||
{
|
||||
public function spam(&$uids, $src_mbox, $dst_mbox)
|
||||
{
|
||||
$this->_edit_headers($uids, true, $dst_mbox);
|
||||
}
|
||||
|
||||
public function ham(&$uids, $src_mbox, $dst_mbox)
|
||||
{
|
||||
$this->_edit_headers($uids, false, $dst_mbox);
|
||||
}
|
||||
|
||||
private function _edit_headers(&$uids, $spam, $dst_mbox)
|
||||
{
|
||||
$rcube = rcube::get_instance();
|
||||
$args = $rcube->config->get($spam ? 'markasjunk_spam_patterns' : 'markasjunk_ham_patterns');
|
||||
|
||||
if (empty($args['patterns'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$new_uids = [];
|
||||
|
||||
foreach ($uids as $uid) {
|
||||
$raw_message = $rcube->storage->get_raw_body($uid);
|
||||
$raw_headers = $rcube->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 = $rcube->storage->save_message($dst_mbox, $raw_message);
|
||||
|
||||
if ($saved !== false) {
|
||||
$rcube->output->command('markasjunk_move', null, [$uid]);
|
||||
array_push($new_uids, $saved);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($new_uids) > 0) {
|
||||
$uids = $new_uids;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Email learn driver
|
||||
*
|
||||
* @version 3.0
|
||||
*
|
||||
* @author Philip Weir
|
||||
*
|
||||
* Copyright (C) 2009-2017 Philip Weir
|
||||
*
|
||||
* This driver is part of the MarkASJunk plugin for Roundcube.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Roundcube. If not, see https://www.gnu.org/licenses/.
|
||||
*/
|
||||
class markasjunk_email_learn
|
||||
{
|
||||
private $rcube;
|
||||
|
||||
public function spam($uids, $src_mbox, $dst_mbox)
|
||||
{
|
||||
$this->_do_emaillearn($uids, true);
|
||||
}
|
||||
|
||||
public function ham($uids, $src_mbox, $dst_mbox)
|
||||
{
|
||||
$this->_do_emaillearn($uids, false);
|
||||
}
|
||||
|
||||
private function _do_emaillearn($uids, $spam)
|
||||
{
|
||||
$this->rcube = rcube::get_instance();
|
||||
$identity = $this->rcube->user->get_identity();
|
||||
$from = $identity['email'];
|
||||
$from_string = format_email_recipient($from, $identity['name']);
|
||||
$attach = $this->rcube->config->get('markasjunk_email_attach', false);
|
||||
$debug = $this->rcube->config->get('markasjunk_debug');
|
||||
$product = $this->rcube->config->get('product_name');
|
||||
$temp_dir = unslashify($this->rcube->config->get('temp_dir'));
|
||||
|
||||
$subject = (string) $this->rcube->config->get('markasjunk_email_subject');
|
||||
$mailto = (string) $this->rcube->config->get($spam ? 'markasjunk_email_spam' : 'markasjunk_email_ham');
|
||||
$subject = $this->_parse_vars($subject, $spam, $from);
|
||||
$mailto = $this->_parse_vars($mailto, $spam, $from);
|
||||
|
||||
// no address to send to, exit
|
||||
if (!$mailto) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($uids as $i => $uid) {
|
||||
$MESSAGE = new rcube_message($uid);
|
||||
$message_file = null;
|
||||
|
||||
// set message charset as default
|
||||
if (!empty($MESSAGE->headers->charset)) {
|
||||
$this->rcube->storage->set_charset($MESSAGE->headers->charset);
|
||||
}
|
||||
|
||||
$OUTPUT = $this->rcube->output;
|
||||
$SENDMAIL = new rcmail_sendmail(null, [
|
||||
'sendmail' => true,
|
||||
'from' => $from,
|
||||
'mailto' => $mailto,
|
||||
'dsn_enabled' => false,
|
||||
'charset' => 'UTF-8',
|
||||
'error_handler' => function(...$args) use ($OUTPUT) {
|
||||
call_user_func_array([$OUTPUT, 'show_message'], $args);
|
||||
$OUTPUT->send();
|
||||
}
|
||||
]);
|
||||
|
||||
if ($attach) {
|
||||
$headers = [
|
||||
'Date' => $this->rcube->user_date(),
|
||||
'From' => $from_string,
|
||||
'To' => $mailto,
|
||||
'Subject' => $subject,
|
||||
'User-Agent' => $this->rcube->config->get('useragent'),
|
||||
'Message-ID' => $this->rcube->gen_message_id($from),
|
||||
'X-Sender' => $from
|
||||
];
|
||||
|
||||
$message_text = ($spam ? 'Spam' : 'Ham') . " report from $product";
|
||||
|
||||
// create attachment
|
||||
$orig_subject = $MESSAGE->get_header('subject');
|
||||
$disp_name = (!empty($orig_subject) ? $orig_subject : 'message_rfc822') . '.eml';
|
||||
$message_file = tempnam($temp_dir, 'rcm');
|
||||
$attachment = [];
|
||||
|
||||
if ($fp = fopen($message_file, 'w')) {
|
||||
$this->rcube->storage->get_raw_body($uid, $fp);
|
||||
fclose($fp);
|
||||
|
||||
$attachment = [
|
||||
'name' => $disp_name,
|
||||
'mimetype' => 'message/rfc822',
|
||||
'path' => $message_file,
|
||||
'size' => filesize($message_file),
|
||||
'charset' => $MESSAGE->headers->charset
|
||||
];
|
||||
}
|
||||
|
||||
// create message
|
||||
$MAIL_MIME = $SENDMAIL->create_message($headers, $message_text, false, [$attachment]);
|
||||
|
||||
if (count($attachment) > 0) { // sanity check in case creating the attachment failed
|
||||
$folding = (int) $this->rcube->config->get('mime_param_folding');
|
||||
|
||||
$MAIL_MIME->addAttachment($attachment['path'],
|
||||
$attachment['mimetype'], $attachment['name'], true,
|
||||
'8bit', 'attachment', $attachment['charset'], '', '',
|
||||
$folding ? 'quoted-printable' : null,
|
||||
$folding == 2 ? 'quoted-printable' : null,
|
||||
'', RCUBE_CHARSET
|
||||
);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$headers = [
|
||||
'Resent-From' => $from_string,
|
||||
'Resent-To' => $mailto,
|
||||
'Resent-Date' => $this->rcube->user_date(),
|
||||
'Resent-Message-ID' => $this->rcube->gen_message_id($from)
|
||||
];
|
||||
|
||||
// create the bounce message
|
||||
$MAIL_MIME = new rcmail_resend_mail([
|
||||
'bounce_message' => $MESSAGE,
|
||||
'bounce_headers' => $headers,
|
||||
]);
|
||||
}
|
||||
|
||||
$SENDMAIL->deliver_message($MAIL_MIME, $i == count($uids) - 1);
|
||||
|
||||
// clean up
|
||||
if ($message_file) {
|
||||
unlink($message_file);
|
||||
}
|
||||
|
||||
if ($debug) {
|
||||
rcube::write_log('markasjunk', $uid . ($spam ? ' SPAM ' : ' HAM ') . $mailto . ' (' . $subject . ')');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function _parse_vars($data, $spam, $from)
|
||||
{
|
||||
$data = str_replace('%u', $_SESSION['username'], $data);
|
||||
$data = str_replace('%t', $spam ? 'spam' : 'ham', $data);
|
||||
$data = str_replace('%l', $this->rcube->user->get_username('local'), $data);
|
||||
$data = str_replace('%d', $this->rcube->user->get_username('domain'), $data);
|
||||
$data = str_replace('%i', $from, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* MarkAsJunk JS events example
|
||||
* This is an example of how to interact with the markasjunk JS event
|
||||
* markasjunk-update to change the spam/ham options shown for specific
|
||||
* folders
|
||||
*
|
||||
* @version 0.1
|
||||
* @author Philip Weir
|
||||
*
|
||||
* Copyright (C) 2016 Philip Weir
|
||||
*
|
||||
* This driver is part of the MarkASJunk plugin for Roundcube.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Roundcube. If not, see http://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
class markasjunk_jsevent
|
||||
{
|
||||
private $addition_spam_folders = ['spam2', 'spam3'];
|
||||
private $suspicious_folders = ['unknown1', 'unknown2'];
|
||||
|
||||
public function init()
|
||||
{
|
||||
$rcmail = rcmail::get_instance();
|
||||
|
||||
// only execute this code on page load
|
||||
if ($rcmail->output->type != 'html') {
|
||||
return;
|
||||
}
|
||||
|
||||
$js_addition_spam_folders = json_encode($this->addition_spam_folders);
|
||||
$js_suspicious_folders = json_encode($this->suspicious_folders);
|
||||
|
||||
$script = <<<EOL
|
||||
rcmail.addEventListener('markasjunk-update', function(props) {
|
||||
var addition_spam_folders = {$js_addition_spam_folders};
|
||||
var suspicious_folders = {$js_suspicious_folders};
|
||||
|
||||
// ignore this special code when in a multifolder listing
|
||||
if (rcmail.is_multifolder_listing())
|
||||
return;
|
||||
|
||||
if ($.inArray(rcmail.env.mailbox, addition_spam_folders) > -1) {
|
||||
props.disp.spam = false;
|
||||
props.disp.ham = true;
|
||||
}
|
||||
else if ($.inArray(rcmail.env.mailbox, suspicious_folders) > -1) {
|
||||
props.disp.spam = true;
|
||||
props.disp.ham = true;
|
||||
|
||||
// from here it is also possible to alter the buttons themselves...
|
||||
props.objs.spamobj.find('a > span').text('As possibly spam');
|
||||
}
|
||||
else {
|
||||
props.objs.spamobj.find('a > span').text(rcmail.get_label('markasjunk.markasjunk'));
|
||||
}
|
||||
|
||||
return props;
|
||||
});
|
||||
EOL;
|
||||
|
||||
$rcmail->output->add_script($script, 'docready');
|
||||
}
|
||||
|
||||
public function spam(&$uids, $mbox)
|
||||
{
|
||||
// Treat message as spam...
|
||||
}
|
||||
|
||||
public function ham(&$uids, $mbox)
|
||||
{
|
||||
// Treat message as ham...
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* SpamAssassin Blacklist driver
|
||||
*
|
||||
* @version 2.0
|
||||
* @requires SAUserPrefs plugin
|
||||
*
|
||||
* @author Philip Weir
|
||||
*
|
||||
* Copyright (C) 2010-2014 Philip Weir
|
||||
*
|
||||
* This driver is part of the MarkASJunk plugin for Roundcube.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Roundcube. If not, see https://www.gnu.org/licenses/.
|
||||
*/
|
||||
class markasjunk_sa_blacklist
|
||||
{
|
||||
private $sa_user;
|
||||
private $sa_table;
|
||||
private $sa_username_field;
|
||||
private $sa_preference_field;
|
||||
private $sa_value_field;
|
||||
|
||||
public function spam($uids, $src_mbox, $dst_mbox)
|
||||
{
|
||||
$this->_do_list($uids, true);
|
||||
}
|
||||
|
||||
public function ham($uids, $src_mbox, $dst_mbox)
|
||||
{
|
||||
$this->_do_list($uids, false);
|
||||
}
|
||||
|
||||
private function _do_list($uids, $spam)
|
||||
{
|
||||
$rcube = rcube::get_instance();
|
||||
$this->sa_user = $rcube->config->get('sauserprefs_userid', "%u");
|
||||
$this->sa_table = $rcube->config->get('sauserprefs_sql_table_name');
|
||||
$this->sa_username_field = $rcube->config->get('sauserprefs_sql_username_field');
|
||||
$this->sa_preference_field = $rcube->config->get('sauserprefs_sql_preference_field');
|
||||
$this->sa_value_field = $rcube->config->get('sauserprefs_sql_value_field');
|
||||
|
||||
// SAv4 compatibility
|
||||
$blocklist_pref_name = $rcube->config->get('sauserprefs_sav4', false) ? "blocklist_from" : "blacklist_from";
|
||||
$welcomelist_pref_name = $rcube->config->get('sauserprefs_sav4', false) ? "welcomelist_from" : "whitelist_from";
|
||||
|
||||
$identity = $rcube->user->get_identity();
|
||||
$identity = $identity['email'];
|
||||
|
||||
$this->sa_user = str_replace('%u', $_SESSION['username'], $this->sa_user);
|
||||
$this->sa_user = str_replace('%l', $rcube->user->get_username('local'), $this->sa_user);
|
||||
$this->sa_user = str_replace('%d', $rcube->user->get_username('domain'), $this->sa_user);
|
||||
$this->sa_user = str_replace('%i', $identity, $this->sa_user);
|
||||
|
||||
$config_file = $rcube->config->get('markasjunk_sauserprefs_config');
|
||||
$debug = $rcube->config->get('markasjunk_debug');
|
||||
|
||||
if (is_file($config_file) && !$rcube->config->load_from_file($config_file)) {
|
||||
rcube::raise_error([
|
||||
'code' => 527, 'file' => __FILE__, 'line' => __LINE__,
|
||||
'message' => "Failed to load config from $config_file"
|
||||
], true, false
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$db = rcube_db::factory($rcube->config->get('sauserprefs_db_dsnw'), $rcube->config->get('sauserprefs_db_dsnr'), $rcube->config->get('sauserprefs_db_persistent'));
|
||||
$db->set_debug((bool) $rcube->config->get('sql_debug'));
|
||||
$db->db_connect('w');
|
||||
|
||||
// check DB connections and exit on failure
|
||||
if ($err_str = $db->is_error()) {
|
||||
rcube::raise_error([
|
||||
'code' => 603,
|
||||
'type' => 'db',
|
||||
'message' => $err_str
|
||||
], false, true
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($uids as $uid) {
|
||||
$message = new rcube_message($uid);
|
||||
$email = $message->sender['mailto'];
|
||||
|
||||
// skip invalid emails
|
||||
if (!rcube_utils::check_email($email, false)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($spam) {
|
||||
// delete any welcomelisting for this address
|
||||
$db->query(
|
||||
"DELETE FROM `{$this->sa_table}` WHERE `{$this->sa_username_field}` = ? "
|
||||
. "AND `{$this->sa_preference_field}` = ? AND `{$this->sa_value_field}` = ?",
|
||||
$this->sa_user,
|
||||
$welcomelist_pref_name,
|
||||
$email
|
||||
);
|
||||
|
||||
// check address is not already blocklisted
|
||||
$sql_result = $db->query(
|
||||
"SELECT `value` FROM `{$this->sa_table}` WHERE `{$this->sa_username_field}` = ? "
|
||||
. "AND `{$this->sa_preference_field}` = ? AND `{$this->sa_value_field}` = ?",
|
||||
$this->sa_user,
|
||||
$blocklist_pref_name,
|
||||
$email
|
||||
);
|
||||
|
||||
if (!$db->fetch_array($sql_result)) {
|
||||
$db->query(
|
||||
"INSERT INTO `{$this->sa_table}` (`{$this->sa_username_field}`, `{$this->sa_preference_field}`, `{$this->sa_value_field}`)"
|
||||
. " VALUES (?, ?, ?)",
|
||||
$this->sa_user,
|
||||
$blocklist_pref_name,
|
||||
$email
|
||||
);
|
||||
|
||||
if ($debug) {
|
||||
rcube::write_log('markasjunk', $this->sa_user . ' blocklist ' . $email);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
// delete any blocklisting for this address
|
||||
$db->query(
|
||||
"DELETE FROM `{$this->sa_table}` WHERE `{$this->sa_username_field}` = ? AND "
|
||||
. "`{$this->sa_preference_field}` = ? AND `{$this->sa_value_field}` = ?",
|
||||
$this->sa_user,
|
||||
$blocklist_pref_name,
|
||||
$email
|
||||
);
|
||||
|
||||
// check address is not already welcomelisted
|
||||
$sql_result = $db->query(
|
||||
"SELECT `value` FROM `{$this->sa_table}` WHERE `{$this->sa_username_field}` = ? "
|
||||
. "AND `{$this->sa_preference_field}` = ? AND `{$this->sa_value_field}` = ?",
|
||||
$this->sa_user,
|
||||
$welcomelist_pref_name,
|
||||
$email
|
||||
);
|
||||
|
||||
if (!$db->fetch_array($sql_result)) {
|
||||
$db->query(
|
||||
"INSERT INTO `{$this->sa_table}` (`{$this->sa_username_field}`, `{$this->sa_preference_field}`, `{$this->sa_value_field}`)"
|
||||
. " VALUES (?, ?, ?)",
|
||||
$this->sa_user,
|
||||
$welcomelist_pref_name,
|
||||
$email);
|
||||
|
||||
if ($debug) {
|
||||
rcube::write_log('markasjunk', $this->sa_user . ' welcomelist ' . $email);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* SpamAssassin detach ham driver
|
||||
*
|
||||
* @version 2.0
|
||||
*
|
||||
* @author Philip Weir
|
||||
*
|
||||
* Copyright (C) 2011-2014 Philip Weir
|
||||
*
|
||||
* This driver is part of the MarkASJunk plugin for Roundcube.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Roundcube. If not, see https://www.gnu.org/licenses/.
|
||||
*/
|
||||
class markasjunk_sa_detach
|
||||
{
|
||||
public function spam($uids, $src_mbox, $dst_mbox)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public function ham(&$uids, $src_mbox, $dst_mbox)
|
||||
{
|
||||
$rcube = rcube::get_instance();
|
||||
$storage = $rcube->storage;
|
||||
$new_uids = [];
|
||||
|
||||
foreach ($uids as $uid) {
|
||||
$saved = false;
|
||||
$message = new rcube_message($uid);
|
||||
|
||||
foreach ($message->attachments as $part) {
|
||||
if (
|
||||
$part->ctype_primary == 'message'
|
||||
&& $part->ctype_secondary == 'rfc822'
|
||||
&& !empty($part->ctype_parameters['x-spam-type'])
|
||||
&& $part->ctype_parameters['x-spam-type'] == 'original'
|
||||
) {
|
||||
$orig_message_raw = $message->get_part_body($part->mime_id);
|
||||
|
||||
if ($saved = $storage->save_message($dst_mbox, $orig_message_raw)) {
|
||||
$rcube->output->command('markasjunk_move', null, [$uid]);
|
||||
array_push($new_uids, $saved);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count($new_uids) > 0) {
|
||||
$uids = $new_uids;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user