11import requests
22import base64
3- from urllib .parse import urlencode
3+ from urllib .parse import urlencode , parse_qs
44
55class DirectAdminAPI :
66 def __init__ (self , server , username , password ):
@@ -17,54 +17,81 @@ def _make_request(self, cmd, method='GET', data=None):
1717 'Content-Type' : 'application/x-www-form-urlencoded'
1818 }
1919
20- if method == 'GET' :
21- response = self .session .get (url , headers = headers )
22- else :
23- response = self .session .post (url , headers = headers , data = urlencode (data ) if data else None )
20+ try :
21+ if method == 'GET' :
22+ response = self .session .get (url , headers = headers , verify = False , timeout = 10 )
23+ else :
24+ response = self .session .post (url , headers = headers , data = urlencode (data ) if data else None , verify = False , timeout = 10 )
2425
25- return response
26+ if response .status_code == 401 :
27+ raise Exception ("DirectAdmin authentication failed" )
28+
29+ return response
30+ except requests .exceptions .RequestException as e :
31+ raise Exception (f"DirectAdmin connection error: { str (e )} " )
2632
2733 def get_email_accounts (self , domain ):
28- response = self ._make_request (f'API_POP?domain={ domain } ' )
29- if response .status_code == 200 :
30- accounts = []
31- for line in response .text .strip ().split ('\n ' ):
32- if '=' in line :
33- email = line .split ('=' )[0 ]
34- if email != self .username : # Exclude API username
35- accounts .append (f"{ email } @{ domain } " )
36- return accounts
37- return []
34+ try :
35+ response = self ._make_request (f'API_POP?domain={ domain } ' )
36+ if response .status_code == 200 :
37+ accounts = []
38+ # Parse URL-encoded response
39+ data = parse_qs (response .text .strip ())
40+ for key in data :
41+ if '@' not in key and key != self .username :
42+ accounts .append (f"{ key } @{ domain } " )
43+ return sorted (accounts )
44+ return []
45+ except Exception as e :
46+ print (f"Error in get_email_accounts: { e } " )
47+ return []
3848
3949 def get_forwarders (self , domain ):
40- response = self ._make_request (f'API_EMAIL_FORWARDERS?domain={ domain } ' )
41- if response .status_code == 200 :
42- forwarders = []
43- for line in response .text .strip ().split ('\n ' ):
44- if '=' in line :
45- alias , destinations = line .split ('=' , 1 )
46- forwarders .append ({
47- 'alias' : alias ,
48- 'destinations' : destinations .split (',' )
49- })
50- return forwarders
51- return []
50+ try :
51+ response = self ._make_request (f'API_EMAIL_FORWARDERS?domain={ domain } ' )
52+ if response .status_code == 200 :
53+ forwarders = []
54+ # Parse the response
55+ lines = response .text .strip ().split ('\n ' )
56+ for line in lines :
57+ if '=' in line and line .strip ():
58+ parts = line .split ('=' , 1 )
59+ if len (parts ) == 2 :
60+ alias = parts [0 ]
61+ destinations = parts [1 ].split (',' )
62+ forwarders .append ({
63+ 'alias' : alias ,
64+ 'destinations' : [d .strip () for d in destinations if d .strip ()]
65+ })
66+ return forwarders
67+ return []
68+ except Exception as e :
69+ print (f"Error in get_forwarders: { e } " )
70+ return []
5271
5372 def create_forwarder (self , domain , alias , destination ):
54- data = {
55- 'domain' : domain ,
56- 'user' : alias ,
57- 'email' : destination ,
58- 'action' : 'create'
59- }
60- response = self ._make_request ('API_EMAIL_FORWARDERS' , method = 'POST' , data = data )
61- return response .status_code == 200
73+ try :
74+ data = {
75+ 'domain' : domain ,
76+ 'user' : alias ,
77+ 'email' : destination ,
78+ 'action' : 'create'
79+ }
80+ response = self ._make_request ('API_EMAIL_FORWARDERS' , method = 'POST' , data = data )
81+ return response .status_code == 200
82+ except Exception as e :
83+ print (f"Error in create_forwarder: { e } " )
84+ return False
6285
6386 def delete_forwarder (self , domain , alias ):
64- data = {
65- 'domain' : domain ,
66- 'select0' : alias ,
67- 'action' : 'delete'
68- }
69- response = self ._make_request ('API_EMAIL_FORWARDERS' , method = 'POST' , data = data )
70- return response .status_code == 200
87+ try :
88+ data = {
89+ 'domain' : domain ,
90+ 'select0' : alias ,
91+ 'action' : 'delete'
92+ }
93+ response = self ._make_request ('API_EMAIL_FORWARDERS' , method = 'POST' , data = data )
94+ return response .status_code == 200
95+ except Exception as e :
96+ print (f"Error in delete_forwarder: { e } " )
97+ return False
0 commit comments