Feb 5, 2011

Dhiraagu webSMS Conduit

I posted a simple bash script a long time ago to try and ease the process of sending webSMS's. While the age when everybody around here was messing around with the service is long gone, I have personally found the service to be quite useful (it's free after all!), and still occasionally mess around with it. The script works through and through and has served it's purpose well over the ages. The problem however, was that it still required the exchange of a (relatively) large amount of data between the client (me) and the server(Dhiraagu). While this is inconsequential on a regular broadband connection, it is a problem on my rather expensive mobile data connection.

Thus, out of this particular need, and after a brief spurt of late night coding (4:00 am - 4:15am?) the following PHP script was born. It is to live on my hosted server, and there act as a "Conduit" between myself and Dhiraagu. As you can see, it is, more or less, a "port" of the original Bash script to PHP...and I hope the PHP gods won't rain thunder and rocks on my head for the horrible job I did of it.

I do wish however that the people at Dhiraagu would update this site, and maybe provide a simple API or something to developers. They also need to reconsider the security..for instance an unexpected upshot of all this, is that THIS script allows me to use SSL between myself and my conduit. Dhiraagu itself doesn't provide this functionality. Not that I think it's of particular relevance, but SSL is always a good thing.

FUN FACT: No matter how long your webSMS password is, only the first 8 characters matter (I'll admit 8 isn't so bad..)

Anyways...here be the thing.


PHP script to act as a go-between for Dhiraagu websms
to minimize data-exchange. Ideally for use over a
mobile network.
just upload the php file to some place that supports
php_curl and make a standard GET request to the file
with the follwing parameters:
where cookie is optional, and is the session string of
an existing session.
The Script returns a JSON encoded status message.
$old_error_handler = set_error_handler("myErrorHandler");
'status' => 'OK',
'count' => NULL,
'cookie' => NULL,
'retries' => 0,
function terminate_now() {
    global $SessionData;
    echo json_encode($SessionData)."\n";
function myErrorHandler($errno, $errstr, $errfile, $errline)
    global $SessionData;
    if (!(error_reporting() & $errno)) {
    switch ($errno) {
        case E_USER_ERROR:
        $SessionData['status']="ERR: ".$errstr;
        case E_USER_WARNING:
        $SessionData['status']="WARN: ".$errstr;
        $SessionData['status']="ERRUNKWN: ".$errstr;
    return true;
function make_curl_request($url,$post_params,$useCookie) {
    global $SessionData;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 2);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$post_params);
    if($useCookie) {
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    return $output;
function login()
    global $SessionData;
    if(preg_match('/^Set-Cookie: Dhi=(.*?);/m', $return, $cookie_out)) {
        if(preg_match('/send (.*?) more/',$return,$msg_count)) {
                trigger_error('You have Reached your Daily Quota',E_USER_ERROR);
                } else {
        return true;
        } else {
        trigger_error('Invalid Username andor Password',E_USER_ERROR);
        return false;
function sendsms() {
    global $SessionData;
    if(strlen($message) > 140) {
        trigger_error('Message Truncated',E_USER_WARNING);
    if(preg_match('/send (.*?) more/',$return,$msg_count)) {
        } else {
            trigger_error('maximum number of retries exceeded',E_USER_ERROR);
if (isset($_GET['user']) && isset($_GET['pass']) && isset($_GET['msg']) && isset($_GET['num'])) {
        } else {
        trigger_error('Invalid Number Format',E_USER_ERROR);
    } else {
    trigger_error('Insufficiant Arguments',E_USER_ERROR);
    } else {

and then we have this modified version of the bash script to help make calls to the conduit

#!/usr/bin/env bash

# Bash helper script for Dhiraagu webSMS
# using the webSMS Conduit script from
# http://www.kudanai.com
# 2010 - kudanai

user= #defaultuser
pass= #defaultpass

function printhelp {
cat << EOT
Dhiraague WebSMS Conduit helper- KudaNai (kudanai.blogspot.com)
USAGE: $0 [OPTIONS...] -n number 'message'

 -h  Print this help and exit
 -N  New Session. Do not reuse old cookies.
 -d  Override default directory file. The Directory file
     is a comma seperated file containing name,number pairs
 -u  USERNAME Override default username.
 -p  PASSWORD Override default password.

  Please note that the -n argument is MANDATORY


function sendsms {
 if [ -e ${dirlist} ];then dcheck=`cat ${dirlist} | grep -w ${number} | cut -f2 -d","`;fi 
 if [ -n "${dcheck}" ];then number=${dcheck};fi

 if [ $purge -lt 1 ]
  if [ -e /tmp/$user.wsmscookie ]
   cookie=$(cat /tmp/$user.wsmscookie)
 return=$(curl --silent -G \
  -d "user=${user}" \
  -d "pass=${pass}" \
  -d "num=${number}" \
  --data-urlencode "msg=${message}" \
  -d "cookie=${cookie}" \
 echo $return

 if [[ "${return}" =~ \"cookie\":\"(.*)?\", ]]
  echo ${BASH_REMATCH[1]} > /tmp/$user.wsmscookie

while getopts 'hNn:u:p:d:' Option
 case $Option in
  u) user="${OPTARG}" ;;
  p) pass="${OPTARG}" ;;
  N) purge=1;;
  d) dirlist="${OPTARG}" ;;
  n) number="${OPTARG}" ;;
  h) printhelp ;;
  ?) printhelp ;;

shift $(( $OPTIND - 1 ))


Anonymous said...


//Sub/Corpus said...

I wish wataniya had webSMS ... :(

SoE said...

don't we all? pffft.

Anonymous said...

good job. the funniest part was that you put the time it took. i'm sure you could have done it in less time. about 5 secs? makes me smile and also laugh. good work boy.

Anonymous said...

yeah mentioning the time is probably a compensation for something else... remind me of jaa when he was young.

SoE said...

mentioning the time is actually a poor excuse for bad coding quality.

Anonymous said...

can you upload it somewhere. So we all can use it?

SoE said...



Anonymous said...

Dhiraagu WebSMS on Facebook:

Post a Comment