Platon Technologies
neprihlásený Prihlásiť Registrácia
SlovakEnglish
open source software development oslavujeme 10 rokov vývoja otvoreného softvéru! Štvrtok, 28. marec 2024

Súbor: [Platon] / scripts / shell / sms / sms.sh (stiahnutie)

Revízia 1.5, Mon Jan 27 15:41:51 2003 UTC (21 years, 2 months ago) by nepto


Zmeny od 1.4: +25 -12 [lines]

Header update with nice changelog, correct copyright dates and $Platon.

#!/bin/sh

#
# scripts/shell/sms/ - SMS sending console scripts
#
# sms.sh - main configuration handler, plugin loader and SMS sender
# ____________________________________________________________
#
# Developed by Lubomir Host <rajo@platon.sk>
# Copyright (c) 2002-2003 Platon SDG, http://www.platon.sk/
# All rights reserved.
#
# See README file for more information about this software.
# See COPYING file for license information.
#
# Download the latest version from
# http://www.platon.sk/projects/scripts/
#

# $Platon: phpWebFileManager/index.php,v 1.34 2003/01/07 16:19:04 nepto Exp $

#
# Based on script flash.sh by Ondrej Jombik <nepto@platon.sk>
# and initial implementation by Martin Karas <wolcano@platon.sk>
#
# 2002/06/12 - wolcano - initial working release
# 2002/06/20 - nepto   - alias support added
# 2002/06/21 - rajo    - alias support simplification
# 2002/09/07 - rajo    - added support for images as secure codes
# 2003/01/26 - rajo    - mostly rewritten, added support for plugins
#                      - added plugin for e-zones.sk
#

umask 077

# remove next line to turn debug off
#DEBUG="yes"

# 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_DIR="$HOME/.sms.sh"
CONFIG_FILE="$CONFIG_DIR/conf";
ALIAS_FILE="$CONFIG_DIR/alias";

WWW="http://www.sity.sk"

# Function usage() {{{
usage()
{
    # calculate number of spaces used in aligment of usage message
    S=`echo $0 | cut -b -30 | sed 's/./ /g;'`;
    cat <<EOF
Usage:
  $0
  $S --site <e-zones.sk|sity.sk|sms.markiza.sk>
  $S [ --alias-file  <alias-file>    ]
  $S [ --config-file <config-file>   ]    
  $S [ --tmp-file    <tmp-file>      ]    
  $S [ --debug[=value]               ]
  $S [ --username    <username>      ]
  $S [ --number ]    <number-or-alias>
    
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() }}}

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" ;;

        --site | --sit | --si | --s | \
            -site | -sit | -si | -s)
            prev=SITE ;;
        --site=* | --sit=* | --si=* | --s=* | \
            -site=* | -sit=* | -si=* | -s=*)
            SITE="$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 "$SITE" ]; then # site was not specified on cmdline
        echo -n "Looking for site... ";
        SITE=`awk 'BEGIN { FS="[     =]+" } /^#/ { next; } \
            /^site/ { if (NF > 2) { print $3; exit; } \
                else { print $2; } exit; }' "$CONFIG_FILE"`;
        if [ -z "$SITE" ]; then
            echo "not found.";
        else
            if [ -f "$CONFIG_DIR/$SITE/sms-plugin.sh" ]; then
                echo "found '$SITE'";
            else
                echo; echo "Plugin for site '$SITE' not found" \
                    "(trying '$CONFIG_DIR/$SITE/sms-plugin.sh'"
            fi
        fi
    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
    PASSWD=`awk 'BEGIN { FS="[    =]+" } /^#/ { next; } \
        /^pass/ { if (NF > 2) { print $3; exit; } \
            else { print $2; } exit; }' "$CONFIG_FILE"`;
    if [ ! -z "$DEBUG" ]; then
        if [ -z "$PASSWD" ]; 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

# load site plugin
if [ -f "$CONFIG_DIR/$SITE/sms-plugin.sh" ]; then
    echo "Loading plugin for site '$SITE'"
    . "$CONFIG_DIR/$SITE/sms-plugin.sh"    
else
    if [ "X" = "X$SITE" ]; then
        echo 'No site defined.';
        echo 'Add "site" clausule to config file or pass "--site" option on command line.';
    else
        echo "Plugin for site '$SITE' not found."
    fi;
    exit 1;
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 "$PASSWD" ]; then
    echo -n 'Enter password: ';
    stty -echo;
    IF= read PASSWD;
    stty echo;
    echo "<censored>"; # to write newline on output
fi
# }}}

#
# FUNCTIONS
#
# {{{

strip_html_tags()
{ # {{{
    awk '{
        gsub(/<[^>]*>/, "");
        gsub(/<[^>]*$/, "");
        gsub(/^[^>]*>/, "");
        gsub(/^[     ]*$/, "");
        print;
    }' | cat -s
} # }}}

