This commit was generated by cvs2svn to compensate for changes in r164,
[gnokii.git] / utils / sendsms
1 #!/bin/bash 
2 ###############################################################################
3 #
4 #       SENDSMS   --  Script to send sms by 'gnokii'
5 #   Author : Gabriele Zappi - Rimini <gzappi@inwind.it>
6 #       History:
7 #         feb 17, 2001         Script created (v1.0)
8 ###############################################################################
9
10 ###############################################################################
11 ###############################################################################
12 # PROGNAME=`basename $0`
13
14 # var settings.
15
16 PROGNAME=${0##*/}
17 VERSION="1.02"
18 GNOKII=./gnokii
19 PID=$$
20 SMSDIR=~/tmp/sms
21 TMP=~/tmp/SMStmp.$PID
22 BACKTITLE="SENDSMS V$VERSION"
23 AUTHOR="Gabriele Zappi - Rimini <gzappi@inwind.it>"
24
25 help() {
26 echo "$PROGNAME: $BACKTITLE - A gnokii interface for sending SMS."
27 echo "Written by $AUTHOR"
28
29 echo -e "\
30 \n\
31 Usage: $PROGNAME [OPTION]...\n\
32 \n\
33 Examples:\n\
34   $PROGNAME                 # simply run $PROGNAME in standard mode\n\
35   $PROGNAME --simulate      # Simulates. doesn't really send sms (for debug)\n\
36   echo \"Hello Beauty ;-)\" | $PROGNAME --smsset\n\
37                           # Preset \"Hello Beauty ;-)\" as SMS message.\n\
38   $PROGNAME --smsset < mymessage.txt\n\
39                           # Preset contents of file mymessage.txt \n\
40                             as SMS message.\n\
41   $PROGNAME --version       # Display version, author and quits.\n\
42 \n\
43 NOTE:\n\
44 If you require to pickup number from phone's memory (really from SIM card),\n\
45 for the first time, it anyway reads phonebook from your cellular phone. \n\
46 (It may take a while... please, be patient ;-) \n\
47 \n\
48 Available options:\n\
49   --debug, -D            May bother with more debugging messages ;-)\n\
50                          For debugging purpose.\n\
51   --simul[ate], -S       Simulation mode. Doesn't really send SMS by phone. \n\
52                          Only simulates. For debugging purpose.\n\
53   --forcememread, -F     Forces $PROGNAME to read phonebook from SIM card,\n\
54                          and to parse the generated file in order to update\n\
55                          the numbers' list (useful only if required to pickup\n\
56                          number from the phone's memory). see NOTE.\n\
57   --skipgnokiicheck, -K  Skip the test of the presence of binary 'gnokii'.\n\
58                          (It must be somewhere in your \$PATH). It allows you\n\
59                          to try this script program, even if you don't have\n\
60                          'gnokii' already. ;)\n\
61   --smsset, --setsms     Allows you to preset a SMS message from STDIN. (pipe\n\
62                          or input redirection).\n\
63                          This message will be proposed on the 'SMS message'\n\
64                          field during the program input cycle.\n\
65   --help, -h, /h         display this help and exit\n\
66   --version, -V          output version information and exit"
67 echo 
68 exit 0
69 }
70
71 acex()
72 {
73         rm -rf $TMP >/dev/null 2>&1
74         clear
75         exit $1
76 }
77
78 parse_stdin() {
79         # Switch initialization
80         SW_DEBUG=no
81         SW_SIMUL=no
82         SW_FORCEMREAD=no
83         SW_SKIPGNOKITST=no
84         SW_PRESETSMS=no
85         PRESETSMS=""
86
87         while [ -n "$1" ] 
88         do
89                 case "$1" in 
90                         --help|-h|/h)
91                                         help
92                                         ;;
93                         --version|-V)
94                                         echo
95                                         echo "$BACKTITLE - A gnokii interface for sending SMS."
96                                         echo "Written by $AUTHOR"
97                                         echo
98                                         exit 0
99                                         ;;
100                         --simul*|-S)
101                                         SW_SIMUL=yes
102                                         ;;
103                         --debug*|-D)
104                                         SW_DEBUG=yes
105                                         SW_SIMUL=yes
106                                         ;;
107                         --forcememread|-F)
108                                         SW_FORCEMREAD=yes
109                                         ;;
110                         --skipgnokiicheck|-K)
111                                         SW_SKIPGNOKITST=yes
112                                         ;;
113                         --smsset|--msgset|--setsms)
114                                         SW_PRESETSMS=yes
115                                         # will read from stdin
116                                         echo "reading sms from stdin .."
117                                         PRESETSMS=`cat`
118                                         ;;
119                         *)
120                                         echo "$PROGNAME: Invalid parameter: "
121                                         help
122                                         ;;
123                 esac
124                 shift
125         done
126 }
127
128 sure_to_quit() {
129  dialog --backtitle "$BACKTITLE" \
130                                                                                         --yesno "Really sure to quit? " 6 50
131  if [ $? -eq 0 ]; then
132  acex 0
133  fi
134 }
135
136 SMS_phoneread() {
137 dialog --backtitle "$BACKTITLE" --infobox "Reading phonebook by phone's SIM\n         Please wait" 4 40 
138 $GNOKII --getmemory SM 1 130 > $SMSDIR/.SMlist.temp
139 }
140
141 SMS_choicefrommem() {
142         # read from phonebook.
143         if [ "$SW_FORCEMREAD" = "yes" ] ; then
144                 readsim=1
145         elif [ -s $SMSDIR/.SMlist.sim ]; then
146                 dialog --backtitle "INSERT NUMBER(s).." \
147                                          --yesno "A SIM phonebook file is already present. \
148                     Do you want to read phone memory anyway?" 8 60
149                 if [ $? -eq 0 ] ; then
150                         readsim=1
151                 else
152                         readsim=0
153                 fi
154         else
155                 readsim=1
156         fi
157
158         ERROR=0
159         if [ $readsim -eq 1 ] ; then
160                 SMS_phoneread
161                 if [ $? -ne 0 ] ; then
162                         dialog --backtitle "INSERT NUMBER(s).." \
163                                                  --msgbox "ERROR during reading memory from your phone... \
164                        Insert manually destination number." 8 60
165                         ERROR=1
166                 else
167                         mv -f $SMSDIR/.SMlist.temp $SMSDIR/.SMlist.sim
168                 fi
169         fi
170
171         if [ $ERROR -eq 0 ]; then
172                 # Parsing sim file...
173                 if [ ! -s $SMSDIR/.SMlist.chklst -o ! -s $SMSDIR/.SMlist.choice \
174                                         -o $readsim -eq 1 ] ; then
175                         > $SMSDIR/.SMlist.menu
176                         > $SMSDIR/.SMlist.chklst
177                         > $SMSDIR/.SMlist.choice
178                         totrows=`cat $SMSDIR/.SMlist.sim | wc -l`
179                         rownow=0
180                         (while read riga
181                         do
182                                 rownow=`expr $rownow + 1`
183                                 perc=`expr $rownow \* 100 / $totrows`
184                                 pos=`echo $riga | cut -f4 -d";"`
185                                 name=`echo $riga | cut -f1 -d";"`
186                                 num=`echo $riga | cut -f2 -d";"`
187                                 if [ -n "$pos" -a -n "$num" ] ; then
188                                         echo -e "$pos \"$name - $num\"" >> $SMSDIR/.SMlist.menu
189                                         echo -e "$pos \"$name - $num\" off" >> $SMSDIR/.SMlist.chklst
190                                         echo "$pos;$num" >> $SMSDIR/.SMlist.choice
191                                 fi
192                                 echo $perc
193                         done < $SMSDIR/.SMlist.sim) | dialog \
194                                                                                                         --gauge "Parsing phonebook file..." 12 70 0
195                 fi
196
197                 chkok=0
198                 option=0
199                 while [ $chkok -eq 0 ]
200                 do
201                         eval dialog --backtitle \"SENDSM CKECKLIST\" \
202                                                                         --checklist \"Check your MEMORY number\(s\).\
203                               \\nPress ESC key \for options..... \" \
204                                                                         16 60 9 \
205                                                                         `cat $SMSDIR/.SMlist.chklst` 2> $TMP/rspTEMP02
206                         option=$?
207                         if [ $option -eq 255 ] ; then
208                                 dialog --backtitle "SENDSM CKECKLIST" --menu \
209                                                 "Options sub-menus for Phonebook choise list:" \
210                                                 10 50 4 \
211                                                 "1" "Sort by Memory Pos" \
212                                                 "2" "Sort by Names" \
213                                                 "9" "Quit $PROGNAME" 2> $TMP/rspTEMP04
214                                 if [ $? -eq 0 ]; then
215                                         sorttype=`cat $TMP/rspTEMP04`
216                                         case $sorttype in
217                                                 1) sort -n $SMSDIR/.SMlist.chklst > $TMP/.SMlist.tmp
218                                                          mv -f $TMP/.SMlist.tmp $SMSDIR/.SMlist.chklst
219                                                          ;;
220                                                 2) sort +1 $SMSDIR/.SMlist.chklst > $TMP/.SMlist.tmp
221                                                          mv -f $TMP/.SMlist.tmp $SMSDIR/.SMlist.chklst
222                                                          ;;
223                                                 9) sure_to_quit
224                                                          ;;
225                                         esac
226                                 fi
227                         else
228                                 chkok=1
229                         fi
230                 done
231
232                 if [ $? -eq 0 ] ; then
233                         for val in `cat $TMP/rspTEMP02`
234                         do
235                                 tok1=`eval echo $val`
236                                 tok2=`grep "^$tok1;" $SMSDIR/.SMlist.choice`
237                                 tok3=`echo $tok2 | cut -f2 -d";"`
238                                 if [ -z "$NUMBERS" ] ; then
239                                         NUMBERS="$tok3"
240                                 else
241                                         NUMBERS="$NUMBERS $tok3"
242                                 fi
243                         done
244                 fi
245         fi
246 }
247
248 ###########################################################################
249 ###########################################################################
250 #                               MAIN PROCEDURE                            #
251 ###########################################################################
252 ###########################################################################
253
254 parse_stdin $*
255
256 # This is the 'help' test...
257 # if [ "$1" = "--help" -o "$1" = "/h" ] ; then
258 #       help
259 # fi
260
261 if ! type -all "dialog" >/dev/null 2>&1 ; then
262 echo
263 echo "${PROGNAME}: error:"
264 echo "Can't find 'dialog', i can't run without 'dialog' on your system."
265 echo "You can get a compiled elf version of 'dialog' from here."
266 echo "http://www.tux.org/pub/people/kent-robotti/index.html"
267 echo "ftp://ftp.tux.org/pub/people/kent-robotti 'dialog-0.9a-15.elf.tar.gz'"
268 echo
269 exit 1
270 fi
271
272 if [ $SW_SKIPGNOKITST = no ] ; then
273         if ! type -all "$GNOKII" >/dev/null 2>&1 ; then
274         echo
275         echo "${PROGNAME}: error:"
276         echo "Can't find 'gnokii'. This script needs 'gnokii' in order to work."
277         echo "You can get a version of 'gnokii' from following links:"
278         echo "GNOKII: http://multivac.fatburen.org/gnokii/ , http://www.gnokii.org/"
279         echo
280         echo " Pavel Janik (who is to thank for a vast part of the 6110 development) "
281         echo "also keeps an ftp archive of some recent things."
282         echo " "
283         echo "Source code and pre-compiled binaries for gnokii are available from "
284         echo "a number of sites"
285         echo
286         echo "ftp.gnokii.org located in California, USA"
287         echo "linuxcare.com.au located in Canberra, Australia"
288         echo "multivac.fatburen.org located in Stockholm, Sweden "
289         echo
290         echo "Pre release versions are kept here, maintained by Pawel Kot"
291         echo "urtica.linuxnews.pl located in Warsaw, Poland"
292         echo
293         echo "MYGNOKII (can handle other newer Nokia models...)"
294         echo "--------"
295         echo "Marcin Wiacek -> mailto:marcin-wiacek@topnet.pl"
296         echo "http://marcin-wiacek.topnet.pl (http://www.fkn.pl/marcinw) -> netmonitor,"
297         echo "firmware, mygnokii (GSM & Nokia)"
298         echo "http://grumble.zereau.com/gnokii/ &"
299         echo "http://www.mds.mdh.se/~cel95eig/mygnokii/ -> mygnokii mirrors"
300         echo
301         exit 1
302         fi
303 fi
304
305 if [ ! -d ~/tmp ]; then
306 mkdir ~/tmp
307 chmod 755 ~/tmp
308 fi
309
310 if [ ! -d $SMSDIR ]; then
311 mkdir -p $SMSDIR
312 chmod 755 $SMSDIR
313 fi
314
315 if [ -d $TMP ]; then
316         rm -rf $TMP
317 fi
318 mkdir -p $TMP
319 chmod 700 $TMP
320
321
322 echo "$BACKTITLE" > $TMP/txtTEMP01
323 echo >> $TMP/txtTEMP01
324 echo "Written by Gabriele Zappi <gzappi@inwind.it> " >> $TMP/txtTEMP01
325 echo >> $TMP/txtTEMP01
326 echo "This script is a front-end to type SMS messages directly" >> $TMP/txtTEMP01
327 echo "from your computer and send them using gnokii through" >> $TMP/txtTEMP01
328 echo "your cellular phone connected to your computer." >> $TMP/txtTEMP01
329 echo >> $TMP/txtTEMP01
330 echo "Press [Enter] to continue with SENDSMS." >> $TMP/txtTEMP01
331
332 dialog --backtitle "$BACKTITLE" --textbox "$TMP/txtTEMP01" 14 70 
333 TURN=0
334
335 while true
336 do
337         echo "Please, enter in the box below, your SMS message." > $TMP/txtTEMP$$ 
338         echo "Remember that it cannot be longer than 160 chars." >> $TMP/txtTEMP$$ 
339         echo " " >> $TMP/txtTEMP$$ 
340         echo "Press [ESC] or [CANCEL] to quit the program. " >> $TMP/txtTEMP$$ 
341
342         len=999
343         if [ $TURN -eq 0 ]; then
344                 if [ "$SW_PRESETSMS" = "yes" ]; then
345                         echo "$PRESETSMS" > $TMP/rspTEMP01
346                 else
347                         > $TMP/rspTEMP01
348                 fi
349         fi
350         while test "$len" -le 0 -o "$len" -gt 160
351         do
352                 dialog --backtitle "INSERT SMS ....." \
353                                          --inputbox  "`cat $TMP/txtTEMP$$`" 10 70 "`cat $TMP/rspTEMP01`" \
354                                                                                                                                                                                                                         2> $TMP/rspTEMP01
355           if [ $? -ne 0 ] ; then
356                         sure_to_quit
357                 fi
358                 len=`cat $TMP/rspTEMP01 | wc -c `
359
360                 if [ $len -le 0 -o $len -gt 160 ] ; then
361                         echo "Invalid SMS: It must be < 160 chars and > 0 long.." > $TMP/txtTEMP02
362                         dialog --textbox "$TMP/txtTEMP02" 6 70 
363                 fi
364         done
365
366         NUMBERS=""
367         okcycle=0
368         while [ "$NUMBERS" = "" -o "$okcycle" -eq 0 ]
369         do
370                 dialog --backtitle "INSERT NUMBER(s).." \
371                                          --yesno "would you like to pick up number(s) \
372                     from your phone memory" 8 60
373                                                                                                                 
374
375                 if [ $? -eq 0 ] ; then # Yes, gimme num from phonebook.
376                         SMS_choicefrommem
377                 fi
378
379                 # Manual telephone number completion or insertion.
380                 echo "Please, insert/modify in the box below, the NUMBER(S)" > $TMP/txtTEMP$$ 
381                 echo "where you want to send the message to. " >> $TMP/txtTEMP$$ 
382                 echo "(Multiple numbers must be separated by a space.)  " >> $TMP/txtTEMP$$ 
383                 echo "Press [ESC] or [CANCEL] to quit the program. " >> $TMP/txtTEMP$$ 
384                 echo " " >> $TMP/txtTEMP$$ 
385                 echo "OUTGOING MESSAGE: " >> $TMP/txtTEMP$$ 
386                 echo -n "  "
387                 cat $TMP/rspTEMP01 | cut -c1-60 >> $TMP/txtTEMP$$
388                 cat $TMP/rspTEMP01 | cut -c61-120 >> $TMP/txtTEMP$$
389                 cat $TMP/rspTEMP01 | cut -c121-160 >> $TMP/txtTEMP$$
390                 
391                 okcycle=1
392                 dialog --backtitle "INSERT NUMBER(s).." \
393                                          --inputbox  "`cat $TMP/txtTEMP$$`" \
394                                          16 70 "$NUMBERS"  2> $TMP/rspTEMP03
395         
396           if [ $? -ne 0 ] ; then
397                         sure_to_quit
398                         okcycle=0
399                 else
400                         NUMBERS=`cat $TMP/rspTEMP03`
401                         if [ "$NUMBERS" = "" ] ; then
402                                 dialog --backtitle "INSERT NUMBER(s).." \
403                                                          --infobox "Number cannot be empty.\n     Try again.." 4 28 
404                                 sleep 2
405                                 okcycle=0
406                         fi
407                 fi
408         done
409
410         # Last confirmation before sending messages....
411
412         okcycle=0
413         while [ $okcycle -eq 0 ]
414         do
415                 echo "Ok. If you confirm the operation, the the SMS" > $TMP/txtTEMP$$ 
416                 echo "is going to be sent to the selected number(s)." >> $TMP/txtTEMP$$ 
417                 echo >> $TMP/txtTEMP$$ 
418                 echo "Press [ESC] to quit the program. " >> $TMP/txtTEMP$$ 
419                 echo "Press [CANCEL] to return to SMS input. "  >> $TMP/txtTEMP$$ 
420                 dialog --backtitle "$BACKTITLE" \
421                                          --yesno "`cat $TMP/txtTEMP$$`" 9 60
422                 case $? in
423                         0)
424                                 okcycle=1;;
425                         1)
426                                 TURN=`expr $TURN + 1`
427                                 continue 2;;
428                         255)
429                                 sure_to_quit ;;
430                 esac                                                                            
431         done
432         TURN=`expr $TURN + 1`
433
434         for num in $NUMBERS
435         do
436                 # echo "Sending sms to number $num ... "
437                 dialog --backtitle "$BACKTITLE" --infobox "Sending sms to number $num ...\n         Please wait" 4 45
438                 if [ "$SW_SIMUL" = "yes" ] ; then
439                         sleep 2
440                 else
441                         $GNOKII --sendsms $num < $TMP/rspTEMP01 > /dev/null 2>&1
442                 fi
443                 if [ $? -eq 0 ] ; then
444                         dialog --backtitle "$BACKTITLE" \
445                         --infobox "Message correctly sent to number $num !" 3 55 
446                 else
447                         dialog --backtitle "$BACKTITLE" \
448                         --infobox "Sent Failed to num $num! Sorry!" 3 55 
449                 fi
450                 sleep 2
451         done
452
453 done
454
455
456 acex 0
457 # zap - fin qui
458