|
1 # -*- coding: utf-8 -*- |
|
2 #Copyright (C) 2009 Sandro Knauß <bugs@sandroknauss.de> |
|
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 telnumber import telnumber, NotATelNumber |
|
17 import ConfigParser |
|
18 import xmlrpclib |
|
19 import base64 |
|
20 |
|
21 class NoValidStatusCode(Exception): |
|
22 pass |
|
23 |
|
24 class sipgate(anbieter): |
|
25 """ |
|
26 s. auch http://www.tuxad.com/sipgate.html |
|
27 und http://lists.sipgate.net/pipermail/sipgate-developers/2007-September/000016.html |
|
28 """ |
|
29 section="sipgate" |
|
30 url="https://%s:%s@samurai.sipgate.net/RPC2" |
|
31 def __init__(self,user="",password=""): |
|
32 self.user=user |
|
33 self.password=password |
|
34 |
|
35 def read_basic_config(self,filename): |
|
36 """Read basic options from the config file""" |
|
37 cp = ConfigParser.ConfigParser() |
|
38 cp.read([filename]) |
|
39 self.user=cp.get(self.section, 'user') |
|
40 self.password=cp.get(self.section, 'password') |
|
41 |
|
42 def sendSMS(self,sms,recipients): |
|
43 """send SMS with $sms to $recipients""" |
|
44 args={ |
|
45 "TOS" : "text", |
|
46 "Content" : sms.content |
|
47 } |
|
48 self.__send(args,recipients) |
|
49 |
|
50 def sendFAX(self,fax,recipients): |
|
51 """send the PDF file $fax to $recipients""" |
|
52 pdf=open(fax.attachments[0],"rb") |
|
53 args={ |
|
54 "TOS" : "fax", |
|
55 "Content" : base64.encodestring(pdf.read()) |
|
56 } |
|
57 pdf.close() |
|
58 self.__send(args,recipients) |
|
59 |
|
60 def __connect(self): |
|
61 """connect to sipgate XMLRPC Server""" |
|
62 self.samurai=xmlrpclib.Server(self.url%(self.user,self.password)).samurai |
|
63 args_identify = { |
|
64 "ClientName" : "anbieter.py", |
|
65 "ClientVersion" : "V1.0", |
|
66 "ClientVendor" : "Sandro Knauss" |
|
67 } |
|
68 |
|
69 self.__send_method(self.samurai.ClientIdentify, args_identify) |
|
70 |
|
71 def __send_method(self, func, args=None): |
|
72 """execute $func and test weather if the func ran successfully or not""" |
|
73 if args==None: |
|
74 xmlrpc_result = func() |
|
75 else: |
|
76 xmlrpc_result = func(args) |
|
77 if xmlrpc_result['StatusCode'] != 200: |
|
78 raise NoValidStatusCode("There was an error during identification to the server! %d %s"% (xmlrpc_result['StatusCode'], xmlrpc_result['StatusString'])) |
|
79 |
|
80 return xmlrpc_result |
|
81 |
|
82 def __send(self,args,recipients): |
|
83 """main sending method - sending the args to $recipients""" |
|
84 sended=[] |
|
85 self.__connect() |
|
86 for recipient in recipients: |
|
87 try: |
|
88 tel = telnumber(recipient) |
|
89 |
|
90 if tel in sended: #only send message once per recipient |
|
91 continue |
|
92 sended.append(tel) |
|
93 |
|
94 args["RemoteUri"]="sip:%s%s@sipgate.net"%(tel.land,tel.number) |
|
95 xmlrpc_result = self.__send_method(self.samurai.SessionInitiate, args) |
|
96 self.updateStatus(arranged=recipient) |
|
97 except NotATelNumber,NoValidStatusCode : |
|
98 self.updateStatus(failed=recipient) |
|
99 |
|
100 self.__disconnect() |
|
101 |
|
102 def updateStatus(self, arranged=None, failed=None): |
|
103 """is a function that is called, if a new SMS/FAX was send |
|
104 -arranged is non None, if SMS/FAX was sended successfully |
|
105 -failed is non None, if SMS/FAX sending failed |
|
106 the content will be the recipent""" |
|
107 pass |
|
108 |
|
109 def BalanceGet(self): |
|
110 """get the balance of sipgate""" |
|
111 self.__connect() |
|
112 ret = self.__send_method(self.samurai.BalanceGet ) |
|
113 self.__disconnect() |
|
114 return ret['CurrentBalance'] |
|
115 |
|
116 def getNewMessages(self): |
|
117 """get new messages from inbox""" |
|
118 self.__connect() |
|
119 tmp = self.__send_method(self.samurai.UmSummaryGet) |
|
120 self.__disconnect() |
|
121 tmp=tmp['UmSummary'] |
|
122 ret={} |
|
123 for entry in tmp: |
|
124 ret[entry['TOS']]={'read':entry["Read"],'unread':entry["Unread"]} |
|
125 return ret |
|
126 |
|
127 def getRecommendedInterval(self,methods): |
|
128 """how often you can call one $methods""" |
|
129 self.__connect() |
|
130 args = {"MethodList" : methods } |
|
131 tmp = self.__send_method(self.samurai.RecommendedIntervalGet, args) |
|
132 self. __disconnect() |
|
133 ret={} |
|
134 for entry in tmp['IntervalList']: |
|
135 ret[entry['MethodName']]=entry['RecommendedInterval'] |
|
136 return ret |
|
137 |
|
138 def __disconnect(self): |
|
139 """disconnect xmlrpc client""" |
|
140 self.xmlrpc=None |