;;==============================================================================
;;; ercrc.el
;;
;;; Emacs ERC Configuration
;;
;;; Author: Kyle W T Sherman
;;
;; Time-stamp: <2008-03-20 09:22:21 (kyle)>
;;==============================================================================

(message ";;; ercrc --> Start")

;;==============================================================================
;;; ERC Load
;;==============================================================================

(message ";;; ercrc --> Load")

(require-if-available 'erc)
(when (load "erc" t)
  ;;(load "erc-auto")
  ;;(require 'erc-bbdb)
  ;;(require 'erc-chess)
  (require 'erc-imenu)
  (require 'erc-menu)
  (require 'erc-notify)
  (require 'erc-ring)
  (erc-button-mode t)
  (erc-completion-mode t)
  (erc-fill-mode t)
  (erc-match-mode t)
  (erc-netsplit-mode t)
  (erc-services-mode t)
  (erc-timestamp-mode t)
  (erc-track-mode t)
  (add-hook 'erc-mode-hook 'erc-add-scroll-to-bottom)
  ;; (add-to-list 'erc-nick-popup-alist
  ;;              '("DebianDB" .
  ;;                (shell-command
  ;;                 (format
  ;;                  "ldapsearch -x -P 2 -h db.debian.org -b dc=debian,dc=org ircnick=%s"
  ;;                  nick))))
  )

;;==============================================================================
;;; ERC Customize
;;==============================================================================

(message ";;; ercrc --> Customize")

;; load authentication file
(defvar erc-auth-file-name (expand-file-name "~/.erc-auth"))
(when (file-exists-p erc-auth-file-name)
  (load erc-auth-file-name))

;; erc customizations
(when (load "erc" t)
  ;; create a seperate buffer for private messages
  (setq erc-auto-query t)
  ;; have noticies appear in minibuffer
  (setq erc-echo-notices-in-minibuffer-flag t)
  ;; add users to bbdb
  ;;(setq erc-bbdb-auto-create-on-whois-p t)
  ;; wrap text at edge of window
  (setq erc-fill-column (- (window-width) 2))
  ;; interpret mIRC color codes
  (setq erc-interpret-mirc-color t)
  ;; friends list for notifications
  (setq erc-pals '("jam" "dmitri" "peter" "glenn" "vin"))
  (setq erc-notify-list erc-pals)
  ;; channels to auto-join
  (setq erc-autojoin-channels-alist '((".*freenode\.net" . ("#emacs" "erc" "#lisp" "#olpc"))
                                      (".*dowjones\.net" . ("#dev"))
                                      ("sbkdevtick11" . ("#dev"))
                                      ("localhost" . ("#collab"))))
  ;; modules to load
  (setq erc-modules '(autojoin button completion fill identd irccontrols match menu netsplit noncommands readonly ring scrolltobottom services stamp spelling track))
  ;; C-RET (or C-c RET or C-c C-RET) sends messages, insteasd of RET
  ;; (define-key erc-mode-map (kbd "RET") nil)
  ;; (define-key erc-mode-map (kbd "C-RET") 'erc-send-current-line)
  ;; (define-key erc-mode-map (kbd "C-c RET") 'erc-send-current-line)
  ;; (define-key erc-mode-map (kbd "C-c C-RET") 'erc-send-current-line)
  )

;;==============================================================================
;;; ERC Functions
;;==============================================================================

(message ";;; ercrc --> Functions")

;; determine nick based on machine name
(defun erc-server-name ()
  "Return a nickname based on machine name.
\nDefaults to \"nullman\" if no match is found."
  (let ((server-nick '(("tank.nullware.com" . "nulltank")
                       ("tank" . "nulltank")
                       ("neo.nullware.com" . "nullhome")
                       ("neo" . "nullhome")
                       ("mouse1.nullware.com" . "nullman")
                       ("mouse1" . "nullman")
                       ("min-kyle-linux.production.bigcharts.com" . "nullwork")
                       ("min-kyle-linux" . "nullwork")))
        nick)
    (setq nick (cdr (assoc system-name server-nick)))
    (or nick "nullman")))

;; connect to localhost irc
(defun erc-localhost ()
  "Connect to localhost irc server."
  (interactive)
  (let ((nick (erc-server-name)))
    (erc-services-disable)
    (erc :server "localhost" :port "6667" :nick nick :password nil :full-name "Kyle Sherman")
    ))
(global-set-key "\C-cel" 'erc-localhost)

;; connect to localhost bitlbee
(defun erc-localhost-bitlbee ()
  "Connect to localhost bitlbee server."
  (interactive)
  (let ((nick (erc-server-name)))
    (erc-services-disable)
    (erc :server "localhost" :port "6668" :nick nick :password nil :full-name "Kyle Sherman")
    ))
(global-set-key "\C-ceb" 'erc-localhost-bitlbee)

;; connect to freenode irc
(defun erc-freenode ()
  "Connect to irc.freenode.net irc server."
  (interactive)
  (let ((nick (erc-server-name)))
    (erc-services-enable)
    (erc :server "irc.freenode.net" :port "6667" :nick nick :password nil :full-name "Kyle Sherman")
    ))
(global-set-key "\C-cef" 'erc-freenode)

;; connect to work irc
(defun erc-work ()
  (interactive)
  "Connect to irc.win.dowjones.net irc server."
  (erc-services-disable)
  (erc :server "irc.win.dowjones.net" :port "6667" :nick "kyle" :password nil :full-name "Kyle Sherman")
  )
(global-set-key "\C-cew" 'erc-work)

;; uptime command
(defun erc-cmd-UPTIME (&rest ignore)
  "Display the uptime of the system, as well as some load-related
stuff, to the current ERC buffer."
  (let ((uname-output
         (replace-regexp-in-string
          ", load average: " "] {Load average} ["
          ;; collapse spaces, remove
          (replace-regexp-in-string
           " +" " "
           ;; remove beginning and trailing whitespace
           (replace-regexp-in-string
            "^ +\\|[ \n]+$" ""
            (shell-command-to-string "uptime"))))))
    (erc-send-message
     (concat "{Uptime} [" uname-output "]"))))

(message ";;; ercrc --> End")

;;==============================================================================
;;; End of File
;;==============================================================================