Add XEmacs auto-mode-alist autoload cookie.
[gnus] / lisp / dns-mode.el
1 ;;; dns-mode.el --- a mode for viewing/editing Domain Name System master files
2 ;; Copyright (c) 2000, 2001, 2003, 2004 Free Software Foundation, Inc.
3
4 ;; Author: Simon Josefsson <simon@josefsson.org>
5 ;; Keywords: DNS master zone file SOA
6
7 ;; This file is not part of GNU Emacs.
8
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
13
14 ;; This file is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with This file; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
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
47 ;;; Code:
48
49 (defgroup dns-mode nil
50   "DNS master file mode configuration.")
51
52 (defconst dns-mode-classes '("IN" "CS" "CH" "HS")
53   "List of strings with known DNS classes.")
54
55 (defconst dns-mode-types '("A" "NS" "MD" "MF" "CNAME" "SOA" "MB" "MG" "MR"
56                            "NULL" "WKS" "PTR" "HINFO" "MINFO" "MX" "TXT"
57                            "RP" "AFSDB" "X25" "ISDN" "RT" "NSAP" "NSAP"
58                            "SIG" "KEY" "PX" "GPOS" "AAAA" "LOC" "NXT"
59                            "EID" "NIMLOC" "SRV" "ATMA" "NAPTR" "KX" "CERT"
60                            "A6" "DNAME" "SINK" "OPT" "APL" "DS" "SSHFP"
61                            "RRSIG" "NSEC" "DNSKEY" "UINFO" "UID" "GID"
62                            "UNSPEC" "TKEY" "TSIG" "IXFR" "AXFR" "MAILB"
63                            "MAILA")
64   "List of strings with known DNS types.")
65
66 ;; Font lock.
67
68 (defvar dns-mode-control-entity-face 'font-lock-keyword-face
69   "Name of face used for control entities, e.g. $ORIGIN.")
70
71 (defvar dns-mode-bad-control-entity-face 'font-lock-warning-face
72   "Name of face used for non-standard control entities, e.g. $FOO.")
73
74 (defvar dns-mode-type-face 'font-lock-type-face
75   "Name of face used for DNS types, e.g., SOA.")
76
77 (defvar dns-mode-class-face 'font-lock-constant-face
78   "Name of face used for DNS classes, e.g., IN.")
79
80 (defcustom dns-mode-font-lock-keywords
81   `(("^$ORIGIN" 0 ,dns-mode-control-entity-face)
82     ("^$INCLUDE" 0 ,dns-mode-control-entity-face)
83     ("^$[a-z0-9A-Z]+" 0 ,dns-mode-bad-control-entity-face)
84     (,(regexp-opt dns-mode-classes) 0 ,dns-mode-class-face)
85     (,(regexp-opt dns-mode-types) 0 ,dns-mode-type-face))
86   "Font lock keywords used to highlight text in DNS master file mode."
87   :type 'sexp
88   :group 'dns-mode)
89
90 ;; Syntax table.
91
92 (defvar dns-mode-syntax-table
93   (let ((table (make-syntax-table)))
94     (modify-syntax-entry ?\; "<   " table)
95     (modify-syntax-entry ?\n ">   " table)
96     table)
97   "Syntax table in use in DNS master file buffers.")
98
99 ;; Keymap.
100
101 (defvar dns-mode-map
102   (let ((map (make-sparse-keymap)))
103     (define-key map "\C-c\C-s" 'dns-mode-soa-increment-serial)
104     map)
105   "Keymap for DNS master file mode.")
106
107 ;; Menu.
108
109 (defvar dns-mode-menu nil
110   "Menubar used in DNS master file mode.")
111
112 (easy-menu-define dns-mode-menu dns-mode-map
113   "DNS Menu."
114   '("DNS"
115     ["Increment SOA serial" dns-mode-soa-increment-serial t]))
116
117 ;; Mode.
118
119 ;;;###autoload
120 (define-derived-mode dns-mode text-mode "DNS"
121   "Major mode for viewing and editing DNS master files.
122 This mode is inherited from text mode.  It add syntax
123 highlighting, and some commands for handling DNS master files.
124 Its keymap inherits from `text-mode' and it has the same
125 variables for customizing indentation.  It has its own abbrev
126 table and its own syntax table.
127
128 Turning on DNS mode runs `dns-mode-hook'."
129   (set (make-local-variable 'comment-start) ";")
130   (set (make-local-variable 'comment-end) "")
131   (set (make-local-variable 'comment-start-skip) ";+ *")
132   (unless (featurep 'xemacs)
133     (set (make-local-variable 'font-lock-defaults)
134          '(dns-mode-font-lock-keywords nil nil ((?_ . "w")))))
135   (easy-menu-add dns-mode-menu dns-mode-map))
136
137 ;; Tools.
138
139 ;;;###autoload
140 (defun dns-mode-soa-increment-serial ()
141   "Locate SOA record and increment the serial field."
142   (interactive)
143   (save-excursion
144     (goto-char (point-min))
145     (unless (re-search-forward
146              (concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+"
147                      "[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t)
148       (error "Cannot locate SOA record"))
149     (if (re-search-forward (concat "\\<\\("
150                                    ;; year
151                                    "\\(198\\|199\\|20[0-9]\\)[0-9]"
152                                    ;; month
153                                    "\\(0[0-9]\\|10\\|11\\|12\\)"
154                                    ;; day
155                                    "\\([012][0-9]\\|30\\|31\\)"
156                                    ;; counter
157                                    "\\([0-9]\\{1,3\\}\\)"
158                                    "\\)\\>")
159                            nil t)
160         ;; YYYYMMDDIII format, one to three I's.
161         (let* ((serial (match-string 1))
162                (counterstr (match-string 5))
163                (counter (string-to-number counterstr))
164                (now (format-time-string "%Y%m%d"))
165                (nowandoldserial (concat now counterstr)))
166           (if (string< serial nowandoldserial)
167               (let ((new (format "%s00" now)))
168                 (replace-match new nil nil nil 1)
169                 (message "Replaced old serial %s with %s" serial new))
170             (if (string= serial nowandoldserial)
171                 (let ((new (format (format "%%s%%0%dd" (length counterstr))
172                                    now (1+ counter))))
173                   (replace-match new nil nil nil 1)
174                   (message "Replaced old serial %s with %s" serial new))
175               (error "Current SOA serial is in the future"))))
176       (if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t)
177           ;; Unix time
178           (let* ((serial (match-string 1))
179                  (new (format-time-string "%s")))
180             (if (not (string< serial new))
181                 (error "Current SOA serial is in the future")
182               (replace-match new nil nil nil 1)
183               (message "Replaced old serial %s with %s" serial new)))
184         (if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t)
185             ;; Just any serial number.
186             (let* ((serial (match-string 1))
187                    (new (format "%d" (1+ (string-to-number serial)))))
188               (replace-match new nil nil nil 1)
189               (message "Replaced old serial %s with %s" serial new))
190           (error "Cannot locate serial number in SOA record"))))))
191
192 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.soa\\'" . dns-mode))
193
194 (provide 'dns-mode)
195
196 ;; arch-tag: 6a179f0a-072f-49db-8b01-37b8f23998c0
197 ;;; dns-mode.el ends here