How to use a free Web SMS from Sunrise within a shell script

Many mobile phone providers allow you a free amount of SMS if you use the web interface. Maybe you don’t want to log in every time to use them or you want to send the SMS from your own web site or even from within a shell script.

The following script can be uses to send free SMS with using the web SMS system of the Swiss Mobile provider Sunrise. It is adapted by the great Android tool from play.google.com/store/apps/details?id=de.ub0r.android.websms and especially the Sunrise plugin by Koni (play.google.com/store/apps/details?id=com.rothconsulting.android.websms.connector.sunrise). The source code for it is available under github.com/konir/websms-connector-sunrise.

I use it i.e. to check any incoming email and send me an SMS when ever a special subject is found.

Since I have two account at Sunrise, the commands are a bit more tricky as after the login the correct account has to be selected first. The good thing is that I can check the number of free SMS and switch to the other account once all SMS are used.

#!/bin/bash
##################################################################
# Script to send a SMS over the Sunrise free SMS webinterface    #
# Call it with no parameters to get usage help                   #
# Copyright 2013 by George Ruinelli <george@ruinelli.ch>         #
# Adapted from https://github.com/konir/websms-connector-sunrise #
##################################################################

#Example call:
# bash /home/ruinelli/sms/sms_send.sh -r +41764***** -m "This is an test message" -v -a

account1=076*****
account2=076*****
password="****"

script_path=`dirname "$0"`
logfile="$script_path/send.log"
html_logfile="$script_path/sms_log.htm"
dryrun=0
verbose=0
showhelp=0
force_account1=0

date >>  $logfile

##########################
while getopts r:m:d?va option #see http://linux.about.com/od/Bash_Scripting_Solutions/a/How-To-Pass-Arguments-To-A-Bash-Script.htm
do
case "${option}"
in
r) recipient=${OPTARG};;
m) message=${OPTARG};;
d) dryrun=1;;
v) verbose=1;;
a) force_account1=1;;
?) showhelp=1;;
esac
done

if [ "$recipient" == "" ]; then
echo "ERROR: No recipient given"
showhelp=1
fi

if [ "$message" == "" ]; then
echo "ERROR: No message given"
showhelp=1
fi

if [ $showhelp -ge 1 ]; then
echo "---------------------------------------------------------------------------------"
echo "SMS sender sends a SMS over Sunrises Web2SMS interface"
echo "Since I have 2 accounts, it first uses the free SMS of the"
echo "2nd one, then switches to the 1st one."
echo "Copyright 2013 by George Ruinelli"
echo "Parameter:"
echo " -m MESSAGE (max 160 characters, more will be truncated)"
echo " -r RECIPIENT NUMBER (leading country code is optional)"
echo " -d (optional, dry run, does not actually send the message)"
echo " -v (optional, appends the number of available free SMS to the message)"
echo " -a (optional, force usage of account 2, even when no free SMS left)"
echo ""
echo "The message will be sent from account 2 ($account2) if there are free SMS left,"
echo "else account 1 ($account1) is used (unless -a is applied where)"
echo "Example: bash /home/ruinelli/sms/sms_send.sh -v -r 07641***** -m \"This is a test message\""
echo "---------------------------------------------------------------------------------"
exit
fi

if [ $verbose -ge 1 ]; then #make space for 9 characters additional: " (xx, xx)"
echo "Will append number of free SMS"
valid_message_lenght=151 #160-9
else
valid_message_lenght=160
fi

#check message length
length=`echo ${#message}`

if [ $length -gt $valid_message_lenght ]; then
let valid_message_lenght=valid_message_lenght-4
message=`echo ${message:0:$valid_message_lenght}`
message="$message..."
echo "WARNING, message is too long ($length characters total), will truncate it to: $message"
fi

##########################

