@@ -2079,3 +2079,88 @@ function process_ceo_amount_limit_callback($val) { return $val; }
20792079 process_site_setting ('ceo_rate_limit ' , $ this , 'process_ceo_amount_limit_callback ' );
20802080 }
20812081}
2082+
2083+ /**
2084+ * Report a message as spam
2085+ * @subpackage imap/handler
2086+ */
2087+ class Hm_Handler_imap_report_spam extends Hm_Handler_Module {
2088+ /**
2089+ * Use IMAP to move the selected message to the junk folder
2090+ */
2091+ public function process () {
2092+ Hm_Debug::add ('Report Spam handler starting ' );
2093+ list ($ success , $ form ) = $ this ->process_form (array ('imap_msg_uid ' , 'imap_server_id ' , 'folder ' ));
2094+ Hm_Debug::add ('Form processing result: ' , array ('success ' => $ success , 'form ' => $ form ));
2095+
2096+ if (!$ success ) {
2097+ Hm_Debug::add ('Form processing failed ' );
2098+ return ;
2099+ }
2100+
2101+ $ junk_folder = false ;
2102+ $ form_folder = hex2bin ($ form ['folder ' ]);
2103+ $ errors = 0 ;
2104+ $ status = null ;
2105+
2106+ $ specials = get_special_folders ($ this , $ form ['imap_server_id ' ]);
2107+ Hm_Debug::add ('Special folders: ' , $ specials );
2108+
2109+ if (array_key_exists ('junk ' , $ specials ) && $ specials ['junk ' ]) {
2110+ $ junk_folder = $ specials ['junk ' ];
2111+ Hm_Debug::add ('Found junk folder: ' , $ junk_folder );
2112+ }
2113+
2114+ $ mailbox = Hm_IMAP_List::get_connected_mailbox ($ form ['imap_server_id ' ], $ this ->cache );
2115+ Hm_Debug::add ('Mailbox connection: ' , array (
2116+ 'connected ' => ($ mailbox !== false ),
2117+ 'authed ' => ($ mailbox && $ mailbox ->authed ()),
2118+ 'is_imap ' => ($ mailbox && $ mailbox ->is_imap ())
2119+ ));
2120+
2121+ if ($ mailbox && !$ mailbox ->is_imap () && empty ($ junk_folder )) {
2122+ Hm_Debug::add ('Using EWS JUNK action ' );
2123+ // EWS supports moving to junk folder directly
2124+ $ status = $ mailbox ->message_action ($ form_folder , 'JUNK ' , array ($ form ['imap_msg_uid ' ]))['status ' ];
2125+ Hm_Debug::add ('EWS JUNK action result: ' , $ status );
2126+ } else {
2127+ if (!$ junk_folder ) {
2128+ Hm_Debug::add ('No junk folder configured ' );
2129+ Hm_Msgs::add ('No junk folder configured for this IMAP server ' , 'warning ' );
2130+ $ errors ++;
2131+ }
2132+
2133+ if (!$ errors && $ mailbox && $ mailbox ->authed ()) {
2134+ $ junk_exists = count ($ mailbox ->get_folder_status ($ junk_folder ));
2135+ Hm_Debug::add ('Junk folder status: ' , array (
2136+ 'folder ' => $ junk_folder ,
2137+ 'exists ' => $ junk_exists
2138+ ));
2139+
2140+ if (!$ junk_exists ) {
2141+ Hm_Debug::add ('Junk folder does not exist ' );
2142+ Hm_Msgs::add ('Configured junk folder for this IMAP server does not exist ' , 'warning ' );
2143+ $ errors ++;
2144+ }
2145+
2146+ /* try to move the message */
2147+ if (!$ errors ) {
2148+ Hm_Debug::add ('Attempting to move message to junk folder ' );
2149+ $ status = $ mailbox ->message_action ($ form_folder , 'MOVE ' , array ($ form ['imap_msg_uid ' ]), $ junk_folder )['status ' ];
2150+ Hm_Debug::add ('Move action result: ' , $ status );
2151+ }
2152+ }
2153+ }
2154+
2155+ if ($ status ) {
2156+ Hm_Debug::add ('Message successfully reported as spam ' );
2157+ Hm_Msgs::add ("Message reported as spam " );
2158+ } else {
2159+ Hm_Debug::add ('Failed to report message as spam ' );
2160+ Hm_Msgs::add ('An error occurred reporting the message as spam ' , 'danger ' );
2161+ }
2162+
2163+ $ this ->out ('imap_report_spam_error ' , !$ status );
2164+ $ this ->save_hm_msgs ();
2165+ }
2166+ }
0 commit comments