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