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