iro/anbieter/sipgate.py
changeset 0 a3b6e531f0d2
child 9 4c5f1cf088f6
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/iro/anbieter/sipgate.py	Thu Oct 22 10:00:01 2009 +0200
@@ -0,0 +1,140 @@
+# -*- coding: utf-8 -*-
+#Copyright (C) 2009  Sandro Knauß <bugs@sandroknauss.de>
+
+#This program is free software; you can redistribute it and/or modify it under the terms
+#of the GNU General Public License as published by the Free Software Foundation;
+#either version 3 of the License, or any later version.
+#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+#without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+#See the GNU General Public License for more details.
+
+#You should have received a copy of the GNU General Public License
+#along with this program; if not, see <http://www.gnu.org/licenses/>.
+
+
+from anbieter import anbieter
+from telnumber import telnumber, NotATelNumber
+import ConfigParser
+import xmlrpclib
+import base64
+
+class NoValidStatusCode(Exception):
+    pass
+
+class sipgate(anbieter):
+    """
+        s. auch http://www.tuxad.com/sipgate.html
+        und http://lists.sipgate.net/pipermail/sipgate-developers/2007-September/000016.html
+    """
+    section="sipgate"
+    url="https://%s:%s@samurai.sipgate.net/RPC2"
+    def __init__(self,user="",password=""):
+        self.user=user
+        self.password=password
+
+    def read_basic_config(self,filename):
+        """Read basic options from the config file"""
+        cp = ConfigParser.ConfigParser()
+        cp.read([filename])
+        self.user=cp.get(self.section, 'user')
+        self.password=cp.get(self.section, 'password')
+
+    def sendSMS(self,sms,recipients):
+        """send SMS with $sms to $recipients"""
+        args={
+                "TOS" : "text",
+                "Content" : sms.content
+                }
+        self.__send(args,recipients)
+
+    def sendFAX(self,fax,recipients):
+        """send the PDF file $fax  to $recipients"""
+        pdf=open(fax.attachments[0],"rb")
+        args={
+                "TOS" : "fax",
+                "Content" : base64.encodestring(pdf.read())
+                }
+        pdf.close()
+        self.__send(args,recipients)
+
+    def __connect(self):
+        """connect to sipgate XMLRPC Server"""
+        self.samurai=xmlrpclib.Server(self.url%(self.user,self.password)).samurai
+        args_identify = {
+                "ClientName"    : "anbieter.py",
+                "ClientVersion" : "V1.0",
+                "ClientVendor"  : "Sandro Knauss"
+                }
+                
+        self.__send_method(self.samurai.ClientIdentify, args_identify)
+
+    def __send_method(self, func, args=None):
+        """execute $func and test weather if the func  ran successfully or not"""
+        if args==None:
+            xmlrpc_result = func()
+        else:
+            xmlrpc_result = func(args)
+        if xmlrpc_result['StatusCode'] != 200:
+            raise NoValidStatusCode("There was an error during identification to the server! %d %s"% (xmlrpc_result['StatusCode'], xmlrpc_result['StatusString']))
+        
+        return xmlrpc_result
+
+    def __send(self,args,recipients):
+        """main sending method - sending the args to $recipients"""
+        sended=[]
+        self.__connect()
+        for recipient in recipients:
+            try:
+                tel = telnumber(recipient)
+                
+                if tel in sended:                                                                           #only send message once per recipient
+                  continue
+                sended.append(tel)
+                
+                args["RemoteUri"]="sip:%s%s@sipgate.net"%(tel.land,tel.number)
+                xmlrpc_result = self.__send_method(self.samurai.SessionInitiate, args)
+                self.updateStatus(arranged=recipient)
+            except NotATelNumber,NoValidStatusCode :
+                self.updateStatus(failed=recipient)
+
+        self.__disconnect()
+
+    def updateStatus(self, arranged=None, failed=None):
+        """is a function that is called, if a new SMS/FAX was send
+        -arranged is non None, if SMS/FAX was sended successfully
+        -failed is non None, if SMS/FAX sending failed
+        the content will be the recipent"""
+        pass
+
+    def BalanceGet(self):
+        """get the balance of sipgate"""
+        self.__connect()
+        ret = self.__send_method(self.samurai.BalanceGet )
+        self.__disconnect()
+        return ret['CurrentBalance']
+
+    def getNewMessages(self):
+        """get new messages from inbox"""
+        self.__connect()
+        tmp = self.__send_method(self.samurai.UmSummaryGet)
+        self.__disconnect()
+        tmp=tmp['UmSummary']
+        ret={}
+        for entry in tmp:
+            ret[entry['TOS']]={'read':entry["Read"],'unread':entry["Unread"]}
+        return ret
+
+    def getRecommendedInterval(self,methods):
+        """how often you can call one $methods"""
+        self.__connect()
+        args = {"MethodList" : methods }
+        tmp = self.__send_method(self.samurai.RecommendedIntervalGet, args)
+        self. __disconnect()
+        ret={}
+        for entry in tmp['IntervalList']:
+            ret[entry['MethodName']]=entry['RecommendedInterval']
+        return ret
+
+    def __disconnect(self):
+        """disconnect xmlrpc client"""
+        self.xmlrpc=None