Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / textmodes / dns-mode.el
CommitLineData
0904599e 1;;; dns-mode.el --- a mode for viewing/editing Domain Name System master files
3731a850 2
73b0cd50 3;; Copyright (C) 2000-2001, 2004-2011
12dc447f 4;; Free Software Foundation, Inc.
0904599e
SJ
5
6;; Author: Simon Josefsson <simon@josefsson.org>
34607612 7;; Keywords: DNS master zone file SOA comm
0904599e
SJ
8
9;; This file is part of GNU Emacs.
10
1fecc8fe 11;; GNU Emacs is free software: you can redistribute it and/or modify
0904599e 12;; it under the terms of the GNU General Public License as published by
1fecc8fe
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
0904599e
SJ
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
1fecc8fe 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
0904599e
SJ
23
24;;; Commentary:
25
26;; Use M-x dns-mode RET to invoke in master files.
27;;
28;; C-c C-s Increment SOA serial.
29;; Understands YYYYMMDDNN, Unix time, and serial number formats,
30;; and complains if it fail to find SOA serial.
0904599e
SJ
31
32;;; References:
33
34;; RFC 1034, "DOMAIN NAMES - CONCEPTS AND FACILITIES", P. Mockapetris.
35;; RFC 1035, "DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION", P. Mockapetris.
36
37;;; Release history:
38
39;; 2004-09-11 Posted on gnu.emacs.sources.
40;; 2004-09-13 Ported to XEmacs.
41;; 2004-09-14 Installed in Emacs CVS.
42
43;;; Code:
44
45(defgroup dns-mode nil
55d56d32
LK
46 "DNS master file mode configuration."
47 :group 'data)
0904599e
SJ
48
49(defconst dns-mode-classes '("IN" "CS" "CH" "HS")
50 "List of strings with known DNS classes.")
51
52(defconst dns-mode-types '("A" "NS" "MD" "MF" "CNAME" "SOA" "MB" "MG" "MR"
53 "NULL" "WKS" "PTR" "HINFO" "MINFO" "MX" "TXT"
54 "RP" "AFSDB" "X25" "ISDN" "RT" "NSAP" "NSAP"
55 "SIG" "KEY" "PX" "GPOS" "AAAA" "LOC" "NXT"
56 "EID" "NIMLOC" "SRV" "ATMA" "NAPTR" "KX" "CERT"
57 "A6" "DNAME" "SINK" "OPT" "APL" "DS" "SSHFP"
58 "RRSIG" "NSEC" "DNSKEY" "UINFO" "UID" "GID"
59 "UNSPEC" "TKEY" "TSIG" "IXFR" "AXFR" "MAILB"
60 "MAILA")
61 "List of strings with known DNS types.")
62
63;; Font lock.
64
65(defvar dns-mode-control-entity-face 'font-lock-keyword-face
66 "Name of face used for control entities, e.g. $ORIGIN.")
67
68(defvar dns-mode-bad-control-entity-face 'font-lock-warning-face
69 "Name of face used for non-standard control entities, e.g. $FOO.")
70
71(defvar dns-mode-type-face 'font-lock-type-face
72 "Name of face used for DNS types, e.g., SOA.")
73
74(defvar dns-mode-class-face 'font-lock-constant-face
75 "Name of face used for DNS classes, e.g., IN.")
76
77(defcustom dns-mode-font-lock-keywords
78 `(("^$ORIGIN" 0 ,dns-mode-control-entity-face)
79 ("^$INCLUDE" 0 ,dns-mode-control-entity-face)
80 ("^$[a-z0-9A-Z]+" 0 ,dns-mode-bad-control-entity-face)
81 (,(regexp-opt dns-mode-classes) 0 ,dns-mode-class-face)
82 (,(regexp-opt dns-mode-types) 0 ,dns-mode-type-face))
83 "Font lock keywords used to highlight text in DNS master file mode."
84 :type 'sexp
85 :group 'dns-mode)
86
c40408fb
RF
87(defcustom dns-mode-soa-auto-increment-serial t
88 "Whether to increment the SOA serial number automatically.
89
90If this variable is t, the serial number is incremented upon each save of
91the file. If it is `ask', Emacs asks for confirmation whether it should
92increment the serial upon saving. If nil, serials must be incremented
93manually with \\[dns-mode-soa-increment-serial]."
94 :type '(choice (const :tag "Always" t)
95 (const :tag "Ask" ask)
96 (const :tag "Never" nil))
97 :group 'dns-mode)
98
0904599e
SJ
99;; Syntax table.
100
101(defvar dns-mode-syntax-table
102 (let ((table (make-syntax-table)))
103 (modify-syntax-entry ?\; "< " table)
104 (modify-syntax-entry ?\n "> " table)
105 table)
106 "Syntax table in use in DNS master file buffers.")
107
108;; Keymap.
109
110(defvar dns-mode-map
111 (let ((map (make-sparse-keymap)))
112 (define-key map "\C-c\C-s" 'dns-mode-soa-increment-serial)
113 map)
114 "Keymap for DNS master file mode.")
115
116;; Menu.
117
118(defvar dns-mode-menu nil
119 "Menubar used in DNS master file mode.")
120
121(easy-menu-define dns-mode-menu dns-mode-map
122 "DNS Menu."
123 '("DNS"
124 ["Increment SOA serial" dns-mode-soa-increment-serial t]))
125
126;; Mode.
127
128;;;###autoload
129(define-derived-mode dns-mode text-mode "DNS"
130 "Major mode for viewing and editing DNS master files.
131This mode is inherited from text mode. It add syntax
132highlighting, and some commands for handling DNS master files.
133Its keymap inherits from `text-mode' and it has the same
134variables for customizing indentation. It has its own abbrev
135table and its own syntax table.
136
137Turning on DNS mode runs `dns-mode-hook'."
138 (set (make-local-variable 'comment-start) ";")
139 (set (make-local-variable 'comment-end) "")
140 (set (make-local-variable 'comment-start-skip) ";+ *")
141 (unless (featurep 'xemacs)
142 (set (make-local-variable 'font-lock-defaults)
143 '(dns-mode-font-lock-keywords nil nil ((?_ . "w")))))
aa7adb2a 144 (add-hook 'before-save-hook 'dns-mode-soa-maybe-increment-serial
c40408fb 145 nil t)
0904599e
SJ
146 (easy-menu-add dns-mode-menu dns-mode-map))
147
c40408fb
RF
148;;;###autoload (defalias 'zone-mode 'dns-mode)
149
0904599e
SJ
150;; Tools.
151
152;;;###autoload
153(defun dns-mode-soa-increment-serial ()
154 "Locate SOA record and increment the serial field."
155 (interactive)
156 (save-excursion
157 (goto-char (point-min))
158 (unless (re-search-forward
159 (concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+"
160 "[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t)
161 (error "Cannot locate SOA record"))
162 (if (re-search-forward (concat "\\<\\("
163 ;; year
164 "\\(198\\|199\\|20[0-9]\\)[0-9]"
165 ;; month
166 "\\(0[0-9]\\|10\\|11\\|12\\)"
167 ;; day
168 "\\([012][0-9]\\|30\\|31\\)"
169 ;; counter
170 "\\([0-9]\\{1,3\\}\\)"
171 "\\)\\>")
172 nil t)
173 ;; YYYYMMDDIII format, one to three I's.
174 (let* ((serial (match-string 1))
175 (counterstr (match-string 5))
176 (counter (string-to-number counterstr))
177 (now (format-time-string "%Y%m%d"))
178 (nowandoldserial (concat now counterstr)))
179 (if (string< serial nowandoldserial)
180 (let ((new (format "%s00" now)))
181 (replace-match new nil nil nil 1)
182 (message "Replaced old serial %s with %s" serial new))
183 (if (string= serial nowandoldserial)
184 (let ((new (format (format "%%s%%0%dd" (length counterstr))
185 now (1+ counter))))
186 (replace-match new nil nil nil 1)
187 (message "Replaced old serial %s with %s" serial new))
188 (error "Current SOA serial is in the future"))))
189 (if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t)
190 ;; Unix time
191 (let* ((serial (match-string 1))
192 (new (format-time-string "%s")))
193 (if (not (string< serial new))
194 (error "Current SOA serial is in the future")
195 (replace-match new nil nil nil 1)
196 (message "Replaced old serial %s with %s" serial new)))
197 (if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t)
198 ;; Just any serial number.
199 (let* ((serial (match-string 1))
200 (new (format "%d" (1+ (string-to-number serial)))))
201 (replace-match new nil nil nil 1)
202 (message "Replaced old serial %s with %s" serial new))
203 (error "Cannot locate serial number in SOA record"))))))
204
c40408fb
RF
205(defun dns-mode-soa-maybe-increment-serial ()
206 "Increment SOA serial if needed.
207
aa7adb2a 208This function is run from `before-save-hook'."
c40408fb
RF
209 (when (and (buffer-modified-p)
210 dns-mode-soa-auto-increment-serial
211 (or (eq dns-mode-soa-auto-increment-serial t)
212 (y-or-n-p "Increment SOA serial? ")))
08eedb79
RF
213 ;; If `dns-mode-soa-increment-serial' signals an error saving will
214 ;; fail but that probably means that the serial should be fixed to
215 ;; comply with the RFC anyway! -rfr
aa7adb2a 216 (progn (dns-mode-soa-increment-serial)
08eedb79
RF
217 ;; We return nil in case this is used in write-contents-functions.
218 nil)))
c40408fb 219
0904599e
SJ
220(provide 'dns-mode)
221
0904599e 222;;; dns-mode.el ends here