iro/anbieter/smstrade.py
changeset 15 c04a21066aad
parent 9 4c5f1cf088f6
child 18 f0d31c70744d
--- a/iro/anbieter/smstrade.py	Sun Nov 08 16:09:25 2009 +0100
+++ b/iro/anbieter/smstrade.py	Fri Nov 13 01:42:38 2009 +0100
@@ -20,28 +20,16 @@
 import base64
 import urllib, httplib
 
-class smstrade(anbieter):
-    """
-    s. auch http://kundencenter.smstrade.de/sites/smstrade.de.kundencenter/__pdf/SMS-Gateway_HTTP_API_v2.pdf
-    """
-    section="smstrade"
-    url="https://gateway.smstrade.de"
-    def __init__(self):       
-        self.domain = "smstrade.de" # website of the sms service
-        self.gateway = "gateway.smstrade.de" # gateway where the request will be sent
-        self.gatewayPort = 80 # port of the gateway
-        self.script = "/"  # full path to the script that will handle the request
-        self.method = "POST" # method that will be used. Currently only POST is supported
+class UnknownStatusCode(Exception):
+    def __init__(self,code):
+        self.code=code
 
-        self.maxMessageLength = None # maximum length of message; None if should not be set
-        self.smsCanBeSendDelayed = True # True if sms can be sent delayed by the sms service. Otherwise False
-        self.senderRe = r"^.{0,11}|[0-9]{0,15}" # RegEx for the sender-input-field
+    def __str__(self):
+        return "StatusCode %i is unknown"%self.code
 
-        self.routes = ("basicplus", "economy", "gold", "direct") # possible routes that can be used
-        self.routesWithSourceIdentifier = ("gold", "direct") # routes where a sender can be defined
 
-        # statusCodes that the sms service returns on requests
-        self.statusCodes = {10 : "Empfaengernummer nicht korrekt",
+class StatusCode:
+     statusCodes = {10 : "Empfaengernummer nicht korrekt",
             20 : "Absenderkennung nicht korrekt",
             30 : "Nachrichtentext nicht korrekt",
             31 : "Messagetyp nicht korrekt",
@@ -55,7 +43,38 @@
             100 : "SMS wurde versendet",
             999 : "SMS wird zeitversetzt verschickt"}
 
-        self.parameters = {} # don't write anything into this dict! Don't delete it!                   
+     def __init__(self,code):
+        if code in self.statusCodes.keys():
+            self.code=code
+        else:
+            raise UnknownStatusCode(code)
+     
+     def __str__(self):
+        try:
+            return self.statusCodes[self.code]
+        except IndexError:
+            raise UnkownStatusCode(self.code)
+
+     def __int__(self):
+        if not self.code in self.statusCodes.keys():
+            raise UnknownStatusCode(self.code)
+        return self.code
+
+
+
+
+class smstrade(anbieter):
+    """
+    s. auch http://kundencenter.smstrade.de/sites/smstrade.de.kundencenter/__pdf/SMS-Gateway_HTTP_API_v2.pdf
+    """
+    section="smstrade"
+    url="https://gateway.smstrade.de"
+    def __init__(self):       
+        self.domain = "smstrade.de" # website of the sms service
+        self.gateway = "gateway.smstrade.de" # gateway where the request will be sent
+        self.gatewayPort = 80 # port of the gateway
+        self.script = "/"  # full path to the script that will handle the request
+        self.method = "POST" # method that will be used. Currently only POST is supported
 
     def read_basic_config(self,filename):
         """Read basic options from the config file"""
@@ -77,13 +96,15 @@
         for recipient in recipients:
             try:
                 tel = telnumber(recipient)                
-                if tel in sended:                                                                           #only send message once per recipient
+                if tel in sended:                                             #only send message once per recipient
                     continue
                 sended.append(tel)	
                 to = unicode((tel.number)).strip()						                
-                code, smsSendStatus = self.__send(key, route, to, message, from_, timestamp)	
-                if code in(100, 999):
+                smsSendStatus = self.__send(key, route, to, message, from_, timestamp)	
+                if int(smsSendStatus) in(100, 999):
                     self.updateStatus(arranged=recipient)
+                else:
+                    self.updateStatus(failed=recipient)
             except (NotATelNumber,NoValidStatusCode,InternetConnectionError):
                 self.updateStatus(failed=recipient)
 
@@ -91,27 +112,22 @@
         """ This function is the main part of the request to the sms service.    
         The function has to return a unicode formated string that will represent the answer of the sms service
         to the request."""
-        self.parameters["key"] = key        
-        self.parameters["route"] = route
-        self.parameters["to"] = to
-        self.parameters["message"] = message
-        self.parameters["debug"] = self.debug				
+        parameters= {"key": key,
+                "route": route,
+                "to": to,
+                "message": message,
+                "debug": self.debug,
+                }
         
         if from_ is not None:
-            self.parameters["from"] = from_
-        else:
-            if "from" in self.parameters.keys():
-                del(self.parameters["from"])
+            parameters["from"] = from_
         
         if timestamp is not None:
-            self.parameters["senddate"] = unicode(timestamp)
-        else:
-            if "senddate" in self.parameters.keys():
-                del(self.parameters["senddate"])
+            parameters["senddate"] = unicode(timestamp)
 
-        self.parameters["concat_sms"] = "1" if len(message) > 160 else "0"
+        parameters["concat_sms"] = "1" if len(message) > 160 else "0"
 
-        params = urllib.urlencode(dict([k, v.encode('iso-8859-1')] for k, v in self.parameters.items()))
+        params = urllib.urlencode(dict([k, v.encode('iso-8859-1')] for k, v in parameters.items()))
         headers = {"Content-type": "application/x-www-form-urlencoded",
             "Accept": "text/plain"}
         conn = httplib.HTTPConnection("%s:%i" % (self.gateway, self.gatewayPort))
@@ -126,10 +142,10 @@
             conn.close()
 
         try:
-            return int(data), self.statusCodes[int(data)]
-        except ValueError:
+            return StatusCode(int(data))
+        except UnknownStatusCode:
             # this happens if the sms will be send delayed
-            return 999, self.statusCodes[999]
+            return StatusCode(999)
 
     def updateStatus(self, arranged=None, failed=None):
         """is a function that is called, if a new SMS/FAX was send