Created
June 17, 2011 08:50
-
-
Save pklaus/1031088 to your computer and use it in GitHub Desktop.
Command Line Tool to send an SMS via the Sipgate API found on <http://irq0.org/Code?action=AttachFile&do=view&target=sms.py> on 2008-03-16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[account] | |
user: spam | |
passwd: eggs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
# -*- coding: utf-8 -*- | |
import sys,os | |
import getopt | |
import re | |
import xmlrpclib | |
from ConfigParser import ConfigParser | |
IDENT={'ClientName': 'serious-net.org sms-tool', | |
'ClientVersion': '20071016'} | |
PATH=sys.path[0] | |
def cli(): | |
# parse commandline | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], "hn:t:f:a") | |
except getopt.GetoptError: | |
# print help information and exit: | |
printUsage() | |
sys.exit(23) | |
caller_id = None | |
text = None | |
ask = False | |
for o, a in opts: | |
if o == "-h": | |
printUsage() | |
sys.exit() | |
if o == "-n": | |
caller_id = a | |
if o == "-t": | |
text = a | |
if o == "-f": | |
text = readFile(a) | |
if o == "-a": | |
ask = True | |
if not caller_id: | |
caller_id = raw_input("Caller ID: ") | |
if not text: | |
text = "" | |
line = "" | |
while line != '.': | |
line = sys.stdin.readline()[:-1] | |
text += line + "\n" | |
# input checks | |
caller_id = caller_id.strip().replace(" ","").replace("-","").replace("/","") | |
# convert caller_id to sip_uri | |
sip_uri = "" | |
if re.compile("^49[1-9][0-9]*$").match(caller_id): | |
# print "match ^49[1-9][0-9]*$ " | |
sip_uri = "sip:%s@sipgate.net" % (caller_id) | |
elif re.compile("^0[1-9]*$").match(caller_id): | |
# print "match ^0[1-9]*$" | |
sip_uri = "sip:49%s@sipgate.net" % (caller_id[1:]) | |
elif re.compile("^\+49[1-9][0-9]*$").match(caller_id): | |
# print "match ^+49[1-9][0-9]*$" | |
sip_uri = "sip:%s@sipgate.net" % (caller_id[1:]) | |
else: | |
# print "invalid caller id ..exiting" | |
sys.exit(23) | |
# open config file | |
config = ConfigParser() | |
config.read([os.path.join(PATH,'sms.conf')]) | |
user = config.get('account','user') | |
passwd = config.get('account','passwd') | |
# open connection | |
sipgate = initConnection(user, passwd) | |
if ask: | |
print "Really send SMS message?" | |
print "sip uri:", sip_uri | |
print "text:\n", text | |
confirm = raw_input("(y/n) ") | |
if str(confirm) == 'n': | |
sys.exit(0) | |
sms(sipgate, sip_uri, text) | |
def readFile(filename): | |
try: | |
f = file(filename, 'r') | |
return f.read() | |
except IOError: | |
print "error reading file ..exiting" | |
sys.exit(23) | |
def printUsage(): | |
print """ | |
sms.py - simple sms tool for sipgate using xmlrpc | |
author: Marcel Lauhoff <ml@serious-net.org> | |
usage: | |
sms.py [OPTIONS] | |
when called without args caller id and text is read from commandline | |
-n <caller_id> destination caller id | |
-t <text> sms text | |
-f <filename> file containing sms text | |
-a ask for confirmation before sending | |
""" | |
def sms(rpc_conn, sip_uri, text): | |
result = rpc_conn.samurai.SessionInitiate({'RemoteUri': sip_uri, | |
'TOS': 'text', | |
'Content': text}) | |
if result['StatusCode'] == 200: | |
print "SMS sent :)" | |
else: | |
print "SMS send faild :(" | |
sys.exit(23) | |
def initConnection(user, passwd): | |
# open connection | |
sipgate = xmlrpclib.ServerProxy("https://%s:%s@samurai.sipgate.net/RPC2" % (user, passwd)) | |
# do identify | |
result = sipgate.samurai.ClientIdentify(IDENT) | |
if result['StatusCode'] == 200: | |
# print "Command ClientIdentify successful" | |
pass | |
else: | |
print "Command ClientIdentify failed.. exiting" | |
sys.exit(23) | |
return sipgate | |
if __name__ == '__main__': cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment