Skip to content

Commit 8632056

Browse files
Update directadmin_api.py
1 parent 2ba9e5e commit 8632056

File tree

1 file changed

+50
-39
lines changed

1 file changed

+50
-39
lines changed

app/directadmin_api.py

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,7 @@ def _make_request(self, endpoint, data=None, method='POST'):
6464
text = response.text.strip()
6565
print(f"Raw response: {text[:500]}...") # First 500 chars for debugging
6666

67-
# Check for error
68-
if text.startswith('error=') or 'error=' in text:
69-
error_msg = text.split('error=')[1].split('&')[0]
70-
print(f"API Error: {urllib.parse.unquote(error_msg)}")
71-
return None
72-
73-
# Parse different response formats
67+
# Parse response into dictionary first
7468
result = {}
7569

7670
# Format 1: URL encoded (key=value&key2=value2)
@@ -89,12 +83,26 @@ def _make_request(self, endpoint, data=None, method='POST'):
8983
key, value = pair.split('=', 1)
9084
result[urllib.parse.unquote(key)] = urllib.parse.unquote(value)
9185

86+
# IMPORTANT: Check if this is an error response
87+
# error=0 means SUCCESS in DirectAdmin!
88+
if 'error' in result:
89+
error_code = result.get('error', '1')
90+
if error_code != '0': # Only treat non-zero as error
91+
error_msg = result.get('text', 'Unknown error')
92+
print(f"API Error {error_code}: {error_msg}")
93+
return None
94+
else:
95+
print(f"Success (error=0): {result.get('text', 'Operation completed')}")
96+
97+
return result
98+
9299
# Format 2: Line-based (key=value\nkey2=value2)
93100
elif '\n' in text and '=' in text:
94101
for line in text.split('\n'):
95102
if '=' in line:
96103
key, value = line.split('=', 1)
97104
result[key.strip()] = value.strip()
105+
return result
98106

99107
# Format 3: Simple list (one item per line)
100108
elif '\n' in text and '@' in text:
@@ -390,15 +398,15 @@ def create_forwarder(self, address, destination):
390398
print(f"\n=== Creating Forwarder ===")
391399
print(f"Username: {username}")
392400
print(f"Domain: {self.domain}")
393-
print(f"Destination (full): {destination}") # Show the full address
401+
print(f"Destination (full): {destination}")
394402

395403
# Use the correct parameter format that DirectAdmin expects
396404
endpoint = '/CMD_API_EMAIL_FORWARDERS'
397405
data = {
398406
'domain': self.domain,
399407
'action': 'create',
400408
'user': username,
401-
'email': destination # This MUST be a full email address
409+
'email': destination
402410
}
403411

404412
print(f"Sending parameters: {data}")
@@ -409,33 +417,23 @@ def create_forwarder(self, address, destination):
409417
print(f"Got response: {response}")
410418

411419
if isinstance(response, dict):
412-
# Check for errors
413-
if 'error' in response:
414-
error_msg = response.get('error', 'Unknown error')
415-
details = response.get('details', '')
416-
text = response.get('text', '')
417-
418-
if details:
419-
# Parse URL-encoded details
420-
details = urllib.parse.unquote(details)
421-
if text:
422-
text = urllib.parse.unquote(text)
423-
424-
# Provide meaningful error message
425-
if 'invalid email' in details.lower():
426-
return False, f"Invalid email address format: {details}"
427-
elif details or text:
428-
return False, f"{text}: {details}" if text and details else (text or details)
429-
else:
430-
return False, f"Error: {error_msg}"
420+
# Check the error code properly
421+
error_code = response.get('error', '1')
431422

432-
# Check for success indicators
433-
if any(key in response for key in ['success', 'created', 'added']):
423+
# error=0 means SUCCESS!
424+
if error_code == '0' or error_code == 0:
434425
return True, f"Forwarder {username}@{self.domain}{destination} created successfully"
435426

436-
# If no error and no explicit success, might still be OK
437-
if not any(key.startswith('error') for key in response.keys()):
438-
return True, f"Forwarder {username}@{self.domain}{destination} created"
427+
# Non-zero error code means actual error
428+
details = response.get('details', '')
429+
text = response.get('text', '')
430+
431+
if details:
432+
details = urllib.parse.unquote(details)
433+
if text:
434+
text = urllib.parse.unquote(text)
435+
436+
return False, f"{text}: {details}" if text and details else "Failed to create forwarder"
439437

440438
elif isinstance(response, str):
441439
if 'error' not in response.lower():
@@ -473,15 +471,28 @@ def delete_forwarder(self, address):
473471
response = self._make_request(endpoint, data)
474472

475473
if response:
476-
# Check for success
477474
if isinstance(response, dict):
478-
if 'error' in response:
479-
return False, response.get('error', 'Unknown error')
480-
elif 'success' in response or 'deleted' in response:
475+
# Check the error code properly
476+
error_code = response.get('error', '1')
477+
478+
# error=0 means SUCCESS!
479+
if error_code == '0' or error_code == 0:
481480
return True, f"Forwarder {address} deleted successfully"
482481

483-
# If no error, assume success
484-
return True, f"Forwarder {address} deleted"
482+
# Non-zero error code means actual error
483+
text = response.get('text', 'Unknown error')
484+
details = response.get('details', '')
485+
486+
if text:
487+
text = urllib.parse.unquote(text)
488+
if details:
489+
details = urllib.parse.unquote(details)
490+
491+
return False, f"{text}: {details}" if details else text
492+
493+
elif isinstance(response, str):
494+
if 'error' not in response.lower():
495+
return True, f"Forwarder {address} deleted"
485496

486497
return False, "Failed to delete forwarder"
487498

0 commit comments

Comments
 (0)