Script Locking everyone out of Mailtraq

This forum is for discussing Mailtraq's Scripting features. Get help with Mailtraq scripts, templates and external ActiveX scripting.

Script Locking everyone out of Mailtraq

Postby steved-abs » Wed Aug 22nd, 2007 10:45am

This isn't a particularly complicated script, having only two lines - But I still wouldn't be surprised to learn that I've mad a mistake.

Basically it's just a script to delete all messages from a spam folder - manually deleting them just takes too long (between 2 and 3,000 a day). The particular mailbox is the 'catch all mailbox' so we can't even reject the mail as it arrives. When run the script ties the server up for over 30 minutes, blocking all other access. I say over 30 minutes but I don't know how much over, as I had to terminate mailtraq and restart it to allow e-mail access for the rest of the users (about 40). Even tried running the script when there was just 300 or so spam messages in the folder. It deletes the messages almost instantly, but the server keeps on churning.

Mailtraq Version: 2.11.0.2198
Access: IMAP
DB: Firebird

The script:
Code: Select all
<@LANGUAGE=JavaScript@>
<%

var mailslot = Config.GetMailslot("user_name");
mailslot.EmptyFolder(18);

%>

Any suggestions ?
steved-abs
 
Posts: 7
Joined: Wed Aug 22nd, 2007 9:42am

Re: Script Locking everyone out of Mailtraq

Postby Martin Clayton » Wed Aug 22nd, 2007 2:28pm

Hi,
steved-abs wrote:Even tried running the script when there was just 300 or so spam messages in the folder. It deletes the messages almost instantly, but the server keeps on churning.

Deleting messages can be very slow and I think the recommended route is to expire or let Mailtraq's system maintenance do the work on anything in the 'Deleted Items' or 'Junk Mail' folders.

If you want to try different script options (they may not be any faster) the following may help:

Code: Select all
<@LANGUAGE=Javascript@>
<%

  // Delete messages in mailslot-Y, folder-X

  var MailslotName = "user_name";
  var FolderToClear = 18;
 
  var mailslot = Config.GetMailslot(MailslotName);

  // count (max) number of slot messages for QueryMessages()
  var MsgCount = 0;
  var FolderInfo = new String(mailslot.QueryFolders()).split(",");
  for(var i = 2; i < FolderInfo.length; i += 6) {
    MsgCount += (+FolderInfo[i]);
  }

  // create QueryMessages() template, "|" separates id from folder id
  var template = "$id|$folder";

  var querylist = mailslot.QueryMessages(0, 0, MsgCount, template, "", "", "\t").split("\t");

  var IDsToClear = new Array();
  var current_id = "";
  var current_folder = 0;

  // loop through query result, store message id if it's the target folder
  for(var i = 0; i < querylist.length; i++) {
    current_folder  = querylist[i].replace(/^.*?\|(.*)/, "$1");
    if((+current_folder) == (+FolderToClear)) {
      current_id = querylist[i].replace(/^(.*?)\|.*/, "$1");
      IDsToClear.push(current_id);
    }
  }
 
  // purge
  if(IDsToClear.length > 0)
    mailslot.DeleteMessage(IDsToClear.join(","));
   
%>

Note that I haven't tested this script. You could try replacing the last line with mailslot.MoveMessagesToFolder(IDsToClear.join(","), 3); to see if moving to 'Deleted Items' is faster than purging. More drastically, you could try avoiding the database overhead by running .DeleteFolder() followed by a quick .AddFolder().

Cheers, Martin
User avatar
Martin Clayton
Expert User
 
Posts: 529
Joined: Sat Jan 15th, 2005 8:20am
Location: London, UK

Re: Script Locking everyone out of Mailtraq

Postby Martin Clayton » Thu Aug 23rd, 2007 3:52am

Martin Clayton wrote:
Code: Select all
  var querylist = mailslot.QueryMessages(0, 0, MsgCount, template, "", "", "\t").split("\t");

Yikes, sorry -- I didn't have time to test yesterday and I misused QueryMessages(). The first parameter is the folder-index (so the script would only have worked when FolderToClear=0). This simpler version should work:

Code: Select all
<@LANGUAGE=Javascript@>
<%
  // Delete messages in mailslot-Y, folder-X

  var MailslotName = "user_name";
  var FolderToClear = 18;
     
  var mailslot = Config.GetMailslot(MailslotName);

  // count (max) number of slot messages for QueryMessages()
  var MsgCount = 0;
  var FolderInfo = new String(mailslot.QueryFolders()).split(",");
  for(var i = 2; i < FolderInfo.length; i += 6) {
    MsgCount += (+FolderInfo[i]);
  }

  var IDsToClear = mailslot.QueryMessages(FolderToClear, 0, MsgCount, "$id", "", "", "\t").split("\t");

  // purge
  if(IDsToClear.length > 0)
    mailslot.DeleteMessage(IDsToClear);
       
%>


Cheers, Martin
User avatar
Martin Clayton
Expert User
 
Posts: 529
Joined: Sat Jan 15th, 2005 8:20am
Location: London, UK

Re: Script Locking everyone out of Mailtraq

Postby steved-abs » Tue Aug 28th, 2007 5:45am

Thanks for your suggestions Martin - I have been having a short break so have yet to test them out (back today though) and over the three day weekend the mailbox has gathered 13000 spam messages.

May have to give the delete folder idea a try - It's either that or move to a third party anti spam solution so that we can block it from the server completely (why oh why cant we reject spam when it is received at the server(for aliases, obviously we can for users)).

I will post the results of the various suggestions back here in case others find them useful.
steved-abs
 
Posts: 7
Joined: Wed Aug 22nd, 2007 9:42am

Re: Script Locking everyone out of Mailtraq

Postby steved-abs » Tue Aug 28th, 2007 6:56am

Martin, you are a genius sir - Your little script to delete the messages one at a time as opposed to using the empty folder command works a treat. Not only is it faster than emptying the folder, but it doesn't tie up the server while it operates either.

I owe you a virtual pint :lol:
steved-abs
 
Posts: 7
Joined: Wed Aug 22nd, 2007 9:42am

Re: Script Locking everyone out of Mailtraq

Postby Martin Clayton » Tue Aug 28th, 2007 11:13am

steved-abs wrote:Martin, you are a genius sir

I'm glad it worked but it was far from genius. I didn't know if it would be any quicker and there's still some jetsam in the script -- the QueryMessages lookup only needs to be:

Code: Select all
  var IDsToClear = mailslot.QueryMessages(FolderToClear, 0, MsgCount, "$id", "", "", ",");

I owe you a virtual pint :lol:

How does that work? I'm thirsty. ;-)

I suspect that you can kill the problem at source. Is the "catch-all" mailbox just for unknown users? If so, can you reject messages to unknowns (Options, Incoming Mail, Undelivered, Option 1)? Also, have you considered DBL functionality -- zen.spamhaus.org and spamcop drive protocol rejections for the vast bulk of my smtp transactions -- rejecting trash before any data's received. They are proving to be very effective and reliable services. I'm pretty sure you could apply protocol level antispam protection on all inbound mail but I'm not sure of the best way of going about it (maybe that's better taken up in the support forum).

Cheers!
Martin
User avatar
Martin Clayton
Expert User
 
Posts: 529
Joined: Sat Jan 15th, 2005 8:20am
Location: London, UK


Return to Mailtraq Scripting

Who is online

Users browsing this forum: Bing [Bot] and 2 guests

cron