#
#   Kermit script that sends SMS messages through a Nokia SMSC server
#    using the CIMD protocoll
#
#  By: Jacob Lundqvist jaclu@ibk.se
#      1997-01-11
#
#  Tested with GSM provider: Europolitan (Sweden), using faked checksum
#
#	Terms
#	-----
#	SMS	Short Message Service
#	SMSC	Short Message Service Center
#	CIMD	Computer Interface to Message Distribution
#
#
#  Modem --- SMSC --- GSM/PCN phone (with SMS capacity)
#
DEFINE \%a \&@[2]	# modem line
DEFINE \%b 123456	# number to your SMSC server
DEFINE \%c cimd3	# login to access CIMD, to select kind of service
			# cimd3 = fake all checksums
DEFINE \%d user		# CIMD account, identifying application for Provider
DEFINE \%e paswword    	# CIMD password
DEFINE \%f \{9}11EE\10	# End of command sequence 11=Faked CRC EE=Stop

#
# other variables
#
# \%i loop
# \%m message

#======================================================
#
# Macro deffinitions
#

#
# Pressentation
#
define ABOUT_PGM \
    echo, \
    echo Kermit SMS ver 1.2 by Jacob Lundqvist - jaclu@ibk.se, \
    echo

#
#  Terminate script
#
define TERMINATE hangup,quit

#
#  Help
#
define SHOW_HELP \
    ABOUT_PGM, \
    echo Syntax: kermit sms.scr line file number [number [number [...]]], \
    echo (multiple numbers can be specified),\
    echo only first line of file is used - file MUST be terminated with LF!!!, \
    echo, \
    echo Example: kermit sms.scr /dev/cua0 /tmp/smsc.msg 4670890510 4670812345, \
    echo, TERMINATE

#
#  No message
#
define NO_MSG echo ERROR: no message in file \%1, set exit status 3, TERMINATE

#
# To few Arguments
#
define TO_FEW_ARGS echo ERROR: to few arguments (try ?), \
	set exit status 3, TERMINATE

#
#  No such device
#
define NO_DEV echo ERROR: no such device: \%1, set exit status 1, TERMINATE

#
# Check if device exists
#
define CHECK_DEV if < \Ffiles(\%1) 1 NO_DEV \%1

#
# No Connection
#
define NO_CONNECT echo ERROR: No Connection, set exit status 1, TERMINATE

#
#  If No Ack is received, do a output, so this is noted in logfile,
#   hopefully remote end wont care, since it's gone anyhow
#
define NO_ACK echo ERROR: No ACK, set exit status 2, TERMINATE

#
#  _ is not valid, remove
#   Actually it is a escape character, but we don't handle it for now...
#
define REMOVE_USCORE define \%i \Freplace(\%n,_,\32), \
    assign \%n \%i, \

#
#  Collect message into variable, max 160 chars!!
#
define GET_MSG open read \&@[3], \
    read \%n, \
    REMOVE_USCORE, \
    define \%m \Fsubstr(\%n,1,160), \
    echo message is[\Flength(\%m)]: \%m, \
    if < \Flength(\%m) 1 NO_MSG \&@[3], \
    close read

#
#  CIMD Login As User
#
define CIMD_LOGIN output AA01\{9}\%d\{9}\%e\%f, input 20 AAAck, \
        if failure NO_ACK

#
#  CIMD Submit A New Message
#    since remote end echoes output, we must catch our own Ack
#      
#
define CIMD_SUBMIT output AA03\{9}\%1\{9}\%m\%f, input 60 AAAck, \
        if failure NO_ACK, \
	input 10 11EE, sleep 1, output AAAck\%f, input 20 11EE

#
#  CIMD Logout Of Session
#
define CIMD_LOGOUT output AA02\%f, input 10 AAAck if failure NO_ACK


#========================================================
#
# Main
#

#
# Want help?
#
if equal \&@[2] ? SHOW_HELP

ABOUT_PGM

#
# Check params
#
if < \v(args) 5 TO_FEW_ARGS

CHECK_DEV  \%a

#
# Check message
#
GET_MSG

#
# Set line params
#
set modem hayes         # plain normal modem
set line \&@[2]
set flow xon/xoff       # My SMSC server demands XON/XOFF
set speed 9600

#
# Debugging
#
#log session session.log new

#
#  Connect to host
#
hangup                  # Make sure line is available
dial \%b
if failure NO_CONNECT
sleep 5

#
# Start CIMD application
#
output \%c\10      
sleep 1


#
# CIMD session starts
#

CIMD_LOGIN

#
#  Send to all recipients
#
assign \%j  \v(args) 
decrement \%j
for \%i 4 \%j 1 { CIMD_SUBMIT \&@[\%i] }

CIMD_LOGOUT

#
# Terminate kermit
#
set exit status 0
TERMINATE

