Skip to content

Commit b6c50e7

Browse files
refactor(sieve_filters): Update sieve filters helpers
1 parent 007dce8 commit b6c50e7

File tree

5 files changed

+145
-235
lines changed

5 files changed

+145
-235
lines changed

modules/core/navigation/routes.js

Lines changed: 81 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,87 @@
22
NOTE: Handlers are registered as strings instead of functions because some modules might not be enabled, making their pages' handler functions unaccessible.
33
*/
44
const modulesRoutes = [
5-
{
6-
page: "hello_world",
7-
handler: "applyHelloWorldPageHandlers",
8-
},
9-
{
10-
page: "message_list",
11-
handler: "applyMessageListPageHandlers",
12-
},
13-
{
14-
page: "message",
15-
handler: "applyMessagePageHandlers",
16-
},
17-
{
18-
page: "compose",
19-
handler: "applyComposePageHandlers",
20-
},
21-
{
22-
page: "servers",
23-
handler: "applyServersPageHandlers",
24-
},
25-
{
26-
page: "settings",
27-
handler: "applySettingsPageHandlers",
28-
},
29-
{
30-
page: "search",
31-
handler: "applySearchPageHandlers",
32-
},
33-
{
34-
page: "home",
35-
handler: "applyHomePageHandlers",
36-
},
37-
{
38-
page: "info",
39-
handler: "applyInfoPageHandlers",
40-
},
41-
{
42-
page: "calendar",
43-
handler: "applyCalendarPageHandlers",
44-
},
45-
{
46-
page: "advanced_search",
47-
handler: "applyAdvancedSearchPageHandlers",
48-
},
49-
{
50-
page: "contacts",
51-
handler: "applyContactsPageHandlers",
52-
},
53-
{
54-
page: "history",
55-
handler: "applyHistoryPageHandlers",
56-
},
57-
{
58-
page: "folders",
59-
handler: "applyFoldersPageHandlers",
60-
},
61-
{
62-
page: "folders_subscription",
63-
handler: "applyFoldersSubscriptionPageHandlers",
64-
},
65-
{
66-
page: "pgp",
67-
handler: "applyPgpPageHandlers",
68-
},
69-
{
70-
page: "profiles",
71-
handler: "applyProfilesPageHandler",
72-
},
73-
{
74-
page: "block_list",
75-
handler: "applyBlockListPageHandlers",
76-
},
77-
{
78-
page: "sieve_filters",
79-
handler: "applySieveFiltersPageHandler",
80-
},
81-
{
82-
page: "message",
83-
handler: "applySieveFiltersPageHandler",
84-
},
85-
{
86-
page: "shortcuts",
87-
handler: "applyShortcutsPageHandlers",
88-
},
89-
];
5+
{
6+
page: 'hello_world',
7+
handler: 'applyHelloWorldPageHandlers'
8+
},
9+
{
10+
page: 'message_list',
11+
handler: 'applyMessageListPageHandlers'
12+
},
13+
{
14+
page: 'message',
15+
handler: 'applyMessagePageHandlers'
16+
},
17+
{
18+
page: 'compose',
19+
handler: 'applyComposePageHandlers'
20+
},
21+
{
22+
page: 'servers',
23+
handler: 'applyServersPageHandlers'
24+
},
25+
{
26+
page: 'settings',
27+
handler: 'applySettingsPageHandlers'
28+
},
29+
{
30+
page: 'search',
31+
handler: 'applySearchPageHandlers'
32+
},
33+
{
34+
page: 'home',
35+
handler: 'applyHomePageHandlers'
36+
},
37+
{
38+
page: 'info',
39+
handler: 'applyInfoPageHandlers'
40+
},
41+
{
42+
page: 'calendar',
43+
handler: 'applyCalendarPageHandlers'
44+
},
45+
{
46+
page: 'advanced_search',
47+
handler: 'applyAdvancedSearchPageHandlers'
48+
},
49+
{
50+
page: 'contacts',
51+
handler: 'applyContactsPageHandlers'
52+
},
53+
{
54+
page: 'history',
55+
handler: 'applyHistoryPageHandlers'
56+
},
57+
{
58+
page: 'folders',
59+
handler: 'applyFoldersPageHandlers'
60+
},
61+
{
62+
page: 'folders_subscription',
63+
handler: 'applyFoldersSubscriptionPageHandlers'
64+
},
65+
{
66+
page: 'pgp',
67+
handler: 'applyPgpPageHandlers'
68+
},
69+
{
70+
page: 'profiles',
71+
handler: 'applyProfilesPageHandler'
72+
},
73+
{
74+
page: 'block_list',
75+
handler: 'applyBlockListPageHandlers'
76+
},
77+
{
78+
page: 'sieve_filters',
79+
handler: 'applySieveFiltersPageHandler'
80+
},
81+
{
82+
page: 'shortcuts',
83+
handler: 'applyShortcutsPageHandlers'
84+
}
85+
]
9086

