|
9
|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
#Copyright (C) 2009 Georg Bischoff |
|
|
3 |
|
|
|
4 |
#This program is free software; you can redistribute it and/or modify it under the terms |
|
|
5 |
#of the GNU General Public License as published by the Free Software Foundation; |
|
|
6 |
#either version 3 of the License, or any later version. |
|
|
7 |
#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
|
|
8 |
#without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
|
9 |
#See the GNU General Public License for more details. |
|
|
10 |
|
|
|
11 |
#You should have received a copy of the GNU General Public License |
|
|
12 |
#along with this program; if not, see <http://www.gnu.org/licenses/>. |
|
|
13 |
|
|
|
14 |
|
|
|
15 |
from anbieter import anbieter |
|
|
16 |
from sipgate import NoValidStatusCode |
|
|
17 |
from telnumber import telnumber, NotATelNumber |
|
|
18 |
import ConfigParser |
|
|
19 |
import xmlrpclib |
|
|
20 |
import base64 |
|
|
21 |
import urllib, httplib |
|
|
22 |
|
|
|
23 |
class smstrade(anbieter): |
|
|
24 |
""" |
|
|
25 |
s. auch http://kundencenter.smstrade.de/sites/smstrade.de.kundencenter/__pdf/SMS-Gateway_HTTP_API_v2.pdf |
|
|
26 |
""" |
|
|
27 |
section="smstrade" |
|
|
28 |
url="https://gateway.smstrade.de" |
|
|
29 |
def __init__(self): |
|
|
30 |
self.domain = "smstrade.de" # website of the sms service |
|
|
31 |
self.gateway = "gateway.smstrade.de" # gateway where the request will be sent |
|
|
32 |
self.gatewayPort = 80 # port of the gateway |
|
|
33 |
self.script = "/" # full path to the script that will handle the request |
|
|
34 |
self.method = "POST" # method that will be used. Currently only POST is supported |
|
|
35 |
|
|
|
36 |
self.maxMessageLength = None # maximum length of message; None if should not be set |
|
|
37 |
self.smsCanBeSendDelayed = True # True if sms can be sent delayed by the sms service. Otherwise False |
|
|
38 |
self.senderRe = r"^.{0,11}|[0-9]{0,15}" # RegEx for the sender-input-field |
|
|
39 |
|
|
|
40 |
self.routes = ("basicplus", "economy", "gold", "direct") # possible routes that can be used |
|
|
41 |
self.routesWithSourceIdentifier = ("gold", "direct") # routes where a sender can be defined |
|
|
42 |
|
|
|
43 |
# statusCodes that the sms service returns on requests |
|
|
44 |
self.statusCodes = {10 : "Empfaengernummer nicht korrekt", |
|
|
45 |
20 : "Absenderkennung nicht korrekt", |
|
|
46 |
30 : "Nachrichtentext nicht korrekt", |
|
|
47 |
31 : "Messagetyp nicht korrekt", |
|
|
48 |
40 : "SMS Route nicht korrekt", |
|
|
49 |
50 : "Identifikation fehlgeschlagen", |
|
|
50 |
60 : "nicht genuegend Guthaben", |
|
|
51 |
70 : "Netz wird von Route nicht abgedeckt", |
|
|
52 |
71 : "Feature nicht ueber diese Route moeglich", |
|
|
53 |
80 : "Uebergabe an SMS-C fehlgeschlagen", |
|
|
54 |
90 : "Versand nicht moeglich", |
|
|
55 |
100 : "SMS wurde versendet", |
|
|
56 |
999 : "SMS wird zeitversetzt verschickt"} |
|
|
57 |
|
|
|
58 |
self.parameters = {} # don't write anything into this dict! Don't delete it! |
|
|
59 |
|
|
|
60 |
def read_basic_config(self,filename): |
|
|
61 |
"""Read basic options from the config file""" |
|
|
62 |
cp = ConfigParser.ConfigParser() |
|
|
63 |
cp.read([filename]) |
|
|
64 |
self.key=cp.get(self.section, 'key') |
|
|
65 |
self.route=cp.get(self.section, 'route') |
|
|
66 |
self.from_=cp.get(self.section, 'from') |
|
|
67 |
self.debug=cp.get(self.section, 'debug') |
|
|
68 |
|
|
|
69 |
def sendSMS(self,sms,recipients): |
|
|
70 |
"""send SMS with $sms to $recipients""" |
|
|
71 |
sended = [] |
|
|
72 |
key = self.key |
|
|
73 |
route = unicode(self.route) |
|
|
74 |
message = unicode(sms.content) |
|
|
75 |
from_ = unicode(self.from_) |
|
|
76 |
timestamp = None |
|
|
77 |
for recipient in recipients: |
|
|
78 |
try: |
|
|
79 |
tel = telnumber(recipient) |
|
|
80 |
if tel in sended: #only send message once per recipient |
|
|
81 |
continue |
|
|
82 |
sended.append(tel) |
|
|
83 |
to = unicode((tel.number)).strip() |
|
|
84 |
code, smsSendStatus = self.__send(key, route, to, message, from_, timestamp) |
|
|
85 |
if code in(100, 999): |
|
|
86 |
self.updateStatus(arranged=recipient) |
|
|
87 |
except (NotATelNumber,NoValidStatusCode,InternetConnectionError): |
|
|
88 |
self.updateStatus(failed=recipient) |
|
|
89 |
|
|
|
90 |
def __send(self, key, route, to, message, from_=None, timestamp=None): |
|
|
91 |
""" This function is the main part of the request to the sms service. |
|
|
92 |
The function has to return a unicode formated string that will represent the answer of the sms service |
|
|
93 |
to the request.""" |
|
|
94 |
self.parameters["key"] = key |
|
|
95 |
self.parameters["route"] = route |
|
|
96 |
self.parameters["to"] = to |
|
|
97 |
self.parameters["message"] = message |
|
|
98 |
self.parameters["debug"] = self.debug |
|
|
99 |
|
|
|
100 |
if from_ is not None: |
|
|
101 |
self.parameters["from"] = from_ |
|
|
102 |
else: |
|
|
103 |
if "from" in self.parameters.keys(): |
|
|
104 |
del(self.parameters["from"]) |
|
|
105 |
|
|
|
106 |
if timestamp is not None: |
|
|
107 |
self.parameters["senddate"] = unicode(timestamp) |
|
|
108 |
else: |
|
|
109 |
if "senddate" in self.parameters.keys(): |
|
|
110 |
del(self.parameters["senddate"]) |
|
|
111 |
|
|
|
112 |
self.parameters["concat_sms"] = "1" if len(message) > 160 else "0" |
|
|
113 |
|
|
|
114 |
params = urllib.urlencode(dict([k, v.encode('iso-8859-1')] for k, v in self.parameters.items())) |
|
|
115 |
headers = {"Content-type": "application/x-www-form-urlencoded", |
|
|
116 |
"Accept": "text/plain"} |
|
|
117 |
conn = httplib.HTTPConnection("%s:%i" % (self.gateway, self.gatewayPort)) |
|
|
118 |
try: |
|
|
119 |
conn.request(self.method, self.script, params, headers) |
|
|
120 |
response = conn.getresponse() |
|
|
121 |
|
|
|
122 |
data = response.read() |
|
|
123 |
except socket.gaierror: |
|
|
124 |
raise InternetConnectionError("%s:%i" % (self.gateway, self.gatewayPort)) |
|
|
125 |
else: |
|
|
126 |
conn.close() |
|
|
127 |
|
|
|
128 |
try: |
|
|
129 |
return int(data), self.statusCodes[int(data)] |
|
|
130 |
except ValueError: |
|
|
131 |
# this happens if the sms will be send delayed |
|
|
132 |
return 999, self.statusCodes[999] |
|
|
133 |
|
|
|
134 |
def updateStatus(self, arranged=None, failed=None): |
|
|
135 |
"""is a function that is called, if a new SMS/FAX was send |
|
|
136 |
-arranged is non None, if SMS/FAX was sended successfully |
|
|
137 |
-failed is non None, if SMS/FAX sending failed |
|
|
138 |
the content will be the recipent""" |
|
|
139 |
pass |
|
|
140 |
|
|
|
141 |
class InternetConnectionError(Exception): |
|
|
142 |
def __init__(self, url): |
|
|
143 |
self.url = url |
|
|
144 |
|
|
|
145 |
def __str__(self): |
|
|
146 |
return "InternetConnectionError: It is not possible to open 'http://%s'. Please check your connection to the Internet!" % self.url |