Friday, October 14, 2005

[Tcl] Send SMS to Singtel customer via command line

#! /usr/local/bin/wish

#
# Send SMS via the SingTel web page by cracking their
# spamcode.
#   step1: visit send sms page to get valid cookies
#   step2: extract the spamcode image url and retrieve
#   step3: send sms with the codes and crack spamcode
#
# Note: you need X windows
#



if { $argc < 2 } {
 puts stderr "Usage: $argv0 handphone message ..."
 exit 1
}
set handphone [lindex $argv 0]
set message [string range $argv [string first [lindex $argv 1] $argv] end]
if { ![regexp {^9\d{7}$} $handphone] } {
 puts stderr "Error. Invalid handphone number"
 exit 2
}


package require http
wm withdraw .


#
# url
#
set host 165.21.20.138
set url1 "http://$host/consumer/msg_center/internet_sms.asp"
set url2 "http://$host/consumer/msg_center/"
set url3 "http://$host/consumer/msg_center/internet_sms_process.asp"


#
# look like a browser
#
http::config  -useragent {Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.0.1) Gecko/20020920 Netscape/7.0}  -accept {text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1}


#
# http headers
#
set headers [list  Accept-Language {en-us, en;q=0.50}  Accept-Encoding {gzip, deflate, compress;q=0.9}  Accept-Charset {ISO-8859-1, utf-8;q=0.66, *;q=0.66}  Keep-Alive {300}  Connection {close} ]

 

#
# finding SITEID and ASSPSESSIONID cookies
#
set s [http::geturl $url1 -headers $headers]
set d [http::data $s]
set ck [lsearch [set ${s}(meta)] {Set-Cookie}]
set cookie1 [lindex [split [lindex [set ${s}(meta)] [expr $ck + 1]] {;}] 0]
set lmeta [lrange [set ${s}(meta)] [expr $ck + 1] end]
set ck [lsearch $lmeta {Set-Cookie}]
set cookie2 [lindex [split [lindex $lmeta [expr $ck + 1]] {;}] 0]
http::cleanup $s


#
# find the spamcode url
#
set rc [regexp {Your SMS ID is \(<img src='([^']+)'>} $d dummy spamcode]
if { $rc != 1 } {
 puts "Error. Unable to extract the spamcode url. Abort"
 exit 1
}
set url2 "$url2$spamcode"


#
# get spamcode image
#
set tmpfile ".tmp[pid].gif"
set fp [open $tmpfile w]
fconfigure $fp -translation binary
lappend headers Cookie $cookie1 Cookie $cookie2
set s [http::geturl $url2 -channel $fp -headers $headers]
close $fp


#
# decode gif image to numbers
#
set i [image create photo -file $tmpfile]
set idata [$i data -background #ff0000]
 #
 # spamcode Nxx
 #
 set nxx {}
 for {set j 0 } { $j < 9 } { incr j } {
  append nxx [join [lrange [lindex $idata $j] 1 5] {}]
 }
 regsub -all {#ff0000} $nxx {o} a
 regsub -all {#000000} $a {i} b
 set nxx $b

 #
 # spamcode xNx
 #
 set xnx {}
 for {set j 0 } { $j < 9 } { incr j } {
  append xnx [join [lrange [lindex $idata $j] 8 12] {}]
 }
 regsub -all {#ff0000} $xnx {o} a
 regsub -all {#000000} $a {i} b
 set xnx $b

 #
 # spamcode xxN
 #
 set xxn {}
 for {set j 0 } { $j < 9 } { incr j } {
  append xxn [join [lrange [lindex $idata $j] 15 19] {}]
 }
 regsub -all {#ff0000} $xxn {o} a
 regsub -all {#000000} $a {i} b
 set xxn $b

 #
 # image to number mapping
 #
 set code(0) oiiioioooiioooiioooiioooiioooiioooiioooioiiio
 set code(1) oiiioioooiooooiooooiooiioooooiooooiioooioiiio
 set code(2) oiiioioooiooooiooooioooiooooioooioooioooiiiii
 set code(3) oiiioioooiooooiooooiooiioooooiooooiioooioiiio
 set code(4) oooioooiioooiiooioiooioioiooioiiiiioooiooooio
 set code(5) oiiiioioooiooooiiiioioooiooooiooooiioooioiiio
 set code(6) oiiioioooiiooooioiioiiooiioooiioooiioooioiiio
 set code(7) iiiiioooiooooioooiooooiooooioooiooooiooooiooo
 set code(8) oiiioioooiioooiioooioiiioioooiioooiioooioiiio
 set code(9) oiiioioooiioooiioooiiooiioiioiooooiioooioiiio

 #
 # matching
 #
 for { set j 0 } { $j <= 9 } { incr j } {
  if { [string equal $nxx $code($j)] == 1 } { set a $j }
  if { [string equal $xnx $code($j)] == 1 } { set b $j }
  if { [string equal $xxn $code($j)] == 1 } { set c $j }
 }


#
# send sms message
#
set q [http::formatQuery  mobile_no $handphone  typed_spam_code $a$b$c  message "$message"  charstyped [llength $message] ]
set s [http::geturl $url3 -query $q -headers $headers]
http::cleanup $s


#
# cleaning up
#
file delete -force $tmpfile



exit

1 Comments:

Blogger das said...

Hi,

IS it possible to send SMS to mobile from unix script?Pls send me the code.

11:59 AM  

Post a Comment

<< Home