]> git.ipfire.org Git - oddments/ddns.git/blobdiff - src/ddns/providers.py
Add Namecheap as new provider.
[oddments/ddns.git] / src / ddns / providers.py
index f2e470091fa192876143dcc6e5389299ef6a19a1..9ffd264b916ffc7e655b3ae639b7153bd31e469a 100644 (file)
@@ -20,6 +20,7 @@
 ###############################################################################
 
 import logging
+import xml.dom.minidom
 
 from i18n import _
 
@@ -377,11 +378,14 @@ class DDNSProviderDynU(DDNSProviderDynDNS):
        url = "https://api.dynu.com/nic/update"
 
        def _prepare_request_data(self):
-               data = {
-                       "hostname" : self.hostname,
-                       "myip"     : self.get_address("ipv4"),
+               data = DDNSProviderDynDNS._prepare_request_data(self)
+
+               # This one supports IPv6
+               data.update({
                        "myipv6"   : self.get_address("ipv6"),
-               }
+               })
+
+               return data
 
 
 class DDNSProviderEasyDNS(DDNSProviderDynDNS):
@@ -501,6 +505,77 @@ class DDNSProviderLightningWireLabs(DDNSProvider):
                raise DDNSUpdateError
 
 
+class DDNSProviderNamecheap(DDNSProvider):
+       INFO = {
+               "handle"    : "namecheap.com",
+               "name"      : "Namecheap",
+               "website"   : "http://namecheap.com",
+               "protocols" : ["ipv4",]
+       }
+
+       # Information about the format of the HTTP request is to be found
+       # https://www.namecheap.com/support/knowledgebase/article.aspx/9249/0/nc-dynamic-dns-to-dyndns-adapter
+       # https://community.namecheap.com/forums/viewtopic.php?f=6&t=6772
+
+       url = "https://dynamicdns.park-your-domain.com/update"
+
+       def parse_xml(self, document, content):
+               # Send input to the parser.
+               xmldoc = xml.dom.minidom.parseString(document)
+
+               # Get XML elements by the given content.
+               element = xmldoc.getElementsByTagName(content)
+
+               # If no element has been found, we directly can return None.
+               if not element:
+                       return None
+
+               # Only get the first child from an element, even there are more than one.
+               firstchild = element[0].firstChild
+
+               # Get the value of the child.
+               value = firstchild.nodeValue
+
+               # Return the value.
+               return value
+               
+       def update(self):
+               # Namecheap requires the hostname splitted into a host and domain part.
+               host, domain = self.hostname.split(".", 1)
+
+               data = {
+                       "ip"       : self.get_address("ipv4"),
+                       "password" : self.password,
+                       "host"     : host,
+                       "domain"   : domain
+               }
+
+               # Send update to the server.
+               response = self.send_request(self.url, data=data)
+
+               # Get the full response message.
+               output = response.read()
+
+               # Handle success messages.
+               if self.parse_xml(output, "IP") == self.get_address("ipv4"):
+                       return
+
+               # Handle error codes.
+               errorcode = self.parse_xml(output, "ResponseNumber")
+
+               if errorcode == "304156":
+                       raise DDNSAuthenticationError
+               elif errorcode == "316153":
+                       raise DDNSRequestError(_("Domain not found."))
+               elif errorcode == "316154":
+                       raise DDNSRequestError(_("Domain not active."))
+               elif errorcode in ("380098", "380099"):
+                       raise DDNSInternalServerError
+
+               # If we got here, some other update error happened.
+               raise DDNSUpdateError
+
+
 class DDNSProviderNOIP(DDNSProviderDynDNS):
        INFO = {
                "handle"    : "no-ip.com",
@@ -541,11 +616,12 @@ class DDNSProviderOVH(DDNSProviderDynDNS):
        url = "https://www.ovh.com/nic/update"
 
        def _prepare_request_data(self):
-               data = {
-                       "hostname" : self.hostname,
-                       "myip"     : self.get_address("ipv4"),
-                       "system"   : "dyndns",
-               }
+               data = DDNSProviderDynDNS._prepare_request_data(self)
+               data.update({
+                       "system" : "dyndns",
+               })
+
+               return data
 
 
 class DDNSProviderRegfish(DDNSProvider):
@@ -686,3 +762,5 @@ class DDNSProviderVariomedia(DDNSProviderDynDNS):
                        "hostname" : self.hostname,
                        "myip"     : self.get_address(self.proto)
                }
+
+               return data