*** empty log message ***
[bpt/emacs.git] / lisp / net / net-utils.el
CommitLineData
8749abea
GM
1;;; net-utils.el --- Network functions
2
3;; Author: Peter Breton <pbreton@cs.umb.edu>
4;; Created: Sun Mar 16 1997
5;; Keywords: network communications
6;; Time-stamp: <1999-11-13 10:19:01 pbreton>
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
24
25;;; Commentary:
26;;
27;; There are three main areas of functionality:
28;;
29;; * Wrap common network utility programs (ping, traceroute, netstat,
30;; nslookup, arp, route). Note that these wrappers are of the diagnostic
31;; functions of these programs only.
32;;
33;; * Implement some very basic protocols in Emacs Lisp (finger and whois)
34;;
35;; * Support connections to HOST/PORT, generally for debugging and the like.
36;; In other words, for doing much the same thing as "telnet HOST PORT", and
37;; then typing commands.
38;;
39;; PATHS
40;;
41;; On some systems, some of these programs are not in normal user path,
42;; but rather in /sbin, /usr/sbin, and so on.
43
44
45;;; Code:
46(eval-when-compile
47 (require 'comint))
48
49;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
50;; Customization Variables
51;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
52
53(defgroup net-utils nil
54 "Network utility functions."
55 :prefix "net-utils-"
56 :group 'comm
57 :version "20.3"
58 )
59
60(defcustom net-utils-remove-ctl-m
61 (member system-type (list 'windows-nt 'msdos))
62 "If non-nil, remove control-Ms from output."
63 :group 'net-utils
64 :type 'boolean
65 )
66
67(defcustom traceroute-program
68 (if (eq system-type 'windows-nt)
69 "tracert"
70 "traceroute")
71 "Program to trace network hops to a destination."
72 :group 'net-utils
73 :type 'string
74 )
75
76(defcustom traceroute-program-options nil
77 "Options for the traceroute program."
78 :group 'net-utils
79 :type '(repeat string)
80 )
81
82(defcustom ping-program "ping"
83 "Program to send network test packets to a host."
84 :group 'net-utils
85 :type 'string
86 )
87
88;; On Linux and Irix, the system's ping program seems to send packets
89;; indefinitely unless told otherwise
90(defcustom ping-program-options
91 (and (memq system-type (list 'linux 'gnu/linux 'irix))
92 (list "-c" "4"))
93 "Options for the ping program.
94These options can be used to limit how many ICMP packets are emitted."
95 :group 'net-utils
96 :type '(repeat string)
97 )
98
99(defcustom ipconfig-program
100 (if (eq system-type 'windows-nt)
101 "ipconfig"
102 "ifconfig")
103 "Program to print network configuration information."
104 :group 'net-utils
105 :type 'string
106 )
107
108(defcustom ipconfig-program-options
109 (list
110 (if (eq system-type 'windows-nt)
111 "/all" "-a"))
112 "Options for ipconfig-program."
113 :group 'net-utils
114 :type '(repeat string)
115 )
116
117(defcustom netstat-program "netstat"
118 "Program to print network statistics."
119 :group 'net-utils
120 :type 'string
121 )
122
123(defcustom netstat-program-options
124 (list "-a")
125 "Options for netstat-program."
126 :group 'net-utils
127 :type '(repeat string)
128 )
129
130(defcustom arp-program "arp"
131 "Program to print IP to address translation tables."
132 :group 'net-utils
133 :type 'string
134 )
135
136(defcustom arp-program-options
137 (list "-a")
138 "Options for arp-program."
139 :group 'net-utils
140 :type '(repeat string)
141 )
142
143(defcustom route-program
144 (if (eq system-type 'windows-nt)
145 "route"
146 "netstat")
147 "Program to print routing tables."
148 :group 'net-utils
149 :type 'string
150 )
151
152(defcustom route-program-options
153 (if (eq system-type 'windows-nt)
154 (list "print")
155 (list "-r"))
156 "Options for route-program."
157 :group 'net-utils
158 :type '(repeat string)
159 )
160
161(defcustom nslookup-program "nslookup"
162 "Program to interactively query DNS information."
163 :group 'net-utils
164 :type 'string
165 )
166
167(defcustom nslookup-program-options nil
168 "List of options to pass to the nslookup program."
169 :group 'net-utils
170 :type '(repeat string)
171 )
172
173(defcustom nslookup-prompt-regexp "^> "
174 "Regexp to match the nslookup prompt."
175 :group 'net-utils
176 :type 'regexp
177 )
178
179(defcustom dig-program "dig"
180 "Program to query DNS information."
181 :group 'net-utils
182 :type 'string
183 )
184
185(defcustom ftp-program "ftp"
186 "Progam to run to do FTP transfers."
187 :group 'net-utils
188 :type 'string
189 )
190
191(defcustom ftp-program-options nil
192 "List of options to pass to the FTP program."
193 :group 'net-utils
194 :type '(repeat string)
195 )
196
197(defcustom ftp-prompt-regexp "^ftp>"
198 "Regexp which matches the FTP program's prompt."
199 :group 'net-utils
200 :type 'regexp
201 )
202
203(defcustom smbclient-program "smbclient"
204 "Smbclient program."
205 :group 'net-utils
206 :type 'string
207 )
208
209(defcustom smbclient-program-options nil
210 "List of options to pass to the smbclient program."
211 :group 'net-utils
212 :type '(repeat string)
213 )
214
215(defcustom smbclient-prompt-regexp "^smb: \>"
216 "Regexp which matches the smbclient program's prompt."
217 :group 'net-utils
218 :type 'regexp
219 )
220
76540917
PB
221;; Internal variables
222(defvar network-connection-service nil)
223(defvar network-connection-host nil)
224
8749abea
GM
225;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
226;; Nslookup goodies
227;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
228
229(defconst nslookup-font-lock-keywords
230 (and window-system
231 (progn
232 (require 'font-lock)
233 (list
234 (list nslookup-prompt-regexp 0 font-lock-reference-face)
235 (list "^[A-Za-z0-9 _]+:" 0 font-lock-type-face)
236 (list "\\<\\(SOA\\|NS\\|MX\\|A\\|CNAME\\)\\>"
237 1 font-lock-keyword-face)
238 ;; Dotted quads
239 (list
240 (mapconcat 'identity
241 (make-list 4 "[0-9]+")
242 "\\.")
243 0 font-lock-variable-name-face)
244 ;; Host names
245 (list
246 (let ((host-expression "[-A-Za-z0-9]+"))
247 (concat
248 (mapconcat 'identity
249 (make-list 2 host-expression)
250 "\\.")
251 "\\(\\." host-expression "\\)*")
252 )
253 0 font-lock-variable-name-face)
254 )))
255 "Expressions to font-lock for nslookup.")
256
257;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
258;; FTP goodies
259;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
260
261(defconst ftp-font-lock-keywords
262 (and window-system
263 (progn
264 (require 'font-lock)
265 (list
266 (list ftp-prompt-regexp 0 font-lock-reference-face)))))
267
268;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
269;; smbclient goodies
270;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
271
272(defconst smbclient-font-lock-keywords
273 (and window-system
274 (progn
275 (require 'font-lock)
276 (list
277 (list smbclient-prompt-regexp 0 font-lock-reference-face)))))
278
279;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
280;; Utility functions
281;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
282
283;; Simplified versions of some at-point functions from ffap.el.
284;; It's not worth loading all of ffap just for these.
285(defun net-utils-machine-at-point ()
286 (let ((pt (point)))
287 (buffer-substring-no-properties
288 (save-excursion
289 (skip-chars-backward "-a-zA-Z0-9.")
290 (point))
291 (save-excursion
292 (skip-chars-forward "-a-zA-Z0-9.")
293 (skip-chars-backward "." pt)
294 (point)))))
295
296(defun net-utils-url-at-point ()
297 (let ((pt (point)))
298 (buffer-substring-no-properties
299 (save-excursion
300 (skip-chars-backward "--:=&?$+@-Z_a-z~#,%")
301 (skip-chars-forward "^A-Za-z0-9" pt)
302 (point))
303 (save-excursion
304 (skip-chars-forward "--:=&?$+@-Z_a-z~#,%")
305 (skip-chars-backward ":;.,!?" pt)
306 (point)))))
307
308
309(defun net-utils-remove-ctrl-m-filter (process output-string)
310 "Remove trailing control Ms."
311 (let ((old-buffer (current-buffer))
312 (filtered-string output-string))
313 (unwind-protect
314 (let ((moving))
315 (set-buffer (process-buffer process))
316 (setq moving (= (point) (process-mark process)))
317
318 (while (string-match "\r" filtered-string)
319 (setq filtered-string
320 (replace-match "" nil nil filtered-string)))
321
322 (save-excursion
323 ;; Insert the text, moving the process-marker.
324 (goto-char (process-mark process))
325 (insert filtered-string)
326 (set-marker (process-mark process) (point)))
327 (if moving (goto-char (process-mark process))))
328 (set-buffer old-buffer))))
329
330(defmacro net-utils-run-program (name header program &rest args)
331 "Run a network information program."
332 ` (let ((buf (get-buffer-create (concat "*" ,name "*"))))
333 (set-buffer buf)
334 (erase-buffer)
335 (insert ,header "\n")
336 (set-process-filter
337 (apply 'start-process ,name buf ,program ,@args)
338 'net-utils-remove-ctrl-m-filter)
339 (display-buffer buf)))
340
341;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
342;; Wrappers for external network programs
343;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
344
345;;;###autoload
346(defun traceroute (target)
347 "Run traceroute program for TARGET."
348 (interactive "sTarget: ")
349 (let ((options
350 (if traceroute-program-options
351 (append traceroute-program-options (list target))
352 (list target))))
353 (net-utils-run-program
354 (concat "Traceroute" " " target)
355 (concat "** Traceroute ** " traceroute-program " ** " target)
356 traceroute-program
357 options
358 )))
359
360;;;###autoload
361(defun ping (host)
362 "Ping HOST.
363If your system's ping continues until interrupted, you can try setting
364`ping-program-options'."
365 (interactive
366 (list (read-from-minibuffer "Ping host: " (net-utils-machine-at-point))))
367 (let ((options
368 (if ping-program-options
369 (append ping-program-options (list host))
370 (list host))))
371 (net-utils-run-program
372 (concat "Ping" " " host)
373 (concat "** Ping ** " ping-program " ** " host)
374 ping-program
375 options
376 )))
377
378;;;###autoload
379(defun ipconfig ()
380 "Run ipconfig program."
381 (interactive)
382 (net-utils-run-program
383 "Ipconfig"
384 (concat "** Ipconfig ** " ipconfig-program " ** ")
385 ipconfig-program
386 ipconfig-program-options
387 ))
388
389;; This is the normal name on most Unixes.
390;;;###autoload
391(defalias 'ifconfig 'ipconfig)
392
393;;;###autoload
394(defun netstat ()
395 "Run netstat program."
396 (interactive)
397 (net-utils-run-program
398 "Netstat"
399 (concat "** Netstat ** " netstat-program " ** ")
400 netstat-program
401 netstat-program-options
402 ))
403
404;;;###autoload
405(defun arp ()
406 "Run the arp program."
407 (interactive)
408 (net-utils-run-program
409 "Arp"
410 (concat "** Arp ** " arp-program " ** ")
411 arp-program
412 arp-program-options
413 ))
414
415;;;###autoload
416(defun route ()
417 "Run the route program."
418 (interactive)
419 (net-utils-run-program
420 "Route"
421 (concat "** Route ** " route-program " ** ")
422 route-program
423 route-program-options
424 ))
425
426;; FIXME -- Needs to be a process filter
427;; (defun netstat-with-filter (filter)
428;; "Run netstat program."
429;; (interactive "sFilter: ")
430;; (netstat)
431;; (set-buffer (get-buffer "*Netstat*"))
432;; (goto-char (point-min))
433;; (delete-matching-lines filter)
434;; )
435
436;;;###autoload
437(defun nslookup-host (host)
438 "Lookup the DNS information for HOST."
439 (interactive
440 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
441 (let ((options
442 (if nslookup-program-options
443 (append nslookup-program-options (list host))
444 (list host))))
445 (net-utils-run-program
446 "Nslookup"
447 (concat "** "
448 (mapconcat 'identity
449 (list "Nslookup" host nslookup-program)
450 " ** "))
451 nslookup-program
452 options
453 )))
454
455
456;;;###autoload
457(defun nslookup ()
458 "Run nslookup program."
459 (interactive)
460 (require 'comint)
461 (comint-run nslookup-program)
462 (set-process-filter (get-buffer-process "*nslookup*")
463 'net-utils-remove-ctrl-m-filter)
464 (nslookup-mode)
465 )
466
467;; Using a derived mode gives us keymaps, hooks, etc.
468(define-derived-mode
469 nslookup-mode comint-mode "Nslookup"
470 "Major mode for interacting with the nslookup program."
471 (set
472 (make-local-variable 'font-lock-defaults)
473 '((nslookup-font-lock-keywords)))
474 (setq local-abbrev-table nslookup-mode-abbrev-table)
475 (abbrev-mode t)
476 (make-local-variable 'comint-prompt-regexp)
477 (setq comint-prompt-regexp nslookup-prompt-regexp)
478 (make-local-variable 'comint-input-autoexpand)
479 (setq comint-input-autoexpand t)
480 )
481
482(define-key nslookup-mode-map "\t" 'comint-dynamic-complete)
483
484(define-abbrev nslookup-mode-abbrev-table "e" "exit")
485(define-abbrev nslookup-mode-abbrev-table "f" "finger")
486(define-abbrev nslookup-mode-abbrev-table "h" "help")
487(define-abbrev nslookup-mode-abbrev-table "lse" "lserver")
488(define-abbrev nslookup-mode-abbrev-table "q" "exit")
489(define-abbrev nslookup-mode-abbrev-table "r" "root")
490(define-abbrev nslookup-mode-abbrev-table "s" "set")
491(define-abbrev nslookup-mode-abbrev-table "se" "server")
492(define-abbrev nslookup-mode-abbrev-table "v" "viewer")
493
494;;;###autoload
495(defun dig (host)
496 "Run dig program."
497 (interactive
498 (list
499 (progn
500 (require 'ffap)
501 (read-from-minibuffer
502 "Lookup host: "
503 (or (ffap-string-at-point 'machine) "")))))
504 (net-utils-run-program
505 "Dig"
506 (concat "** "
507 (mapconcat 'identity
508 (list "Dig" host dig-program)
509 " ** "))
510 dig-program
511 (list host)
512 ))
513
514;; This is a lot less than ange-ftp, but much simpler.
515;;;###autoload
516(defun ftp (host)
517 "Run ftp program."
518 (interactive
519 (list
520 (read-from-minibuffer
521 "Ftp to Host: " (net-utils-machine-at-point))))
522 (require 'comint)
523 (let ((buf (get-buffer-create (concat "*ftp [" host "]*"))))
524 (set-buffer buf)
525 (comint-mode)
526 (comint-exec buf (concat "ftp-" host) ftp-program nil
527 (if ftp-program-options
528 (append (list host) ftp-program-options)
529 (list host)))
530 (ftp-mode)
531 (switch-to-buffer-other-window buf)
532 ))
533
534(define-derived-mode
535 ftp-mode comint-mode "FTP"
536 "Major mode for interacting with the ftp program."
537
538 (set
539 (make-local-variable 'font-lock-defaults)
540 '((ftp-font-lock-keywords)))
541
542 (make-local-variable 'comint-prompt-regexp)
543 (setq comint-prompt-regexp ftp-prompt-regexp)
544
545 (make-local-variable 'comint-input-autoexpand)
546 (setq comint-input-autoexpand t)
547
548 ;; Already buffer local!
549 (setq comint-output-filter-functions
550 (list 'comint-watch-for-password-prompt))
551
552 (setq local-abbrev-table ftp-mode-abbrev-table)
553 (abbrev-mode t)
554 )
555
556(define-abbrev ftp-mode-abbrev-table "q" "quit")
557(define-abbrev ftp-mode-abbrev-table "g" "get")
558(define-abbrev ftp-mode-abbrev-table "p" "prompt")
559(define-abbrev ftp-mode-abbrev-table "anon" "anonymous")
560
561;; Occasionally useful
562(define-key ftp-mode-map "\t" 'comint-dynamic-complete)
563
564(defun smbclient (host service)
565 "Connect to SERVICE on HOST via SMB."
566 (interactive
567 (list
568 (read-from-minibuffer
569 "Connect to Host: " (net-utils-machine-at-point))
570 (read-from-minibuffer "SMB Service: ")))
571 (require 'comint)
572 (let* ((name (format "smbclient [%s\\%s]" host service))
573 (buf (get-buffer-create (concat "*" name "*")))
574 (service-name (concat "\\\\" host "\\" service)))
575 (set-buffer buf)
576 (comint-mode)
577 (comint-exec buf name smbclient-program nil
578 (if smbclient-program-options
579 (append (list service-name) smbclient-program-options)
580 (list service-name)))
581 (smbclient-mode)
582 (switch-to-buffer-other-window buf)
583 ))
584
585(defun smbclient-list-shares (host)
586 "List services on HOST."
587 (interactive
588 (list
589 (read-from-minibuffer
590 "Connect to Host: " (net-utils-machine-at-point))
591 ))
592 (let ((buf (get-buffer-create (format "*SMB Shares on %s*" host))))
593 (set-buffer buf)
594 (comint-mode)
595 (comint-exec
596 buf
597 "smbclient-list-shares"
598 smbclient-program
599 nil
600 (list "-L" host)
601 )
602 (smbclient-mode)
603 (switch-to-buffer-other-window buf)))
604
605(define-derived-mode
606 smbclient-mode comint-mode "smbclient"
607 "Major mode for interacting with the smbclient program."
608
609 (set
610 (make-local-variable 'font-lock-defaults)
611 '((smbclient-font-lock-keywords)))
612
613 (make-local-variable 'comint-prompt-regexp)
614 (setq comint-prompt-regexp smbclient-prompt-regexp)
615
616 (make-local-variable 'comint-input-autoexpand)
617 (setq comint-input-autoexpand t)
618
619 ;; Already buffer local!
620 (setq comint-output-filter-functions
621 (list 'comint-watch-for-password-prompt))
622
623 (setq local-abbrev-table smbclient-mode-abbrev-table)
624 (abbrev-mode t)
625 )
626
627(define-abbrev smbclient-mode-abbrev-table "q" "quit")
628
629
630;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
631;; Network Connections
632;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
633
634;; Full list is available at:
635;; ftp://ftp.isi.edu/in-notes/iana/assignments/port-numbers
636(defvar network-connection-service-alist
637 (list
638 (cons 'echo 7)
639 (cons 'active-users 11)
640 (cons 'daytime 13)
641 (cons 'chargen 19)
642 (cons 'ftp 21)
643 (cons 'telnet 23)
644 (cons 'smtp 25)
645 (cons 'time 37)
646 (cons 'whois 43)
647 (cons 'gopher 70)
648 (cons 'finger 79)
649 (cons 'www 80)
650 (cons 'pop2 109)
651 (cons 'pop3 110)
652 (cons 'sun-rpc 111)
653 (cons 'nntp 119)
654 (cons 'ntp 123)
655 (cons 'netbios-name 137)
656 (cons 'netbios-data 139)
657 (cons 'irc 194)
658 (cons 'https 443)
659 (cons 'rlogin 513)
660 )
661 "Alist of services and associated TCP port numbers.
662This list in not complete.")
663
664;; Workhorse macro
665(defmacro run-network-program (process-name host port
666 &optional initial-string)
667 `
668 (let ((tcp-connection)
669 (buf)
670 )
671 (setq buf (get-buffer-create (concat "*" ,process-name "*")))
672 (set-buffer buf)
673 (or
674 (setq tcp-connection
675 (open-network-stream
676 ,process-name
677 buf
678 ,host
679 ,port
680 ))
681 (error "Could not open connection to %s" ,host))
682 (erase-buffer)
683 (set-marker (process-mark tcp-connection) (point-min))
684 (set-process-filter tcp-connection 'net-utils-remove-ctrl-m-filter)
685 (and ,initial-string
686 (process-send-string tcp-connection
687 (concat ,initial-string "\r\n")))
688 (display-buffer buf)))
689
690;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
691;; Simple protocols
692;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
693
694;; Finger protocol
695;;;###autoload
696(defun finger (user host)
697 "Finger USER on HOST."
698 ;; One of those great interactive statements that's actually
699 ;; longer than the function call! The idea is that if the user
700 ;; uses a string like "pbreton@cs.umb.edu", we won't ask for the
701 ;; host name. If we don't see an "@", we'll prompt for the host.
702 (interactive
703 (let* ((answer (read-from-minibuffer "Finger User: "
704 (net-utils-url-at-point)))
705 (index (string-match (regexp-quote "@") answer)))
706 (if index
707 (list
708 (substring answer 0 index)
709 (substring answer (1+ index)))
710 (list
711 answer
712 (read-from-minibuffer "At Host: " (net-utils-machine-at-point))))))
713 (let* (
714 (user-and-host (concat user "@" host))
715 (process-name
716 (concat "Finger [" user-and-host "]"))
717 )
718 (run-network-program
719 process-name
720 host
721 (cdr (assoc 'finger network-connection-service-alist))
722 user-and-host
723 )))
724
725(defcustom whois-server-name "rs.internic.net"
726 "Default host name for the whois service."
727 :group 'net-utils
728 :type 'string
729 )
730
731(defcustom whois-server-list
732 '(("whois.arin.net") ; Networks, ASN's, and related POC's (numbers)
733 ("rs.internic.net") ; domain related info
734 ("whois.abuse.net")
735 ("whois.apnic.net")
736 ("nic.ddn.mil")
737 ("whois.nic.mil")
738 ("whois.nic.gov")
739 ("whois.ripe.net"))
740 "A list of whois servers that can be queried."
741 :group 'net-utils
742 :type '(repeat (list string)))
743
744(defcustom whois-server-tld
745 '(("rs.internic.net" . "com")
746 ("rs.internic.net" . "org")
747 ("whois.ripe.net" . "be")
748 ("whois.ripe.net" . "de")
749 ("whois.ripe.net" . "dk")
750 ("whois.ripe.net" . "it")
751 ("whois.ripe.net" . "fi")
752 ("whois.ripe.net" . "fr")
753 ("whois.ripe.net" . "uk")
754 ("whois.apnic.net" . "au")
755 ("whois.apnic.net" . "ch")
756 ("whois.apnic.net" . "hk")
757 ("whois.apnic.net" . "jp")
758 ("whois.nic.gov" . "gov")
759 ("whois.nic.mil" . "mil"))
760 "Alist to map top level domains to whois servers."
761 :group 'net-utils
762 :type '(repeat (cons string string)))
763
764(defcustom whois-guess-server t
765 "If non-nil then whois will try to deduce the appropriate whois
766server from the query. If the query doesn't look like a domain or hostname
767then the server named by whois-server-name is used."
768 :group 'net-utils
769 :type 'boolean)
770
771(defun whois-get-tld (host)
772 "Return the top level domain of `host', or nil if it isn't a domain name."
773 (let ((i (1- (length host)))
774 (max-len (- (length host) 5)))
775 (while (not (or (= i max-len) (char-equal (aref host i) ?.)))
776 (setq i (1- i)))
777 (if (= i max-len)
778 nil
779 (substring host (1+ i)))))
780
781;; Whois protocol
782;;;###autoload
783(defun whois (arg search-string)
784 "Send SEARCH-STRING to server defined by the `whois-server-name' variable.
785If `whois-guess-server' is non-nil, then try to deduce the correct server
786from SEARCH-STRING. With argument, prompt for whois server."
787 (interactive "P\nsWhois: ")
788 (let* ((whois-apropos-host (if whois-guess-server
789 (rassoc (whois-get-tld search-string)
790 whois-server-tld)
791 nil))
792 (server-name (if whois-apropos-host
793 (car whois-apropos-host)
794 whois-server-name))
795 (host
796 (if arg
797 (completing-read "Whois server name: "
798 whois-server-list nil nil "whois.")
799 server-name)))
800 (run-network-program
801 "Whois"
802 host
803 (cdr (assoc 'whois network-connection-service-alist))
804 search-string
805 )))
806
807(defcustom whois-reverse-lookup-server "whois.arin.net"
808 "Server which provides inverse DNS mapping."
809 :group 'net-utils
810 :type 'string
811 )
812
813;;;###autoload
814(defun whois-reverse-lookup ()
815 (interactive)
816 (let ((whois-server-name whois-reverse-lookup-server))
817 (call-interactively 'whois)))
818
819;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
820;;; General Network connection
821;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
822
76540917
PB
823;; Using a derived mode gives us keymaps, hooks, etc.
824(define-derived-mode
825 network-connection-mode comint-mode "Network-Connection"
826 "Major mode for interacting with the network-connection program."
827 )
828
829(defun network-connection-mode-setup (host service)
830 (let ((network-abbrev-table
831 (or
832 (assoc service network-connection-service-abbrev-alist)
833 (and (rassoc service network-connection-service-alist)
834 (assoc
835 (elt (rassoc service network-connection-service-alist) 0)
836 network-connection-service-abbrev-alist)))))
837 (make-local-variable 'network-connection-host)
838 (setq network-connection-host host)
839 (make-local-variable 'network-connection-service)
840 (setq network-connection-service service)
841 (and network-abbrev-table
842 (setq local-abbrev-table (cdr network-abbrev-table))
843 (abbrev-mode t)
844 )))
845
8749abea
GM
846;;;###autoload
847(defun network-connection-to-service (host service)
848 "Open a network connection to SERVICE on HOST."
849 (interactive
850 (list
851 (read-from-minibuffer "Host: " (net-utils-machine-at-point))
852 (completing-read "Service: "
853 (mapcar
854 (function
855 (lambda (elt)
856 (list (symbol-name (car elt)))))
857 network-connection-service-alist))))
858 (network-connection
859 host
860 (cdr (assoc (intern service) network-connection-service-alist)))
861 )
862
863;;;###autoload
864(defun network-connection (host port)
865 "Open a network connection to HOST on PORT."
866 (interactive "sHost: \nnPort: ")
867 (network-service-connection host (number-to-string port)))
868
869(defun network-service-connection (host service)
870 "Open a network connection to SERVICE on HOST."
871 (require 'comint)
76540917 872 (let* (
8749abea
GM
873 (process-name (concat "Network Connection [" host " " service "]"))
874 (portnum (string-to-number service))
76540917 875 (buf (get-buffer-create (concat "*" process-name "*")))
8749abea
GM
876 )
877 (or (zerop portnum) (setq service portnum))
878 (make-comint
879 process-name
880 (cons host service))
76540917
PB
881 (set-buffer buf)
882 (network-connection-mode)
883 (network-connection-mode-setup host service)
884 (pop-to-buffer buf)
8749abea
GM
885 ))
886
887(provide 'net-utils)
888
889;;; net-utils.el ends here