;; Timestamps
(setq erc-insert-away-timestamp-function #'erc-insert-timestamp-left)
(setq erc-hide-timestamps nil)
(setq erc-timestamp-only-if-changed-flag nil)

(setq erc-accidental-paste-threshold-seconds 0.5)
(setq erc-autojoin-mode t)
(setq erc-autojoin-timing (quote ident))
(setq erc-email-userid user-mail-address)
(setq erc-flood-protect t)
(setq erc-join-buffer (quote bury))
(setq erc-query-display 'bury)
(setq erc-auto-query 'bury)
(setq erc-kill-buffer-on-part nil)
(setq erc-kill-server-buffer-on-quit nil)
(setq erc-networks-alist nil)
(setq erc-nick-uniquifier nil)
(setq erc-server-auto-reconnect t)
(setq erc-server-reconnect-attempts t)
(setq erc-server-reconnect-timeout 60)
(setq erc-track-position-in-mode-line (quote t))
(setq erc-track-priority-faces-only (quote all))
(setq erc-track-switch-direction (quote importantce))
(setq erc-try-new-nick-p nil)
(setq erc-user-full-name "Oleg Pykhalov")
(setq erc-whowas-on-nosuchnick t)
(setq erc-track-exclude-types
      '("JOIN" "QUIT" "PART" "NICK" "333" "353"))
(setq erc-hide-list '("JOIN" "PART" "QUIT"))
(setq erc-modules
      '(autojoin button completion fill irccontrols list match menu notifications
        move-to-prompt netsplit networks ring smiley stamp track))

(setq erc-fill-function 'erc-fill-variable)
(setq erc-fill-prefix " ")
(setq erc-fill-column 80)

(defun wi-erc-connect-localhost ()
  "Connect to localhost irc network."
  (interactive)
  (erc :server "localhost"
       :port 6667
       :nick "natsu"
       :password nil))

(defun wi-erc-connect-twitch ()
  "Connect to twitch irc network."
  (interactive)
  (add-to-list 'erc-networks-alist '(twitch "irc.chat.twitch.tv"))
  (erc-tls :server "irc.chat.twitch.tv"
           :port 6697
           :nick "wigust"
           :password nil))

(defun wi-erc-connect-globalgamers ()
  "Connect to globalgamers irc network."
  (interactive)
  (add-to-list 'erc-networks-alist
               '(globalgamers "irc.globalgamers.net"))
  (erc-tls :server "irc.globalgamers.net"
           :port 6660
           :nick "wigust"
           :password nil))

(defun wi-erc-connect-indymedia ()
  "Connect to indymedia irc network."
  (interactive)
  (add-to-list 'erc-networks-alist '(indymedia "irc.indymedia.org"))
  (erc-tls :server "irc.indymedia.org"
           :port 6697
           :nick "wigust"
           :password nil))

(defun wi-erc-connect-gitter ()
  "Connect to gitter irc network."
  (interactive)
  (add-to-list 'erc-networks-alist '(gitter "irc.gitter.im"))
  (erc-tls :server "irc.gitter.im"
           :port 6697
           :nick "wigust"
           :password nil))

(defun wi-erc-connect-gnome ()
  "Connect to gnome irc network."
  (interactive)
  (erc-tls :server "irc.gnome.org"
           :port 6697
           :nick "wigust"))

(defun wi-erc-connect-freenode ()
  "Connect to freenode irc network."
  (interactive)
  (erc-tls :server "irc.freenode.net"
           :port 6697
           :nick "wigust"
           :password nil))

(defun wi-erc-connect-debian ()
  "Connect to debian irc network."
  (interactive)
  (erc-tls :server "irc.oftc.net"
           :port 6697
           :nick "wigust"))

(defun wi-erc-connect-rizon ()
  "Connect to highway irc network."
  (interactive)
  (erc-tls :server "irc.rizon.net"
           :port 6697
           :nick "wigust"))

(defun wi-erc-connect-highway ()
  "Connect to highway irc network."
  (interactive)
  (erc-tls :server "irc.irchighway.net"
           :port 6697
           :nick "wigust"))

(defun wi-erc-connect-all ()
  "Connect to all configured irc networks."
  (interactive)
  (wi-erc-connect-localhost) (wi-erc-connect-debian)
  (wi-erc-connect-freenode) (wi-erc-connect-gnome)
  (wi-erc-connect-gitter) (wi-erc-connect-twitch)
  (wi-erc-connect-rizon) (wi-erc-connect-globalgamers)
  ;; (wi-erc-connect-highway) ; No autojoin channels
  (wi-erc-connect-indymedia))

(defvar wi-irc-gnome-servers '("umu.se" "gimp.net" "gimp.ca"
                               "gnome.org" "y.se" "poop.nl"))

(defvar wi-irc-gnome-channels
  '("#bugs" "#docs" "#gnome" "#gnome-hackers" "#gnome-shell"
    "#newcomers"))

(defun wi-erc-netlist (irc-networks irc-channels)
  "A machinery to create list of IRC-NETWORKS and IRC-CHANNELS."
  (let (wi-erc-netlist)
    (dolist (irc-network irc-networks wi-erc-netlist)
      (if (equal wi-erc-netlist nil)
          (setq wi-erc-netlist
                (list (cons irc-network irc-channels)))
        (setq wi-erc-netlist
              (append wi-erc-netlist
                      (list (cons irc-network irc-channels))))))))

(defvar wi-erc-netlist-gnome (wi-erc-netlist wi-irc-gnome-servers
                                             wi-irc-gnome-channels))

(setq erc-autojoin-channels-alist
      (quote
       (;; ("freenode.net"
        ;;  ;; "#icecat" "#emacs" "#grub" "#conkeror" "#erc"
        ;;  ;; "#clojure" "##math"
        ;;  ;; "##c" "#gdb" "#bash" "#SDL" "#chicken"
        ;;  ;; "#fedora" "#fedora-admin" "#fedora-devel"
        ;;  ;; "#fedora-noc" "#fedora-meeting" "#fedora-qa"
        ;;  ;; "#gnu" "#fsf" "#gnus"
        ;;  "#guile" "#guix" "#lisp" "#scheme"
        ;;  "#stumpwm" "#bootstrappable"
        ;;  ;; "#replicant"
        ;;  ;; "##linux" "#linuxdistrocommunity"
        ;;  ;; "#nixos" "#haskell" "#xmonad"
        ;;  ;; "#filmsbykris" "##japanese" "#latex"
        ;;  ;; "#python" "#scipy" "#sagemath"
        ;;  )
        ;; ("indymedia.org" "#riseup")
        ;; ("gitter.im")
        ;; ("oftc.net" "#debian" "#debian-next")
        ;; ("globalgamers" "#Touhou")
        ;; ("twitch.tv" "#tsoding" "#cattzs" "#retched"
        ;;  "#bbsssssssss" "#team_treehouse" "#rw_grim")
        ;; ("uworld.se" "#coalgirls")
        )))

(defun erc-netlist (wi-erc-netlist)
  (dolist (irc-net wi-erc-netlist)
    (append erc-autojoin-channels-alist irc-net)))

(setq erc-autojoin-channels-alist
      (append erc-autojoin-channels-alist wi-erc-netlist-gnome))


;;;
;;; ZNC integration
;;;

;; Based on https://raw.githubusercontent.com/vincentbernat/dot.emacs/master/znc.conf.el

(defun znc-setup (server port user networks)
  "Add a server to the list of ZNC servers.

We use SSL inconditionaly. Moreover, we don't store the password
but put nil instead. At least, we tweak the username to contain
the network name later, this will be separated again."
  (setq znc-servers
        (list (list server port
                    nil ;; SSL enabled
                    (mapcar (function (lambda (slug)
                                        (list slug
                                              (format "%s/%s" user slug)
                                              nil)))
                            networks)))))

(defun vbe:znc-erc-connector (&rest R)
  "Connect to ERC using and retrieve password with `auth-source-search'.

Moreover, handle multiple networks by sending the password with
the appropriate network slug that we extract from the nick."
  (let* ((user (nth 0 (split-string (plist-get R :nick) "/")))
         (slug (nth 1 (split-string (plist-get R :nick) "/")))
         (port (plist-get R :port))
         (found (nth 0 (auth-source-search :host (plist-get R :server)
                                           :port port
                                           :user user
                                           :require '(:user :secret)
                                           :max 1))))
    (if found
        (let ((password (let ((secret (plist-get found :secret)))
                          (if (functionp secret)
                              (funcall secret)
                            secret))))
          (plist-put R :password (format "%s/%s:%s" user slug password))
          (plist-put R :nick user)
          (apply 'erc R)))))

(setq znc-erc-connector 'vbe:znc-erc-connector)

;; ;; Define networks
(znc-setup "localhost" 8060 "wigust" '(freenode))
