(defgroup dns-mode nil
"DNS master file mode configuration."
:group 'data)
(defconst dns-mode-classes '("IN" "CS" "CH" "HS")
"List of strings with known DNS classes.")
(defconst dns-mode-types '("A" "NS" "MD" "MF" "CNAME" "SOA" "MB" "MG" "MR"
"NULL" "WKS" "PTR" "HINFO" "MINFO" "MX" "TXT"
"RP" "AFSDB" "X25" "ISDN" "RT" "NSAP" "NSAP"
"SIG" "KEY" "PX" "GPOS" "AAAA" "LOC" "NXT"
"EID" "NIMLOC" "SRV" "ATMA" "NAPTR" "KX" "CERT"
"A6" "DNAME" "SINK" "OPT" "APL" "DS" "SSHFP"
"RRSIG" "NSEC" "DNSKEY" "UINFO" "UID" "GID"
"UNSPEC" "TKEY" "TSIG" "IXFR" "AXFR" "MAILB"
"MAILA")
"List of strings with known DNS types.")
(defvar dns-mode-control-entity-face 'font-lock-keyword-face
"Name of face used for control entities, e.g. $ORIGIN.")
(defvar dns-mode-bad-control-entity-face 'font-lock-warning-face
"Name of face used for non-standard control entities, e.g. $FOO.")
(defvar dns-mode-type-face 'font-lock-type-face
"Name of face used for DNS types, e.g., SOA.")
(defvar dns-mode-class-face 'font-lock-constant-face
"Name of face used for DNS classes, e.g., IN.")
(defcustom dns-mode-font-lock-keywords
`(("^$ORIGIN" 0 ,dns-mode-control-entity-face)
("^$INCLUDE" 0 ,dns-mode-control-entity-face)
("^$[a-z0-9A-Z]+" 0 ,dns-mode-bad-control-entity-face)
(,(regexp-opt dns-mode-classes) 0 ,dns-mode-class-face)
(,(regexp-opt dns-mode-types) 0 ,dns-mode-type-face))
"Font lock keywords used to highlight text in DNS master file mode."
:type 'sexp
:group 'dns-mode)
(defcustom dns-mode-soa-auto-increment-serial t
"Whether to increment the SOA serial number automatically.
If this variable is t, the serial number is incremented upon each save of
the file. If it is `ask', Emacs asks for confirmation whether it should
increment the serial upon saving. If nil, serials must be incremented
manually with \\[dns-mode-soa-increment-serial]."
:type '(choice (const :tag "Always" t)
(const :tag "Ask" ask)
(const :tag "Never" nil))
:group 'dns-mode)
(defvar dns-mode-syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?\ (modify-syntax-entry ?\n "> " table)
table)
"Syntax table in use in DNS master file buffers.")
(defvar dns-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-c\C-s" 'dns-mode-soa-increment-serial)
map)
"Keymap for DNS master file mode.")
(defvar dns-mode-menu nil
"Menubar used in DNS master file mode.")
(easy-menu-define dns-mode-menu dns-mode-map
"DNS Menu."
'("DNS"
["Increment SOA serial" dns-mode-soa-increment-serial t]))
(define-derived-mode dns-mode text-mode "DNS"
"Major mode for viewing and editing DNS master files.
This mode is inherited from text mode. It add syntax
highlighting, and some commands for handling DNS master files.
Its keymap inherits from `text-mode' and it has the same
variables for customizing indentation. It has its own abbrev
table and its own syntax table.
Turning on DNS mode runs `dns-mode-hook'."
(set (make-local-variable 'comment-start) ";")
(set (make-local-variable 'comment-end) "")
(set (make-local-variable 'comment-start-skip) ";+ *")
(unless (featurep 'xemacs)
(set (make-local-variable 'font-lock-defaults)
'(dns-mode-font-lock-keywords nil nil ((?_ . "w")))))
(add-hook 'before-save-hook 'dns-mode-soa-maybe-increment-serial
nil t)
(easy-menu-add dns-mode-menu dns-mode-map))
(defun dns-mode-soa-increment-serial ()
"Locate SOA record and increment the serial field."
(interactive)
(save-excursion
(goto-char (point-min))
(unless (re-search-forward
(concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+"
"[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t)
(error "Cannot locate SOA record"))
(if (re-search-forward (concat "\\<\\("
"\\(198\\|199\\|20[0-9]\\)[0-9]"
"\\(0[0-9]\\|10\\|11\\|12\\)"
"\\([012][0-9]\\|30\\|31\\)"
"\\([0-9]\\{1,3\\}\\)"
"\\)\\>")
nil t)
(let* ((serial (match-string 1))
(counterstr (match-string 5))
(counter (string-to-number counterstr))
(now (format-time-string "%Y%m%d"))
(nowandoldserial (concat now counterstr)))
(if (string< serial nowandoldserial)
(let ((new (format "%s00" now)))
(replace-match new nil nil nil 1)
(message "Replaced old serial %s with %s" serial new))
(if (string= serial nowandoldserial)
(let ((new (format (format "%%s%%0%dd" (length counterstr))
now (1+ counter))))
(replace-match new nil nil nil 1)
(message "Replaced old serial %s with %s" serial new))
(error "Current SOA serial is in the future"))))
(if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t)
(let* ((serial (match-string 1))
(new (format-time-string "%s")))
(if (not (string< serial new))
(error "Current SOA serial is in the future")
(replace-match new nil nil nil 1)
(message "Replaced old serial %s with %s" serial new)))
(if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t)
(let* ((serial (match-string 1))
(new (format "%d" (1+ (string-to-number serial)))))
(replace-match new nil nil nil 1)
(message "Replaced old serial %s with %s" serial new))
(error "Cannot locate serial number in SOA record"))))))
(defun dns-mode-soa-maybe-increment-serial ()
"Increment SOA serial if needed.
This function is run from `before-save-hook'."
(when (and (buffer-modified-p)
dns-mode-soa-auto-increment-serial
(or (eq dns-mode-soa-auto-increment-serial t)
(y-or-n-p "Increment SOA serial? ")))
(progn (dns-mode-soa-increment-serial)
nil)))
(provide 'dns-mode)