1 ;;; net-utils.el --- network functions
3 ;; Copyright (C) 1998-2015 Free Software Foundation, Inc.
5 ;; Author: Peter Breton <pbreton@cs.umb.edu>
6 ;; Created: Sun Mar 16 1997
7 ;; Keywords: network comm
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
27 ;; There are three main areas of functionality:
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.
33 ;; * Implement some very basic protocols in Emacs Lisp (finger and whois)
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.
41 ;; On some systems, some of these programs are not in normal user path,
42 ;; but rather in /sbin, /usr/sbin, and so on.
47 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48 ;; Customization Variables
49 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
51 (defgroup net-utils nil
52 "Network utility functions."
57 (defcustom traceroute-program
58 (if (eq system-type
'windows-nt
)
61 "Program to trace network hops to a destination."
65 (defcustom traceroute-program-options nil
66 "Options for the traceroute program."
68 :type
'(repeat string
))
70 (defcustom ping-program
"ping"
71 "Program to send network test packets to a host."
75 ;; On GNU/Linux and Irix, the system's ping program seems to send packets
76 ;; indefinitely unless told otherwise
77 (defcustom ping-program-options
78 (and (memq system-type
'(gnu/linux irix
))
80 "Options for the ping program.
81 These options can be used to limit how many ICMP packets are emitted."
83 :type
'(repeat string
))
85 (define-obsolete-variable-alias 'ipconfig-program
'ifconfig-program
"22.2")
87 (defcustom ifconfig-program
88 (if (eq system-type
'windows-nt
)
91 "Program to print network configuration information."
95 (define-obsolete-variable-alias 'ipconfig-program-options
96 'ifconfig-program-options
"22.2")
98 (defcustom ifconfig-program-options
100 (if (eq system-type
'windows-nt
)
102 "Options for the ifconfig program."
104 :type
'(repeat string
))
106 (defcustom iwconfig-program
"iwconfig"
107 "Program to print wireless network configuration information."
112 (defcustom iwconfig-program-options nil
113 "Options for the iwconfig program."
115 :type
'(repeat string
)
118 (defcustom netstat-program
"netstat"
119 "Program to print network statistics."
123 (defcustom netstat-program-options
125 "Options for the netstat program."
127 :type
'(repeat string
))
129 (defcustom arp-program
"arp"
130 "Program to print IP to address translation tables."
134 (defcustom arp-program-options
136 "Options for the arp program."
138 :type
'(repeat string
))
140 (defcustom route-program
141 (if (eq system-type
'windows-nt
)
144 "Program to print routing tables."
148 (defcustom route-program-options
149 (if (eq system-type
'windows-nt
)
152 "Options for the route program."
154 :type
'(repeat string
))
156 (defcustom nslookup-program
"nslookup"
157 "Program to interactively query DNS information."
161 (defcustom nslookup-program-options nil
162 "Options for the nslookup program."
164 :type
'(repeat string
))
166 (defcustom nslookup-prompt-regexp
"^> "
167 "Regexp to match the nslookup prompt.
169 This variable is only used if the variable
170 `comint-use-prompt-regexp' is non-nil."
174 (defcustom dig-program
"dig"
175 "Program to query DNS information."
179 (defcustom ftp-program
"ftp"
180 "Program to run to do FTP transfers."
184 (defcustom ftp-program-options nil
185 "Options for the ftp program."
187 :type
'(repeat string
))
189 (defcustom ftp-prompt-regexp
"^ftp>"
190 "Regexp which matches the FTP program's prompt.
192 This variable is only used if the variable
193 `comint-use-prompt-regexp' is non-nil."
197 (defcustom smbclient-program
"smbclient"
202 (defcustom smbclient-program-options nil
203 "Options for the smbclient program."
205 :type
'(repeat string
))
207 (defcustom smbclient-prompt-regexp
"^smb: \>"
208 "Regexp which matches the smbclient program's prompt.
210 This variable is only used if the variable
211 `comint-use-prompt-regexp' is non-nil."
215 (defcustom dns-lookup-program
"host"
216 "Program to interactively query DNS information."
220 (defcustom dns-lookup-program-options nil
221 "Options for the dns-lookup program."
223 :type
'(repeat string
))
225 ;; Internal variables
226 (defvar network-connection-service nil
)
227 (defvar network-connection-host nil
)
229 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
233 (defvar nslookup-font-lock-keywords
235 (list "^[A-Za-z0-9 _]+:" 0 'font-lock-type-face
)
236 (list "\\<\\(SOA\\|NS\\|MX\\|A\\|CNAME\\)\\>"
237 1 'font-lock-keyword-face
)
241 (make-list 4 "[0-9]+")
243 0 'font-lock-variable-name-face
)
246 (let ((host-expression "[-A-Za-z0-9]+"))
249 (make-list 2 host-expression
)
251 "\\(\\." host-expression
"\\)*"))
252 0 'font-lock-variable-name-face
))
253 "Expressions to font-lock for nslookup.")
255 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
256 ;; General network utilities mode
257 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
259 (defvar net-utils-font-lock-keywords
263 (mapconcat 'identity
(make-list 4 "[0-9]+") "\\.")
264 0 'font-lock-variable-name-face
)
265 ;; Simple rfc4291 addresses
267 "\\( \\([[:xdigit:]]+\\(:\\|::\\)\\)+[[:xdigit:]]+\\)"
269 "\\(::[[:xdigit:]]+\\)")
270 0 'font-lock-variable-name-face
)
273 (let ((host-expression "[-A-Za-z0-9]+"))
275 (mapconcat 'identity
(make-list 2 host-expression
) "\\.")
276 "\\(\\." host-expression
"\\)*"))
277 0 'font-lock-variable-name-face
))
278 "Expressions to font-lock for general network utilities.")
280 (define-derived-mode net-utils-mode special-mode
"NetworkUtil"
281 "Major mode for interacting with an external network utility."
282 (set (make-local-variable 'font-lock-defaults
)
283 '((net-utils-font-lock-keywords)))
284 (setq-local revert-buffer-function
#'net-utils--revert-function
))
286 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
288 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
290 ;; Simplified versions of some at-point functions from ffap.el.
291 ;; It's not worth loading all of ffap just for these.
292 (defun net-utils-machine-at-point ()
294 (buffer-substring-no-properties
296 (skip-chars-backward "-a-zA-Z0-9.")
299 (skip-chars-forward "-a-zA-Z0-9.")
300 (skip-chars-backward "." pt
)
303 (defun net-utils-url-at-point ()
305 (buffer-substring-no-properties
307 (skip-chars-backward "--:=&?$+@-Z_a-z~#,%")
308 (skip-chars-forward "^A-Za-z0-9" pt
)
311 (skip-chars-forward "--:=&?$+@-Z_a-z~#,%")
312 (skip-chars-backward ":;.,!?" pt
)
315 (defun net-utils-remove-ctrl-m-filter (process output-string
)
316 "Remove trailing control Ms."
317 (with-current-buffer (process-buffer process
)
319 (let ((inhibit-read-only t
)
320 (filtered-string output-string
))
321 (while (string-match "\r" filtered-string
)
322 (setq filtered-string
323 (replace-match "" nil nil filtered-string
)))
324 ;; Insert the text, moving the process-marker.
325 (goto-char (process-mark process
))
326 (insert filtered-string
)
327 (set-marker (process-mark process
) (point))))))
329 (defun net-utils-run-program (name header program args
)
330 "Run a network information program."
331 (let ((buf (get-buffer-create (concat "*" name
"*")))
332 (coding-system-for-read
333 ;; MS-Windows versions of network utilities output text
334 ;; encoded in the console (a.k.a. "OEM") codepage, which is
335 ;; different from the default system (a.k.a. "ANSI")
337 (if (eq system-type
'windows-nt
)
338 (intern (format "cp%d" (w32-get-console-output-codepage)))
339 coding-system-for-read
)))
344 (apply 'start-process name buf program args
)
345 'net-utils-remove-ctrl-m-filter
)
349 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
350 ;; General network utilities (diagnostic)
351 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
353 ;; Todo: This data could be saved in a bookmark.
354 (defvar net-utils--revert-cmd nil
)
356 (defun net-utils-run-simple (buffer program-name args
&optional nodisplay
)
357 "Run a network utility for diagnostic output only."
358 (with-current-buffer (if (stringp buffer
) (get-buffer-create buffer
) buffer
)
359 (let ((proc (get-buffer-process (current-buffer))))
361 (set-process-filter proc nil
)
362 (delete-process proc
)))
363 (let ((inhibit-read-only t
)
364 (coding-system-for-read
365 ;; MS-Windows versions of network utilities output text
366 ;; encoded in the console (a.k.a. "OEM") codepage, which is
367 ;; different from the default system (a.k.a. "ANSI")
369 (if (eq system-type
'windows-nt
)
370 (intern (format "cp%d" (w32-get-console-output-codepage)))
371 coding-system-for-read
)))
374 (setq-local net-utils--revert-cmd
375 `(net-utils-run-simple ,(current-buffer)
376 ,program-name
,args nodisplay
))
378 (apply 'start-process program-name
379 (current-buffer) program-name args
)
380 'net-utils-remove-ctrl-m-filter
)
381 (unless nodisplay
(display-buffer (current-buffer)))))
383 (defun net-utils--revert-function (&optional ignore-auto noconfirm
)
384 (message "Reverting `%s'..." (buffer-name))
385 (apply (car net-utils--revert-cmd
) (cdr net-utils--revert-cmd
))
386 (let ((proc (get-buffer-process (current-buffer))))
388 (set-process-sentinel
390 (lambda (process event
)
391 (when (string= event
"finished\n")
392 (message "Reverting `%s' done" (process-buffer process
))))))))
396 "Run ifconfig and display diagnostic output."
398 (net-utils-run-simple
399 (format "*%s*" ifconfig-program
)
401 ifconfig-program-options
))
403 (defalias 'ipconfig
'ifconfig
)
407 "Run iwconfig and display diagnostic output."
409 (net-utils-run-simple
410 (format "*%s*" iwconfig-program
)
412 iwconfig-program-options
))
416 "Run netstat and display diagnostic output."
418 (net-utils-run-simple
419 (format "*%s*" netstat-program
)
421 netstat-program-options
))
425 "Run arp and display diagnostic output."
427 (net-utils-run-simple
428 (format "*%s*" arp-program
)
430 arp-program-options
))
434 "Run route and display diagnostic output."
436 (net-utils-run-simple
437 (format "*%s*" route-program
)
439 route-program-options
))
441 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
442 ;; Wrappers for external network programs
443 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
446 (defun traceroute (target)
447 "Run traceroute program for TARGET."
448 (interactive "sTarget: ")
450 (if traceroute-program-options
451 (append traceroute-program-options
(list target
))
453 (net-utils-run-simple
454 (concat "Traceroute" " " target
)
461 If your system's ping continues until interrupted, you can try setting
462 `ping-program-options'."
464 (list (read-from-minibuffer "Ping host: " (net-utils-machine-at-point))))
466 (if ping-program-options
467 (append ping-program-options
(list host
))
469 (net-utils-run-program
470 (concat "Ping" " " host
)
471 (concat "** Ping ** " ping-program
" ** " host
)
475 ;; FIXME -- Needs to be a process filter
476 ;; (defun netstat-with-filter (filter)
477 ;; "Run netstat program."
478 ;; (interactive "sFilter: ")
480 ;; (set-buffer (get-buffer "*Netstat*"))
481 ;; (goto-char (point-min))
482 ;; (delete-matching-lines filter))
485 (defun nslookup-host (host)
486 "Lookup the DNS information for HOST."
488 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
490 (if nslookup-program-options
491 (append nslookup-program-options
(list host
))
493 (net-utils-run-program
497 (list "Nslookup" host nslookup-program
)
504 "Run nslookup program."
506 (switch-to-buffer (make-comint "nslookup" nslookup-program
))
509 (defvar comint-prompt-regexp
)
510 (defvar comint-input-autoexpand
)
512 (autoload 'comint-mode
"comint" nil t
)
514 (defvar nslookup-mode-map
515 (let ((map (make-sparse-keymap)))
516 (define-key map
"\t" 'completion-at-point
)
519 ;; Using a derived mode gives us keymaps, hooks, etc.
520 (define-derived-mode nslookup-mode comint-mode
"Nslookup"
521 "Major mode for interacting with the nslookup program."
523 (make-local-variable 'font-lock-defaults
)
524 '((nslookup-font-lock-keywords)))
525 (setq comint-prompt-regexp nslookup-prompt-regexp
)
526 (setq comint-input-autoexpand t
))
529 (defun dns-lookup-host (host)
530 "Lookup the DNS information for HOST (name or IP address)."
532 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
534 (if dns-lookup-program-options
535 (append dns-lookup-program-options
(list host
))
537 (net-utils-run-program
538 (concat "DNS Lookup [" host
"]")
541 (list "DNS Lookup" host dns-lookup-program
)
546 (autoload 'ffap-string-at-point
"ffap")
549 (defun run-dig (host)
553 (read-from-minibuffer "Lookup host: "
554 (or (ffap-string-at-point 'machine
) ""))))
555 (net-utils-run-program
559 (list "Dig" host dig-program
)
564 (autoload 'comint-exec
"comint")
566 ;; This is a lot less than ange-ftp, but much simpler.
572 (read-from-minibuffer
573 "Ftp to Host: " (net-utils-machine-at-point))))
574 (let ((buf (get-buffer-create (concat "*ftp [" host
"]*"))))
577 (comint-exec buf
(concat "ftp-" host
) ftp-program nil
578 (if ftp-program-options
579 (append (list host
) ftp-program-options
)
581 (pop-to-buffer buf
)))
584 (let ((map (make-sparse-keymap)))
585 ;; Occasionally useful
586 (define-key map
"\t" 'completion-at-point
)
589 (define-derived-mode ftp-mode comint-mode
"FTP"
590 "Major mode for interacting with the ftp program."
591 (setq comint-prompt-regexp ftp-prompt-regexp
)
592 (setq comint-input-autoexpand t
)
593 ;; Only add the password-prompting hook if it's not already in the
594 ;; global hook list. This stands a small chance of losing, if it's
595 ;; later removed from the global list (very small, since any
596 ;; password prompts will probably immediately follow the initial
597 ;; connection), but it's better than getting prompted twice for the
599 (unless (memq 'comint-watch-for-password-prompt
600 (default-value 'comint-output-filter-functions
))
601 (add-hook 'comint-output-filter-functions
'comint-watch-for-password-prompt
604 (defun smbclient (host service
)
605 "Connect to SERVICE on HOST via SMB."
608 (read-from-minibuffer
609 "Connect to Host: " (net-utils-machine-at-point))
610 (read-from-minibuffer "SMB Service: ")))
611 (let* ((name (format "smbclient [%s\\%s]" host service
))
612 (buf (get-buffer-create (concat "*" name
"*")))
613 (service-name (concat "\\\\" host
"\\" service
)))
616 (comint-exec buf name smbclient-program nil
617 (if smbclient-program-options
618 (append (list service-name
) smbclient-program-options
)
619 (list service-name
)))
620 (pop-to-buffer buf
)))
622 (defun smbclient-list-shares (host)
623 "List services on HOST."
626 (read-from-minibuffer
627 "Connect to Host: " (net-utils-machine-at-point))))
628 (let ((buf (get-buffer-create (format "*SMB Shares on %s*" host
))))
631 (comint-exec buf
"smbclient-list-shares"
632 smbclient-program nil
(list "-L" host
))
633 (pop-to-buffer buf
)))
635 (define-derived-mode smbclient-mode comint-mode
"smbclient"
636 "Major mode for interacting with the smbclient program."
637 (setq comint-prompt-regexp smbclient-prompt-regexp
)
638 (setq comint-input-autoexpand t
)
639 ;; Only add the password-prompting hook if it's not already in the
640 ;; global hook list. This stands a small chance of losing, if it's
641 ;; later removed from the global list (very small, since any
642 ;; password prompts will probably immediately follow the initial
643 ;; connection), but it's better than getting prompted twice for the
645 (unless (memq 'comint-watch-for-password-prompt
646 (default-value 'comint-output-filter-functions
))
647 (add-hook 'comint-output-filter-functions
'comint-watch-for-password-prompt
651 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
652 ;; Network Connections
653 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
655 ;; Full list is available at:
656 ;; http://www.iana.org/assignments/port-numbers
657 (defvar network-connection-service-alist
660 (cons 'active-users
11)
676 (cons 'netbios-name
137)
677 (cons 'netbios-data
139)
681 "Alist of services and associated TCP port numbers.
682 This list is not complete.")
685 (defun run-network-program (process-name host port
&optional initial-string
)
686 (let ((tcp-connection)
688 (setq buf
(get-buffer-create (concat "*" process-name
"*")))
692 (open-network-stream process-name buf host port
))
693 (error "Could not open connection to %s" host
))
695 (set-marker (process-mark tcp-connection
) (point-min))
696 (set-process-filter tcp-connection
'net-utils-remove-ctrl-m-filter
)
698 (process-send-string tcp-connection
699 (concat initial-string
"\r\n")))
700 (display-buffer buf
)))
702 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
704 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
706 (defcustom finger-X
.500-host-regexps nil
707 "A list of regular expressions matching host names.
708 If a host name passed to `finger' matches one of these regular
709 expressions, it is assumed to be a host that doesn't accept
710 queries of the form USER@HOST, and wants a query containing USER only."
712 :type
'(repeat regexp
)
717 (defun finger (user host
)
718 "Finger USER on HOST."
719 ;; One of those great interactive statements that's actually
720 ;; longer than the function call! The idea is that if the user
721 ;; uses a string like "pbreton@cs.umb.edu", we won't ask for the
722 ;; host name. If we don't see an "@", we'll prompt for the host.
724 (let* ((answer (read-from-minibuffer "Finger User: "
725 (net-utils-url-at-point)))
726 (index (string-match (regexp-quote "@") answer
)))
728 (list (substring answer
0 index
)
729 (substring answer
(1+ index
)))
731 (read-from-minibuffer "At Host: "
732 (net-utils-machine-at-point))))))
733 (let* ((user-and-host (concat user
"@" host
))
734 (process-name (concat "Finger [" user-and-host
"]"))
735 (regexps finger-X
.500-host-regexps
)
738 (while (not (string-match (car regexps
) host
))
739 (setq regexps
(cdr regexps
)))
741 (setq user-and-host user
)))
745 (cdr (assoc 'finger network-connection-service-alist
))
748 (defcustom whois-server-name
"rs.internic.net"
749 "Default host name for the whois service."
753 (defcustom whois-server-list
754 '(("whois.arin.net") ; Networks, ASN's, and related POC's (numbers)
755 ("rs.internic.net") ; domain related info
756 ("whois.publicinterestregistry.net")
763 "A list of whois servers that can be queried."
765 :type
'(repeat (list string
)))
767 ;; FIXME: modern whois clients include a much better tld <-> whois server
768 ;; list, Emacs should probably avoid specifying the server as the client
769 ;; will DTRT anyway... -rfr
770 (defcustom whois-server-tld
771 '(("rs.internic.net" .
"com")
772 ("whois.publicinterestregistry.net" .
"org")
773 ("whois.ripe.net" .
"be")
774 ("whois.ripe.net" .
"de")
775 ("whois.ripe.net" .
"dk")
776 ("whois.ripe.net" .
"it")
777 ("whois.ripe.net" .
"fi")
778 ("whois.ripe.net" .
"fr")
779 ("whois.ripe.net" .
"uk")
780 ("whois.apnic.net" .
"au")
781 ("whois.apnic.net" .
"ch")
782 ("whois.apnic.net" .
"hk")
783 ("whois.apnic.net" .
"jp")
784 ("whois.nic.gov" .
"gov")
785 ("whois.nic.mil" .
"mil"))
786 "Alist to map top level domains to whois servers."
788 :type
'(repeat (cons string string
)))
790 (defcustom whois-guess-server t
791 "If non-nil then whois will try to deduce the appropriate whois
792 server from the query. If the query doesn't look like a domain or hostname
793 then the server named by `whois-server-name' is used."
797 (defun whois-get-tld (host)
798 "Return the top level domain of `host', or nil if it isn't a domain name."
799 (let ((i (1- (length host
)))
800 (max-len (- (length host
) 5)))
801 (while (not (or (= i max-len
) (char-equal (aref host i
) ?.
)))
805 (substring host
(1+ i
)))))
809 (defun whois (arg search-string
)
810 "Send SEARCH-STRING to server defined by the `whois-server-name' variable.
811 If `whois-guess-server' is non-nil, then try to deduce the correct server
812 from SEARCH-STRING. With argument, prompt for whois server."
813 (interactive "P\nsWhois: ")
814 (let* ((whois-apropos-host (if whois-guess-server
815 (rassoc (whois-get-tld search-string
)
818 (server-name (if whois-apropos-host
819 (car whois-apropos-host
)
823 (completing-read "Whois server name: "
824 whois-server-list nil nil
"whois.")
829 (cdr (assoc 'whois network-connection-service-alist
))
832 (defcustom whois-reverse-lookup-server
"whois.arin.net"
833 "Server which provides inverse DNS mapping."
838 (defun whois-reverse-lookup ()
840 (let ((whois-server-name whois-reverse-lookup-server
))
841 (call-interactively 'whois
)))
843 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
844 ;;; General Network connection
845 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
847 ;; Using a derived mode gives us keymaps, hooks, etc.
849 network-connection-mode comint-mode
"Network-Connection"
850 "Major mode for interacting with the network-connection program.")
852 (defun network-connection-mode-setup (host service
)
853 (make-local-variable 'network-connection-host
)
854 (setq network-connection-host host
)
855 (make-local-variable 'network-connection-service
)
856 (setq network-connection-service service
))
859 (defun network-connection-to-service (host service
)
860 "Open a network connection to SERVICE on HOST."
863 (read-from-minibuffer "Host: " (net-utils-machine-at-point))
864 (completing-read "Service: "
868 (list (symbol-name (car elt
)))))
869 network-connection-service-alist
))))
872 (cdr (assoc (intern service
) network-connection-service-alist
))))
875 (defun network-connection (host port
)
876 "Open a network connection to HOST on PORT."
877 (interactive "sHost: \nnPort: ")
878 (network-service-connection host
(number-to-string port
)))
880 (defun network-service-connection (host service
)
881 "Open a network connection to SERVICE on HOST."
882 (let* ((process-name (concat "Network Connection [" host
" " service
"]"))
883 (portnum (string-to-number service
))
884 (buf (get-buffer-create (concat "*" process-name
"*"))))
885 (or (zerop portnum
) (setq service portnum
))
890 (network-connection-mode)
891 (network-connection-mode-setup host service
)
892 (pop-to-buffer buf
)))
894 (defvar comint-input-ring
)
896 (defun network-connection-reconnect ()
897 "Reconnect a network connection, preserving the old input ring."
899 (let ((proc (get-buffer-process (current-buffer)))
900 (old-comint-input-ring comint-input-ring
)
901 (host network-connection-host
)
902 (service network-connection-service
))
903 (if (not (or (not proc
)
904 (eq (process-status proc
) 'closed
)))
905 (message "Still connected")
906 (goto-char (point-max))
907 (insert (format "Reopening connection to %s\n" host
))
908 (network-connection host
909 (if (numberp service
)
911 (cdr (assoc service network-connection-service-alist
))))
912 (and old-comint-input-ring
913 (setq comint-input-ring old-comint-input-ring
)))))
917 ;;; net-utils.el ends here