net-utils.el 25 KB
Newer Older
Gerd Moellmann's avatar
Gerd Moellmann committed
1 2 3 4 5
;;; net-utils.el --- Network functions

;; Author:  Peter Breton <pbreton@cs.umb.edu>
;; Created: Sun Mar 16 1997
;; Keywords: network communications
6
;; Time-stamp: <2000-10-04 22:39:05 pbreton>
Gerd Moellmann's avatar
Gerd Moellmann committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:
;;
;; There are three main areas of functionality:
28
;;
Gerd Moellmann's avatar
Gerd Moellmann committed
29 30 31
;; * Wrap common network utility programs (ping, traceroute, netstat,
;; nslookup, arp, route). Note that these wrappers are of the diagnostic
;; functions of these programs only.
32
;;
Gerd Moellmann's avatar
Gerd Moellmann committed
33
;; * Implement some very basic protocols in Emacs Lisp (finger and whois)
34
;;
Gerd Moellmann's avatar
Gerd Moellmann committed
35 36 37 38 39 40 41
;; * Support connections to HOST/PORT, generally for debugging and the like.
;; In other words, for doing much the same thing as "telnet HOST PORT", and
;; then typing commands.
;;
;; PATHS
;;
;; On some systems, some of these programs are not in normal user path,
42
;; but rather in /sbin, /usr/sbin, and so on.
Gerd Moellmann's avatar
Gerd Moellmann committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59


;;; Code:
(eval-when-compile
  (require 'comint))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Customization Variables
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defgroup net-utils nil
  "Network utility functions."
  :prefix "net-utils-"
  :group 'comm
  :version "20.3"
  )

60
(defcustom net-utils-remove-ctl-m
Gerd Moellmann's avatar
Gerd Moellmann committed
61 62 63 64 65 66
  (member system-type (list 'windows-nt 'msdos))
  "If non-nil, remove control-Ms from output."
  :group 'net-utils
  :type  'boolean
  )

67 68
(defcustom traceroute-program
  (if (eq system-type 'windows-nt)
Gerd Moellmann's avatar
Gerd Moellmann committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
      "tracert"
    "traceroute")
  "Program to trace network hops to a destination."
  :group 'net-utils
  :type  'string
  )

(defcustom traceroute-program-options nil
  "Options for the traceroute program."
  :group 'net-utils
  :type  '(repeat string)
  )

(defcustom ping-program "ping"
  "Program to send network test packets to a host."
  :group 'net-utils
  :type  'string
  )

;; On Linux and Irix, the system's ping program seems to send packets
;; indefinitely unless told otherwise
90
(defcustom ping-program-options
Gerd Moellmann's avatar
Gerd Moellmann committed
91 92 93 94 95 96 97 98
  (and (memq system-type (list 'linux 'gnu/linux 'irix))
       (list "-c" "4"))
  "Options for the ping program.
These options can be used to limit how many ICMP packets are emitted."
  :group 'net-utils
  :type  '(repeat string)
  )

99
(defcustom ipconfig-program
Gerd Moellmann's avatar
Gerd Moellmann committed
100 101 102 103 104 105 106 107 108
  (if (eq system-type 'windows-nt)
      "ipconfig"
    "ifconfig")
  "Program to print network configuration information."
  :group 'net-utils
  :type  'string
  )

(defcustom ipconfig-program-options
109
   (list
Gerd Moellmann's avatar
Gerd Moellmann committed
110 111 112 113 114 115 116 117 118 119 120 121 122
    (if (eq system-type 'windows-nt)
	"/all" "-a"))
  "Options for ipconfig-program."
  :group 'net-utils
  :type  '(repeat string)
  )

(defcustom netstat-program  "netstat"
  "Program to print network statistics."
  :group 'net-utils
  :type  'string
  )

123
(defcustom netstat-program-options
Gerd Moellmann's avatar
Gerd Moellmann committed
124 125 126 127 128 129 130 131 132 133 134 135
  (list "-a")
  "Options for netstat-program."
  :group 'net-utils
  :type  '(repeat string)
  )

(defcustom arp-program  "arp"
  "Program to print IP to address translation tables."
  :group 'net-utils
  :type  'string
  )

136
(defcustom arp-program-options
Gerd Moellmann's avatar
Gerd Moellmann committed
137 138 139 140 141 142
  (list "-a")
  "Options for arp-program."
  :group 'net-utils
  :type  '(repeat string)
  )

143
(defcustom route-program
Gerd Moellmann's avatar
Gerd Moellmann committed
144 145 146 147 148 149 150 151
  (if (eq system-type 'windows-nt)
      "route"
    "netstat")
  "Program to print routing tables."
  :group 'net-utils
  :type  'string
  )

152
(defcustom route-program-options
Gerd Moellmann's avatar
Gerd Moellmann committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
  (if (eq system-type 'windows-nt)
      (list "print")
    (list "-r"))
  "Options for route-program."
  :group 'net-utils
  :type  '(repeat string)
  )

(defcustom nslookup-program  "nslookup"
  "Program to interactively query DNS information."
  :group 'net-utils
  :type  'string
  )

(defcustom nslookup-program-options  nil
  "List of options to pass to the nslookup program."
  :group 'net-utils
  :type  '(repeat string)
  )

(defcustom nslookup-prompt-regexp "^> "
  "Regexp to match the nslookup prompt."
  :group 'net-utils
  :type  'regexp
  )

(defcustom dig-program  "dig"
  "Program to query DNS information."
  :group 'net-utils
  :type  'string
  )

(defcustom ftp-program "ftp"
  "Progam to run to do FTP transfers."
  :group 'net-utils
  :type  'string
  )

(defcustom ftp-program-options nil
  "List of options to pass to the FTP program."
  :group 'net-utils
  :type  '(repeat string)
  )

(defcustom ftp-prompt-regexp "^ftp>"
  "Regexp which matches the FTP program's prompt."
  :group 'net-utils
  :type  'regexp
  )

(defcustom smbclient-program "smbclient"
  "Smbclient program."
  :group 'net-utils
  :type  'string
  )

(defcustom smbclient-program-options nil
  "List of options to pass to the smbclient program."
  :group 'net-utils
  :type  '(repeat string)
  )

(defcustom smbclient-prompt-regexp "^smb: \>"
  "Regexp which matches the smbclient program's prompt."
  :group 'net-utils
  :type  'regexp
  )

221 222 223 224
;; Internal variables
(defvar network-connection-service nil)
(defvar network-connection-host    nil)

Gerd Moellmann's avatar
Gerd Moellmann committed
225 226 227 228 229
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Nslookup goodies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defconst nslookup-font-lock-keywords
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  (progn
    (require 'font-lock)
    (list
     (list nslookup-prompt-regexp 0 font-lock-reference-face)
     (list "^[A-Za-z0-9 _]+:"     0 font-lock-type-face)
     (list "\\<\\(SOA\\|NS\\|MX\\|A\\|CNAME\\)\\>"
	   1 font-lock-keyword-face)
     ;; Dotted quads
     (list
      (mapconcat 'identity
		 (make-list 4 "[0-9]+")
		 "\\.")
      0 font-lock-variable-name-face)
     ;; Host names
     (list
      (let ((host-expression "[-A-Za-z0-9]+"))
	(concat
	 (mapconcat 'identity
		    (make-list 2 host-expression)
		    "\\.")
	 "\\(\\." host-expression "\\)*")
	)
      0 font-lock-variable-name-face)
     ))
  "Expressions to font-lock for nslookup.")
Gerd Moellmann's avatar
Gerd Moellmann committed
255 256 257 258 259 260

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; FTP goodies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defconst ftp-font-lock-keywords
261 262 263 264
  (progn
    (require 'font-lock)
    (list
     (list ftp-prompt-regexp 0 font-lock-reference-face))))
Gerd Moellmann's avatar
Gerd Moellmann committed
265 266 267 268 269 270

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; smbclient goodies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defconst smbclient-font-lock-keywords
271 272 273 274
  (progn
    (require 'font-lock)
    (list
     (list smbclient-prompt-regexp 0 font-lock-reference-face))))
Gerd Moellmann's avatar
Gerd Moellmann committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Utility functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Simplified versions of some at-point functions from ffap.el.
;; It's not worth loading all of ffap just for these.
(defun net-utils-machine-at-point ()
  (let ((pt (point)))
    (buffer-substring-no-properties
     (save-excursion
       (skip-chars-backward "-a-zA-Z0-9.")
       (point))
     (save-excursion
       (skip-chars-forward "-a-zA-Z0-9.")
       (skip-chars-backward "." pt)
       (point)))))

(defun net-utils-url-at-point ()
  (let ((pt (point)))
    (buffer-substring-no-properties
     (save-excursion
       (skip-chars-backward "--:=&?$+@-Z_a-z~#,%")
       (skip-chars-forward "^A-Za-z0-9" pt)
       (point))
     (save-excursion
       (skip-chars-forward "--:=&?$+@-Z_a-z~#,%")
       (skip-chars-backward ":;.,!?" pt)
       (point)))))


(defun net-utils-remove-ctrl-m-filter (process output-string)
  "Remove trailing control Ms."
  (let ((old-buffer (current-buffer))
	(filtered-string output-string))
    (unwind-protect
	(let ((moving))
	  (set-buffer (process-buffer process))
	  (setq moving (= (point) (process-mark process)))
314

Gerd Moellmann's avatar
Gerd Moellmann committed
315 316 317 318 319 320 321 322 323 324 325
	  (while (string-match "\r" filtered-string)
	       (setq filtered-string
		     (replace-match "" nil nil filtered-string)))

	  (save-excursion
	    ;; Insert the text, moving the process-marker.
	    (goto-char (process-mark process))
	    (insert filtered-string)
	    (set-marker (process-mark process) (point)))
	  (if moving (goto-char (process-mark process))))
      (set-buffer old-buffer))))
326

Gerd Moellmann's avatar
Gerd Moellmann committed
327 328 329 330 331 332
(defmacro net-utils-run-program (name header program &rest args)
  "Run a network information program."
  ` (let ((buf (get-buffer-create (concat "*" ,name "*"))))
      (set-buffer buf)
      (erase-buffer)
      (insert ,header "\n")
333
      (set-process-filter
Gerd Moellmann's avatar
Gerd Moellmann committed
334 335
       (apply 'start-process ,name buf ,program ,@args)
       'net-utils-remove-ctrl-m-filter)
336 337
      (display-buffer buf)
      buf))
Gerd Moellmann's avatar
Gerd Moellmann committed
338 339 340 341 342 343 344 345 346

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Wrappers for external network programs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;###autoload
(defun traceroute (target)
  "Run traceroute program for TARGET."
  (interactive "sTarget: ")
347
  (let ((options
Gerd Moellmann's avatar
Gerd Moellmann committed
348 349 350 351 352 353 354 355 356 357 358 359 360
	 (if traceroute-program-options
	     (append traceroute-program-options (list target))
	   (list target))))
    (net-utils-run-program
     (concat "Traceroute" " " target)
     (concat "** Traceroute ** " traceroute-program " ** " target)
     traceroute-program
     options
     )))

;;;###autoload
(defun ping (host)
  "Ping HOST.
361
If your system's ping continues until interrupted, you can try setting
Gerd Moellmann's avatar
Gerd Moellmann committed
362
`ping-program-options'."
363
  (interactive
Gerd Moellmann's avatar
Gerd Moellmann committed
364
   (list (read-from-minibuffer "Ping host: " (net-utils-machine-at-point))))
365
  (let ((options
Gerd Moellmann's avatar
Gerd Moellmann committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
	 (if ping-program-options
	     (append ping-program-options (list host))
	   (list host))))
    (net-utils-run-program
     (concat "Ping" " " host)
     (concat "** Ping ** " ping-program " ** " host)
     ping-program
     options
     )))

;;;###autoload
(defun ipconfig ()
  "Run ipconfig program."
  (interactive)
  (net-utils-run-program
   "Ipconfig"
   (concat "** Ipconfig ** " ipconfig-program " ** ")
   ipconfig-program
   ipconfig-program-options
   ))

;; This is the normal name on most Unixes.
;;;###autoload
389
(defalias 'ifconfig 'ipconfig)
Gerd Moellmann's avatar
Gerd Moellmann committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

;;;###autoload
(defun netstat ()
  "Run netstat program."
  (interactive)
  (net-utils-run-program
   "Netstat"
   (concat "** Netstat ** " netstat-program " ** ")
   netstat-program
   netstat-program-options
   ))

;;;###autoload
(defun arp ()
  "Run the arp program."
  (interactive)
  (net-utils-run-program
   "Arp"
   (concat "** Arp ** " arp-program " ** ")
   arp-program
   arp-program-options
   ))

;;;###autoload
(defun route ()
  "Run the route program."
  (interactive)
  (net-utils-run-program
   "Route"
   (concat "** Route ** " route-program " ** ")
   route-program
   route-program-options
   ))

;; FIXME -- Needs to be a process filter
;; (defun netstat-with-filter (filter)
;;   "Run netstat program."
;;   (interactive "sFilter: ")
;;   (netstat)
;;   (set-buffer (get-buffer "*Netstat*"))
;;   (goto-char (point-min))
;;   (delete-matching-lines filter)
;;   )

;;;###autoload
(defun nslookup-host (host)
  "Lookup the DNS information for HOST."
  (interactive
   (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
439
  (let ((options
Gerd Moellmann's avatar
Gerd Moellmann committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	 (if nslookup-program-options
	     (append nslookup-program-options (list host))
	   (list host))))
    (net-utils-run-program
     "Nslookup"
     (concat "** "
      (mapconcat 'identity
		(list "Nslookup" host nslookup-program)
		" ** "))
     nslookup-program
     options
     )))


;;;###autoload
(defun nslookup ()
  "Run nslookup program."
  (interactive)
  (require 'comint)
  (comint-run nslookup-program)
  (set-process-filter (get-buffer-process "*nslookup*")
   'net-utils-remove-ctrl-m-filter)
  (nslookup-mode)
  )

;; Using a derived mode gives us keymaps, hooks, etc.
466
(define-derived-mode
Gerd Moellmann's avatar
Gerd Moellmann committed
467 468
  nslookup-mode comint-mode "Nslookup"
  "Major mode for interacting with the nslookup program."
469
  (set
Gerd Moellmann's avatar
Gerd Moellmann committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
   (make-local-variable 'font-lock-defaults)
   '((nslookup-font-lock-keywords)))
  (setq local-abbrev-table nslookup-mode-abbrev-table)
  (abbrev-mode t)
  (make-local-variable 'comint-prompt-regexp)
  (setq comint-prompt-regexp nslookup-prompt-regexp)
  (make-local-variable 'comint-input-autoexpand)
  (setq comint-input-autoexpand t)
  )

(define-key nslookup-mode-map "\t" 'comint-dynamic-complete)

(define-abbrev nslookup-mode-abbrev-table "e"   "exit")
(define-abbrev nslookup-mode-abbrev-table "f"   "finger")
(define-abbrev nslookup-mode-abbrev-table "h"   "help")
(define-abbrev nslookup-mode-abbrev-table "lse" "lserver")
(define-abbrev nslookup-mode-abbrev-table "q"   "exit")
(define-abbrev nslookup-mode-abbrev-table "r"   "root")
(define-abbrev nslookup-mode-abbrev-table "s"   "set")
(define-abbrev nslookup-mode-abbrev-table "se"  "server")
(define-abbrev nslookup-mode-abbrev-table "v"   "viewer")

;;;###autoload
(defun dig (host)
  "Run dig program."
  (interactive
   (list
    (progn
      (require 'ffap)
499 500
      (read-from-minibuffer
       "Lookup host: "
Gerd Moellmann's avatar
Gerd Moellmann committed
501 502 503 504 505 506 507 508 509
       (or (ffap-string-at-point 'machine) "")))))
  (net-utils-run-program
   "Dig"
   (concat "** "
	   (mapconcat 'identity
		      (list "Dig" host dig-program)
		      " ** "))
   dig-program
   (list host)
510
   ))
Gerd Moellmann's avatar
Gerd Moellmann committed
511 512 513 514 515

;; This is a lot less than ange-ftp, but much simpler.
;;;###autoload
(defun ftp (host)
  "Run ftp program."
516
  (interactive
Gerd Moellmann's avatar
Gerd Moellmann committed
517
   (list
518
    (read-from-minibuffer
Gerd Moellmann's avatar
Gerd Moellmann committed
519 520 521 522 523 524 525 526 527 528 529 530 531
     "Ftp to Host: " (net-utils-machine-at-point))))
  (require 'comint)
  (let ((buf (get-buffer-create (concat "*ftp [" host "]*"))))
    (set-buffer buf)
    (comint-mode)
    (comint-exec buf (concat "ftp-" host) ftp-program nil
		 (if ftp-program-options
		     (append (list host) ftp-program-options)
		   (list host)))
    (ftp-mode)
    (switch-to-buffer-other-window buf)
    ))

532
(define-derived-mode
Gerd Moellmann's avatar
Gerd Moellmann committed
533 534 535
  ftp-mode comint-mode "FTP"
  "Major mode for interacting with the ftp program."

536
  (set
Gerd Moellmann's avatar
Gerd Moellmann committed
537 538
   (make-local-variable 'font-lock-defaults)
   '((ftp-font-lock-keywords)))
539

Gerd Moellmann's avatar
Gerd Moellmann committed
540 541
  (make-local-variable 'comint-prompt-regexp)
  (setq comint-prompt-regexp ftp-prompt-regexp)
542

Gerd Moellmann's avatar
Gerd Moellmann committed
543 544
  (make-local-variable 'comint-input-autoexpand)
  (setq comint-input-autoexpand t)
545

Gerd Moellmann's avatar
Gerd Moellmann committed
546 547 548
  ;; Already buffer local!
  (setq comint-output-filter-functions
	(list 'comint-watch-for-password-prompt))
549

Gerd Moellmann's avatar
Gerd Moellmann committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563
  (setq local-abbrev-table ftp-mode-abbrev-table)
  (abbrev-mode t)
  )

(define-abbrev ftp-mode-abbrev-table "q"    "quit")
(define-abbrev ftp-mode-abbrev-table "g"    "get")
(define-abbrev ftp-mode-abbrev-table "p"    "prompt")
(define-abbrev ftp-mode-abbrev-table "anon" "anonymous")

;; Occasionally useful
(define-key ftp-mode-map "\t" 'comint-dynamic-complete)

(defun smbclient (host service)
  "Connect to SERVICE on HOST via SMB."
564
  (interactive
Gerd Moellmann's avatar
Gerd Moellmann committed
565
   (list
566
    (read-from-minibuffer
Gerd Moellmann's avatar
Gerd Moellmann committed
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
     "Connect to Host: " (net-utils-machine-at-point))
    (read-from-minibuffer "SMB Service: ")))
  (require 'comint)
  (let* ((name (format "smbclient [%s\\%s]" host service))
	 (buf (get-buffer-create (concat "*" name "*")))
	 (service-name (concat "\\\\" host "\\" service)))
    (set-buffer buf)
    (comint-mode)
    (comint-exec buf name smbclient-program nil
		 (if smbclient-program-options
		     (append (list service-name) smbclient-program-options)
		   (list service-name)))
    (smbclient-mode)
    (switch-to-buffer-other-window buf)
    ))

(defun smbclient-list-shares (host)
  "List services on HOST."
585
  (interactive
Gerd Moellmann's avatar
Gerd Moellmann committed
586
   (list
587
    (read-from-minibuffer
Gerd Moellmann's avatar
Gerd Moellmann committed
588 589 590 591 592
     "Connect to Host: " (net-utils-machine-at-point))
    ))
  (let ((buf (get-buffer-create (format "*SMB Shares on %s*" host))))
    (set-buffer buf)
    (comint-mode)
593 594 595 596
    (comint-exec
     buf
     "smbclient-list-shares"
     smbclient-program
Gerd Moellmann's avatar
Gerd Moellmann committed
597 598 599 600 601
     nil
     (list "-L" host)
     )
    (smbclient-mode)
    (switch-to-buffer-other-window buf)))
602 603

(define-derived-mode
Gerd Moellmann's avatar
Gerd Moellmann committed
604 605 606
  smbclient-mode comint-mode "smbclient"
  "Major mode for interacting with the smbclient program."

607
  (set
Gerd Moellmann's avatar
Gerd Moellmann committed
608 609
   (make-local-variable 'font-lock-defaults)
   '((smbclient-font-lock-keywords)))
610

Gerd Moellmann's avatar
Gerd Moellmann committed
611 612
  (make-local-variable 'comint-prompt-regexp)
  (setq comint-prompt-regexp smbclient-prompt-regexp)
613

Gerd Moellmann's avatar
Gerd Moellmann committed
614 615
  (make-local-variable 'comint-input-autoexpand)
  (setq comint-input-autoexpand t)
616

Gerd Moellmann's avatar
Gerd Moellmann committed
617 618 619
  ;; Already buffer local!
  (setq comint-output-filter-functions
	(list 'comint-watch-for-password-prompt))
620

Gerd Moellmann's avatar
Gerd Moellmann committed
621 622 623 624 625 626 627 628 629 630 631 632 633
  (setq local-abbrev-table smbclient-mode-abbrev-table)
  (abbrev-mode t)
  )

(define-abbrev smbclient-mode-abbrev-table "q"    "quit")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Network Connections
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Full list is available at:
;; ftp://ftp.isi.edu/in-notes/iana/assignments/port-numbers
634
(defvar network-connection-service-alist
Gerd Moellmann's avatar
Gerd Moellmann committed
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
  (list
    (cons 'echo          7)
    (cons 'active-users 11)
    (cons 'daytime      13)
    (cons 'chargen      19)
    (cons 'ftp          21)
    (cons 'telnet	23)
    (cons 'smtp		25)
    (cons 'time		37)
    (cons 'whois        43)
    (cons 'gopher       70)
    (cons 'finger       79)
    (cons 'www		80)
    (cons 'pop2		109)
    (cons 'pop3		110)
    (cons 'sun-rpc	111)
    (cons 'nntp		119)
    (cons 'ntp		123)
    (cons 'netbios-name 137)
    (cons 'netbios-data 139)
    (cons 'irc		194)
    (cons 'https	443)
    (cons 'rlogin	513)
    )
  "Alist of services and associated TCP port numbers.
This list in not complete.")

;; Workhorse macro
663
(defmacro run-network-program (process-name host port
Gerd Moellmann's avatar
Gerd Moellmann committed
664 665 666 667 668 669 670
					    &optional initial-string)
  `
   (let ((tcp-connection)
	 (buf)
	 )
    (setq buf (get-buffer-create (concat "*" ,process-name "*")))
    (set-buffer buf)
671
    (or
Gerd Moellmann's avatar
Gerd Moellmann committed
672
     (setq tcp-connection
673
	   (open-network-stream
Gerd Moellmann's avatar
Gerd Moellmann committed
674 675 676 677 678 679 680 681 682 683
	    ,process-name
	    buf
	    ,host
	    ,port
	    ))
     (error "Could not open connection to %s" ,host))
    (erase-buffer)
    (set-marker (process-mark tcp-connection) (point-min))
    (set-process-filter tcp-connection 'net-utils-remove-ctrl-m-filter)
    (and ,initial-string
684
	 (process-send-string tcp-connection
Gerd Moellmann's avatar
Gerd Moellmann committed
685 686 687 688 689 690 691
			      (concat ,initial-string "\r\n")))
    (display-buffer buf)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Simple protocols
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

692 693 694 695 696 697 698 699 700
(defcustom finger-X.500-host-regexps nil
  "A list of regular expressions matching host names.
If a host name passed to `finger' matches one of these regular
expressions, it is assumed to be a host that doesn't accept
queries of the form USER@HOST, and wants a query containing USER only."
  :group 'net-utils
  :type '(repeat regexp)
  :version "21.1")

Gerd Moellmann's avatar
Gerd Moellmann committed
701 702 703 704 705 706 707 708 709 710 711 712 713
;; Finger protocol
;;;###autoload
(defun finger (user host)
  "Finger USER on HOST."
  ;; One of those great interactive statements that's actually
  ;; longer than the function call! The idea is that if the user
  ;; uses a string like "pbreton@cs.umb.edu", we won't ask for the
  ;; host name. If we don't see an "@", we'll prompt for the host.
  (interactive
    (let* ((answer (read-from-minibuffer "Finger User: "
					 (net-utils-url-at-point)))
	   (index  (string-match (regexp-quote "@") answer)))
      (if index
714 715 716 717 718 719 720 721 722 723 724 725 726
	  (list (substring answer 0 index)
		(substring answer (1+ index)))
	(list answer
	      (read-from-minibuffer "At Host: "
				    (net-utils-machine-at-point))))))
  (let* ((user-and-host (concat user "@" host))
	 (process-name (concat "Finger [" user-and-host "]"))
	 (regexps finger-X.500-host-regexps)
	 found)
    (while (not (string-match (car regexps) host))
      (setq regexps (cdr regexps)))
    (when regexps
      (setq user-and-host user))
727 728 729
    (run-network-program
     process-name
     host
Gerd Moellmann's avatar
Gerd Moellmann committed
730
     (cdr (assoc 'finger network-connection-service-alist))
731
     user-and-host)))
Gerd Moellmann's avatar
Gerd Moellmann committed
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

(defcustom whois-server-name "rs.internic.net"
  "Default host name for the whois service."
  :group 'net-utils
  :type  'string
  )

(defcustom whois-server-list
  '(("whois.arin.net")     ; Networks, ASN's, and related POC's (numbers)
    ("rs.internic.net")  ; domain related info
    ("whois.abuse.net")
    ("whois.apnic.net")
    ("nic.ddn.mil")
    ("whois.nic.mil")
    ("whois.nic.gov")
    ("whois.ripe.net"))
  "A list of whois servers that can be queried."
  :group 'net-utils
  :type '(repeat (list string)))

(defcustom whois-server-tld
  '(("rs.internic.net" . "com")
    ("rs.internic.net" . "org")
    ("whois.ripe.net" . "be")
    ("whois.ripe.net" . "de")
    ("whois.ripe.net" . "dk")
    ("whois.ripe.net" . "it")
    ("whois.ripe.net" . "fi")
    ("whois.ripe.net" . "fr")
    ("whois.ripe.net" . "uk")
    ("whois.apnic.net" . "au")
    ("whois.apnic.net" . "ch")
    ("whois.apnic.net" . "hk")
    ("whois.apnic.net" . "jp")
    ("whois.nic.gov" . "gov")
    ("whois.nic.mil" . "mil"))
  "Alist to map top level domains to whois servers."
  :group 'net-utils
  :type '(repeat (cons string string)))

(defcustom whois-guess-server t
  "If non-nil then whois will try to deduce the appropriate whois
server from the query.  If the query doesn't look like a domain or hostname
then the server named by whois-server-name is used."
  :group 'net-utils
  :type 'boolean)

(defun whois-get-tld (host)
  "Return the top level domain of `host', or nil if it isn't a domain name."
  (let ((i (1- (length host)))
	(max-len (- (length host) 5)))
    (while (not (or (= i max-len) (char-equal (aref host i) ?.)))
      (setq i (1- i)))
    (if (= i max-len)
	nil
      (substring host (1+ i)))))

;; Whois protocol
;;;###autoload
(defun whois (arg search-string)
  "Send SEARCH-STRING to server defined by the `whois-server-name' variable.
If `whois-guess-server' is non-nil, then try to deduce the correct server
from SEARCH-STRING.  With argument, prompt for whois server."
  (interactive "P\nsWhois: ")
  (let* ((whois-apropos-host (if whois-guess-server
				 (rassoc (whois-get-tld search-string)
					 whois-server-tld)
			       nil))
	 (server-name (if whois-apropos-host
			  (car whois-apropos-host)
			whois-server-name))
	 (host
	  (if arg
	      (completing-read "Whois server name: "
			       whois-server-list nil nil "whois.")
	    server-name)))
808
    (run-network-program
Gerd Moellmann's avatar
Gerd Moellmann committed
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
     "Whois"
     host
     (cdr (assoc 'whois network-connection-service-alist))
     search-string
     )))

(defcustom whois-reverse-lookup-server "whois.arin.net"
  "Server which provides inverse DNS mapping."
  :group 'net-utils
  :type  'string
  )

;;;###autoload
(defun whois-reverse-lookup ()
  (interactive)
  (let ((whois-server-name whois-reverse-lookup-server))
    (call-interactively 'whois)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; General Network connection
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

831
;; Using a derived mode gives us keymaps, hooks, etc.
832
(define-derived-mode
833 834 835 836 837 838
  network-connection-mode comint-mode "Network-Connection"
  "Major mode for interacting with the network-connection program."
  )

(defun network-connection-mode-setup (host service)
  (let ((network-abbrev-table
839
	 (or
840 841
	  (assoc service network-connection-service-abbrev-alist)
	 (and (rassoc service network-connection-service-alist)
842
	      (assoc
843 844 845 846
	       (elt (rassoc service network-connection-service-alist) 0)
	       network-connection-service-abbrev-alist)))))
    (make-local-variable 'network-connection-host)
    (setq network-connection-host host)
847
    (make-local-variable 'network-connection-service)
848 849 850 851 852 853
    (setq network-connection-service service)
    (and network-abbrev-table
	 (setq local-abbrev-table (cdr network-abbrev-table))
	 (abbrev-mode t)
	 )))

Gerd Moellmann's avatar
Gerd Moellmann committed
854 855 856
;;;###autoload
(defun network-connection-to-service (host service)
  "Open a network connection to SERVICE on HOST."
857
  (interactive
Gerd Moellmann's avatar
Gerd Moellmann committed
858 859
   (list
    (read-from-minibuffer "Host: " (net-utils-machine-at-point))
860 861 862
    (completing-read "Service: "
		     (mapcar
		      (function
Gerd Moellmann's avatar
Gerd Moellmann committed
863 864 865
		       (lambda (elt)
			 (list (symbol-name (car elt)))))
		      network-connection-service-alist))))
866 867
  (network-connection
   host
Gerd Moellmann's avatar
Gerd Moellmann committed
868 869 870 871 872 873 874 875 876 877 878 879
   (cdr (assoc (intern service) network-connection-service-alist)))
  )

;;;###autoload
(defun network-connection (host port)
  "Open a network connection to HOST on PORT."
  (interactive "sHost: \nnPort: ")
  (network-service-connection host (number-to-string port)))

(defun network-service-connection (host service)
  "Open a network connection to SERVICE on HOST."
  (require 'comint)
880
  (let* (
Gerd Moellmann's avatar
Gerd Moellmann committed
881 882
	(process-name (concat "Network Connection [" host " " service "]"))
	(portnum (string-to-number service))
883
	(buf (get-buffer-create (concat "*" process-name "*")))
Gerd Moellmann's avatar
Gerd Moellmann committed
884 885
	)
    (or (zerop portnum) (setq service portnum))
886
    (make-comint
Gerd Moellmann's avatar
Gerd Moellmann committed
887 888
     process-name
     (cons host service))
889 890 891 892
    (set-buffer buf)
    (network-connection-mode)
    (network-connection-mode-setup host service)
    (pop-to-buffer buf)
Gerd Moellmann's avatar
Gerd Moellmann committed
893 894
    ))

895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
(defun network-connection-reconnect  ()
  "Reconnect a network connection, preserving the old input ring."
  (interactive)
  (let ((proc (get-buffer-process (current-buffer)))
	(old-comint-input-ring comint-input-ring)
	(host network-connection-host)
	(service network-connection-service)
	)
    (if (not (or (not proc)
		 (eq (process-status proc) 'closed)))
	(message "Still connected")
      (goto-char (point-max))
      (insert (format "Reopening connection to %s\n" host))
      (network-connection host
       (if (numberp service)
	   service
	 (cdr (assoc service network-connection-service-alist))))
      (and old-comint-input-ring
	   (setq comint-input-ring old-comint-input-ring))
      )))

Gerd Moellmann's avatar
Gerd Moellmann committed
916 917 918
(provide 'net-utils)

;;; net-utils.el ends here