;;;;
;;;;;; Jabber
;;;;

(require 'jabber nil t)
(require 'jabber-autoloads nil t)

(if (boundp 'jabber-account-list)
    (add-to-list 'jabber-account-list `(,(concat "eric@warmenhoven.org/" (mapconcat 'identity (split-string system-name "[-.]") ""))
                                        (:network-server . "talk.google.com")
                                        (:connection-type . ssl))))

;; no debug messages
(setq fsm-debug nil)

;; Don't show the help text at the top by default
(setq jabber-roster-show-bindings nil)

;; Don't show resources
(setq jabber-show-resources nil)

;; Don't show offline contacts by default
(setq jabber-show-offline-contacts nil)

;; Sort by name, not status
(setq jabber-roster-sort-functions '(jabber-roster-sort-by-displayname))

;; expand display slightly
(setq jabber-roster-line-format " %-50n %u %-8s  %S")

;; I don't see a point in this, i can read the buffer name, thank you
(setq jabber-roster-show-title nil)

;; attempt to auto-reconnect
(setq jabber-auto-reconnect t)

;; Send periodic keepalive packets
(add-hook 'jabber-post-connect-hooks 'jabber-keepalive-start)

;; Don't send typing events
(setq jabber-chatstates-confirm nil)

;; Don't fill long lines
(setq jabber-chat-fill-long-lines nil)

;; Spell-check my messages!
(add-hook 'jabber-chat-mode-hook 'flyspell-mode)
(add-hook 'jabber-chat-mode-hook '(lambda () (cd "~/")))

;; Beep on new messages
(add-hook 'jabber-alert-message-hooks 'jabber-message-beep)

;; Don't send a message every time someone changes their status
(setq jabber-alert-presence-message-function 'jabber-presence-only-chat-open-message)
(defadvice jabber-presence-default-message (after eaw-jabber-presence-with-time)
  "Append the time to status messages"
  (setq ad-return-value (concat ad-return-value " at " (current-time-string))))
(ad-activate 'jabber-presence-default-message)

;; Don't allow anonymous authentication
(defadvice jabber-xml-get-children (after eaw-remove-anonymous)
  (setq ad-return-value (remove '(mechanism nil "ANONYMOUS") ad-return-value)))
(ad-activate 'jabber-xml-get-children)

;; Save a log of conversations
(setq jabber-history-enabled t
      jabber-use-global-history t
      jabber-global-history-filename (locate-user-emacs-file "var/jabber.log"))

(setq jabber-vcard-avatars-retrieve nil)

(setq jabber-avatar-cache-directory (locate-user-emacs-file "var/jabber-avatars/"))

(defun eaw-jabber-connected-kill-hook ()
  "Query the user as to whether killing Emacs should be cancelled
when connected to jabber"
  (if (and (boundp '*jabber-connected*) *jabber-connected*)
      (yes-or-no-p
       "You are connected to Jabber, are you sure you want to quit? ")
    t))
(add-to-list 'kill-emacs-query-functions 'eaw-jabber-connected-kill-hook)

(defun eaw-jabber-font-setup ()
  (set-face-attribute 'jabber-roster-user-online nil :foreground 'unspecified)
  (set-face-attribute 'jabber-roster-user-xa nil :slant 'normal :foreground "dark green")
  (set-face-attribute 'jabber-roster-user-dnd nil :slant 'normal :foreground "dark green")
  (set-face-attribute 'jabber-roster-user-away nil :slant 'normal :foreground "green")
  (set-face-attribute 'jabber-roster-user-error nil :slant 'normal)
  (set-face-attribute 'jabber-roster-user-offline nil :slant 'normal :foreground "dark grey")

  (set-face-attribute 'jabber-title-small nil :inherit 'default :width 'normal :foreground "blue" :height 'unspecified)
  (set-face-attribute 'jabber-title-medium nil :inherit 'default :width 'normal :height 'unspecified)
  (set-face-attribute 'jabber-title-large nil :inherit 'default :width 'normal :height 'unspecified))

(add-hook 'jabber-roster-mode-hook 'eaw-jabber-font-setup)

(provide 'ew-jabber)