#!/usr/bin/env python2.5
'''
sms.py
Apparently this script required Python 2.5+.
Be careful to save this script with Unix line endings.
see below for source/modification history
'''
def octify(str):
'''
Returns a list of octet bytes representing
each char of the input str.
'''
bytes = map(ord, str)
bitsconsumed = 0
referencebit = 7
octets = []
while len(bytes):
byte = bytes.pop(0)
byte = byte >> bitsconsumed
try:
nextbyte = bytes[0]
bitstocopy = (nextbyte & (0xff >> referencebit)) << referencebit
octet = (byte | bitstocopy)
except:
octet = (byte | 0x00)
if bitsconsumed != 7:
octets.append(byte | bitstocopy)
bitsconsumed += 1
referencebit -= 1
else:
bitsconsumed = 0
referencebit = 7
return octets
def semi_octify(str):
'''
Expects a string containing two digits.
Returns an octet -
first nibble in the octect is the first
digit and the second nibble represents
the second digit.
'''
try:
#had to tweak variables for this to work
digit_1 = 0
digit_2 = 0
octet = 0
#end tweak
digit_1 = int(str[0])
digit_2 = int(str[1])
octet = (digit_2 << 4) | digit_1
except:
octet = (1 << 4) | digit_1
return octet
def formatnumber(number):
'''
Adds trailing F to number if length is
odd (used to be called resetnumber).
'''
length = len(number)
if (length % 2) != 0:
number = number + 'F'
return number
def createPDUstring(number, msg):
'''
Returns a list of bytes to represent a valid PDU message
'''
if verbose == True: print "Debug: message: " + msg
octifiedmsg = octify(msg)
if verbose == True:
print "Debug: octified message: "
print octifiedmsg
number = formatnumber(number)
if verbose == True: print "Debug: formatted number: " + number
octifiednumber = [ semi_octify(number[i:i+2]) for i in range(0, len(number), 2) ]
if verbose == True:
print "Debug: octified number: "
print octifiednumber
HEADER = 1
FIRSTOCTETOFSMSDELIVERMSG = 10
ADDR_TYPE = 129 #unknown format
number_length = len(number)
msg_length = len(msg)
pdu_message = [HEADER, FIRSTOCTETOFSMSDELIVERMSG, number_length, ADDR_TYPE]
pdu_message.extend(octifiednumber)
pdu_message.append(0)
pdu_message.append(0)
pdu_message.append(msg_length)
pdu_message.extend(octifiedmsg)
return pdu_message
class SMS(object):
'''
Sends sms messages
'''
def __init__(self, msg, number):
self.pdustring = createPDUstring(number, msg)
def send(self):
self.__dbus_send(self.pdustring)
def __dbus_send(self, pdu_string):
import dbus
bus = dbus.SystemBus()
smsobject = bus.get_object('com.nokia.phone.SMS', '/com/nokia/phone/SMS/ba212ae1')
smsiface = dbus.Interface(smsobject, 'com.nokia.csd.SMS.Outgoing')
arr = dbus.Array(pdu_string)
msgdeliver = dbus.Array([arr])
smsiface.Send(msgdeliver,'')
def print_pdustring(self):
print self.pdustring
def usage():
print "Usage:"
print "sms.py [--timeofday=hh:mm] [--verbose] [number] [message]"
print "Sends the message to the destination number."
print ""
print "If number and message are specified, but no timeofday,"
print "the sms will be sent immediately."
print "Example: sms.py +12024561414 ""Hi, Mr. President"""
print ""
print "--timeofday (or -t): hour and minute when the message"
print "must be sent, presumable in 24 hour style???"
print ""
print "If message and/or number isn't specified, the program"
print "will ask you for this."
print ""
print "--verbose (or -v) will print debug info"
if __name__ == '__main__':
'''
Main function.
You can either specify destination number and message between double quotes on the command line
or don't specify anything and let the program ask interactively.
'''
import time
def schedule_task(schedule, fn, *args):
import sched
s = sched.scheduler(time.time, time.sleep)
startTime = time.mktime(time.strptime(schedule, '%b %d %H:%M %Y'))
s.enterabs(startTime, 0, fn, args)
s.run()
def sendsms(number, msg):
sms = SMS(msg, number)
#debugging:
if verbose == True:
print "Debug: pdustring that will be sent:"
sms.print_pdustring()
sms.send()
print 'Message sent.'
import getopt, sys
#getopt may/should? be replaced by argparse, available in python 2.7+
try:
# if adding options, modify the parameter after sys.argv to contain all single-character
# options
opts, args = getopt.getopt(sys.argv[1:],"htv", ["help","time=","verbose"])
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
# Init timeofday to an empty string. We'll need this later
# on to test for user specifying timeofday.
timeofday = ''
verbose = False
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-t", "--time"):
timeofday = arg
elif opt in ("-v", "--verbose"):
verbose = True
print "Debug: printing debug output."
else:
assert False, "Unhandled option"
#If this happens, add additional elif clauses above.
# Get number, message and time of day. If necessary, ask user.
# If number and message specified on command line, but no time of day,
# assume you want to send directly so no need for timeofday
try:
number = args[0]
except:
# probably index out of range because of no arguments.
# I'm too lazy to find out how to properly count arguments in python,
# so I'm using exceptions.
number = ''
try:
msg = args[1]
except:
# same here.
msg = ''
# If number and message specified,
# but no timeofday, assume we want to
# send immediately (handy for batch files)
if number != '' and msg != '' and timeofday == '':
sendnow = True
else:
sendnow = False
if number == '':
number = raw_input('Phone number of receiver: ')
if msg == '':
msg = raw_input('Message to send: ')
# Actually send the message:
if sendnow == False:
timeofday = raw_input('Time of day to send [Hit enter if you want to send it now]: ')
today = time.strftime('%b %d x %Y', time.localtime())
schedule = today.replace('x', timeofday)
if verbose == True:
print "Debug: scheduling message for transmission on:"
print schedule
schedule_task(schedule, sendsms, number, msg)
else:
sendsms(number, msg)
'''
http://talk.maemo.org/showthread.php?t=62754&highlight=sms
sendsms.py
Original filename: pymaemosms or something!?
Sending SMS Using Python Dbus:
I used the following links for reference:
1. http://www.dreamfabric.com/sms/
2. http://www.exothermia.net/monkeys_and_robots/2009/04/24/python-code-to-decode-sms-pdu/
3. http://wiki.maemo.org/Phone_control
4. https://garage.maemo.org/plugins/wiki/index.php?Tools&id=1106&type=g
5. https://garage.maemo.org/plugins/wiki/index.php?CSD%20programming%20information&id=1106&type=g
Additional Notes:
1. Use the dbus-monitor to see dbus communication. Follow link #4 above on how to see the dbus CSD communication log.
2. The phone number is expected to be in semi-octets
3. Each character in the message should be represented as an octet. Refer to this http://www.dreamfabric.com/sms/hello.html link on how to convert a message to octets
'''
#!/usr/bin/env python2.5
'''
sms.py
Apparently this script required Python 2.5+.
Be careful to save this script with Unix line endings.
see below for source/modification history
'''
def octify(str):
'''
Returns a list of octet bytes representing
each char of the input str.
'''
bytes = map(ord, str)
bitsconsumed = 0
referencebit = 7
octets = []
while len(bytes):
byte = bytes.pop(0)
byte = byte >> bitsconsumed
try:
nextbyte = bytes[0]
bitstocopy = (nextbyte & (0xff >> referencebit)) << referencebit
octet = (byte | bitstocopy)
except:
octet = (byte | 0x00)
if bitsconsumed != 7:
octets.append(byte | bitstocopy)
bitsconsumed += 1
referencebit -= 1
else:
bitsconsumed = 0
referencebit = 7
return octets
def semi_octify(str):
'''
Expects a string containing two digits.
Returns an octet -
first nibble in the octect is the first
digit and the second nibble represents
the second digit.
'''
try:
#had to tweak variables for this to work
digit_1 = 0
digit_2 = 0
octet = 0
#end tweak
digit_1 = int(str[0])
digit_2 = int(str[1])
octet = (digit_2 << 4) | digit_1
except:
octet = (1 << 4) | digit_1
return octet
def formatnumber(number):
'''
Adds trailing F to number if length is
odd (used to be called resetnumber).
'''
length = len(number)
if (length % 2) != 0:
number = number + 'F'
return number
def createPDUstring(number, msg):
'''
Returns a list of bytes to represent a valid PDU message
'''
if verbose == True: print "Debug: message: " + msg
octifiedmsg = octify(msg)
if verbose == True:
print "Debug: octified message: "
print octifiedmsg
number = formatnumber(number)
if verbose == True: print "Debug: formatted number: " + number
octifiednumber = [ semi_octify(number[i:i+2]) for i in range(0, len(number), 2) ]
if verbose == True:
print "Debug: octified number: "
print octifiednumber
HEADER = 1
FIRSTOCTETOFSMSDELIVERMSG = 10
ADDR_TYPE = 129 #unknown format
number_length = len(number)
msg_length = len(msg)
pdu_message = [HEADER, FIRSTOCTETOFSMSDELIVERMSG, number_length, ADDR_TYPE]
pdu_message.extend(octifiednumber)
pdu_message.append(0)
pdu_message.append(0)
pdu_message.append(msg_length)
pdu_message.extend(octifiedmsg)
return pdu_message
class SMS(object):
'''
Sends sms messages
'''
def __init__(self, msg, number):
self.pdustring = createPDUstring(number, msg)
def send(self):
self.__dbus_send(self.pdustring)
def __dbus_send(self, pdu_string):
import dbus
bus = dbus.SystemBus()
smsobject = bus.get_object('com.nokia.phone.SMS', '/com/nokia/phone/SMS/ba212ae1')
smsiface = dbus.Interface(smsobject, 'com.nokia.csd.SMS.Outgoing')
arr = dbus.Array(pdu_string)
msgdeliver = dbus.Array([arr])
smsiface.Send(msgdeliver,'')
def print_pdustring(self):
print self.pdustring
def usage():
print "Usage:"
print "sms.py [--timeofday=hh:mm] [--verbose] [number] [message]"
print "Sends the message to the destination number."
print ""
print "If number and message are specified, but no timeofday,"
print "the sms will be sent immediately."
print "Example: sms.py +12024561414 ""Hi, Mr. President"""
print ""
print "--timeofday (or -t): hour and minute when the message"
print "must be sent, presumable in 24 hour style???"
print ""
print "If message and/or number isn't specified, the program"
print "will ask you for this."
print ""
print "--verbose (or -v) will print debug info"
if __name__ == '__main__':
'''
Main function.
You can either specify destination number and message between double quotes on the command line
or don't specify anything and let the program ask interactively.
'''
import time
def schedule_task(schedule, fn, *args):
import sched
s = sched.scheduler(time.time, time.sleep)
startTime = time.mktime(time.strptime(schedule, '%b %d %H:%M %Y'))
s.enterabs(startTime, 0, fn, args)
s.run()
def sendsms(number, msg):
sms = SMS(msg, number)
#debugging:
if verbose == True:
print "Debug: pdustring that will be sent:"
sms.print_pdustring()
sms.send()
print 'Message sent.'
import getopt, sys
#getopt may/should? be replaced by argparse, available in python 2.7+
try:
# if adding options, modify the parameter after sys.argv to contain all single-character
# options
opts, args = getopt.getopt(sys.argv[1:],"htv", ["help","time=","verbose"])
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
# Init timeofday to an empty string. We'll need this later
# on to test for user specifying timeofday.
timeofday = ''
verbose = False
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-t", "--time"):
timeofday = arg
elif opt in ("-v", "--verbose"):
verbose = True
print "Debug: printing debug output."
else:
assert False, "Unhandled option"
#If this happens, add additional elif clauses above.
# Get number, message and time of day. If necessary, ask user.
# If number and message specified on command line, but no time of day,
# assume you want to send directly so no need for timeofday
try:
number = args[0]
except:
# probably index out of range because of no arguments.
# I'm too lazy to find out how to properly count arguments in python,
# so I'm using exceptions.
number = ''
try:
msg = args[1]
except:
# same here.
msg = ''
# If number and message specified,
# but no timeofday, assume we want to
# send immediately (handy for batch files)
if number != '' and msg != '' and timeofday == '':
sendnow = True
else:
sendnow = False
if number == '':
number = raw_input('Phone number of receiver: ')
if msg == '':
msg = raw_input('Message to send: ')
# Actually send the message:
if sendnow == False:
timeofday = raw_input('Time of day to send [Hit enter if you want to send it now]: ')
today = time.strftime('%b %d x %Y', time.localtime())
schedule = today.replace('x', timeofday)
if verbose == True:
print "Debug: scheduling message for transmission on:"
print schedule
schedule_task(schedule, sendsms, number, msg)
else:
sendsms(number, msg)
'''
http://talk.maemo.org/showthread.php?t=62754&highlight=sms
sendsms.py
Original filename: pymaemosms or something!?
Sending SMS Using Python Dbus:
I used the following links for reference:
1. http://www.dreamfabric.com/sms/
2. http://www.exothermia.net/monkeys_and_robots/2009/04/24/python-code-to-decode-sms-pdu/
3. http://wiki.maemo.org/Phone_control
4. https://garage.maemo.org/plugins/wiki/index.php?Tools&id=1106&type=g
5. https://garage.maemo.org/plugins/wiki/index.php?CSD%20programming%20information&id=1106&type=g
Additional Notes:
1. Use the dbus-monitor to see dbus communication. Follow link #4 above on how to see the dbus CSD communication log.
2. The phone number is expected to be in semi-octets
3. Each character in the message should be represented as an octet. Refer to this http://www.dreamfabric.com/sms/hello.html link on how to convert a message to octets
'''