fix typo
[zonemaker.git] / zone.py
diff --git a/zone.py b/zone.py
index b543d31c3c6d0456491519e2c21296f88b953271..68a82b32295de60ef4b68fe2f9220d3f81f5871d 100644 (file)
--- a/zone.py
+++ b/zone.py
 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-# 
-# The views and conclusions contained in the software and documentation are those
-# of the authors and should not be interpreted as representing official policies, 
-# either expressed or implied, of the FreeBSD Project.
 
 import re, datetime
 #from typing import *
@@ -138,6 +134,21 @@ class MX:
         return zone.RR(owner, 'MX', '{0} {1}'.format(self._priority, zone.abs_hostname(self._name)))
 
 
+class TXT:
+    def __init__(self, name: str, text: str) -> None:
+        # test for bad characters
+        for c in ('\n', '\r', '\t'):
+            if c in text:
+                raise Exception("TXT record {0} contains invalid character")
+        # escape text
+        for c in ('\\', '\"'):
+            text = text.replace(c, '\\'+c)
+        self._text = text
+    
+    def generate_rr(self, owner:str, zone: 'Zone') -> 'Any':
+        return zone.RR(owner, 'TXT', '"{0}"'.format(self._text))
+
+
 class SRV:
     def __init__(self, protocol: str, service: str, name: str, port: int, prio: int, weight: int) -> None:
         self._service = check_label(service)
@@ -264,6 +275,8 @@ class Zone:
         return column_widths((self.abs_hostname(owner), time(self.getTTL(recordType)), recordType, data), (32, 8, 8))
     
     def abs_hostname(self, name):
+        if name == '':
+            raise Exception("Empty domain name is not valid")
         if name == '.' or name == '@':
             return self._name
         if name.endswith('.'):