#!/usr/bin/tclsh

# This plugin looks for image links. .

# This operates on the raw version of the file, since the filtered version has
# the closing </A> tags removed, along with all closing tags as they are often
# used to break up words.

# Written by Michael McConnell, Eridani Star System. This demo file is in the
# public domain. Feel free to use this script as a template for your own.

set MSTRIP_CMD [lindex $argv 0]
set MSTRIP_VER [lindex $argv 1]
set MSTRIP_SRCIP [lindex $argv 2]
set MSTRIP_RMSG [lindex $argv 3]
set MSTRIP_SMSG [lindex $argv 4]

# The score associated with a linked image
set SCORE 15

if { $MSTRIP_CMD == "-id" } {
  puts "Linked Images Plugin"
  puts "0.1.0"
  puts "Eridani Star System, this demo is in the public domain"
  if {![string length $MSTRIP_VER]} {
    puts "No"
  } else {
    puts "Yes"
  }
} elseif { $MSTRIP_CMD == "-scan" } {
  if {[catch {set fh [open $MSTRIP_RMSG "r"]}]} {
    # Could not open the message file. This shouldn't happen!!
    # Ensure it goes to the MailStripper log file / onscreen in 'score'.
    # Returning 0 causes it to have no effect on the final score.
    puts "#Error: Unable to open raw message file"
    puts "0"
    exit
  }
  set datablock [read $fh]
  close $fh

  set r [regexp -all -nocase {<a.*?href="[^\"]*?".*?><img.*?src="[^\"]*?".*?></a>} $datablock]
  puts [expr {$r * $SCORE}]
}
