Merge from emacs--devo--0
[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
4 ;;   Free Software Foundation, Inc.
5
6 ;; Author: Simon Josefsson <simon@josefsson.org>
7 ;; Keywords: DNS master zone file SOA
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 'comm)
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 ;; Syntax table.
93
94 (defvar dns-mode-syntax-table
95   (let ((table (make-syntax-table)))
96     (modify-syntax-entry ?\; "<   " table)
97     (modify-syntax-entry ?\n ">   " table)
98     table)
99   "Syntax table in use in DNS master file buffers.")
100
101 ;; Keymap.
102
103 (defvar dns-mode-map
104   (let ((map (make-sparse-keymap)))
105     (define-key map "\C-c\C-s" 'dns-mode-soa-increment-serial)
106     map)
107   "Keymap for DNS master file mode.")
108
109 ;; Menu.
110
111 (defvar dns-mode-menu nil
112   "Menubar used in DNS master file mode.")
113
114 (easy-menu-define dns-mode-menu dns-mode-map
115   "DNS Menu."
116   '("DNS"
117     ["Increment SOA serial" dns-mode-soa-increment-serial t]))
118
119 ;; Mode.
120
121 ;;;###autoload
122 (define-derived-mode dns-mode text-mode "DNS"
123   "Major mode for viewing and editing DNS master files.
124 This mode is inherited from text mode.  It add syntax
125 highlighting, and some commands for handling DNS master files.
126 Its keymap inherits from `text-mode' and it has the same
127 variables for customizing indentation.  It has its own abbrev
128 table and its own syntax table.
129
130 Turning on DNS mode runs `dns-mode-hook'."
131   (set (make-local-variable 'comment-start) ";")
132   (set (make-local-variable 'comment-end) "")
133   (set (make-local-variable 'comment-start-skip) ";+ *")
134   (unless (featurep 'xemacs)
135     (set (make-local-variable 'font-lock-defaults)
136          '(dns-mode-font-lock-keywords nil nil ((?_ . "w")))))
137   (easy-menu-add dns-mode-menu dns-mode-map))
138
139 ;; Tools.
140
141 ;;;###autoload
142 (defun dns-mode-soa-increment-serial ()
143   "Locate SOA record and increment the serial field."
144   (interactive)
145   (save-excursion
146     (goto-char (point-min))
147     (unless (re-search-forward
148              (concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+"
149                      "[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t)
150       (error "Cannot locate SOA record"))
151     (if (re-search-forward (concat "\\<\\("
152                                    ;; year
153                                    "\\(198\\|199\\|20[0-9]\\)[0-9]"
154                                    ;; month
155                                    "\\(0[0-9]\\|10\\|11\\|12\\)"
156                                    ;; day
157                                    "\\([012][0-9]\\|30\\|31\\)"
158                                    ;; counter
159                                    "\\([0-9]\\{1,3\\}\\)"
160                                    "\\)\\>")
161                            nil t)
162         ;; YYYYMMDDIII format, one to three I's.
163         (let* ((serial (match-string 1))
164                (counterstr (match-string 5))
165                (counter (string-to-number counterstr))
166                (now (format-time-string "%Y%m%d"))
167                (nowandoldserial (concat now counterstr)))
168           (if (string< serial nowandoldserial)
169               (let ((new (format "%s00" now)))
170                 (replace-match new nil nil nil 1)
171                 (message "Replaced old serial %s with %s" serial new))
172             (if (string= serial nowandoldserial)
173                 (let ((new (format (format "%%s%%0%dd" (length counterstr))
174                                    now (1+ counter))))
175                   (replace-match new nil nil nil 1)
176                   (message "Replaced old serial %s with %s" serial new))
177               (error "Current SOA serial is in the future"))))
178       (if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t)
179           ;; Unix time
180           (let* ((serial (match-string 1))
181                  (new (format-time-string "%s")))
182             (if (not (string< serial new))
183                 (error "Current SOA serial is in the future")
184               (replace-match new nil nil nil 1)
185               (message "Replaced old serial %s with %s" serial new)))
186         (if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t)
187             ;; Just any serial number.
188             (let* ((serial (match-string 1))
189                    (new (format "%d" (1+ (string-to-number serial)))))
190               (replace-match new nil nil nil 1)
191               (message "Replaced old serial %s with %s" serial new))
192           (error "Cannot locate serial number in SOA record"))))))
193
194 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.soa\\'" . dns-mode))
195
196 (provide 'dns-mode)
197
198 ;; arch-tag: 6a179f0a-072f-49db-8b01-37b8f23998c0
199 ;;; dns-mode.el ends here