Skip to content

Commit

Permalink
Merge pull request cypht-org#1132 from christer77/implement-screen-email
Browse files Browse the repository at this point in the history
implement-screen-email
  • Loading branch information
kroky authored Aug 26, 2024
2 parents 25707d6 + ede161b commit 9a39ff2
Show file tree
Hide file tree
Showing 27 changed files with 290 additions and 16 deletions.
1 change: 1 addition & 0 deletions language/az.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
1 change: 1 addition & 0 deletions language/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
1 change: 1 addition & 0 deletions language/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,4 +650,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
1 change: 1 addition & 0 deletions language/es.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
1 change: 1 addition & 0 deletions language/et.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,4 +640,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
1 change: 1 addition & 0 deletions language/fa.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,4 +684,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
1 change: 1 addition & 0 deletions language/fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
1 change: 1 addition & 0 deletions language/hu.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
1 change: 1 addition & 0 deletions language/id.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,4 +639,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
1 change: 1 addition & 0 deletions language/it.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
1 change: 1 addition & 0 deletions language/ja.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
1 change: 1 addition & 0 deletions language/nl.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
1 change: 1 addition & 0 deletions language/pt-BR.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
1 change: 1 addition & 0 deletions language/ro.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
1 change: 1 addition & 0 deletions language/ru.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
1 change: 1 addition & 0 deletions language/zh-Hans.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,4 +653,5 @@
'No tags available yet.' => false,
'Server capabilities' => false,
'Capabilities' => false,
'Screen %s first emails' => false,
);
6 changes: 5 additions & 1 deletion lib/repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public static function add($entity, $save = true) {

public static function edit($id, $entity) {
if (array_key_exists($id, self::$entities)) {
self::$entities[$id] = array_merge(self::$entities[$id], $entity);
if (is_array($entity)) {
self::$entities[$id] = array_merge(self::$entities[$id], $entity);
} else {
self::$entities[$id] = $entity;
}
self::save();
return true;
}
Expand Down
50 changes: 50 additions & 0 deletions modules/contacts/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,56 @@ protected function output() {
}
}

/**
* @subpackage contacts/handler
*/
class Hm_Handler_save_contact extends Hm_Handler_Module
{
public function process()
{
list($success, $form ) = $this->process_form(array('email_address'));
if ($success) {
$contacts = $this->get('contact_store');
$contact_list = $contacts->getAll();
$existingEmails = array_map(function($contact){
return $contact->value('email_address');
},$contact_list);

$list_mails = array_unique(explode(",", $form['email_address']));

foreach ($list_mails as $addr) {
$addresses = process_address_fld($addr);
$newEmails = array_column($addresses, 'email');
if (!empty($newEmails)) {
$newContacts = array_filter($newEmails, function ($email) use ($existingEmails) {
return !in_array($email, $existingEmails);
});
$existingContacts = array_filter($existingEmails, function ($email) use ($newEmails) {
return in_array($email, $newEmails);
});
if (!empty($newContacts)) {
$newContacts = array_map(function ($email) {
return ['source' => 'local', 'email_address' => $email, 'display_name' => $email, 'group' => 'Trusted Senders'];
}, $newContacts);
$contacts->add_contact($newContacts[0]);
}
if (!empty($existingContacts)) {
$existingContacts = array_map(function ($email) {
return ['source' => 'local', 'email_address' => $email, 'group' => 'Trusted Senders'];
}, $existingContacts);
foreach ($existingContacts as $key => $contact) {
$contacts->update_contact($key, $contact);
}
}
}
}
$this->session->record_unsaved('Contacts added to Trusted Contacts list');
Hm_Msgs::add('Contacts added to Trusted Contacts list');
}

}
}

/**
* @subpackage contacts/output
*/
Expand Down
7 changes: 5 additions & 2 deletions modules/contacts/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
add_handler('compose', 'load_contacts', true, 'contacts', 'process_compose_form_submit', 'after');
add_handler('compose', 'store_contact_message', true, 'contacts', 'load_contacts', 'after');

add_handler('ajax_imap_folder_display', 'load_contacts', true, 'contacts', 'load_user_data', 'after');

add_handler('ajax_imap_message_content', 'load_contacts', true, 'contacts', 'load_user_data', 'after');
add_handler('ajax_imap_message_content', 'find_message_contacts', true, 'contacts', 'imap_message_content', 'after');
add_output('ajax_imap_message_content', 'add_message_contacts', true, 'contacts', 'filter_message_headers', 'after');
Expand All @@ -31,6 +33,7 @@
setup_base_ajax_page('ajax_add_contact', 'core');
add_handler('ajax_add_contact', 'load_contacts', true, 'contacts', 'load_user_data', 'after');
add_handler('ajax_add_contact', 'save_user_data', true, 'core', 'language', 'after');
add_handler('ajax_add_contact', 'save_contact', true);


setup_base_ajax_page('ajax_autocomplete_contact', 'core');
Expand Down Expand Up @@ -81,8 +84,8 @@
'contact_source' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'contact_type' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'contact_auto_collect' => FILTER_VALIDATE_BOOLEAN,
'enable_warn_contacts_cc_not_exist_in_list_contact' => FILTER_VALIDATE_INT

'enable_warn_contacts_cc_not_exist_in_list_contact' => FILTER_VALIDATE_INT,
'email_address' => FILTER_SANITIZE_FULL_SPECIAL_CHARS
),
'allowed_get' => array(
'contact_id' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
Expand Down
13 changes: 12 additions & 1 deletion modules/core/message_list_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,18 @@ function message_list_row($values, $id, $style, $output_mod, $row_class='') {
if ($row_class) {
$res .= ' '.$output_mod->html_safe($row_class);
}
$res .= '">';
$data_uid = "";
if ($uids = explode("_", $id)) {
if (isset($uids[2])) {
$data_uid = 'data-uid="'. $uids[2] .'"';
}
}
if (!empty($data_uid)) {
$res .= '" '.$data_uid.'>';
} else {
$res .= '">';
}

if ($style == 'news') {
$res .= '<td class="news_cell checkbox_cell">';
}
Expand Down
1 change: 1 addition & 0 deletions modules/core/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@
'filter' => FILTER_DEFAULT,
'sort' => FILTER_DEFAULT,
'keyword' => FILTER_DEFAULT,
'screen_emails' => FILTER_DEFAULT,
),

'allowed_post' => array(
Expand Down
18 changes: 16 additions & 2 deletions modules/imap/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -857,19 +857,33 @@ function imap_refresh_oauth2_token($server, $config) {
* @return int count of messages moved
*/
if (!hm_exists('imap_move_same_server')) {
function imap_move_same_server($ids, $action, $hm_cache, $dest_path) {
function imap_move_same_server($ids, $action, $hm_cache, $dest_path, $screen_emails=false) {
$moved = array();
$keys = array_keys($ids);
$server_id = array_pop($keys);
$cache = Hm_IMAP_List::get_cache($hm_cache, $server_id);
$imap = Hm_IMAP_List::connect($server_id, $cache);
foreach ($ids[$server_id] as $folder => $msgs) {
if (imap_authed($imap) && $imap->select_mailbox(hex2bin($folder))) {
if ($imap->message_action(mb_strtoupper($action), $msgs, hex2bin($dest_path[2]))) {
if ($screen_emails) {
foreach ($msgs as $msg) {
$moved[] = sprintf('imap_%s_%s_%s', $server_id, $msg, $folder);
$email = current(array_column(process_address_fld($imap->get_message_headers($msg)['From']), "email"));
$uids = $imap->search('ALL', false, array(array('FROM', $email)));
foreach ($uids as $uid) {
if ($imap->message_action(mb_strtoupper($action), $uid, hex2bin($dest_path[2]))) {
$moved[] = sprintf('imap_%s_%s_%s', $server_id, $uid, $folder);
}
}
}
} else {
if ($imap->message_action(mb_strtoupper($action), $msgs, hex2bin($dest_path[2]))) {
foreach ($msgs as $msg) {
$moved[] = sprintf('imap_%s_%s_%s', $server_id, $msg, $folder);
}
}
}

}
}
return $moved;
Expand Down
69 changes: 65 additions & 4 deletions modules/imap/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,26 +252,49 @@ class Hm_Handler_imap_process_move extends Hm_Handler_Module {
public function process() {
list($success, $form) = $this->process_form(array('imap_move_to', 'imap_move_page', 'imap_move_action', 'imap_move_ids'));
if ($success) {
$screen = false;
$parts = explode("_", $this->request->get['list_path']);
$imap_server_id = $parts[1] ?? '';
$cache = Hm_IMAP_List::get_cache($this->cache, $imap_server_id);
$imap = Hm_IMAP_List::connect($imap_server_id, $cache);
if ($form['imap_move_action'] == "screen_mail") {
$form['imap_move_action'] = "move";
$screen = true;
$screen_folder = 'Screen emails';
if (!count($imap->get_mailbox_status($screen_folder))) {
$imap->create_mailbox($screen_folder);
}
$form['imap_move_to'] = $parts[0] ."_". $parts[1] ."_".bin2hex($screen_folder);
}

list($msg_ids, $dest_path, $same_server_ids, $other_server_ids) = process_move_to_arguments($form);
$moved = array();
if (count($same_server_ids) > 0) {
$moved = array_merge($moved, imap_move_same_server($same_server_ids, $form['imap_move_action'], $this->cache, $dest_path));
$moved = array_merge($moved, imap_move_same_server($same_server_ids, $form['imap_move_action'], $this->cache, $dest_path, $screen));
}
if (count($other_server_ids) > 0) {
$moved = array_merge($moved, imap_move_different_server($other_server_ids, $form['imap_move_action'], $dest_path, $this->cache));

}
if (count($moved) > 0 && count($moved) == count($msg_ids)) {
if ($form['imap_move_action'] == 'move') {
Hm_Msgs::add('Messages moved');
if ($screen) {
Hm_Msgs::add('Emails moved to Screen email folder');
} else {
Hm_Msgs::add('Messages moved');
}
}
else {
Hm_Msgs::add('Messages copied');
}
}
elseif (count($moved) > 0) {
if ($form['imap_move_action'] == 'move') {
Hm_Msgs::add('Some messages moved (only IMAP message types can be moved)');
if ($screen) {
Hm_Msgs::add('Some Emails moved to Screen email folder');
} else {
Hm_Msgs::add('Some messages moved (only IMAP message types can be moved)');
}
}
else {
Hm_Msgs::add('Some messages copied (only IMAP message types can be copied)');
Expand Down Expand Up @@ -622,6 +645,7 @@ public function process() {
$this->out('list_filter', $this->request->get['filter']);
}
}
$folder = hex2bin($parts[2]);
if (!empty($details)) {
if (array_key_exists('folder_label', $this->request->get)) {
$folder = $this->request->get['folder_label'];
Expand All @@ -636,6 +660,13 @@ public function process() {
}
$this->out('mailbox_list_title', $title);
}

if ($this->module_is_supported("contacts") && $folder == 'INBOX') {
$this->out('folder', $folder);
$this->out('screen_emails', isset($this->request->get['screen_emails']));
$this->out('first_time_screen_emails', $this->user_config->get('first_time_screen_emails_setting', DEFAULT_PER_SOURCE));
$this->out('move_messages_in_screen_email', $this->user_config->get('move_messages_in_screen_email_setting', DEFAULT_PER_SOURCE));
}
}
elseif ($path == 'sent') {
$this->out('mailbox_list_title', array('Sent'));
Expand Down Expand Up @@ -804,7 +835,17 @@ public function process() {
$imap = Hm_IMAP_List::connect($form['imap_server_id'], $cache);
if (imap_authed($imap)) {
$this->out('imap_mailbox_page_path', $path);
list($total, $results) = $imap->get_mailbox_page(hex2bin($form['folder']), $sort, $rev, $filter, $offset, $limit, $keyword);
if (isset($this->request->get['screen_emails']) && hex2bin($form['folder']) == 'INBOX' && $this->module_is_supported("contacts")) {
$contacts = $this->get('contact_store');
$contact_list = $contacts->getAll();

$existingEmails = array_map(function($c){
return $c->value('email_address');
},$contact_list);
list($total, $results) = $imap->get_mailbox_page(hex2bin($form['folder']), $sort, $rev, $filter, $offset, $limit, $keyword, $existingEmails);
} else {
list($total, $results) = $imap->get_mailbox_page(hex2bin($form['folder']), $sort, $rev, $filter, $offset, $limit, $keyword);
}
foreach ($results as $msg) {
$msg['server_id'] = $form['imap_server_id'];
$msg['server_name'] = $details['name'];
Expand Down Expand Up @@ -2128,3 +2169,23 @@ public function process() {
}
}
}

/**
* Process first-time screen emails per page in the settings page
* @subpackage core/handler
*/
class Hm_Handler_process_first_time_screen_emails_per_page_setting extends Hm_Handler_Module {
public function process() {
function process_first_time_screen_emails_callback($val) {
return $val;
}
process_site_setting('first_time_screen_emails', $this, 'process_first_time_screen_emails_callback');
}
}

class Hm_Handler_process_setting_move_messages_in_screen_email extends Hm_Handler_Module {
public function process() {
function process_move_messages_in_screen_email_enabled_callback($val) { return $val; }
process_site_setting('move_messages_in_screen_email', $this, 'process_move_messages_in_screen_email_enabled_callback', true, true);
}
}
Loading

0 comments on commit 9a39ff2

Please sign in to comment.