#logging in
echo "Logging in..." | tee -a $logfile
/usr/local/bin/curl -s  --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 -d LoginForm_Login="$account1" -d LoginForm_Password="$password" -d LoginRedirectSecret="d285715d94fb4cd4613ad70aaeb8f735" -d LoginRedirectURL="https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewStandardCatalog-Browse?CatalogCategoryID=zv7AqFI.Z.gAAAEkbGdQzDCf&LoginOrigin=Mein%20Konto&LoginDestination=Mein%20Konto" -d ajaxhref="http://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewPersonalCodeRetrieval-New?SpcRetrievalMode=ECare" https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewApplication-Login > login.htm

echo "Selecting accounts and fetching left free SMS..." | tee -a $logfile

#getting amount of left free SMS for account 1
echo "" > $html_logfile #make sure no old status is used
/usr/local/bin/curl -s  --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 -d charsLeft="0" -d PhoneNumber="$account1" https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewECareMessaging-Encrypt > b.htm

/usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0  https://mip.sunrise.ch/mip/dyn/login/smsMeinKonto?lang=de > $html_logfile

left1=`grep "Gratis" $html_logfile`
left1=`echo $left1 | sed 's/Gratis//g'| tr -d '\n' | tr -d '\r' | tr -d ' '`

echo "Free SMS left on account 1: $left1" | tee -a $logfile

#getting amount of left free SMS for account 2
echo "" > $html_logfile #make sure no old status is used
/usr/local/bin/curl -s  --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 -d charsLeft="0" -d PhoneNumber="$account2" https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewECareMessaging-Encrypt  > /dev/null

/usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0  https://mip.sunrise.ch/mip/dyn/login/smsMeinKonto?lang=de > $html_logfile

cat $html_logfile > "left_sms_account2.htm"

left2=`grep "Gratis" $html_logfile`
left2=`echo $left2 | sed 's/Gratis//g'| tr -d '\n' | tr -d '\r' | tr -d ' '`

# left2=0 #simulating no SMS left on account 2

echo "Free SMS left on account 2: $left2" | tee -a $logfile

### selecting account for sending
if [ $force_account1 -ge 1 ]; then #force account1
echo "Forcing to use account 1" | tee -a $logfile #if no free SMS left, we pay for them
account=$account1
let left1=left1-1 #update verbose text for situation after sending
left1="*$left1" #mark account
else
if [ $left2 -ge 1 ]; then #use account 2 first
echo "Using account 2" | tee -a $logfile
account=$account2
let left2=left2-1 #update verbose text for situation after sending
left2="*$left2" #mark account
else
echo "Using account 1" | tee -a $logfile #and if no free SMS left, we pay for them
account=$account1
let left1=left1-1 #update verbose text for situation after sending
left1="*$left1" #mark account
fi
fi

#now select right account
echo "Selecting sender: $account..." | tee -a $logfile
/usr/local/bin/curl -s  --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 -d charsLeft="0" -d PhoneNumber="$account" https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewECareMessaging-Encrypt  > /dev/null

#add verbose text
if [ $verbose -ge 1 ]; then #make string showing free SMS: " (xx, xx)"
message="$message ($left1, $left2)"
fi

#sending
echo "" > $html_logfile #make sure no old status is used
echo "Sending message: $message" | tee -a $logfile
echo "to $recipient..." | tee -a $logfile
if [ $dryrun -ge 1 ]; then
echo "Dry-run, not sending it!"
else
echo "Sending..."
/usr/local/bin/curl -s  --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 -d charsLeft="0" -d type="sms", -d recipient="$recipient" -d message="$message" -d send="send" -d task="send" -d currentMsisdn="$account" http://mip.sunrise.ch/mip/dyn/login/smsMeinKonto?lang=de > $html_logfile
fi

#status message
reply_line=`sed -n '/id="errorBlock"/='  $html_logfile`
let reply_line=reply_line+5
status=`head -$reply_line $html_logfile | tail -1`
status=`echo $status | sed 's/<br\/>//g'`

echo "---------------------------------------------------------"
echo "Status:     $status" | tee -a $logfile
echo "---------------------------------------------------------" | tee -a $logfile
<pre>

c:geo on BlackBerry BB10

I am hacgeoppy to announce that c:geo is now also available for BlackBerry BB10. Get it for free at appworld.blackberry.com/webstore/content/28277410. If you are still stuck on BB10.0, you download it from here.

