Remove arch-tags from all files, since these are no longer needed.
[gnus] / lisp / dns-mode.el
1 ;;; dns-mode.el --- a mode for viewing/editing Domain Name System master files
2
3 ;; Copyright (C) 2000, 2001, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;;   Free Software Foundation, Inc.
5
6 ;; Author: Simon Josefsson <simon@josefsson.org>
7 ;; Keywords: DNS master zone file SOA comm
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
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
51   "DNS master file mode configuration."
52   :group 'data)
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
92 (defcustom dns-mode-soa-auto-increment-serial t
93   "Whether to increment the SOA serial number automatically.
94
95 If this variable is t, the serial number is incremented upon each save of
96 the file.  If it is `ask', Emacs asks for confirmation whether it should
97 increment the serial upon saving.  If nil, serials must be incremented
98 manually 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
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.
136 This mode is inherited from text mode.  It add syntax
137 highlighting, and some commands for handling DNS master files.
138 Its keymap inherits from `text-mode' and it has the same
139 variables for customizing indentation.  It has its own abbrev
140 table and its own syntax table.
141
142 Turning 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")))))
149   (add-hook 'before-save-hook 'dns-mode-soa-maybe-increment-serial
150             nil t)
151   (easy-menu-add dns-mode-menu dns-mode-map))
152
153 ;;;###autoload (defalias 'zone-mode 'dns-mode)
154 ;;;###autoload (add-to-list 'auto-mode-alist (purecopy '("\\.zone\\'" . zone-mode)))
155
156 ;; Tools.
157
158 ;;;###autoload
159 (defun dns-mode-soa-increment-serial ()
160   "Locate SOA record and increment the serial field."
161   (interactive)
162   (save-excursion
163     (goto-char (point-min))
164     (unless (re-search-forward
165              (concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+"
166                      "[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t)
167       (error "Cannot locate SOA record"))
168     (if (re-search-forward (concat "\\<\\("
169                                    ;; year
170                                    "\\(198\\|199\\|20[0-9]\\)[0-9]"
171                                    ;; month
172                                    "\\(0[0-9]\\|10\\|11\\|12\\)"
173                                    ;; day
174                                    "\\([012][0-9]\\|30\\|31\\)"
175                                    ;; counter
176                                    "\\([0-9]\\{1,3\\}\\)"
177                                    "\\)\\>")
178                            nil t)
179         ;; YYYYMMDDIII format, one to three I's.
180         (let* ((serial (match-string 1))
181                (counterstr (match-string 5))
182                (counter (string-to-number counterstr))
183                (now (format-time-string "%Y%m%d"))
184                (nowandoldserial (concat now counterstr)))
185           (if (string< serial nowandoldserial)
186               (let ((new (format "%s00" now)))
187                 (replace-match new nil nil nil 1)
188                 (message "Replaced old serial %s with %s" serial new))
189             (if (string= serial nowandoldserial)
190                 (let ((new (format (format "%%s%%0%dd" (length counterstr))
191                                    now (1+ counter))))
192                   (replace-match new nil nil nil 1)
193                   (message "Replaced old serial %s with %s" serial new))
194               (error "Current SOA serial is in the future"))))
195       (if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t)
196           ;; Unix time
197           (let* ((serial (match-string 1))
198                  (new (format-time-string "%s")))
199             (if (not (string< serial new))
200                 (error "Current SOA serial is in the future")
201               (replace-match new nil nil nil 1)
202               (message "Replaced old serial %s with %s" serial new)))
203         (if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t)
204             ;; Just any serial number.
205             (let* ((serial (match-string 1))
206                    (new (format "%d" (1+ (string-to-number serial)))))
207               (replace-match new nil nil nil 1)
208               (message "Replaced old serial %s with %s" serial new))
209           (error "Cannot locate serial number in SOA record"))))))
210
211 (defun dns-mode-soa-maybe-increment-serial ()
212   "Increment SOA serial if needed.
213
214 This function is run from `before-save-hook'."
215   (when (and (buffer-modified-p)
216              dns-mode-soa-auto-increment-serial
217              (or (eq dns-mode-soa-auto-increment-serial t)
218                  (y-or-n-p "Increment SOA serial? ")))
219     ;; If `dns-mode-soa-increment-serial' signals an error saving will
220     ;; fail but that probably means that the serial should be fixed to
221     ;; comply with the RFC anyway! -rfr
222     (progn (dns-mode-soa-increment-serial)
223            ;; We return nil in case this is used in write-contents-functions.
224            nil)))
225
226 ;;;###autoload(add-to-list 'auto-mode-alist (purecopy '("\\.soa\\'" . dns-mode)))
227
228 (provide 'dns-mode)
229
230 ;;; dns-mode.el ends here