1# Copyright (C) 2003-2007, 2009, 2010 Nominum, Inc.
2#
3# Permission to use, copy, modify, and distribute this software and its
4# documentation for any purpose with or without fee is hereby granted,
5# provided that the above copyright notice and this permission notice
6# appear in all copies.
7#
8# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
9# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
11# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16import dns.exception
17import dns.rdata
18import dns.name
19
20class RP(dns.rdata.Rdata):
21    """RP record
22
23    @ivar mbox: The responsible person's mailbox
24    @type mbox: dns.name.Name object
25    @ivar txt: The owner name of a node with TXT records, or the root name
26    if no TXT records are associated with this RP.
27    @type txt: dns.name.Name object
28    @see: RFC 1183"""
29
30    __slots__ = ['mbox', 'txt']
31
32    def __init__(self, rdclass, rdtype, mbox, txt):
33        super(RP, self).__init__(rdclass, rdtype)
34        self.mbox = mbox
35        self.txt = txt
36
37    def to_text(self, origin=None, relativize=True, **kw):
38        mbox = self.mbox.choose_relativity(origin, relativize)
39        txt = self.txt.choose_relativity(origin, relativize)
40        return "%s %s" % (str(mbox), str(txt))
41
42    def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
43        mbox = tok.get_name()
44        txt = tok.get_name()
45        mbox = mbox.choose_relativity(origin, relativize)
46        txt = txt.choose_relativity(origin, relativize)
47        tok.get_eol()
48        return cls(rdclass, rdtype, mbox, txt)
49
50    from_text = classmethod(from_text)
51
52    def to_wire(self, file, compress = None, origin = None):
53        self.mbox.to_wire(file, None, origin)
54        self.txt.to_wire(file, None, origin)
55
56    def to_digestable(self, origin = None):
57        return self.mbox.to_digestable(origin) + \
58            self.txt.to_digestable(origin)
59
60    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
61        (mbox, cused) = dns.name.from_wire(wire[: current + rdlen],
62                                           current)
63        current += cused
64        rdlen -= cused
65        if rdlen <= 0:
66            raise dns.exception.FormError
67        (txt, cused) = dns.name.from_wire(wire[: current + rdlen],
68                                          current)
69        if cused != rdlen:
70            raise dns.exception.FormError
71        if not origin is None:
72            mbox = mbox.relativize(origin)
73            txt = txt.relativize(origin)
74        return cls(rdclass, rdtype, mbox, txt)
75
76    from_wire = classmethod(from_wire)
77
78    def choose_relativity(self, origin = None, relativize = True):
79        self.mbox = self.mbox.choose_relativity(origin, relativize)
80        self.txt = self.txt.choose_relativity(origin, relativize)
81
82    def _cmp(self, other):
83        v = cmp(self.mbox, other.mbox)
84        if v == 0:
85            v = cmp(self.txt, other.txt)
86        return v
87