9187
/*
9288
Now let's validate and use handlers that are given.

modules/core/site.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2752,7 +2752,8 @@ const Hm_Filters = (function (my) {
27522752
],
27532753
function (res) {
27542754
if (Object.keys(res.script_details).length === 0) {
2755-
window.location = window.location;
2755+
window.location.href = "?page=sieve_filters";
2756+
27562757
} else {
27572758
edit_script_modal.open();
27582759
$(".modal_sieve_script_textarea").val(res.script_details.gen_script);
@@ -2941,13 +2942,15 @@ class Hm_Filter_Modal extends Hm_Modal {
29412942
this.addFooterBtn("Save", "btn-primary ms-auto", async () => {
29422943
let result = save_filter(current_account);
29432944
if (result) {
2945+
Hm_Notices.show("Filter saved", "success");
29442946
this.hide();
29452947
}
29462948
});
29472949

29482950
this.addFooterBtn("Convert to code", "btn-warning", async () => {
29492951
let result = save_filter(current_account, true);
29502952
if (result) {
2953+
Hm_Notices.show("Filter saved", "success");
29512954
this.hide();
29522955
}
29532956
});

modules/imap/setup.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
add_handler('ajax_imap_message_content', 'save_imap_cache', true);
156156
add_handler('ajax_imap_message_content', 'save_imap_servers', true);
157157
add_handler('ajax_imap_message_content', 'close_session_early', true, 'core');
158-
// add_output('ajax_imap_message_content', 'filter_message_headers', true);
158+
add_output('ajax_imap_message_content', 'filter_message_headers', true);
159159
add_output('ajax_imap_message_content', 'filter_message_body', true);
160160
add_output('ajax_imap_message_content', 'filter_message_struct', true);
161161

@@ -337,6 +337,7 @@
337337
'do_not_flag_as_read_on_open' => array(FILTER_VALIDATE_BOOLEAN, false),
338338
'ajax_imap_folders_permissions' => array(FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY),
339339
'move_responses' => array(FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY),
340+
'mailbox_name' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
340341
),
341342

342343
'allowed_get' => array(

modules/imap/site.js

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -970,23 +970,21 @@ var imap_move_copy = function(e, action, context) {
970970
return false;
971971
};
972972

973-
974973
$(function () {
975974
$(document).on("submit", "#create-filter-form", function (e) {
976975
e.preventDefault();
977976
const current_account = $(this).attr("account");
978-
console.log("Current account mailbox:", current_account);
979-
980-
981-
const edit_filter_modal = new Hm_Filter_Modal();
977+
978+
const edit_filter_modal = new Hm_Filter_Modal(current_account);
982979
hm_sieve_button_events(edit_filter_modal);
983980
edit_filter_modal.setTitle("Add Filter for message like this");
981+
const add_filter_condition = Hm_Filters.add_filter_condition;
982+
const add_filter_action = Hm_Filters.add_filter_action;
984983

985984
const $form = $(this);
986985
const $btn = $form.find("#create_filter").prop("disabled", true);
987986
const data = {};
988987

989-
// collect checked conditions
990988
if ($form.find("#use_from").is(":checked"))
991989
data["from"] = $form.find('input[name="from"]').val();
992990
if ($form.find("#use_to").is(":checked"))
@@ -997,21 +995,58 @@ $(function () {
997995
data["reply-to"] = $form.find('input[name="reply-to"]').val();
998996

999997
if ($.isEmptyObject(data)) {
1000-
Hm_Notices.show("Please check at least one condition to create a filter.", "danger");
1001-
$btn.prop("disabled", false);
1002-
return;
1003-
}
998+
Hm_Notices.show(
999+
"Please check at least one condition to create a filter.",
1000+
"danger"
1001+
);
1002+
$btn.prop("disabled", false);
1003+
return;
1004+
}
10041005

1005-
function encodeParams(obj) {
1006-
return Object.entries(obj)
1007-
.map(([k, v]) => encodeURIComponent(k) + "=" + encodeURIComponent(v))
1008-
.join("&");
1009-
}
1006+
edit_filter_modal.open();
1007+
1008+
// After modal content is ready, auto-fill conditions
1009+
setTimeout(() => {
1010+
const allFields = hm_sieve_condition_fields();
1011+
const availableFields = [
1012+
...allFields.Message.map((f) => f.name),
1013+
...allFields.Header.map((f) => f.name),
1014+
];
10101015

1011-
const params = encodeParams(data);
1012-
edit_filter_modal.open();
1013-
1016+
for (const [key, value] of Object.entries(data)) {
1017+
// If key is not in available fields, skip it
1018+
if (!availableFields.includes(key)) {
1019+
console.warn("Skipping unrecognized field:", key);
1020+
continue;
1021+
}
10141022

1023+
add_filter_condition();
1024+
1025+
const $lastRow = $(".sieve_list_conditions_modal tr").last();
1026+
const $selectField = $lastRow.find(".add_condition_sieve_filters");
1027+
const $selectOp = $lastRow.find(".condition_options");
1028+
const $inputVal = $lastRow.find('input[name="sieve_selected_option_value[]"]');
1029+
$selectField.val(key);
1030+
$selectOp.val("Contains");
1031+
$inputVal.val(value);
1032+
1033+
}
1034+
1035+
if (data["reply-to"]) {
1036+
1037+
add_filter_action("autoreply");
1038+
1039+
const $lastRow = $(".filter_actions_modal_table tr").last();
1040+
const $select = $lastRow.find(".sieve_actions_select");
1041+
$select.val("autoreply").trigger("change");
1042+
1043+
// Focus the input field for the message
1044+
const $input = $lastRow.find('input[name="sieve_selected_action_value[]"]');
1045+
if ($input.length) {
1046+
$input.focus();
1047+
}
1048+
}
1049+
}, 250);
10151050
});
10161051
});
10171052

0 commit comments

Comments
 (0)