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