COCHES DE SEGUNDA MANO
  (require 'erc)
  (require 'erc-stamp)
  (require 'erc-join)
  (require 'erc-chess)
  (require 'erc-nicklist)
  (require 'erc-match)

;; load color theme when ERC start
(color-theme-exal-erc)

(setq erc-fill-column 72)

;(setq erc-prompt "ERC>")
(setq erc-prompt-for-password nil
      erc-prompt-interactive-input t
      erc-hide-timestamps nil)

(add-hook 'erc-insert-modify-hook 'erc-fill)
(add-hook 'erc-send-modify-hook 'erc-fill)

;; Add current time to every new line
(erc-timestamp-mode t)

;; Notify me if someone calls me
(erc-match-mode t)

;; autoquery stuff
(setq erc-auto-query 'window)


;; Some basic settings for erc package
    (setq erc-server "irc.freenode.net"
          erc-port 6667
          erc-nick (capitalize (user-login-name)) 
          erc-user-full-name (concat (user-full-name) " - exal.0x2.org")
          erc-email-userid "root"
          erc-server-coding-system 'utf-8
          erc-current-nick-highlight-type 'nick
          erc-timestamp-only-if-changed-flag nil
          erc-timestamp-format "%H:%M "
          erc-pals '("Cubano" "nanda" "lxo" "delysid" "Exal" "Bernardita" 
                     "vuln" "SLot" "tsdh" "mwolson" "franja")
;         erc-ignore-list '("root")
          erc-current-nick-highlight-type 'nick)

(setq erc-autoaway-idle-seconds 6666
      erc-autoaway-message "I'm gone (autoaway after %i seconds of idletime)"
      erc-auto-set-away t
      erc-button-url-regexp "\\([-a-zA-Z0-9_=!?#$@~`%&*+\\/:;,]+\\.\\)+[-a-zA-Z0-9_=!?#$@~`%&*+\\/:;,]*[-a-zA-Z0-9\\/]")

 (add-hook 'erc-after-connect
               (lambda (server nick)
                 (add-hook 'erc-server-NOTICE-hook 'erc-auto-query)))


;; faith

       (defun erc-cmd-FAITH (&rest ignore)
          "Post faith in the current channel." 
          (interactive)
          (require 'faith)
          (erc-send-message
          (mapconcat 'identity (split-string (faith-quote) "\n") " ")))

;; WTF

(defun erc-cmd-WTF (term &rest ignore)
  "Look up definition for TERM."
  (require 'wtf)
  (let ((def (wtf-is term)))
    (if def
        (erc-send-message
         (concat "Term " (upcase term) " is " def))
      (message (concat "No definition found for " (upcase term))))))


(defun erc-cmd-WTFp (term &rest ignore)
  "Look up definition for TERM for me only, not send to channel."
  (require 'wtf)
  (let ((def (wtf-is term)))
    (if def
        (erc-display-line 
         (erc-make-notice (concat "Term " (upcase term) " is " def) ) 'active)
      (message (concat "No definition found for " (upcase term))))))


;; custom promt
   (setq erc-prompt (lambda ()
     (if (and (boundp 'erc-default-recipients) (erc-default-target))
         (erc-propertize (concat (erc-default-target) ">") 'read-only t 'rear-nonsticky t 'front-nonsticky t)
       (erc-propertize (concat "ERC>") 'read-only t 'rear-nonsticky t 'front-nonsticky t))))


(setq  erc-server-auto-reconnect t
       erc-log-mode nil
       erc-paranoid t
       erc-log-file-coding-system 'utf-8
       erc-log-channels-directory "~/.irclogs")



(erc-autojoin-mode 1)

(setq erc-autojoin-channels-alist
          '(("freenode" . ("#gnu" "#gnu-webmasters" "#fisl" "#emacs-br" "#stumpwm"
                 "#bash" "#lisp-br" "#lisp" "#emacs" "#erc" "#muse" "#debian-br" "#ubuntu-br"
                 "#lidsol" "#gnu-mx" "#linux-bh" "#emacs-es" "#gnus" "#php-es" "#fsfla"))))





(defun exal-erc-mode-stuff ()
  "Set fill column according to `frame-width'."
  (set (make-local-variable 'erc-fill-column) (- (frame-width) 10))
  (set (make-local-variable 'erc-timestamp-right-column)
       (1+ erc-fill-column)))
(add-hook 'erc-mode-hook 'exal-erc-mode-stuff)


(defun exal-identify ()
  "This function is for identify my nick when I'm currently logged."
  (interactive)
  (erc-message "PRIVMSG" 
               (concat "NickServ identify " exal-irc-passwd)))

(defalias 'erc-cmd-IDENTIFY 'exal-identify)


;;; ERC fun stuff


;; current Emms song 
(defun short-music ()
(shell-command-to-string 
 (concat "echo " exal-emms-current "| awk -F/ '{print $6}'" )))

(defun erc-cmd-NP (&rest ignore)
  "Prints the current song playing in Emms."
  (let* ((cur-music (exal-emms-current))
         (short-music (setq bar (file-name-nondirectory (exal-emms-current))))
         (mpg-strings `[,(concat "is playing " short-music)
                        ,(concat "está escutando " short-music)
                        ,(concat "está escuchando " short-music)])

         (text (elt mpg-strings (random (length mpg-strings)))))
    (if (string= "nil: " short-music)
        (erc-send-action (erc-default-target) "is not playing absolutely nothing")
      (erc-send-action (erc-default-target) (substring text 0 -1)))))


(defun exal-emms-np (&rest ignore)
  "Prints the current song playing in Emms, this when I'm on bitlbee."
  (interactive)
  (let* ((cur-music (exal-emms-current))
         (short-music (setq bar (file-name-nondirectory (exal-emms-current))))
         (mpg-strings `[,(concat " is playing " short-music)
                        ,(concat " está escutando " short-music)
                        ,(concat " está escuchando " short-music)])

         (text (elt mpg-strings (random (length mpg-strings)))))
    (if (string= "nil: " short-music)
        (erc-send-message (concat "* " erc-nick " is not playing absolutely nothing"))
      (erc-send-message (concat "* " erc-nick (substring text 0 -1))))))

(defalias 'np 'exal-emms-np)


(defun exal-amarok ()
(shell-command-to-string
                "dcop amarok player nowPlaying"))

(defun erc-cmd-NPA ()
      "Say the current amarok mp3 song title to the current ERC channel"
      (goto-char (point-max))
      (let* ((cur-music (exal-amarok))
    (mpg-strings `[,(concat "is playing " cur-music)
                   ,(concat "está escuchando " cur-music)])
         (text (elt mpg-strings (random (length mpg-strings)))))
    (erc-send-action (erc-default-target) text)
    t))

(defun erc-cmd-THINK (&rest line)
  (let ((text
         (concat ".oO{ "
                 (erc-trim-string (mapconcat 'identity line " "))
                 " }")))
    (erc-send-action (erc-default-target) text)))

(defun exal-match-irc-nick (match-type nick message)
  "Function that match my current ERC nickname and open a visual
   window popup."
  (when (and (eq match-type 'current-nick)
             ;; I don't want to see anything from the erc server
             (not (string-match "\\`\\([sS]erver\\|localhost\\|root\\)" nick))
             ;; or bots
             (not (string-match "\\(^CIA[^!]*\\|bot\\|serv\\)!" nick)))
    (if (executable-find "osd_cat")
        (shell-command-to-string
         (concat "echo -n " nick " | osd_cat --font='-b&h-lucida-medium-r-normal-*-64-*-*-*-p-*-iso10646-1' \
                                    --color=green \
                                    --pos=middle \
                                    --align=center \
                                    --offset=50 \
                                    --indent=50"))
    (shell-command-to-string 
     (concat "kdialog --msgbox \"" "Calling: \n" nick " \"" " ~ 2> /dev/null")))))

    ;(notifications-add (concat "erc: " nick))))

 (defun notifications-add (str)
   (interactive "sNotification: ")
   (start-process "notifications-add" nil
                  "stumpish" "notifications-add" str))

(defun exal-erc-match-irc-nick-sound (strings)
   "Play sound file if STRINGS are matched on a channel."
   (when (and (string-match "[bB]ernardita\\|[eE]xal" strings)
              (not (string-match "\\`\\([sS]erver\\|localhost\\|root\\)" strings))
              (not (string-match "@Exal\\|Connect\\(?:ed\\|ing\\)\\|Logg\\(?:ed\\|ing\\)\\|exal_" strings))
              (not (erc-foolish-content strings))
              (play-sound-file (expand-file-name "bing.wav" exal-emacs-dir)))))


 (defvar erc-foolish-content 
   '("Connected" "Connecting" "Logging" "Logged" "exal_" "@Exal")
   "Words that I don't want to display.")
 
   (defun erc-foolish-content (msg)
      "Check whether MSG is foolish."
      (erc-list-match erc-foolish-content msg))


 (add-hook 'erc-insert-pre-hook
              (lambda (s)
                (when (erc-foolish-content s)
                  (setq erc-insert-this nil))))
      



;;; Bitlbee identify


 (defun exal-bitlbee-identify ()
   "If we're on the bitlbee server, send the identify command to the 
 &bitlbee channel."
   (when (and (string= "localhost" erc-session-server)
              (string= "&bitlbee" (buffer-name)))
     (erc-message "PRIVMSG" (format "%s identify %s" 
                                    (erc-default-target) 
                                    exal-bitlbee-passwd))))





(provide 'exal-erc)

;;; exal-erc.el ends here