[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Needed: new cellcom SMS script
On Sun, May 21, 2000, Alon Altman wrote about "Needed: new cellcom SMS script":
> Hi,
> A cellcom script written by Nadav Har'el has been posted here some time
> ago. Recently (today), cellcom has reformed their site, and the script no
> longer works.
> It would be nice if an updated script will be posted here.
>
> Thanks,
> Alon
Ok. Exactly 12 hours after Cellcom switched sites, I'm attaching the new
sendsms script which can send short messages to cellcom phones (using the
new site) and Orange (using their official site, not gosms.com, currently,
although I also have a version using gosms.com). Needless to say, sending
messages in this way is free, and you don't have to be a client of these
companies.
Don't forget to read the Installation Instructions inside the perl script.
The script will not work if you don't follow these instructions!
BTW, for users of the previous scripts I released, note the following points:
1. Newlines in messages to cellcom no longer worked correctly (they never
worked in Orange). Unless Cellcom cooperates, it appears it will never
work.
2. If you used Cellcom's old site and have a username and password, please
try sending one message from their new site (isend.cellcom.co.il) before
using the script, to see that it works. Note that instead of your "user
name", you'll have to use your full email now (the same email you gave
cellcom when you originally registered). The password remains the same.
Cellcom will show you a new license - read it.
Note, though, that if your password had weird punctuations in it, you
won't be able to enter it into the new site, because of their weird
Javascript! In this case, just try to use my script immediately. I think
it should work, even if you never do get to read that new license.
Have fun, and send any ideas or patches (particularly a patch to add pelephon
support!) to me.
Nadav.
--
Nadav Har'El | Sunday, May 21 2000, 17 Iyyar 5760
nyh@math.technion.ac.il |-----------------------------------------
Phone: +972-53-245868, ICQ 13349191 |There are 2 ways to do it - my way and
http://nadav.harel.org.il |the right way
-- Attached file included as plaintext by Listar --
#!/usr/local/bin/perl -w
##############################################################################
# SMS sending script for Cellcom and Orange Israeli cellular phones
##############################################################################
#
#Copyright (C) 1999-2000 Nadav Har'El
#License: GNU GPL
#
##############################################################################
#
#Installation instructions: (don't forget to read this part!)
#
#1. You'll have to install the following perl libraries first
# Digest-MD5-2.09.tar.gz MIME-Base64-2.11.tar.gz libnet-1.0606.tar.gz
# HTML-Parser-2.23.tar.gz URI-1.04.tar.gz libwww-perl-5.44.tar.gz
# (the version numbers are the ones I have - later versions should be ok)
# Get them from www.cpan.org, if you don't have them.
# Change the "BEGIN" line below to list the directory where you installed
# these libraries (or remove that line altogether if you installed them
# in the system's official Perl directory).
#
#2. You'll need accounts from cellcom and/or orange to send SMSs. These
# are free, and have nothing to do with you having a cellular phone from
# them or not. Register on the following sites:
# Cellcom: http://isend.cellcom.co.il/English/Registration.htm
# Orange: http://www.orange.co.il/orange_online/free_sms/scripts/reg.asp
#
#3. Set the CELLCOMTEXT_USER, CELLCOMTEXT_PASSWORD, ORANGETEXT_USER and
# ORANGETEXT_PASSWORD below. If you do not do that for a particular company
# (Cellcom or Orange), you won't be able to send SMSs to numbers in that
# company.
#
##############################################################################
#TODO: convert this to object oriented, with different classes for different
# companies with the same interface (use interitance).
require 5.004;
BEGIN {push @INC, ("/home/nyh/sendsms/lib/perl5/site_perl")}
use strict;
use Carp;
use LWP::UserAgent;
use URI::Escape;
use HTTP::Cookies; # Orange uses cookies;
#############################################################################
### User-specific information: TODO: this information should be got from
### a user configuration file.
#DON'T FORGET TO CONFIGURE THIS! (see instructions above)
my $CELLCOMTEXT_USER='you@your.address'; my $CELLCOMTEXT_PASSWORD='pAsswOrd';
my $ORANGETEXT_USER='you'; my $ORANGETEXT_PASSWORD='pAsswOrd';
#############################################################################
if($#ARGV+1 != 3){
print STDERR "Usage: $0 phonenum sender message\n";
exit 1;
}
my $phonenum=$ARGV[0];
# remove optional spaces, parantheses, or hyphens from phone number
$phonenum =~ s/[ ()-]//go;
my $sender=$ARGV[1];
my $message=$ARGV[2];
# Cellcom allows 1 ("Normal"), 2 ("Urgent") or 3 ("Emergency") messages.
# Emergency messages, for example get listed on the phone as "new emergency
# message", and so on. TODO: allow the user to change the urgency
my $urgency="1";
my ($t_sec,$t_min,$t_hour,$t_mday,$t_mon,$t_year,$t_wday,$t_yday,$t_isdst) =
localtime(time);
print STDERR sprintf("[%02d/%02d/%02d %02d:%02d] ",$t_year%100,$t_mon+1,$t_mday,
$t_hour,$t_min). "Sending to $phonenum: $message ($sender)\n";
##################################
my ($ua,$req);
$ua = new LWP::UserAgent;
$ua->agent("Messegila/1.0");
if($phonenum =~ m/^05[238][0-9][0-9][0-9][0-9][0-9][0-9]/){
############################ CELLCOM #######################################
my ($hiddenfields,$res,$ntries);
my $base = "http://isend.cellcom.co.il/English";
# If we get an HTTP error, we are going to retry 3 times with short
# sleep intervals; since we expect the above URL to work, we'll retry
# on any type of error - however strange. The error we expect is
# "502 Gateway Error" which appears in cellcom's site once in a while
# and another attempt usually succeeds. In the future, if the retries
# don't help (e.g., in case of a longer-term problem), the message
# should be queued for later delivery.
$ntries=0;
while(1){
if($ntries>=5){
print STDERR "Failed $ntries login attempts - giving up.\n";
exit 3;
last; # not reached
};
# sleep a little until the next try
sleep(30*$ntries*$ntries) if ($ntries>0);
$ntries++;
# Log in. We found the script we need to call, and the form parameters
# we need to set, in the $base/Login.htm page.
$req = new HTTP::Request POST => "$base/LoginCheck.asp";
$req->content_type('application/x-www-form-urlencoded');
$req->content("mfn=".uri_escape($CELLCOMTEXT_USER, '^A-Za-z')."&".
"iccid=".uri_escape($CELLCOMTEXT_PASSWORD, '^A-Za-z')."&".
"browser=".uri_escape("whatever")."&".
"os=".uri_escape("whatever")."&".
"browserVersion=".uri_escape("whatever"));
$req->header('Accept' => 'text/html');
$res = $ua->request($req);
# What we get from sending the previous form, is another form,
# with various hidden fields. We need to remember them, to use in
# the final, SMS sending form.
if ($res->is_success) {
my (@tmplist,$s);
# look for transient errors, that simply retrying may solve:
if($res->content =~ /Timeout expired/){
print STDERR "Error: timeout expired inside cellcom's database. trying again.\n";
next;
}
# look for hidden fields in the returned form (res->content)
# but not RecipientString, which we will change:
@tmplist=split '\n', $res->content;
$hiddenfields="";
foreach $s (grep /"hidden"/, @tmplist){
#next if $s =~ /RecipientString/;
# TODO: uri_escape the various fields
$s =~ s/^.*name="//o;
$s =~ s/"[ \t]*value="/=/o;
$s =~ s/">.*/\&/;
$hiddenfields=$hiddenfields . $s;
}
if($ntries>1){
print STDERR "Success on attempt #$ntries\n";
}
last; # stop retrying
} else {
print STDERR "Error: " . $res->status_line . "\n";
######## CONTINUE HERE!!!!!!!
print $res->content."\n";
# go on to try again
}
}
if($hiddenfields eq ""){
# try to figure out what went wrong by looking at $res->content
if($res->content =~ /Connection to Database failed../){
print STDERR "Error: Cellcom cannot process " .
"connection request because its database is down.\n".
"Sorry - cannot retry later because sendsms ".
"doesn't have queue support yet...\n";
exit 4;
}
# Something was wrong in our login attempt, but we don't know
# what was wrong... in the future we should try to recover
# somehow (try use another account?)
print STDERR "Error: bad response from Cellcom's site\n";
print STDERR "-------- THE ERROR: --------\n".
$res->content. "\n".
"----------------------------\n";
#print STDERR $req->as_string. "\n";
exit 4;
}
# now that we have session information ($hiddenfields), send the
# sms.
# Newlines in the message stop the message (this is an interesting
# method not to get the (sender) line... - in the old site), but to
# get actual newline breaks we need to change them into
# carriage-returns.
# TODO: this no longer works in the new site: the carriage returns
# look like spaces. figure out how to do this.
my $mymessage=$message;
$mymessage =~ s/\n/\r/g;
# TODO: we can change heb=false in $hiddenfields to heb=true if
# there are Hebrew characters in the text - this will work only
# for phones showing Hebrew on SMS (e.g., it will not work on
# Nokia 5120, I think)
$ntries=0;
while(1){
if($ntries>=5){
print STDERR "Failed $ntries message sends - giving up.\n";
exit 5;
last; # not reached
};
# sleep a little until the next try
sleep(30*$ntries*$ntries) if ($ntries>0);
$ntries++;
$req = new HTTP::Request POST => "$base/SendSMS.asp";
$req->content_type('application/x-www-form-urlencoded');
# NOTE: we currently don't use cellcom's feature to send the same
# message to up-to-5 phones. In the future, we can add such a
# capability to sendsms, but it doesn't seem so important..
# NOTE: cellcom ignores the counter value it gets.
$req->content("$hiddenfields".
"txtSender=".uri_escape($sender, "^A-Za-z")."&".
"areacode1=".uri_escape(substr($phonenum,0,3), "^A-Za-z")."&".
"txtNum1=".uri_escape(substr($phonenum,3), "^A-Za-z")."&".
"areacode2=&txtNum2=&areacode3=&txtNum3=&".
"areacode4=&txtNum4=&areacode5=&txtNum5=&".
"arrMessage=".uri_escape($mymessage, "^A-Za-z")."&".
"counter=".uri_escape(140-length($mymessage), "^A-Za-z")."&".
"selPriority=".uri_escape($urgency, "^A-Za-z"));
$req->header('Accept' => 'text/html');
$res = $ua->request($req);
if ($res->is_success) {
# On success, we get some text like: "Your message delivery
# request has been accepted in the service.", "The message
# will be delivered to subscriber number"
if($res->content =~ /accepted in the service/){
print STDERR "Sent successfully.\n";
} elsif($res->content =~ /Send Failed- No Hebrew support/){
# TODO: the above text is different in the new
# interface. change this case.
print STDERR "Send failed because cellcom refuses to send what looks like Hebrew chatacters...\n";
exit 7;
} else {
# TODO: try to recover somehow maybe try to figure out
# what went wrong by looking at $res->content.
print STDERR "Error: unexpected response from Celcom's site\n";
print STDERR "-------- THE ERROR: --------\n".
$res->content. "\n".
"----------------------------\n";
#print STDERR $req->as_string. "\n";
exit 6;
}
if($ntries>1){
print STDERR "Success on attempt #$ntries\n";
}
last; # stop retrying
} else {
print STDERR "Error: " . $res->status_line . "\n";
# go on to try again
}
}
############################################################################
} elsif($phonenum =~ m/^054[0-9][0-9][0-9][0-9][0-9][0-9]/){
############################ ORANGE ########################################
# (using orange's own form. gosms.com (mivzak.com) is an alternative)
#die "Too many characters in message!\n" if length($message) > 108;
my $res;
my $cookie_jar = HTTP::Cookies->new;
# To get actual newline breaks we need to change them into
# carriage-returns. However, this doesn't seem to work...
my $mymessage=$message;
$mymessage =~ s/\n/\r/g;
my $base = "http://www.orange.co.il/orange_online/free_sms/scripts";
# Log in, setting a cookie that we need for subsequent queries.
$req = new HTTP::Request POST => "$base/login_check.asp";
$req->content_type('application/x-www-form-urlencoded');
$req->content("mfn=".uri_escape($ORANGETEXT_USER, "^A-Za-z")."&".
"iccid=" . uri_escape($ORANGETEXT_PASSWORD, "^A-Za-z"));
$req->header('Accept' => 'text/html');
$res = $ua->request($req);
# get the cookies...
$cookie_jar->extract_cookies($res);
if($res->as_string !~ /Object moved.*smssend/s) {
print STDERR "Failed login into Orange: " . $res->status_line . "\n";
print STDERR $res->content. "\n";
print STDERR $res->as_string. "\n";
exit 1;
}
# now, send the message
$req = new HTTP::Request POST => "$base/smsprocess.asp";
$req->content_type('application/x-www-form-urlencoded');
$req->content( "mfn=".uri_escape($phonenum, "^A-Za-z")."&".
"sender=".uri_escape($sender, "^A-Za-z")."&".
"message=".uri_escape($mymessage, "^A-Za-z")."&".
"counter=".(108-length($mymessage)-length($sender)));
$req->header('Accept' => 'text/html');
# (put the cookies in the header)
$cookie_jar->add_cookie_header($req);
$res = $ua->request($req);
## Check if the sending failed
my $scs = "\347\354\371\360 \351\360\345\370\350\367\354\340\344"
." \367\346\341\356\344";
if ($res->is_success) {
# TODO: check that the returned text is correct, then we'll
# be really sure the message was sent.
if($res->content =~ /$scs/){
print STDERR "Sent successfully.\n";
} else {
# TODO: try to recover somehow maybe try to figure out
# what went wrong by looking at $res->content.
print STDERR "Error: unexpected response from Oranges's site\n";
exit 6;
}
} else {
# TODO: retry several times, sleeping between tries.
print STDERR "Error: " . $res->status_line . "\n";
exit 5;
}
############################################################################
} elsif($phonenum =~ m/^05[01][0-9][0-9][0-9][0-9][0-9][0-9]/){
print STDERR "Sorry, Pelephone number $phonenum not yet supported.\n";
} else {
print STDERR "Sorry, unrecognized cellphone number $phonenum.\n";
print STDERR "Currently, recognized numbers must begin with 052, 053, 054 or 058\n";
print STDERR "followed by exactly 6 digits.\n";
exit 2;
}
1;
=================================================================
To unsubscribe, send mail to linux-il-request@linux.org.il with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail linux-il-request@linux.org.il