Is it possible to script ThunderBird filters

17
2014-04
  • Nifle

    Is it possible to write your own filters for Thunderbird, I don't care what language is used.

    I want to be able to do a full analysis of the email (headers and text) and take certain actions based on the content.

    Bonus points if I'm able to compare the current email with older received messages (mainly to remove duplicate log messages)

  • Answers
  • minghua

    Actually it is doable with FiltaQuilla. (Tried FiltaQuilla 1.1.0, with Thunderbird 3.1.9).

    Below is a test script put under "javascript action with body" supplied by FiltaQuilla. It does show all the header properties and the body in the error console.

    Once those data fields are available to the code, it needs to traverse through messenger folders and messages to compare them. Maybe using the "threadParent" property on the hdr can simplify the logic, refer to nsIMsgDBHdr on MDC for a hint. In the code, extra properties can be set. For example, set "hdr.myvar='1234'" in the code for duplicated messages, then in a later filter match myvar to 1234 and do a normal action to delete them.

    Since this script is the action part of a filter, the condition part of the filter needs to match all messages of interest. In this test the script is arranged after an action to copy the message to a local folder, that may or may not be necessary but it does download the message body to the local machine on an IMAP account so the action will do have a body to access.

    //FiltaQuilla filter action script: 
    for (let index = 0; index < msgHdrs.length; index++)
    { //get msg hdr:
      let hdr = msgHdrs.queryElementAt(index, Ci.nsIMsgDBHdr);
    
      //show subject: get it in two ways
      let s = hdr.getStringProperty("subject");
      if ( s != undefined && s.length != undefined && s.length > 0)
          Cu.reportError("  flt log1: " + s.length + " " + s);
      Cu.reportError("  flt log2: " + hdr.subject );
    
      //show all properties of hdr: uncomment to show.
      //let e = hdr.propertyEnumerator;
      //while ( e.hasMore() ) Cu.reportError("  flt log3: " + e.getNext() ); 
    
      //get body: from an example on MDC: getMessageBody(aMessageHeader)
      //Actions configured in the filter: Without [1], it takes long long time. 
      //    [1] copy msg to a local folder. 
      //    [2] "Javascript Action with Body" with this script. 
      { let messenger = Components.classes["@mozilla.org/messenger;1"]
                          .createInstance(Components.interfaces.nsIMessenger);
        let listener = Components.classes["@mozilla.org/network/sync-stream-listener;1"]
                          .createInstance(Components.interfaces.nsISyncStreamListener);
        let uri = hdr.folder.getUriForMsg(hdr);
        messenger.messageServiceFromURI(uri)
                    .streamMessage(uri, listener, null, null, false, "");
        let folder = hdr.folder;
        let body = folder.getMsgTextFromStream(listener.inputStream,
                                  hdr.Charset, 65536, 32768, false, true, { });
        //show body:
        Cu.reportError("  flt log4: " + body.length + " " + body);
      }
    }
    
  • mivk

    No, this doesn't seem to be possible.

    Such processing is probably easiest to do on the mail server, with stuff like procmail, Courier maildrop, amavisd-new, etc.

    Amavisd-new is specifically designed to pass mail to external processing programs before delivery, usually anti-virus and anti-spam daemons.

    If you cannot do it on the server side, you would need to write an extension, but that is probably far too much work.

    Finally, maybe you can satisfy a good part of your needs with the FiltaQuilla extension, which supports regular expressions, running external programs, and javascript actions. You can find more info on the author's page.

    Personally, if I didn't have access to the mail server and if FiltaQuilla didn't satisfy my needs, I would chose to set up a local Postfix / IMAP mail server fetching the mail from the ISP and processing it before delivery with my custom scripts. It should be much easier than writing a TB extension. But it may be overkill for your needs.

  • RedGrittyBrick

    You could look for a POP3/IMAP proxy that has the scriptable filtering capabilities you need. It could be a proxy which runs on the same PC as Thunderbird.

  • G. Harrington

    With the extension: http://mesquilla.com/extensions/filtaquilla/

    you can create JavaScript and/or external files to execute, so it looks like a solid "maybe"


  • Related Question

    How to filter HTML encoded messages in Thunderbird?
  • avelldiroll

    I am using Thunderbird E-mail client v. 2.0.0.23

    I would like to filter all HTML encoded messages. For this I created a message filter with a customized header "Content-Type" but that does not work as expected (no messages are filtered).

    The filter looks like that:

    "Content-Type" "contains" "text/html"
    

    I did not find any related bug or workaround (no add-ons).

    Is my filter wrong? Should I try Thunderbird V. 3 beta?


  • Related Answers
  • avelldiroll

    For filters on the body of a message to work on an IMAP account, said account needs to be set up for offline use.

    In order to do so go to:

    Account settings > Offline & diskspace

    and tick "Make the messages in my inbox available when I am working offline"

    Then select the ad hoc folders after clicking on "Select folders for offline use ..."

    After that my filter will work.

  • random

    Any emails you receive in HTML format will contain the Content-type: text/html specification in the actual body of the message.

    So change your filter to the following:

    Body     contains     Content-type: text/html
    

    This will now allow you to filter the emails sent in HTML.