c:geo is a simple to use but powerful geocaching client with a lot of additional features. In contrast with other similar applications, c:geo doesn’t require web browser nor exports. You can just go geocaching with your phone and without any home preparation or worries. Of course, you can go without paying – it’s free.

Main features:
- View caches on a Live Map
- Search for caches by various criteria
- Log your finds online or offline
- Store cache information on your device
- Create and manage waypoints
- Navigate using compass, map, or other apps
- Import/Export GPX files
- Full support for trackables
- Offline caching functions including offline maps

The BlackBerry version brings some (minor) limitations:
- No Google maps/satellite maps
- No additional tools like GCC or the calendar plugin available

c:geo is developed by the c:geo team.
I only maintain only the BlackBerry version!
For help, you can ask me or directly the c:geo team at www.cgeo.org if it is not a BlackBerry specific problem.

Geocaching with the Blackberry Z10

Update 2

I removed c:geo from the Blackberry World as I am unable to update it (lost the signing key and Blackberry gives no way to restore it).

Also there is no need for it anymore since the Android version works very good and with no issues. Get it from http://www.cgeo.org/cgeo.apk and sideload it or use any Android Store tool like http://redlightoflove.com/snap/

Update

The official version is now available in the BlackBerry Store: appworld.blackberry.com/webstore/content/28277410.

Continue reading

Android applications under Blackberry 10

With their new Operating System BB10 Blackberry made it very easy to convert existing Android applications into Blackberry 10 apps. My test revealed that around 50% of the applications work very well on my Blackberry Z10. The other ones will crash at their start, most likely due the incomplete API. With the soon coming update this might get even better.

I want to show you how easy it is to download, convert and install Android apps on your Blackberry device. The easiest way is to use the Chrome browser as it has 2 extensions which make our work much easier.

If you already have a BAR file (i.e. cgeo from here), you can start with step 10. :)

  1. Install Chrome Web browser
  2. Install the extension APK-Downloader
  3. Install the extension PlayBook App Manager
  4. Configure the APK-Downloader
  5. Configure the PlayBook App Manager and the Blackberry device
  6. With the Chrome browser go to the Google Play Store page of the desired application, i.e. c:geo.
  7. Download the APK file with the green icon behind the address bar
  8. Go to the page http://apk2bar.unker.net/ and upload the apk file
  9. Wait and download the generated BAR file.
  10. Open the PlayBook App Manager and install the BAR file

Done

Synchronize the contacts and calendar of your Blackberry 10 with OwnCloud

The new Blackberry 10 devices support CalDAV as well as CardDAV. With a bit of modifications of your Owncloud installation, it is possible to synchronize your calendar with CalDAV and your contacts with CardDAV.

Based on Nicolas Vivien’s modifications, I implemented my own patched Ownloud server. Nicolas helped me a lot tracing down the various problems and stumbling blocks.

First of all some important Blackberry limitations

On the current Blackberry 10 OS (10.9.10.35), CardDAV only works with a server which supports HTTPS. On the other side, CalDAV also accepts an insecure HTTP server (but first tries HTTPS).
Hopefully Blackberry will fix this in the near future.

My Workaround

Since my hoster only provides HTTPS at an extra cost, I had to look for another solution.
Luckily, my Synology DiskStation als has a webserver and even provides HTTPS.
So I moved my OwnCloud installation to my local DiskStation which is accessible through HTTP over the internet.

Required OwnCloud modifications

To keep the server addresses of the Server short, I added some htaccess RewriteRules to the root directory of my webserver:

RewriteEngine on
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteCond %{HTTP_HOST} ^carddav
RewriteCond %{REQUEST_URI} !^/apps
RewriteRule .* /owncloud/apps/contacts/carddav.php/$1 [L]

RewriteCond %{HTTP_HOST} ^caldav
RewriteCond %{REQUEST_URI} !^/apps
RewriteRule .* /owncloud/apps/calendar/caldav.php/$1 [L]

