;;; $Id: irchat-misc.el,v 1.2 1994/08/01 11:28:29 tmo Exp $
;;;
;;; see file irchat-copyright.el for change log and copyright info

(eval-when-compile
  (require 'irchat-inlines)
  (require 'irchat-globals)
  (require 'irchat-filter))

(defun irchat-own-message (message)
  (let ((obuf (current-buffer)))
    (set-buffer irchat-Dialogue-buffer)
    (let (buffer-read-only)
      (goto-char (point-max))
      (irchat-handle-msg-msg nil message))
    (set-window-point (irchat-get-buffer-window irchat-Dialogue-buffer)
		      (point-max))))


(defun irchat-send (&rest args)
  (let ((item (apply 'format args)) ditem)
    (let ((conv-list irchat-send-convert-list))
      (while conv-list
	(setq item (replace-in-string item (car (car conv-list))
				      (car (cdr (car conv-list)))))
	(setq conv-list (cdr conv-list))))
    (process-send-string irchat-server-process
			 (concat item "\r"))
    (setq ditem (downcase item))
    (if (string-match "^list" (downcase ditem))
	(if (string-match "\\(^list\\) \\(.+\\)" ditem)
	    (setq irchat-channel-filter (matching-substring ditem 2))
	  (setq irchat-channel-filter "")))))    


(defun irchat-clean-hostname (hostname)
  "Return the arg HOSTNAME, but if is a dotted-quad, put brackets around it."
  (let ((data (match-data)))
    (unwind-protect
	(if (string-match "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+" hostname)
	    (concat "[" hostname "]")
	  hostname)
      (store-match-data data))))


(defun replace-in-string (string from to)
  (let ((newstring "")
	(pos 0))
    (while (string-match from string pos)
      (setq newstring (concat newstring
			      (substring string pos (match-beginning 0))
			      to))
      (setq pos (match-end 0)))
    (concat newstring (substring string pos (length string)))))


(defun irchat-scroll-if-visible (window)
  (if window (set-window-point window (point-max))))


(defun irchat-completing-default-read 
  (prompt table predicate require-match initial-input)
  "completing-read w/ default argument like in 'kill-buffer'"
  (let ((default-read
	  (completing-read
	   (if initial-input
	       (format "%s(default %s) "
		       prompt initial-input)
	     (format "%s" prompt))
	   table predicate require-match nil)))
    (if (and (string= default-read "") initial-input)
	initial-input
      default-read)))


(defun irchat-greet-user (user chnl)
  (let ((u (intern user)))
    (beep t)
    (message "IRCHAT: %s has entered! (%s)" user
	     (if (string= chnl "0") "on no channel yet"
	       (concat "on channel " chnl)))
    (if (get u 'irchat-greeting)
	(irchat-send "PRIVMSG %s :%s" user (get u 'irchat-greeting)))
    (put u 'irchat-waited-for nil)
    (put u 'irchat-greeting nil)))


(defun irchat-greet-author ()
  (setq irchat-greet-author nil)
  (irchat-send "PRIVMSG %s :%s <%s@%s> is using irchat version %s"
	       irchat-author-nickname 
	       (user-full-name)
	       (user-login-name) 
	       irchat-system-fqdname
	       irchat-version))


(defun irchat-change-nick-of (old new)
  (let ((pair (assoc old irchat-nick-alist)))
    (if pair
	(rplaca pair new)
      (setq irchat-nick-alist (cons (cons new nil) irchat-nick-alist)))))


(defun irchat-convert-seconds (time)
  "Convert seconds to printable string."
  (let* ((seconds (string-to-int time))
	 (minutes (/ seconds 60))
	 (seconds (if minutes (% seconds 60) seconds))
	 (hours (/ minutes 60))
	 (minutes (if hours (% minutes 60) minutes))
	 (days (/ hours 24))
	 (hours (if days (% hours 24) hours))
	 (ds (and (/= 0 days)
		  (format "%d day%s, " days
			  (if (> days 1) "s" ""))))
	 (hs (and (/= 0 hours)
		  (format "%d hour%s, " hours
			  (if (> hours 1) "s" ""))))
	 (ms (and (/= 0 minutes)
		  (format "%d minute%s " minutes
			  (if (> minutes 1) "s" ""))))
	 (ss (format "%d seconds" seconds)))
    (concat ds hs ms (if seconds ss ""))))


(defun irchat-msg-from-ignored (prefix rest)
  (save-excursion
    (let ((buf (current-buffer)))
      (set-buffer irchat-IGNORED-buffer)
      (goto-char (point-max))
      (insert (format "%s::%s\n" prefix rest))
      (set-buffer buf)
      t)))


;;;
;;; eof
;;;
