#!/bin/sh # # sms.sh - sity.sk SMS sender # # Written by Martin Karas. # [2002/06/02] - Ondrej Jombik 'Nepto' # Alias support # [2002/06/21] - Lubomir Host 'rajo' # Alias support simplification # [2002/09/07] - Lubomir Host 'rajo' # Added support for images as secure codes :-) # Based on script flash.sh by Ondrej Jombik # Copyright (c) 2002 Platon SDG, http://www.platon.sk/ # # $Id: sms.sh,v 1.45 2002/09/12 19:57:01 rajo Exp $ umask 077 # Uncoment following line, if you will use /usr/bin/GET, or specify # to your own 'GET' program. Program must accept '-e' '-P' '-H' options # and use cookies. #GET="/usr/bin/GET" GET="make_http_request_GET" # use build-in function # default CONFIG_FILE="$HOME/.sms.sh.conf"; ALIAS_FILE="$HOME/.sms.sh.alias"; WWW="http://www.sity.sk" # Function usage() {{{ usage() { # calculate number of spaces used in aligment of usage message S=`echo $0 | sed 's/./ /g;'` cat < ] $S [--config-file ] $S [--tmp-file ] $S [--debug[=value] ] $S [--username ] $S [--number] Message is taken from standart input. Use special characters with care. Spaces are handled properly. If --alias-file and/or --config-file isn't specified, default values are used. EOF exit 1; } # usage() }}} # Function make_http_request_GET() {{{ make_http_request_GET() { # if --debug option is specified, save output from $GET to $TMP_FILE, echo "## Time: " >> $TMP_FILE echo "## Request: $* {{{" >> $TMP_FILE for option do case "$option" in -*) ;; # ignore Cookie:*) cookies="`echo $option | sed 's/^Cookie: //;'`" ;; http:*) http="$option" ;; esac done if [ ! -z "$DEBUG" ]; then echo "GET -H 'Cookie: $cookies' '$http'" > /dev/stderr fi echo "$cookies $http" | perl -w -e ' #!/usr/bin/perl -w use IO::Socket; $| = 1; # autoflush sub http_get { my $proxy = shift; my $url = shift; my $cookies = shift; my $port = 80; my $msg = ""; my $sock = undef; my $tries = 0; while ( !$sock && $tries < 5 ) { $sock = new IO::Socket::INET( Proto => "tcp", PeerAddr => $proxy, PeerPort => $port, Type => SOCK_STREAM, ); if ( !$sock ) { print; print "Can NOT connect to ${proxy}:${port} ! Trying again ..."; $tries++; } } if ( !$sock ) { print "$proxy is death... :(\n\n"; return; } # make http request print $sock "GET $url HTTP/1.1\r\n"; print $sock "TE: deflate, gzip;q=0.3\r\n"; print $sock "Connection: TE, close\r\n"; print $sock "Host: $proxy\r\n"; print $sock "User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Linux [en]\r\n"; if ($cookies =~ "Cookie: ") { print $sock "$cookies\r\n"; } print $sock "\r\n"; $sock->flush(); while (<$sock>) { print; } $sock->close(); return; } while () { my $http; my $server; my $url; my $cookies = ""; chomp; ($cookies, $http) = split /http:\/\//, $_, 2; #$http =~ s|^http://||; ($server, $url) = split /\//, $http, 2; substr($cookies, 0, 0) = "Cookie: "; # put string at begin substr($url, 0, 0) = "/"; #print "http=$http\ncookies=$cookies\nurl=$url\nserver=$server\n"; http_get($server, $url, $cookies); } exit;' | tee -a $TMP_FILE echo "## }}} request done " >> $TMP_FILE } # make_http_request_GET() }}} # implementation of img2text {{{ img2text() { awk '#!/usr/bin/awk -f # $Id: sms.sh,v 1.45 2002/09/12 19:57:01 rajo Exp $ # Author: Lubomir Host rajo # License: GNU GPL # Script for converting images to text files. # Usefull for script, which will send SMS message over public SMS gateway. ;) # Usage: cat img.png | convert - text:- | ./img2text # Enjoy flooding sms gateway! ;)) BEGIN { FS="[,:# \t]+" prev_line = 0; line = 1; column = 0; color = ""; start_char = 41; } { n = split($0, a, FS); column = a[1]; line = a[2]; color = a[6]; if (colormap[color] == 0) { #printf "adding color %s to colormap\n", color; colormap[color] = ++start_char; } if (column == 0 && line == 0) { # first pixel 0,0 is probably background color, # we will background filled with spaces colormap[color] = 32; } output[line] = sprintf("%s%c", output[line], colormap[color]); } END { for (i = 0; i <= line; i++) { if (output[i] !~ /^ +$/) printf "%s\n", output[i]; } }' } # }}} img2text() if [ "$#" -lt 1 ]; then usage fi # Cmdline parsing {{{ for option do # If the previous option needs an argument, assign it. if test -n "$prev"; then eval "$prev=\$option" prev= continue fi case "$option" in -*=*) optarg=`echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'` ;; *) optarg= ;; esac case "$option" in --number | --numbe | --numb | --num | --nu | --n | \ -number | -numbe | -numb | -num | -nu | -n) prev=prev_number ;; --number=* | --numbe=* | --numb=* | --num=* | --nu=* | --n=* | \ -number=* | -numbe=* | -numb=* | -num=* | -nu=* | -n=*) prev_number="$optarg" ;; --alias-file | --alias-fil | --alias-fi | --alias-f | --alias | --alia | \ --ali | --al | --a | \ -alias-file | -alias-fil | -alias-fi | -alias-f | -alias | -alia | \ -ali | -al | -a) prev=ALIAS_FILE ;; --alias-file=* | --alias-fil=* | --alias-fi=* | --alias-f=* | --alia=* | \ --ali=* | --al=* | --a=* | \ -alias-file=* | -alias-fil=* | -alias-fi=* | -alias-f=* | -alia=* | \ -ali=* | -al=* | -a=*) ALIAS_FILE="$optarg" ;; --config-file | --config-fil | --config-fi | --config-f | --config | \ --confi | --conf | --con | --co | --c | \ -config-file | -config-fil | -config-fi | -config-f | -config | \ -confi | -conf | -con | -co | -c) prev=CONFIG_FILE ;; --config-file=* | --config-fil=* | --config-fi=* | --config-f=* | --config=* | \ --confi=* | --conf=* | --con=* | --co=* | --c=* | \ -config-file=* | -config-fil=* | -config-fi=* | -config-f=* | -config=* | \ -confi=* | -conf=* | -con=* | -co=* | -c=*) CONFIG_FILE="$optarg" ;; --tmp-file | --tmp-fil | --tmp-fi | --tmp-f | \ --tmp | --tm | --t | \ -tmp-file | -tmp-fil | -tmp-fi | -tmp-f | \ -tmp | -tm | -t) prev=TMP_FILE ;; --tmp-file=* | --tmp-fil=* | --tmp-fi=* | --tmp-f=* | \ --tmp=* | --tm=* | --t=* | \ -tmp-file=* | -tmp-fil=* | -tmp-fi=* | -tmp-f=* | \ -tmp=* | -tm=* | -t=*) TMP_FILE="$optarg" ;; --username | --usernam | --userna | --usern | --user | \ --use | --us | --u | \ -username | -usernam | -userna | -usern | -user | \ -use | -us | -u) prev=NAME ;; --username=* | --usernam=* | --userna=* | --usern=* | --user=* | \ --use=* | --us=* | --u=* | \ -username=* | -usernam=* | -userna=* | -usern=* | -user=* | \ -use=* | -us=* | -u=*) NAME="$optarg" ;; --debug | --debu | --deb | --de | --d | \ -debug | -debu | -deb | -de | -d) DEBUG="yes" ;; --debug=* | --debu=* | --deb=* | --de=* | --d=* | \ -debug=* | -debu=* | -deb=* | -de=* | -d=*) DEBUG="$optarg" ;; --help | --hel | --he | --h | -help | -hel | -he | -h) usage; ;; -*) { echo "error: $option: invalid option; use --help to show usage" 1>&2; exit 1; } ;; *) prev_number="$option" ;; esac done # Cmdline parsing }}} # create tmpfile only if '--debug' is on if [ ! -z "$DEBUG" ]; then # if '--tmpfile' not specified on cmdline, create tmpfile with tempfile test -z "$TMP_FILE" \ && TMP_FILE=`tempfile -p sms. -s .html \ || mktemp /tmp/sms.XXXXXX \ || echo /tmp/sms.$$.html` echo "Saving debug output to $TMP_FILE" else test -z "$TMP_FILE" \ && TMP_FILE="/dev/null" fi number=`echo "$prev_number" | grep '^[0-9]\+$'`; # check for alias {{{ if [ -z "$number" ]; then if [ ! -z "$DEBUG" ]; then echo "Parameter '$prev_number' treated as an alias."; echo -n "Looking for alias file '$ALIAS_FILE'... "; fi if [ -f "$ALIAS_FILE" ]; then if [ ! -z "$DEBUG" ]; then echo "found."; fi echo -n "Looking for alias '$prev_number'... "; number=`awk "BEGIN { alias=tolower(\"$prev_number\"); } \ /^#/ { next; } \ // { \\$0 = tolower(\\$0); } \ \\$0 ~ alias { if (NF > 2) { print \\$3; } \ else { print \\$2; } exit; }" "$ALIAS_FILE"`; if [ "$number" ]; then echo "found number '$number'."; else echo "not found."; exit 1; fi else if [ ! -z "$DEBUG" ]; then echo "not found."; else echo "Alias file '$ALIAS_FILE' not found."; fi exit 1; fi fi # }}} # Config file parsing {{{ # Lines begining with "^#" are comments and are ignored. # equal sign '=' is not needed # username, usernam userna, .. user supported # password, ... supported if [ ! -z "$DEBUG" ]; then echo -n "Looking for config file '$CONFIG_FILE'... "; fi if [ -f "$CONFIG_FILE" ]; then if [ ! -z "$DEBUG" ]; then echo "found."; fi if [ -z "$NAME" ]; then # username was not specified on cmdline echo -n "Looking for username... "; NAME=`awk 'BEGIN { FS="[ =]+" } /^#/ { next; } \ /^user/ { if (NF > 2) { print $3; exit; } \ else { print $2; } exit; }' "$CONFIG_FILE"`; if [ -z "$NAME" ]; then echo "not found."; else echo "found '$NAME'"; fi fi if [ ! -z "$DEBUG" ]; then echo -n "Looking for password... "; fi PASS=`awk 'BEGIN { FS="[ =]+" } /^#/ { next; } \ /^pass/ { if (NF > 2) { print $3; exit; } \ else { print $2; } exit; }' "$CONFIG_FILE"`; if [ ! -z "$DEBUG" ]; then if [ -z "$PASS" ]; then echo "not found."; else echo "found."; fi fi else if [ ! -z "$DEBUG" ]; then echo "not found."; else echo "Config file '$CONFIG_FILE' not found."; fi fi # }}} # User's login from stdin {{{ if [ -z "$NAME" ]; then echo -n 'Enter username: '; IF= read NAME; fi if [ -z "$NAME" ]; then echo "Empty username is not allowed."; exit 1; fi if [ -z "$PASS" ]; then echo -n 'Enter password: '; stty -echo; IF= read PASS; stty echo; echo ""; # to write newline on output fi # }}} # Get cookies {{{ # download the file via special wget which stores session cookie to a file # Cookies format: # ^Set-Cookie: id=0a06b40e84a780fa;expires=Saturday, 07-Sep-2002 07:43:41 GMT # ^Set-Cookie: SITY=32E0DDB9AF9293E774AA1C0B5BCC0992;path=/$ echo 'Logging in...'; COOKIE=`$GET -P -e -H "Cookie: SITY=SITY" \ $WWW/pls/sity/u_registrate2.login2?NAME="$NAME"\&PASSWD="$PASS"\&ASERVICE=0\&CMD=%21u_sms.home\&SERVICE=0 \ | awk 'BEGIN { FS="[ ;]+" } /^Set-Cookie:/ { print $2; }'`; # }}} # Retrieving submit forms... {{{ echo 'Retrieving submit forms...'; #we now extract textarea name, which will be used later request=` $GET -P -H "Cookie: $COOKIE" \ $WWW/pls/sity/\!u_sms.home \ | awk -v number="$number" ' BEGIN { printf "&SMSMsgLen=480&SMScount=&"; } /textarea cols/ { sub(/^.*name="/, ""); sub(/".*$/, ""); printf "%s=%s&BT.x=0&BT.y=0&DETAIL=", $0, number; } /DETAIL/ { sub(/^.*value="/, ""); sub(/".*$/, ""); printf "%s", $0; } /CHECK/ { # kontrolny retazec sub(/^.*src="/, ""); sub(/>.*$/, ""); printf "CHECK=%s", $0; } '` # }}} # Getting check code {{{ url=`echo $request | sed 's/.*CHECK=\([^"]\+\)".*$/\1/;'` # we must set id=... also as cookies :-) COOKIE=`echo "$url; $COOKIE" | sed 's/^[^?]*?//g;'` if [ ! -z "$DEBUG" ]; then echo "image URL: $WWW$url" fi while [ -z "$checkcode" ]; do $GET -P -H "Cookie: $COOKIE" "$WWW$url" \ | perl -e 'while () { chomp; last unless (m/\S/); } my $haluz=; while () { print; }' \ | convert - txt:- | img2text | head -n 15 echo -en "Please enter CHECK code (or press Ctrl+d to obtain another version): " read checkcode; done request=`echo $request | sed "s/CHECK=[^\"]\+\"\(.*\)$/CHECK=$checkcode\&\1/g;"` # }}} # Checking number of free SMSs {{{ echo -n 'Checking number of free SMSs... '; smss_sent=`$GET -P -H "Cookie: $COOKIE" \ $WWW/pls/sity/u_profil.show_stat \ | awk '/^[0-9]+<\/TD>$/ { gsub(/[><]/, " "); print $2; exit; } '`; echo "100-$smss_sent" | bc; # }}} # Read message from stdin {{{ echo 'Enter your message (max 420 chars, end with CTRL-D):'; echo 'Please wait at least 4 seconds to submit form.'; text=`cat` # text from stdin, multiple lines text="`echo \"$text\" \ | awk 'BEGIN { ORS="+" } \ { gsub(/%/, "%25"); \ gsub(/\+/, "%2B"); \ gsub(/ /, "+"); \ gsub(/\&/, "%26"); \ print; }'`" # }}} if [ "x$text" == "x+" ]; then echo "Empty message is not allowed."; exit 1; fi # Submitting SMS... {{{ echo 'Submitting SMS...'; smss_left=`$GET -P -H "Cookie: $COOKIE" \ $WWW/pls/sity/\!u_sms.send_sms?MSG_TEXT="$text""$request" \ | grep "Tento mesiac" \ | sed 's/.*va \([0-9]*\) spr.*/\1/'` ; # }}} if [ $smss_left ]; then echo "Message sent. $smss_left SMSs left for this month."; else if [ -z "$DEBUG" ]; then echo "Something went wrong, please use '--debug' option for debugging"; else echo "Something went wrong, tmp file $TMP_FILE not removed for debug purposes!"; fi fi # Modeline {{{ # vim:set ts=4: # vim600:fdm=marker fdl=0 fdc=3 vb t_vb=: # }}}