img2text()
{ # {{{

    awk '#!/usr/bin/awk -f

    # $Id: sms.sh,v 1.5 2003/01/27 15:41:51 nepto Exp $

    # Author:  Lubomir Host rajo <rajo AT host.sk>
    # 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; # different char. for colors
            colormap[color] = 35; # same char. for colors

        }
        if (column == 0 && line == 0 ||
            column == 1 && line == 1) {
            # first pixel 0,0 or 1,1 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()

make_http_request_GET()
{ # {{{

    # if --debug option is specified, save output from $GET to $TMP_FILE,
    echo "## Time: `date`"        >> $TMP_FILE
    echo "## Request: $* {{{"    >> $TMP_FILE

    for option
    do
        case "$option" in

            -*) ;; # ignore

            Cookie:*)
                cookies="`echo $option | sed 's/^Cookie: //;'`" ;;
            http:*)
                eval "http=\"$option\"" ;;
        esac
    done
    if [ -z "$cookies" ]; then
        cookies="$COOKIE"
    fi
    if [ ! -z "$DEBUG" ]; then
        echo "GET -H 'Cookie: $cookies' '$http'" > /dev/stderr
    fi
    echo "$cookies $http" | perl -w -e '
    #!/usr/bin/perl -w
    # GET implementation {{{

    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 (<STDIN>) {
        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);
    }
    # end of GET implementation }}}
    exit;' | tee -a $TMP_FILE
    echo "## }}} request done " >> $TMP_FILE

} # }}}

http_set_cookies()
{ # {{{
    #echo "http_set_cookies: parameter=$1"
    COOKIE_tmp="`$GET -P -e "$BASE_URL$1" \
        | awk 'BEGIN { FS="[ ;]+" } /^Set-Cookie:/ { print $2; }'`";
    if [ ! -z "$COOKIE_tmp" ]; then
        if [ -z "$COOKIE" ]; then
            COOKIE="$COOKIE_tmp"
        else
            if [ -z "`echo '$COOKIE' | grep '$COOKIE_tmp'`" ]; then
                COOKIE="$COOKIE; $COOKIE_tmp"
            fi
        fi
        # create also variable COOKIE_<variable_name>
        eval "COOKIE_$COOKIE_tmp"
        #echo "created variable COOKIE_$COOKIE_tmp"
        #echo "Cookies are '$COOKIE'"
    else
        if [ ! -z "$DEBUG" ]; then
            echo "No cookies found at '$1'"
        fi
    fi
} # }}}

http_authentification()
{ # {{{
    # $1 = "/pls/ezones/!e_single_sign_on.login"
    # $2 = "http_auth_request"
    # http_auth_request = "frame=0&page=&menu=&extrapar=&user_name=$NAME&passwd=$PASSWD"
    url="$1?${!2}" # total cool brutal ;)
    
    #echo "http_authentification: parameter=$BASE_URL$1"
    eval "http_set_cookies \"$url\""

} # }}}

make_request()
{ # {{{
    for request
    do
        case "$request" in
            
            read_message*)
                read_message ;;

            http_get_cookies*)
                http_set_cookies "${!request}";;

            http_auth*)
                http_authentification "${!request}" "${request}_request";;

            http_check_image*)
                get_check_code "${!request}";;

            http_send_msg*)
                http_send_msg "${!request}";;

            *)
            echo "Unknown request type $request";
            exit 1 ;;
        esac
    done
} # }}}

read_message()
{ # {{{
    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;
        }'`"
    TEXT_LEN="`echo \"$TEXT\" | wc -c | awk '{ print $1}'`"

    if [ "x$TEXT" == "x+" ]; then
        echo "Empty message is not allowed.";
        exit 1;
    fi
} # }}}

get_check_code()
{ # {{{
    if [ ! -z "$DEBUG" ]; then
        echo "image URL: $1"
    fi
    while [ -z "$CHECKCODE" ]; do
        $GET -P -H "Cookie: $COOKIE" "$BASE_URL$1" \
        | perl -e 'while (<STDIN>) { chomp; last unless (m/\S/); } my $haluz=<STDIN>; while (<STDIN>) { print; }' \
        | convert - txt:- | img2text

        echo -en "Please enter CHECK code (or press Ctrl+d to obtain another version): "
        read CHECKCODE;
    done
} # }}}

http_send_msg()
{ # {{{
    if [ -z "$OUTPUT_FILTER" ]; then
        $GET -P -H "Cookie: $COOKIE" "$BASE_URL$1";
    else
        eval "$GET -P -H \"Cookie: $COOKIE\" \"$BASE_URL$1\" | $OUTPUT_FILTER"
    fi

} # }}}

# FUNCTIONS }}}

#
# MAIN
#
#
if [ -z "$HTTP_REQUESTS_ORDER" ]; then
    echo 'Did you specify $HTTP_REQUESTS_ORDER in your plugin??'
    exit 1
fi
for curr_request in $HTTP_REQUESTS_ORDER; do
    #echo "Requesting ${!curr_request}";
    make_request $curr_request    
done

# SUCCESS ! ;)

exit

# Modeline {{{
# vim:set ts=4:
# vim600:fdm=marker fdl=0 fdc=3 vb t_vb=:
# }}}


Platon Group <platon@platon.sk> http://platon.sk/
Copyright © 2002-2006 Platon Group
Stránka používa redakčný systém Metafox
Na začiatok