Roundcube Community Forum

 

Spam/Ham Report AddOn

Started by rosali, February 02, 2008, 12:59:33 AM

Previous topic - Next topic

rosali

No, it is correct. "-1" will copy message file to defined spam collecting folder (either by filesystem or by FTP)  and it will move the message to users spam folder. "-2" moves message to inbox and will copy it to defined ham collecting folder.

NOTICE: This mod was designed for v1.0/1.1. I do not think it works with current v2 releases.

-Roland
Regards,
Rosali
__________________
MyRoundcube Project (commercial)

javiaw

Hi rosali,

yes it's correct, and it works!!!. It was my fault, I should have read all the thread. :$

I've modified the code because I didn't like the way it reported the spam/ham. Now if a mail was moved to spam/ham folder and then moved again to ham/spam, the first file created is deleted.
To do it, I need to name the file with an unique ID to have a reference, so I use the 'Message-ID' from the mail headers. I don't know if Message-ID is the same for every recipient when is sent to several ones or it changes, so I append the session user id to avoid duplicated file names.


function str_makerand ($minlength, $maxlength, $useupper, $usespecial, $usenumbers){
  $key = '';
  $charset = "abcdefghijklmnopqrstuvwxyz";
 
  if ($useupper) $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  if ($usenumbers) $charset .= "0123456789";
  if ($usespecial) $charset .= &quot;~@#$%^*()_+={}|][&quot;; // Note: using all special characters this reads: &quot;~!@#$%^&*()_+`-={}|\\]?[\&quot;:;'><,./&quot;;
  if ($minlength > $maxlength) $length = mt_rand ($maxlength, $minlength);
  else $length = mt_rand ($minlength, $maxlength);
  for ($i=0; $i<$length; $i++) $key .= $charset[(mt_rand(0,(strlen($charset)-1)))];
  return $key;
}

function create_file_name($message){
  $temp = explode(&quot;\n&quot;,$message);

  $str = '';
  foreach ($temp as $lin){
    if (eregi('message-id',$lin)){
      $str = $lin;
      break;
    }
  }
  unset($temp);

  $str = explode(':',$str);
  $file = '';
  if (sizeof($str)==2){               // Exepected 'Message-ID' format
    $str = trim($str[1]);
    $file =  $str . '_' . $_SESSION['username'] . &quot;.eml&quot;;
  }
  else{                               // Line is not formed as expected, random string created  
    $file = str_makerand(20, 20, false, false, false) . &quot;.eml&quot;;
  }

  return $file;
}

if ($uid = get_input_value('_uid', RCUBE_INPUT_POST)){

  $a_uid = explode(&quot;,&quot;,$uid);
  $folder = '';
  $folder_del = '';
  $file = '';
  $err_type = '';

  if(get_input_value('_target_mbox', RCUBE_INPUT_POST) == -1){
    $folder = $CONFIG['report_spam_folder'];
    $folder_del = $CONFIG['report_ham_folder'];
    $ftp_folder = $CONFIG['report_spam_ftp_folder'];
  }
  else if(get_input_value('_target_mbox', RCUBE_INPUT_POST) == -2){
    $folder = $CONFIG['report_ham_folder'];
    $folder_del = $CONFIG['report_spam_folder'];
    $ftp_folder = $CONFIG['report_ham_ftp_folder'];
  }

  if(isset($CONFIG['report_spam_ham_ftp']) && $CONFIG['report_spam_ham_ftp'] === TRUE){
    $conn_id = ftp_connect($CONFIG['ftp_server'], $CONFIG['ftp_port']);
    $folder = $CONFIG['temp_dir'];
    if(isset($CONFIG['ftp_passive_mode']) && $CONFIG['ftp_passive_mode'] === TRUE){
      ftp_pasv ($conn_id , TRUE);
    }
    $login_result = ftp_login($conn_id, $CONFIG['ftp_user'], $CONFIG['ftp_pass']);
  }

  foreach($a_uid as $key => $uid){
    if(isset($folder) && file_exists($folder) && isset($folder_del) && file_exists($folder_del) ){
     
      $message = $IMAP->get_raw_body($uid);

      $file = create_file_name($message);
      while(file_exists($folder . $file)){
$file = str_makerand (20, 20, false, true, false) . &quot;.eml&quot;;
      }
     
     
     
      if(!@file_put_contents($folder . $file ,$message)){
//failure
$err_type = 'error';
      }
      else{
if(isset($CONFIG['report_spam_ham_ftp']) && $CONFIG['report_spam_ham_ftp'] === TRUE){
 
 if((!$conn_id) || (!$login_result)){
   //failure
   $err_type = 'error';
 }
 else{
   //success
   $ftp_file = $file;
   while(ftp_size($conn_id, $ftp_folder.$ftp_file) > -1){
     $ftp_file = str_makerand (16, 16, false, false, false) . &quot;.eml&quot;;
   }

   $upload = ftp_put($conn_id, $ftp_folder.$ftp_file, $folder.$file, FTP_BINARY);

   if(!$upload){
     //failure
     $err_type = 'error';
   }
   else{
     //success
   }

   @unlink($folder.$ftp_file);
 }
}
      }
    }
    // removes file if it exists in the other folder
    if ( ($err_type == &quot;&quot;) && !(isset($CONFIG['report_spam_ham_ftp']) && $CONFIG['report_spam_ham_ftp'] === TRUE)){
      if (file_exists($folder_del . $file)){
unlink($folder_del . $file);  
      }
    }
  }
 
  if($conn_id){
    ftp_close($conn_id);
  }
 
 
 
 }

if($err_type == &quot;&quot;){
  $message = rcube_label('message(s)_successfully_reported');
  $err_type = 'confirmation';
 }
 else{
   $message = rcube_label('there_was_an_error');
 }

$OUTPUT->show_message($message, $err_type);

if(get_input_value('_target_mbox', RCUBE_INPUT_POST) == -1){
  $_POST['_target_mbox'] = $CONFIG['junk_mbox'];
 }
 else if(get_input_value('_target_mbox', RCUBE_INPUT_POST) == -2){
   $_POST['_target_mbox'] = &quot;INBOX&quot;;
 }
 else{
   $_POST['_target_mbox'] = $_POST['_mbox'];
 }


* I'm not using ftp option so I've just implemented it for the folder case.

The report works when the message is moved using the options list or the buttons (by Ythan) but not when the mail is dragged with the cursor, it's something about javascript but I'm not very skilled at it.

Javi