#!/bin/sh # smsl v.1.4 - send SMS via Saunalahti (if you're their customer) # # Copyright Mikko Rauhala , 2008 # # This program is free software. It comes without any warranty, to # the extent permitted by applicable law. You can redistribute it # and/or modify it under the terms of the Do What The Fuck You Want # To Public License, Version 2, as published by Sam Hocevar at # http://sam.zoy.org/wtfpl/COPYING and reproduced here: # # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # Version 2, December 2004 # # Copyright (C) 2004 Sam Hocevar # 14 rue de Plaisance, 75014 Paris, France # Everyone is permitted to copy and distribute verbatim or modified # copies of this license document, and changing it is allowed as long # as the name is changed. # # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION # # 0. You just DO WHAT THE FUCK YOU WANT TO. # README: # This program doesn't give you free SMS. It merely interfaces with the # Saunalahti web UI at https://oma.saunalahti.fi/settings/smsSend for # sending SMS you've either got coming to you with your service package, # or for which you've paid separately. It's mostly a handy way for # sending SMS from a proper keyboard when you've got one available. # ~/.smslrc defines the variables LOGIN, PASSWORD and optionally NUMBER # for the source number (if NUMBER is not set, it will default to LOGIN - # the Saunalahti web UI will present you with the possible other valid # options). The variables must be on their own lines as "KEY=value", # without the quotes. Empty lines and #-comments are allowed. # Example (though remove the comment markers for the actual file): # LOGIN=0505555555 # PASSWORD=seecreet # And you're set to send SMSes from your login number. # Caveat: Saunalahti doesn't do international recipients. +358 numbers # are converted into Finnish local numbers before sending, though. # Also the error reporting is not as robust as it could be, but at least # it 1) exists now and 2) seems to work with the current web service. # Addendum: It seems that the Saunalahti web service just hangs # sometimes (with a regular browser too), resulting in the script # hanging as well. I might try and hack together timeout detection # and/or retry looping at some point but for now just be aware of it # (or submit a patch ;). If the script _does_ get around to exiting # without errors there should (hopefully) be no trouble. # ChangeLog: # 1.4 * Fixed a couple of nbsps that should've been spaces. Apparently # there was no effect unless mktemp both exists and fails. # 1.3 * Do a couple of sleeps between the HTTP requests to see if # the spurious problems are race conditions in the Saunalahti # service # 1.2 * Hiding the Saunalahti password from curl command line. # * If mktemp not available, falling back to temp files named ~/.smslrc.* # 1.1 * We now use curl instead of wget, since we can then ditch the horrible # URL-encoding code (which also had a byte order bug) # * Due to this, also login and password are urlencoded now properly... # * Checks for some programs not always installed on legacy systems # * Support for a defined cookie jar file in lieu of mktemp # 1.0 * (Simplistic) error checks for server communication # * Use the C locale for consistent grepping # 0.9 * Initial release LC_ALL=C export LC_ALL RETVAL=0 ohnoes() { echo "$1" 1>&2 if [ "a$COOKIEJAR" != a ] then rm -f "$COOKIEJAR" fi if [ "a$PWFILE" != a ] then rm -f "$PWFILE" fi exit 1 } if [ "a$2" = a ] then echo "Usage: smsl number(s) message" 1>&2 echo 1>&2 echo "You may enter multiple numbers separated by commas." 1>&2 echo 1>&2 echo "The SMS server wants ISO-8859-1 encoded messages." 1>&2 echo "Valid UTF-8 messages will be converted if possible," 1>&2 echo "otherwise the message will be passed as is." 1>&2 echo "The message must be given as a single parameter" 1>&2 echo "(ie. you probably want to use \"quotes around it\")." 1>&2 echo 1>&2 exit 0 fi if ! which curl > /dev/null then ohnoes "curl is required for this version of smsl, aborting" fi if which iconv > /dev/null && iconv -l | grep -qi "UTF-8" && iconv -l | grep -qi "ISO-8859-1" then if iconv --version 2> /dev/null | grep -q "GNU" then ICONV_TARGET="ISO-8859-1//TRANSLIT" else ICONV_TARGET="ISO-8859-1" fi fi RECIPIENTS="$1" MESSAGE="$2" RECIPIENTS="`echo -n "$RECIPIENTS" | sed 's/+358/0/g'`" if echo -n "$RECIPIENTS" | grep -q '[^0-9,]' then ohnoes 'Invalid phone number(s). Only digits, "," and "+358" are accepted.\n(Saunalahti only accepts Finnish numbers.)' fi if [ "a$ICONV_TARGET" != a ] && echo -n "$MESSAGE" | iconv -f UTF-8 -t "$ICONV_TARGET" > /dev/null 2>&1 then MESSAGE="`echo -n "$MESSAGE" | iconv -f UTF-8 -t "$ICONV_TARGET"`" fi MESSAGELEN="`echo -n "$MESSAGE" | wc -c`" if [ "$MESSAGELEN" -gt 160 ] then ohnoes 'Message length ($MESSAGELEN) is over the allowed 160 characters, aborting.' fi if [ -e $HOME/.smslrc ] then . $HOME/.smslrc || ohnoes "failed to parse ~/.smslrc, aborting" else ohnoes "~/.smslrc does not exist, aborting" fi if [ "a$LOGIN" = a ] then ohnoes "~/.smslrc doesn't define the user account, aborting" fi if ! grep -q "^PASSWORD=" "$HOME/.smslrc" then ohnoes "~/.smslrc doesn't define the user password, aborting" fi if [ "a$NUMBER" = a ] then NUMBER="$LOGIN" fi umask 077 if which mktemp > /dev/null then COOKIEJAR="`mktemp -t smsl.cookiejar.XXXXXXXXXX`" || ohnoes "mktemp failed, aborting" PWFILE="`mktemp -t smsl.pass.XXXXXXXXXX`" || ohnoes "mktemp failed, aborting" else COOKIEJAR="$HOME/.smslrc.cookiejar.$$" PWFILE="$HOME/.smslrc.pass.$$" fi grep "^PASSWORD=" "$HOME/.smslrc" | tail -n 1 | sed "s/^.*=//" | tr -d "\n" > "$PWFILE" if ! curl -c "$COOKIEJAR" --data-urlencode "username=$LOGIN" --data-urlencode "password@$PWFILE" https://oma.saunalahti.fi/settings/ 2> /dev/null | grep -qi "Tervetuloa Omaan Saunalahteen" then ohnoes "Login to Saunalahti (probably) failed; check your password." fi sleep 1 if ! curl -b "$COOKIEJAR" --data-urlencode "sender=$NUMBER" --data-urlencode "recipients=$RECIPIENTS" --data-urlencode "text=$MESSAGE" --data "send=L%e4het%e4" https://oma.saunalahti.fi/settings/smsSend 2> /dev/null | grep -qi "Viesti l.*hetetty" then echo "Message (probably) not sent successfully." 2>&1 RETVAL=2 fi sleep 1 if ! curl -b "$COOKIEJAR" https://oma.saunalahti.fi/settings/Logout 2> /dev/null | grep -qi "Olet kirjautunut ulos" then echo "Possible error logging out of Saunalahti web services." 2>&1 fi rm -f "$COOKIEJAR" rm -f "$PWFILE" exit $RETVAL