(defun my-async-short-unread-p (data)
  "Return non-nil for short, unread articles."
  (and (gnus-data-unread-p data)
       (< (mail-header-lines (gnus-data-header data))
          100)))

(defmacro my-gnus-sources (ip iface &rest body)
  "If `iface' has `ip', then eval code"
  `(if (string-equal ,ip (notebook-location ,iface))
       ,@body))

(setq gnus-default-charset 'utf-8)
(setq gnus-select-method '(nntp "5070.info"))
(add-to-list 'gnus-secondary-select-methods
             '(nnml ""))

;; Beautify gnus groups buffer
(cond (window-system
       (setq custom-background-mode 'light)
       (defface swarm-group-face
         '((t (:foreground "Red" :bold t))) 
         "Swarm")
       (defface my-group-face-2
         '((t (:foreground "DarkSeaGreen4" :normal t)))
         "Second group face")
       (defface my-group-face-3
         '((t (:foreground "Blue" :bold t))) 
         "Third group face")
       (defface read-group-face
         '((t (:foreground "SteelBlue" :normal t))) 
         "All read")
       (defface unread-group-face
         '((t (:foreground "Green" :bold t))) 
         "Unread")))

(setq gnus-group-highlight
      '(((> unread 200) . swarm-group-face)
        ((and (< level 3) (zerop unread)) . my-group-face-2)
        ((< level 3) . my-group-face-3)
        ((zerop unread) . read-group-face)
        (t . unread-group-face)))

;; Beautify article buffer
(setq gnus-visible-headers
      (message (concat
                "^" (regexp-opt '("From" "To" "Subject"
                                  "Date" "Reply-To" "X-Mailer"
                                  "Content-Type" "X-Return-Path"
                                  "X-Confirm-Reading-To"))
                ":")))

(setq gnus-signature-separator
      '("^-- $"
        "^-- *$"
        "^-------*$"
        "^ *-----*$"
        "^____*$"
        "^====*$")
      mail-signature t
      message-signature t
      message-signature-file "~/.signature"
      message-from-style 'angles)

;; Set group in which sent mail is moved
(setq gnus-outgoing-message-group "nnml:sent")

(setq gnus-fetch-old-headers nil
      mail-source-delete-incoming t
      gnus-asynchronous t)

;;(setq gnus-async-prefetch-article-p 'my-async-short-unread-p)
(setq gnus-async-prefetch-article-p
      '(lambda (data)
         (and (gnus-data-unread-p data)
              (< (mail-header-lines (gnus-data-header data))
                 100))))

;; Sending mail via smtp
(require 'smtpmail)
(load-library "smtpmail")
(setq send-mail-function 'smtpmail-send-it
      message-send-mail-function 'smtpmail-send-it
      user-mail-address "user"
      smtpmail-default-smtp-server "smtp.5070.info"
      smtpmail-smtp-server "smtp.5070.info"
      user-full-name "Stanislav Ivankin"
      smtpmail-sendto-domain "5070.info"
      smtpmail-smtp-service 25
      smtpmail-debug-info t
      smtpmail-auth-credentials '(("smtp.5070.info" 25 "user" "password")))

;; Automatic word wrap
(add-hook 'message-mode-hook     
    (lambda ()
          (setq fill-column 72) 
          (turn-on-auto-fill)))

(setq gnus-treat-display-x-face 'head)

;; Posting style
(setq gnus-posting-styles
      '((".*"
         (name "Stanislav M. Ivankin")
         (address "stas@5070.info")
         ("Face"
"iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAD1BMVEUjFQtkTzlMNiTy7+Se
 hmOMtl6kAAACRElEQVQ4jV2T0ZXrIAxEQesCEFYBSKGAzZoCAlb/Nb0BO7s5zz8JXEZIIxGOv699
 /D/Cx3Z7/A/W0XY2Oc75+wfWsteejx0rP9/gnMB751wdy+f+BojUWnd3i12wlvMCbQJ3EeeQGZs/
 +w3OFUjFfQSF5Jsu4PvRegk5urMqJC87F9Cvo+dQgraeNet+DLmAfLUcKARz4ahRDq/7BM32Zwn4
 yDlHVW0tXkDPgUDQnKVQKgB0KyRNQQw1lZTS1lp6LFD63C9UtlJTTPv5LJciGI6XEtKW6oiKFIJM
 cN3MVnrNMhA3WuAFKvaNY2Z9ebaMRfAFFBeooEYpggqnXt+hLLuXIUPNC9ILdN4KyrD20C7eKQzk
 cgEcqu71sGAzEVS17StUIsPRrSlx4TBdiI8JKFFALx5taE4oFCQuRYC3ExzGDHEgOC0LGBGa9zhG
 10SFDOEc4Ccwqbe2H8KeKAVlukEs6KpLc4H/BS68ARVbQFqgmDRzXGCEmNi9A3QqUhh9hIsTUKp+
 QhFfph6ZR4RZE4S8IZS3VCAkgWV0A7sAxtFn1JfaDSi6uJzP2nymcYMXAHGW7qF61wlE+QIhYw49
 AwyZ4URvxXRaMQO1F/jv2X8BkeW8JV2VurrKDVAREw3OfoOpWGNYtl6SsSywfYAUx7DZeXwdoL7B
 BmTGS9DrLyCLeFC5b/OFIuXONzArxrh69cWHX2A+DTb4yhVTsMq4wJxINjXearICO1E/6wWMuyVk
 lDDPxm8wRxddm888YYy6ZAA7/gGUGJ0QXDwMsAAAAABJRU5ErkJggg==")
;;       (gnus-convert-png-to-face
;;        "~/emacs/etc/face.png")
         ("Reply-To" "stas@5070.info"))))

;; Message headers
(setq message-generate-headers-first t
      gnus-uu-digest-headers nil)

;; Warn if reply
(setq gnus-confirm-mail-reply-to-news t)

;; Message headers format
(setq gnus-group-line-format "%5R%4y%m : %G\n"
      gnus-summary-line-format "%R %d %-7,7k | %-15,15n | %B%S\n")

(setq gnus-thread-sort-functions
      '(gnus-thread-sort-by-number
        gnus-thread-sort-by-date
        gnus-thread-sort-by-total-score))

;; Nice treelook
(when window-system
  (setq gnus-sum-thread-tree-indent "  "
        gnus-sum-thread-tree-root "● "
        gnus-sum-thread-tree-false-root "◯ "
        gnus-sum-thread-tree-single-indent "◎ "
        gnus-sum-thread-tree-leaf-with-other "├─► "
        gnus-sum-thread-tree-vertical "│"
        gnus-sum-thread-tree-single-leaf "╰─► "))

;; Lines will be colored
(add-hook 'gnus-summary-mode-hook 'turn-on-stripes-mode)
;; Me english bad. Me need flyspell. Yes. :)
(add-hook 'message-mode-hook (lambda () (flyspell-mode 1)))     

(add-hook 'gnus-select-group-hook
          (lambda ()
            (cond
             ((string-match
               "^ru\\." (gnus-group-real-name gnus-newsgroup-name))
              (ispell-change-dictionary "russian"))
             (t
              (ispell-change-dictionary "english")))))

;;;;;;;;;;;;;;;;;;;

(setq mail-sources nil)
(setq gnus-nntp-server nil
      gnus-read-active-file nil
      gnus-save-newsrc-file nil
      gnus-read-newsrc-file nil
      gnus-check-new-newsgroups nil)

(setq user-mail-address "stas@concat.info")
(setq gnus-ignored-from-addresses "stas@concat\\.info")

(setq gnus-select-method
      '(nnimap "Maildir"
               (remove-prefix "INBOX.")
               (nnimap-address "localhost")
               (nnimap-stream ssl)
               (nnimap-authinfo-file
                "/home/esgal/.imap-authinfo")
               (nnimap-authenticator login)))

(setq nnimap-split-inbox '("INBOX")
      nnimap-split-predicate "UNDELETED"
      nnimap-split-crosspost nil)
;;      nnimap-split-rule '(("Maildir") ("INBOX" nnimap-split-fancy)))

(setq nnimap-split-rule
      '(("INBOX/UnixTips"    "From:.*@ugu\\.com")
        ("INBOX/LiveJournal" "From:.*livejournal\\.com")
        ("INBOX/EmacsWiki"   "From:.*EmacsWiki: RecentChanges")))