- Code: Select all
{ Script: Attachment Remover.poc
{ Author: PJR
{ Revision History:
{ 08 Feb 2004 - Initial version.
{ 13 Apr 2004 - Show error message if run from "Special" mailbox.
{
{ This is an "All-In-One" script that removes attachments from messages,
{ but keeps the messages.
{
{ If you run this script manually, it will ask you for confirmation to remove
{ each attachment for each of the currently selected messages. In this mode,
{ the script will not remove any attachment from any message
{ unless you explicitly answer "Yes".
{
{ If you run it automatically (with your Incoming Filters for example), then it
{ will automatically remove all attachments from incoming messages that
{ meet the specified criteria WITHOUT ASKING YOU.
{ By default, the specified criteria is that all attachments will be removed.
{ One way to use this script in such an automatic way would be to create
{ an Incoming filter that searches "From" for "spam-domain.com" and,
{ if true, calls this script to delete all attachments from the message.
{ Or, as another example, if you don't want to receive any attachments
{ from anybody, then change the filter to search "From" for "@".
{
{ In addition to using a filter to limit which messages are affected
{ by the script (as previously described), you can also specify
{ which attachments are removed from these messages based on
{ the filenames and/or the filename extensions of the attachments.
{ You specify these conditions in the script itself
{ rather than in the filter that calls the script (there are more notes on this
{ below).
{
{ Note to PocoScripters: I am not using the "External" command to let users
{ customize the behavior of the script because, in my opinion, that is not the
{ right tool for the job in this case. The script uses more complex logic
{ to filter filenames and filename extensions, and it would be sloppier and
{ more difficult to specify these filters in
{ an External variable or External file. I believe that it would be better to
{ just change the script itself.
{
{ This script will work regardless of whether or not you automatically decode
{ attachments to the Attach folder (via Tools > Options > Encoding Options).
{ If you decode attachments like this, this script will also delete
{ the corresponding disk files.
{
{ Note that this script only deletes attachments from messages that you download
{ to your computer. In other words, you cannot use this script to delete
{ attachments or messages on the mail server by calling it from a
{ Predownload filter. This also means that if you configure your anti-virus
{ software to scan incoming messages for viruses and remove infected
{ attachments, then this script might never get the chance to see
{ those attachments.
{
{ In its initial configuration, this script deletes attachments without
{ regard to filenames or extensions.
{ However, you can change the code in the "Filename Filter" subroutine
{ to explicitly include and exclude certain filenames and extensions.
{ The code to change is at the very end of the script. Look for label FF_MAIN.
{
{ If you do this, then
{ including and excluding specific filenames and extensions only
{ applies when you run the script in "automatic mode" such as if you call it
{ from the Incoming filters. In "manual mode", it will always present you with
{ *every* attachment and ask for your confirmation before deleting each one.
{ You can make manual mode work based on filenames and extensions if you
{ comment the first statement of subroutine "File Filter".
Set $OnErrorMessage ""
AddStrings $OnErrorMessage "Error in " $pocoScriptName
Set $OnErrorGoTo "POCO_ERROR"
{ Do not run if currently in a "Special" mailbox. }
StringPos #index "System.pocosys" $currentMailbox
If #index ! 0 Then SPECIAL_MAILBOX_ERROR
{ Get list of attachments for message }
ReadAttached $attachmentList %message
LineCount #attachmentListSize $attachmentList
if #attachmentListSize = 0 Then DONE
{ Determine whether user leaves attachments encoded
Set &isEncoded True
ReadHeader $tmp "X-Poco-Attachment:" %message
If $tmp = "" Then ENCODED
Set &isEncoded False
:ENCODED
ReadHeader $subject "Subject:" %message
ChopString $subject 41 999
Set $wasMessageChanged "False"
Set #i -1
{ For each attachment in the message... }
:MAIN_LOOP
Inc #i
If #i = #attachmentListSize Then SAVE
GetLine $attachmentName #i $attachmentList
Call DELETE_ROUTINE
Goto MAIN_LOOP
{ Now save the resulting message if necessary }
:SAVE
If $wasMessageChanged = "False" Then DONE
If #pocoScriptMode ! 5 Then DONE
SaveMessage %message $currentMailbox
DeleteMessage %message
:DONE
Exit
:SPECIAL_MAILBOX_ERROR
Set $tmp ""
AddStrings $tmp "Cannot remove attachments from messages in \"Special\" mailboxes.\n"
AddStrings $tmp "Please run the script from the actual mailbox (e.g. \"In\")"
MessageBox $tmp
Exit
:POCO_ERROR
Exit
{*******************************************************************************
{* Delete Routine
{*******************************************************************************
{* This subroutine deletes the current attachment, but only if all of the
{* criteria are met.
{*
{* This routine uses the global variables (unlike Filename Filter).
{*******************************************************************************
:DELETE_ROUTINE
Set $FF_filename $attachmentName
Call FILENAME_FILTER
If $FF_doDelete = "False" Then Return
If #pocoScriptMode ! 5 Then DELETE_IT
Set $prompt ""
AddStrings $prompt "MESSAGE: " $subject "\n"
AddStrings $prompt "ATTACHMENT: \"" $attachmentName "\"\n"
AddStrings $prompt "Do you want to delete this attachment? %var%"
InputBox $response $prompt "Yes|No"
If $response = "Yes" Then DELETE_IT
Return
:DELETE_IT
Set $wasMessageChanged "True"
DeleteAttached %message $attachmentName
If &isEncoded Then Return
FileExists &doesFileExist $attachmentName
If &doesFileExist Then DELETE_DISK_FILE
Return
:DELETE_DISK_FILE
DeleteFile $attachmentName
Return
{*******************************************************************************
{* Filename Filter
{*******************************************************************************
{* This subroutine determines whether the passed filename meets the criteria
{* for deletion of the attachment.
{*
{* $FF_filename (in/out) This variable should contain the filename (including
{* the path and extension) of the current attachment in
{* the message. This subroutine removes the path from
{* this variable and converts the remainder to lowercase.
{* $FF_doDelete (out) This variable will be set to "True" if the current
{* attachment should be deleted.
{*******************************************************************************
:FILENAME_FILTER
{ When running the script in manual mode, comment the next line if you want to
{ delete attachments based on filename and extension.
{ (In manual mode, you will be prompted for confirmation
{ regardless of this setting.)
If #pocoScriptMode = 5 Then FF_DELETE
{ First, remove the path to the filename }
StringPos #FF_index \ $FF_filename
If #FF_index = 0 Then FF_PARSE_EXTENSION
ChopString $FF_filename 1 #FF_index
Goto FILENAME_FILTER
{ Second, parse the extension }
:FF_PARSE_EXTENSION
Set $FF_extension ""
Lowercase $FF_filename
Set $FF_tmp $FF_filename
:FF_EXTENSION_LOOP
StringPos #FF_index "." $FF_tmp
If #FF_index = 0 Then FF_MAIN
ChopString $FF_tmp 1 #FF_index
Set $FF_extension $FF_tmp
Goto FF_EXTENSION_LOOP
{ Finally, decide whether or not to remove this attachment. }
:FF_MAIN
{ Uncomment any of the following lines or add your own. }
{ Here are examples that check the extension:
{ If $FF_extension = "jpg" Then FF_KEEP
{ If $FF_extension = "pdf" Then FF_KEEP
{ If $FF_extension = "" Then FF_DELETE
{ If $FF_extension = "bat" Then FF_DELETE
{ If $FF_extension = "exe" Then FF_DELETE
{ If $FF_extension = "scr" Then FF_DELETE
{ Here are examples that check the filename (and extension):
{ If "phrase one" ^ $FF_filename Then FF_DELETE
{ If "phrase two" ^ $FF_filename Then FF_KEEP
{ If no matches were found so far, then use the following default action:
Goto FF_DELETE
{Goto FF_KEEP
:FF_DELETE
Set $FF_doDelete "True"
Return
:FF_KEEP
Set $FF_doDelete "False"
Return