Skip to content

Commit f2de8a2

Browse files
Update directadmin_api.py
1 parent 0c90ee0 commit f2de8a2

File tree

1 file changed

+54
-31
lines changed

1 file changed

+54
-31
lines changed

app/directadmin_api.py

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -97,39 +97,62 @@ def test_connection(self):
9797
except Exception as e:
9898
return False, str(e)
9999

100-
def get_email_accounts(self, domain):
101-
try:
102-
response = self._make_request(f'API_POP?domain={domain}')
103-
if response.status_code == 200:
104-
accounts = []
105-
# Parse the response - DirectAdmin returns URL-encoded list
106-
if response.text.strip():
107-
# Response format: account1=data&account2=data&...
108-
# or sometimes: account1&account2&account3
109-
if '=' in response.text:
110-
parsed = parse_qs(response.text.strip())
111-
for account_name in parsed.keys():
112-
if (account_name != self.username and
113-
not account_name.startswith('_') and
114-
'@' not in account_name):
115-
accounts.append(f"{account_name}@{domain}")
116-
else:
117-
# Simple list format
118-
for line in response.text.strip().split('\n'):
119-
account_name = line.split('&')[0].strip()
120-
if (account_name and
121-
account_name != self.username and
122-
not account_name.startswith('_')):
123-
accounts.append(f"{account_name}@{domain}")
124-
125-
return sorted(accounts)
100+
def get_email_accounts(self):
101+
"""Get all email accounts for the domain"""
102+
try:
103+
# DirectAdmin API endpoint for listing email accounts
104+
endpoint = '/CMD_API_POP'
105+
params = {
106+
'action': 'list',
107+
'domain': self.domain
108+
}
109+
110+
response = self._make_request(endpoint, params)
111+
112+
if response is None:
126113
return []
127-
except Exception as e:
128-
print(f"Error in get_email_accounts: {e}")
129-
return []
130-
131-
# ... rest of the methods remain the same ...
132114

115+
# Parse the response
116+
accounts = []
117+
118+
# DirectAdmin returns data in key=value format
119+
if isinstance(response, dict):
120+
# If it's already parsed as dict
121+
for key, value in response.items():
122+
if '@' in key: # It's an email address
123+
accounts.append(key)
124+
elif key.startswith('list[]'): # Alternative format
125+
accounts.append(value)
126+
else:
127+
# If it's raw text response
128+
lines = response.strip().split('\n') if isinstance(response, str) else []
129+
for line in lines:
130+
if '=' in line:
131+
key, value = line.split('=', 1)
132+
# Check various formats DA might use
133+
if '@' in value:
134+
accounts.append(value)
135+
elif '@' not in key and value and not key.startswith('error'):
136+
# It might be username only, add domain
137+
email = f"{value}@{self.domain}"
138+
accounts.append(email)
139+
140+
# Filter out the API username's email
141+
filtered_accounts = []
142+
api_email = f"{self.username}@{self.domain}"
143+
144+
for email in accounts:
145+
if email.lower() != api_email.lower():
146+
filtered_accounts.append(email)
147+
148+
print(f"Found email accounts: {filtered_accounts}")
149+
return sorted(filtered_accounts)
150+
151+
except Exception as e:
152+
print(f"Error getting email accounts: {e}")
153+
import traceback
154+
traceback.print_exc()
155+
return []
133156

134157
def get_forwarders(self, domain):
135158
try:

0 commit comments

Comments
 (0)