This will redirect any call from https://carddav.DOMAIN.ch/ to https://DOMAIN.ch/owncloud/apps/contacts/carddav.php and https://caldav.DOMAIN.ch to https://DOMAIN.ch//owncloud/apps/calendar/caldav.php.

You then should see pages similar to this:

principals Collection 2013-03-17T13:43:01+00:00
calendars Collection 2013-03-17T13:43:01+00:00

and

principals Collection 2013-03-17T13:44:48+00:00
addressbooks Collection 2013-03-17T13:44:48+00:00

Note:
Due my modifications, A direct call to the redirected URLs will lead to a Sabre_DAV_Exception_NotFound exception! How ever this doesn’t matter in normal operation since you can always use the much shorter and nicer sub domain call.

Blackberry configuration

CalDAV account

Description: [What ever you want]
User name: Your Owncloud user name
Email address: [Blackberry requires a valid emailadress. How ever Owncloud will ignore it]
Password: Your Owncloud password
Server address: carddav.DOMAIN.ch/calendars/USERNAME/defaultcalendar [add the /calendars/USERNAME/defaultcalendar, where USERNAME has to be replaced by your real user name, else Blackberry will not list the account. If you only have HTTP, you will heve to append a http://, else it uses implies https://]

CardDAV account

Description: [What ever you want]
User name: Your Owncloud user name
Email address: [Blackberry requires a valid emailadress. How ever Owncloud will ignore it]
Password: Your Owncloud password
Server address: carddav.DOMAIN.ch [omit the HTTPS://]

Required OwnCloud modifications

Jet to be documented…

For now, have a look at Nicolas’s patches: http://www.progweb.com/2012/05/playbook-pret-pour-le-cloud/

Local OpenStreetMap server

When you are one of those users who have a huge usage of OpenSTreetMap tiles (i.e. to generate offline maps for your phone), the you might know that most tile providers limit the access due the huge workload.

A solution to avoid this is to set up your own server. http://switch2osm.org/ documents very well, how this can be done. Since I am working on Linux (Kubuntu) anyway, I followed this guide: http://switch2osm.org/serving-tiles/building-a-tile-server-from-packages/.

You well need quite some RAM to generate the database, but afterwards, it works quite well and you can fetch as many tiles as you want :)

Using Zarafa with Z-Push on Kubuntu 12.10

This is a quick guide how the set up the  Zarafa groupware on (K)ubuntu 12.10 and use it with Mail for Exchange.

Zarafa

To set up Zarafa, I followed the (German) guide on http://www.dirk-hagedorn.de/?page_id=841 but used zcp-7.1.0-36420-ubuntu-12.04-x86_64. It is written for in a virtual machine, but works also on a real system. There is also a new (English) tutorial on http://www.linux-answered.com/2012/10/02/install-zarafa-server-on-ubuntu-12-04-lts/, but I haven’t tested that one.

Quick check

Call <SERVER_IP>/webaccess in  your webbrowser. You should need to enter the credentials of the user you created earlier. If everything is right, you will see the groupware page.

Z-Push

To be able to sync with mail for exchange (I tested with my Nokia N9), you also need Z-Push. I followed this tutorial but used z-push-2.0.4-1497: http://www.linux-answered.com/2012/04/25/zarafa-z-push-exchange-activesync-on-ubuntu/.

Quick check

Call <SERVER_IP>/Microsoft-Server-ActiveSync in  your webbrowser. You should need to enter the credentials of the user you created earlier. If everything is right, you will see a page with “…GET not suported…”

Mail for Exchange (Client side)

On my N9, I then created a Mail for Exchange account:
Domain: keep empty
Server address: <SERVER_IP>
Secure connection: disable
Port: 80

Calendar

After that, I was able to create and sync the new calendar on my N9 as well as on the webinterface.

Email

Also, the sending of emails (to myself only since STMP on the server is not configured yet)  and syncing them works well.

Contacts

Contacts sadly seem not to be supported yet. I am not yet sure if there is something missing or if it is only available in the commercial version.

Others

There are other people who got it working more or less successfully: