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