|
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 |
|
|
15
|
23 |
class UnknownStatusCode(Exception): |
|
|
24 |
def __init__(self,code): |
|
|
25 |
self.code=code |
|
9
|
26 |
|
|
15
|
27 |
def __str__(self): |
|
|
28 |
return "StatusCode %i is unknown"%self.code |
|
9
|
29 |
|
|
|
30 |
|
|
15
|
31 |
class StatusCode: |
|
|
32 |
statusCodes = {10 : "Empfaengernummer nicht korrekt", |
|
9
|
33 |
20 : "Absenderkennung nicht korrekt", |
|
|
34 |
30 : "Nachrichtentext nicht korrekt", |
|
|
35 |
31 : "Messagetyp nicht korrekt", |
|
|
36 |
40 : "SMS Route nicht korrekt", |
|
|
37 |
50 : "Identifikation fehlgeschlagen", |
|
|
38 |
60 : "nicht genuegend Guthaben", |
|
|
39 |
70 : "Netz wird von Route nicht abgedeckt", |
|
|
40 |
71 : "Feature nicht ueber diese Route moeglich", |
|
|
41 |
80 : "Uebergabe an SMS-C fehlgeschlagen", |
|
|
42 |
90 : "Versand nicht moeglich", |
|
|
43 |
100 : "SMS wurde versendet", |
|
|
44 |
999 : "SMS wird zeitversetzt verschickt"} |
|
|
45 |
|
|
15
|
46 |
def __init__(self,code): |
|
|
47 |
if code in self.statusCodes.keys(): |
|
|
48 |
self.code=code |
|
|
49 |
else: |
|
|
50 |
raise UnknownStatusCode(code) |
|
|
51 |
|
|
|
52 |
def __str__(self): |
|
|
53 |
try: |
|
|
54 |
return self.statusCodes[self.code] |
|
|
55 |
except IndexError: |
|
|
56 |
raise UnkownStatusCode(self.code) |
|
|
57 |
|
|
|
58 |
def __int__(self): |
|
|
59 |
if not self.code in self.statusCodes.keys(): |
|
|
60 |
raise UnknownStatusCode(self.code) |
|
|
61 |
return self.code |
|
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
class smstrade(anbieter): |
|
|
67 |
""" |
|
|
68 |
s. auch http://kundencenter.smstrade.de/sites/smstrade.de.kundencenter/__pdf/SMS-Gateway_HTTP_API_v2.pdf |
|
|
69 |
""" |
|
|
70 |
section="smstrade" |
|
|
71 |
url="https://gateway.smstrade.de" |
|
|
72 |
def __init__(self): |
|
|
73 |
self.domain = "smstrade.de" # website of the sms service |
|
|
74 |
self.gateway = "gateway.smstrade.de" # gateway where the request will be sent |
|
|
75 |
self.gatewayPort = 80 # port of the gateway |
|
|
76 |
self.script = "/" # full path to the script that will handle the request |
|
|
77 |
self.method = "POST" # method that will be used. Currently only POST is supported |
|
9
|
78 |
|
|
|
79 |
def read_basic_config(self,filename): |
|
|
80 |
"""Read basic options from the config file""" |
|
|
81 |
cp = ConfigParser.ConfigParser() |
|
|
82 |
cp.read([filename]) |
|
|
83 |
self.key=cp.get(self.section, 'key') |
|
|
84 |
self.route=cp.get(self.section, 'route') |
|
|
85 |
self.from_=cp.get(self.section, 'from') |
|
|
86 |
self.debug=cp.get(self.section, 'debug') |
|
|
87 |
|
|
|
88 |
def sendSMS(self,sms,recipients): |
|
|
89 |
"""send SMS with $sms to $recipients""" |
|
|
90 |
sended = [] |
|
|
91 |
key = self.key |
|
|
92 |
route = unicode(self.route) |
|
|
93 |
message = unicode(sms.content) |
|
|
94 |
from_ = unicode(self.from_) |
|
|
95 |
timestamp = None |
|
|
96 |
for recipient in recipients: |
|
|
97 |
try: |
|
|
98 |
tel = telnumber(recipient) |
|
15
|
99 |
if tel in sended: #only send message once per recipient |
|
9
|
100 |
continue |
|
|
101 |
sended.append(tel) |
|
|
102 |
to = unicode((tel.number)).strip() |
|
15
|
103 |
smsSendStatus = self.__send(key, route, to, message, from_, timestamp) |
|
|
104 |
if int(smsSendStatus) in(100, 999): |
|
9
|
105 |
self.updateStatus(arranged=recipient) |
|
15
|
106 |
else: |
|
|
107 |
self.updateStatus(failed=recipient) |
|
9
|
108 |
except (NotATelNumber,NoValidStatusCode,InternetConnectionError): |
|
|
109 |
self.updateStatus(failed=recipient) |
|
|
110 |
|
|
|
111 |
def __send(self, key, route, to, message, from_=None, timestamp=None): |
|
|
112 |
""" This function is the main part of the request to the sms service. |
|
|
113 |
The function has to return a unicode formated string that will represent the answer of the sms service |
|
|
114 |
to the request.""" |
|
15
|
115 |
parameters= {"key": key, |
|
|
116 |
"route": route, |
|
|
117 |
"to": to, |
|
|
118 |
"message": message, |
|
|
119 |
"debug": self.debug, |
|
|
120 |
} |
|
9
|
121 |
|
|
|
122 |
if from_ is not None: |
|
15
|
123 |
parameters["from"] = from_ |
|
9
|
124 |
|
|
|
125 |
if timestamp is not None: |
|
15
|
126 |
parameters["senddate"] = unicode(timestamp) |
|
9
|
127 |
|
|
15
|
128 |
parameters["concat_sms"] = "1" if len(message) > 160 else "0" |
|
9
|
129 |
|
|
15
|
130 |
params = urllib.urlencode(dict([k, v.encode('iso-8859-1')] for k, v in parameters.items())) |
|
9
|
131 |
headers = {"Content-type": "application/x-www-form-urlencoded", |
|
|
132 |
"Accept": "text/plain"} |
|
|
133 |
conn = httplib.HTTPConnection("%s:%i" % (self.gateway, self.gatewayPort)) |
|
|
134 |
try: |
|
|
135 |
conn.request(self.method, self.script, params, headers) |
|
|
136 |
response = conn.getresponse() |
|
|
137 |
|
|
|
138 |
data = response.read() |
|
|
139 |
except socket.gaierror: |
|
|
140 |
raise InternetConnectionError("%s:%i" % (self.gateway, self.gatewayPort)) |
|
|
141 |
else: |
|
|
142 |
conn.close() |
|
|
143 |
|
|
|
144 |
try: |
|
15
|
145 |
return StatusCode(int(data)) |
|
|
146 |
except UnknownStatusCode: |
|
9
|
147 |
# this happens if the sms will be send delayed |
|
15
|
148 |
return StatusCode(999) |
|
9
|
149 |
|
|
|
150 |
def updateStatus(self, arranged=None, failed=None): |
|
|
151 |
"""is a function that is called, if a new SMS/FAX was send |
|
|
152 |
-arranged is non None, if SMS/FAX was sended successfully |
|
|
153 |
-failed is non None, if SMS/FAX sending failed |
|
|
154 |
the content will be the recipent""" |
|
|
155 |
pass |
|
|
156 |
|
|
|
157 |
class InternetConnectionError(Exception): |
|
|
158 |
def __init__(self, url): |
|
|
159 |
self.url = url |
|
|
160 |
|
|
|
161 |
def __str__(self): |
|
|
162 |
return "InternetConnectionError: It is not possible to open 'http://%s'. Please check your connection to the Internet!" % self.url |