Skip to content
This repository has been archived by the owner on Nov 10, 2019. It is now read-only.

Commit

Permalink
improve extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
johndoh committed Nov 25, 2016
1 parent 5753012 commit 805939e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Roundcube Webmail MarkAsJunk2
=============================

* Add JS event markasjunk2-update to allow other plugins to influence the spam/ham options show
* Add init method to allow drivers to access markasjunk2-update JS event
* Replace config options markasjunk2_mb_toolbar and markasjunk2_cp_toolbar with new markasjunk2_toolbar option

Version 1.9 (2015-01-06, rc-1.1)
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ The Learning Driver
-------------------
The learning driver allows you to perform additional processing on each message
marked as spam/ham. A driver must contain a class named markasjunk2_{driver
file name}. The class must contain 2 functions:
file name}. The class must contain 3 functions:

**spam:** This function should take 2 arguments: an array of UIDs of message(s)
being marked as spam, the name of the mailbox containing those messages

**ham:** This function should take 2 arguments: an array of UIDs of message(s)
being marked as ham, the name of the mailbox containing those messages

**init:** Optional, this function should take 0 arguments. eg: allows drivers
to add JS to the page to control which of the spam/ham options are displayed
An [example driver][jsevents] is available to show how to use the JS events

Several drivers are provided by default they are:

**cmd_learn:** This driver calls an external command (for example salearn) to
Expand Down Expand Up @@ -137,4 +141,5 @@ $config['markasjunk2_ham_patterns'] = array(
[rcplugrepo]: http://plugins.roundcube.net/packages/johndoh/markasjunk2
[releases]: http://github.com/JohnDoh/Roundcube-Plugin-Mark-as-Junk-2/releases
[gpl]: http://www.gnu.org/licenses/gpl.html
[multidriver]: http://gist.github.com/JohnDoh/8173505
[multidriver]: http://gist.github.com/JohnDoh/8173505
[jsevents]: http://gist.github.com/JohnDoh/37ab8610f9fa63052197c89e5ef89266
27 changes: 16 additions & 11 deletions markasjunk2.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,23 @@ function rcmail_markasjunk2_update() {
hamobj = hamobj.parent();
}

if (rcmail.env.markasjunk2_override || rcmail.is_multifolder_listing()) {
spamobj.show();
hamobj.show();
}
else if (rcmail.env.markasjunk2_spam_mailbox && rcmail.env.mailbox != rcmail.env.markasjunk2_spam_mailbox) {
spamobj.show();
hamobj.hide();
}
else {
spamobj.hide();
hamobj.show();
var disp = {'spam': true, 'ham': true};
if (!rcmail.is_multifolder_listing() && rcmail.env.markasjunk2_spam_mailbox) {
if (rcmail.env.mailbox != rcmail.env.markasjunk2_spam_mailbox) {
disp.ham = false;
}
else {
disp.spam = false;
}
}

var evt_rtn = rcmail.triggerEvent('markasjunk2-update', {'objs': {'spamobj': spamobj, 'hamobj': hamobj}, 'disp': disp});
if (evt_rtn && evt_rtn.abort)
return;
disp = evt_rtn ? evt_rtn.disp : disp;

disp.spam ? spamobj.show() : spamobj.hide();
disp.ham ? hamobj.show() : hamobj.hide();
}

$(document).ready(function() {
Expand Down
35 changes: 14 additions & 21 deletions markasjunk2.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,37 +58,28 @@ function init()
$this->add_texts('localization', true);
$this->include_stylesheet($this->local_skin_path() .'/markasjunk2.css');

// check which folder we are currently in to display the correct button
$mb_override = ($this->spam_mbox) ? false : true;
$display_junk = $display_not_junk = '';

if ($_SESSION['mbox'] == $this->spam_mbox) {
$display_junk = 'display: none;';
}
elseif (!$mb_override) {
$display_not_junk = 'display: none;';
}

if ($this->toolbar) {
// add the buttons to the main toolbar
$this->add_button(array('command' => 'plugin.markasjunk2.junk', 'type' => 'link', 'class' => 'button buttonPas markasjunk2 disabled', 'classact' => 'button markasjunk2', 'classsel' => 'button markasjunk2Sel', 'title' => 'markasjunk2.buttonjunk', 'label' => 'junk', 'style' => $display_junk), 'toolbar');
$this->add_button(array('command' => 'plugin.markasjunk2.not_junk', 'type' => 'link', 'class' => 'button buttonPas markasnotjunk2 disabled', 'classact' => 'button markasnotjunk2', 'classsel' => 'button markasnotjunk2Sel', 'title' => 'markasjunk2.buttonnotjunk', 'label' => 'markasjunk2.notjunk', 'style' => $display_not_junk), 'toolbar');
$this->add_button(array('command' => 'plugin.markasjunk2.junk', 'type' => 'link', 'class' => 'button buttonPas markasjunk2 disabled', 'classact' => 'button markasjunk2', 'classsel' => 'button markasjunk2Sel', 'title' => 'markasjunk2.buttonjunk', 'label' => 'junk'), 'toolbar');
$this->add_button(array('command' => 'plugin.markasjunk2.not_junk', 'type' => 'link', 'class' => 'button buttonPas markasnotjunk2 disabled', 'classact' => 'button markasnotjunk2', 'classsel' => 'button markasnotjunk2Sel', 'title' => 'markasjunk2.buttonnotjunk', 'label' => 'markasjunk2.notjunk'), 'toolbar');
}
else {
// add the buttons to the mark message menu
$markjunk = $this->api->output->button(array('command' => 'plugin.markasjunk2.junk', 'label' => 'markasjunk2.markasjunk', 'id' => 'markasjunk2', 'class' => 'icon markasjunk2', 'classact' => 'icon markasjunk2 active', 'innerclass' => 'icon markasjunk2'));
$marknotjunk = $this->api->output->button(array('command' => 'plugin.markasjunk2.not_junk', 'label' => 'markasjunk2.markasnotjunk', 'id' => 'markasnotjunk2', 'class' => 'icon markasnotjunk2', 'classact' => 'icon markasnotjunk2 active', 'innerclass' => 'icon markasnotjunk2'));
$this->api->add_content(html::tag('li', array('style' => $display_junk, 'role' => 'menuitem'), $markjunk), 'markmenu');
$this->api->add_content(html::tag('li', array('style' => $display_not_junk, 'role' => 'menuitem'), $marknotjunk), 'markmenu');
$this->api->add_content(html::tag('li', array('role' => 'menuitem'), $markjunk), 'markmenu');
$this->api->add_content(html::tag('li', array('role' => 'menuitem'), $marknotjunk), 'markmenu');
}

// add markasjunk2 folder settings to the env for JS
$this->api->output->set_env('markasjunk2_override', $mb_override);
$this->api->output->set_env('markasjunk2_ham_mailbox', $this->ham_mbox);
$this->api->output->set_env('markasjunk2_spam_mailbox', $this->spam_mbox);

$this->api->output->set_env('markasjunk2_move_spam', $rcmail->config->get('markasjunk2_move_spam', false));
$this->api->output->set_env('markasjunk2_move_ham', $rcmail->config->get('markasjunk2_move_ham', false));

// check for init method from driver
$this->_call_driver('init');
}
}

Expand Down Expand Up @@ -183,7 +174,7 @@ private function _spam(&$messageset, $dest_mbox = NULL)
$storage->set_folder($mbox);

if ($rcmail->config->get('markasjunk2_learning_driver', false)) {
$result = $this->_call_driver($uids, $mbox, true);
$result = $this->_call_driver('spam', $uids, $mbox);

// abort function of the driver says so
if (!$result)
Expand Down Expand Up @@ -213,7 +204,7 @@ private function _ham(&$messageset, $dest_mbox = NULL)
$storage->set_folder($mbox);

if ($rcmail->config->get('markasjunk2_learning_driver', false)) {
$result = $this->_call_driver($uids, $mbox, false);
$result = $this->_call_driver('ham', $uids, $mbox);

// abort function of the driver says so
if (!$result)
Expand All @@ -233,7 +224,7 @@ private function _ham(&$messageset, $dest_mbox = NULL)
return $result;
}

private function _call_driver(&$uids, $mbox, $spam)
private function _call_driver($action, &$uids = null, $mbox = null)
{
$driver = $this->home.'/drivers/'. rcube::get_instance()->config->get('markasjunk2_learning_driver', 'cmd_learn') .'.php';
$class = 'markasjunk2_' . rcube::get_instance()->config->get('markasjunk2_learning_driver', 'cmd_learn');
Expand Down Expand Up @@ -262,10 +253,12 @@ private function _call_driver(&$uids, $mbox, $spam)

// call the relevant function from the driver
$object = new $class;
if ($spam)
if ($action == 'spam')
$object->spam($uids, $mbox);
else
elseif ($action == 'ham')
$object->ham($uids, $mbox);
elseif ($action == 'init' && method_exists($object, 'init')) // method_exists check here for backwards compatibility, init method added 20161127
$object->init();

return $object->is_error ? false : true;
}
Expand Down

0 comments on commit 805939e

Please sign in to comment.