;
;;; Gnus Profile
;;;


;;;;(load-library "mail-lib")
(global-set-key [f11] 'mime-insert-encoded-file)
(global-set-key [f16] 'mml-attach-file)

(require 'bbdb)
(bbdb-initialize 'message 'sc)
      (add-hook 'gnus-startup-hook 'bbdb-insinuate-message 'bbdb-insinuate-sc)

;      (add-hook 'gnus-startup-hook 'bbdb-insinuate-gnus)
;	(bbdb-insinuate-gnus)
;	(bbdb-insinuate-message)
;	(bbdb-insinuate-sc)

;; Periodically connect to server
(defun my-gnus-demon-get-mail ()
  (save-excursion
    (set-buffer gnus-group-buffer)
    (gnus-group-get-new-news))
    (message "Get mail by demon, time %s" (current-time-string)))

(add-hook 'gnus-started-hook
          (lambda () (gnus-demon-add-handler 'my-gnus-demon-get-mail 5 t)))


;; GPG
(require 'pgg)

;; To be called from gnudoit
(defun MyGNUS ()
  (deiconify-frame)
  (gnus))

(defun toolbar-news ()
  "Run Gnus in the frame it was started from."
  (interactive)
  (gnus))

(setq kill-whole-line t
      default-major-mode 'text-mode
      gnus-read-active-file nil
      gnus-auto-select-next nil
      gnus-check-new-newsgroups 'ask-server
      delete-key-deletes-forward t
      options-save-faces t
      next-line-add-newlines nil
      passwd-invert-frame-when-keyboard-grabbed nil
      scroll-step 1
      european-calendar-style t)

(add-hook 'gnus-article-display-hook
          '(lambda ()
           (add-spec-list-to-specifier
           (face-property 'default 'font)
           '((global (nil . "-*-courier-medium-r-*-*-15-*-*-*-*-*-koi8-*"))))
            (gnus-article-highlight)
            (gnus-article-hide-headers-if-wanted)
            (article-emphasize)))

(defcustom gnus-visible-headers
  "^From:\\|^Subject:\\|^X-Comment-To:\\|^Comment-To:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^To:\\|^[BGF]?Cc:"
  "*All headers that do not match this regexp will be hidden.
This variable can also be a list of regexp of headers to remain visible.
If this variable is non-nil, `gnus-ignored-headers' will be ignored."
  :type '(repeat :value-to-internal (lambda (widget value)
				      (custom-split-regexp-maybe value))
		 :match (lambda (widget value)
			  (or (stringp value)
			      (widget-editable-list-match widget value)))
		 regexp)
  :group 'gnus-article-hiding)

(defcustom gnus-sorted-header-list
  '("^Date:" "^From:" "X-Comment-To:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
    "^Followup-To:" "^To:" "^Cc:" "^Organization:")
  "*This variable is a list of regular expressions.
If it is non-nil, headers that match the regular expressions will
be placed first in the article buffer in the sequence specified by
this list."
  :type '(repeat regexp)
  :group 'gnus-article-hiding)

;;;
;(setq nntp-authinfo-function t)
(setq gnus-check-bogus-newsgroups t)
(setq gnus-large-newsgroup 1000)
(setq message-insert-canlock nil)
;(add-hook 'nntp-prepare-post-hook 'canlock-insert-header)
(setq gnus-extra-headers '(X-Comment-To)
      nnmail-extra-headers gnus-extra-headers)

;;
;; Gnus use ``XOVER'' command for news-server (INN supported this)
;;

(setq gnus-nov-is-evil nil)
;;;;;;;; ^^^^^^^^gnus-inhibit-startup-message t

;;; keywords related to ddt.demos.su registration. Modified, of course. =)

(local-set-key "a" 'message-news)

    (setq gnus-posting-styles
       '((".*"  ;;default
          (name "Olli Artemjev")
          (keywords "1234545942")
          (address "olli@digger.org.ru"))
         ((message-news-p)  ;;Usenet news?
          (name "Olli Artemjev")
          (address "olli@digger.org.ru")
          (keywords "1234545942"))
         ((message-mail-p)  ;;mail?
          (name "Olli Artemjev")
          (address "olli@digger.org.ru")
          (keywords "whocare4mail"))
))


;;

;;;;;;;;;;;;;;;;;;; Вставляем .signature, формируем X-Comment-To, формируем квотинг
;;

(load-library "supercite")
(setq sc-preferred-header-style 0
      sc-rewrite-header-list '((my-header-line-generator))
      sc-citation-leader " "
      sc-confirm-always-p nil
      sc-preferred-attribution-list 
      '( "x-attribution" "initials" "firstname" "sc-lastchoice" )
      )

(defun my-header-line-generator ()
  (let ((sc-mumble "")
	(whofrom (sc-whofrom)))
    (if whofrom
	(insert 
	        (concat "\n\t\tДоброго времени суток, " (sc-mail-field "sc-firstname") "?\n\n")
                (sc-hdr "On " (format-time-string "%d %b %Y at %R" 
	        (gnus-date-get-time (sc-mail-field "date"))) ", ")
		(sc-hdr "\"" (sc-mail-field "sc-attribution") "\", ")
                (sc-hdr ""   (sc-mail-field "sc-author") " ")
	        "wrote:\n")
      )
    )
  )

(setq sc-preferred-attribution-list
      '( "x-attribution" "initials" "firstname" "sc-lastchoice" ))

;;; sc hack by Bor.
(defun sc-add-citation-level-m ()
  "Add citation level to cited string"
  (interactive)
  (if (looking-at (sc-cite-regexp))
      (if (re-search-forward 
           (concat sc-citation-leader-regexp sc-citation-root-regexp)
           )
          (insert sc-citation-delimiter)
        )
    (sc-cite-line) 
    )
  )

(setq sc-default-cite-frame 
	(quote ((begin (progn (sc-fill-if-different))) 
	    ("^[         ]*$" 
		(if sc-cite-blank-lines-p (sc-cite-line) (sc-fill-if-different ""))) 
	
	(sc-reference-tag-string) 
	(sc-tmp-dumb-regexp (sc-add-citation-level-m)) 
	(sc-tmp-nested-regexp (sc-add-citation-level-m)) 
	(sc-tmp-nonnested-regexp (sc-add-citation-level-m)) 
	(sc-nested-citation-p (sc-add-citation-level-m)) 
	(t (sc-add-citation-level-m)) 
	(end (sc-fill-if-different "")))))

(add-hook 
 'message-setup-hook
 '(lambda ()
    ;; Add X-Comment-To: field. We need From: field from original
    ;; letter. How to get right?
    (if message-reply-buffer
        (let ((cur-buffer (current-buffer)) from-field)
          (save-excursion
            (set-buffer message-reply-buffer)
            (if (string-match "^From:" (buffer-string))
                (progn
                  (setq from-start (+ (match-end 0) 1))
                  (goto-char from-start)
                  (end-of-line)
                  (setq from-field (buffer-substring from-start (point))))))
          (if from-field
              (save-excursion
                (goto-char (point-min))
                (insert "X-Comment-To: " (Make-Comment-To from-field) "\n"))))
      )))

(defun Make-Comment-To (from)
  (if (string-match " *<.*> *" from)
      (concat (substring from 0 (match-beginning 0)) (substring from
                                                                (match-end 0)))
    (if (string-match "\(.*\)" from)
        (substring from (+ (match-beginning 0) 1) (- (match-end 0) 1))
      from
      )
    )
  )

(add-hook 'message-setup-hook
	  '(lambda ()
	    (setq message-signature 'My-Signature)
	     ;; Add X-Comment-To: field. We need From: field from original
	     ;; letter. How to get right? 
	     (if message-reply-buffer
		 (let ((cur-buffer (current-buffer)) from-field)
		   (save-excursion
		     (set-buffer message-reply-buffer)
		     (if (string-match "^From: " (buffer-string))
			 (progn
			   (setq from-start (+ (match-end 0) 1))
			   (goto-char from-start)
			   (end-of-line)
			   (setq from-field (buffer-substring from-start (point))))))
		   ))))


(defun sc-break-cited-line ()
  "Break cited line. Do it correctly."
  (interactive)
  (delete-horizontal-space)
  (newline)
  (forward-line -1)
  (let ((cited (looking-at (sc-cite-regexp))))
    (forward-line)
    (if cited (progn (sc-cite-line) (just-one-space)))))

(defun sc-install-rewrite-header-fn (new-fn)
  "Install new header rewrite function and make it preffered"
  (setq sc-preferred-header-style (length sc-rewrite-header-list))
  (setq sc-rewrite-header-list
        (append sc-rewrite-header-list `((,new-fn)))))

(defun ic-cite-message ()
  (sc-cite-original)
  (local-set-key "\C-m" 'sc-break-cited-line))

(setq message-cite-function 'ic-cite-message)

(setq mail-citation-hook 'ic-cite-message)
;(add-hook 'mail-citation-hook 'ic-cite-message)

(add-hook 'sc-load-hook (lambda ()
                          (sc-install-rewrite-header-fn
                           'sc-header-hello-attributed-writes)
			     '(sc-setup-filladapt)
                          (setq sc-confirm-always-p nil)))

(setq news-reply-header-hook nil)

;;;
;;;;;;;;;;;;;; END ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;

(setq gnus-use-sc t
      gnus-asynchronous nil
      gnus-read-newsrc-file nil
      gnus-save-newsrc-file nil
      nnspool-active-file "/var/lib/news/active"
      nnspool-active-times-file "/var/lib/news/active.times"
      message-signature 'My-Signature
;;; ;      default-mime-charset 'koi8-r
;;; ;      message-default-charset (quote koi8-r)
      mime-setup-use-sc t
;;; ;      mime-editor/set-transfer-level-8bit t
)

(setq rfc2047-encode-message-header t)
(setq message-default-charset 'koi8-r)
(setq rfc2047-header-encoding-alist                                           
      '(("Newsgroups" . B)
        ("From" . B)
        ("X-Comment-To" . B)
        ("Message-ID" . B)
        ("Subject" . B)
        (t . B)))

(setq gnus-summary-show-article-charset-alist
   '((1 . koi8-r)
     (2 . windows-1251)
     (3 . iso-8859-5)
     (4 . cyrillic-alternativnyj)
     (5 . utf-8)))
        
;;; ;(defvar gnus-use-mailcrypt t)

;;; Use GPG 
(load-library "mailcrypt")
(setq mc-encrypt-for-me t)
(mc-setversion "gpg")
(add-hook 'gnus-summary-mode-hook 'mc-install-read-mode)
(add-hook 'message-mode-hook 'mc-install-write-mode)
(add-hook 'news-reply-mode-hook 'mc-install-write-mode)
;;; End Use GPG

;;; ;(require 'gnus-setup)
;;; ;(require 'gnus-charset)

;;; ;(gnus-set-newsgroup-default-charset ".*" "koi8-r")

(add-hook 'gnus-summary-mode-hook
	  '(lambda ()
	     (local-set-key [tab] 'gnus-summary-next-unread-article)))

(setq gnus-treat-display-smileys nil)

(add-hook 'gnus-summary-exit-hook 'gnus-summary-bubble-group)

 (add-hook 'message-mode-hook
	  '(lambda ()
             (setq sc-auto-fill-region-p t)
	     (setq sc-fixup-whitespace-p t)
	     (auto-fill-mode 1)
	     (font-lock-mode 1)
	     (setq fill-column 75)))


;(add-hook 'message-send-hook 'ispell-buffer)
;;;;;;;exp

(defun My-Signature ()
  (save-excursion
    (set-buffer (get-buffer-create "*fortune*"))
    (delete-region (point-min) (point-max))
(call-process "/usr/bin/fortune" nil t nil (concat "/etc/phrases.mail"))
(concat "Bye.Olli.\nmailto: olliOUT@digger.org.ru (remove: OUT)\n*:" (buffer-string))))
    
;;;;;;; end exp

;;;;;;; Gnus Mailgroup setup
;(setq
; gnus-secondary-select-methods '((nnml ""))
; nnmail-split-methods
; '(("ftn" "^From:.*@*.fidonet.org")
;   ("local" "^From:.*@*.digger.org.ru")
;   ("other" "")
;                                   )
;             gnus-asynchronous t
;                                   )
;(setq mail-source-delete-incoming t)

;;;;;; End Mailgroup Gnus Setup
;;;;;; IMAP Conf

;;(setq nnimap-stream 'ssl)
;;(setq imap-use-utf7 nil)
;;(setq nnimap-split-crosspost nil)
;;(setq nnimap-split-inbox '("inbox"))
;;(setq gnus-secondary-select-methods '((nnimap "mail"
;;    (nnimap-authenticator login)))
;;      nnimap-split-rule
;;        '(("INBOX.FTN" "^From:.*@*.fidonet.org")
;;          ("INBOX.LOCAL" "^From:.*@*.digger.org.ru")
;;          ("INBOX.OTHER" "")
;;))

;(setq gnus-outgoing-message-group "nnimap:inbox.Sent")

;;;;;; End IMAP Conf

;;; Kiboze 

(setq gnus-extra-headers
      '(X-Comment-To Newsgroups X-Newsgroups To))

;;; This is carbon copy

;;;; Mail aliases

 (add-hook 'mail-setup-hook
           '(lambda ()
              (substitute-key-definition
                'next-line 'mail-abbrev-next-line
                mail-mode-map global-map)
              (substitute-key-definition
                'end-of-buffer 'mail-abbrev-end-of-buffer
                mail-mode-map global-map)))


(define-mail-alias "peter" "Peter V. Chernikoff <peter@alien.digger.org.ru>")


;; SEE ALSO ~/.mailrc 

;;;; End mail aliases


;;;; The Summary Method

;;;(setq gnus-article-sort-functions
;;;'(gnus-article-sort-by-thread
;;;gnus-article-sort-by-author
;;;gnus-article-sort-by-date))
;;;(setq gnus-thread-sort-functions
;;;'(gnus-thread-sort-by-author
;;;gnus-thread-sort-by-date))

;; Hide threads
;;;(setq gnus-thread-hide-subtree t)
(setq gnus-thread-sort-functions
     '(gnus-thread-sort-by-subject
      (not gnus-thread-sort-by-date)
      gnus-thread-sort-by-total-score))
;;;; End Summary Method

;;;; Scoring

(setq gnus-score-followup-thread t)

(make-face 'tick)
(make-face 'art1)
(make-face 'art2)
(make-face 'art3)
(make-face 'art4)
(make-face 'art5)
(set-face-foreground 'tick "magenta")
(set-face-foreground 'art1 "Blue")
(set-face-foreground 'art2 "Cyan")
(set-face-foreground 'art3 "Yellow")
(set-face-foreground 'art4 "Green")
(set-face-foreground 'art5 "Red")
(setq gnus-summary-highlight (list 
                               '((eq mark gnus-ticked-mark) . tick)
			       '((and (> score 90) (< score 101)) . art1)
			       '((and (> score 100) (< score 201)) . art2)
			       '((and (> score 200) (< score 501)) . art3)
			       '((and (> score 502) (< score 601)) . art4)
			       '((> score 602) . art5)
			       nil
                             ))

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

;;;; End Scoring

;;;; Gnus Topics
(add-hook 'gnus-group-mode-hook 'gnus-topic-mode)

;;;; End Gnus Topics

;;;; Colors

;;;; Don't split large (>> 1000 strings) messages
(setq mime-editor/split-message nil)
;;;; End don't split large (>> 1000 strings) messages

(defcustom gnus-cite-attribution-face 'gnus-cite-attribution-face
  "Face used for attribution lines.
It is merged with the face for the cited text belonging to the attribution."
  :group 'gnus-cite
  :type 'face)

(defface gnus-cite-face-0 '((((class color)
			      (background dark))
			     (:foreground "cyan"))
			    (((class color)
			      (background dark))
			     (:foreground "cyan"))
			    (t
			     (:bold t)))
  "Citation face.")

(defface gnus-cite-face-1 '((((class color)
			      (background dark))
			     (:foreground "cyan"))
			    (((class color)
			      (background dark))
			     (:foreground "cyan"))
			    (t
			     (:bold t)))
  "Citation face.")

(defface gnus-cite-face-2 '((((class color)
			      (background dark))
			     (:foreground "cyan"))
			    (((class color)
			      (background light))
			     (:foreground "cyan"))
			    (t
			     (:bold t)))
  "Citation face.")

(defface gnus-cite-face-3 '((((class color)
			      (background dark))
			     (:foreground "cyan"))
			    (((class color)
			      (background light))
			     (:foreground "cyan"))
			    (t
			     (:bold t)))
  "Citation face.")

(defface gnus-cite-face-4 '((((class color)
			      (background dark))
			     (:foreground "cyan"))
			    (((class color)
			      (background light))
			     (:foreground "cyan"))
			    (t
			     (:bold t)))
  "Citation face.")

(defface gnus-cite-face-5 '((((class color)
			      (background dark))
			     (:foreground "cyan"))
			    (((class color)
			      (background light))
			     (:foreground "cyan"))
			    (t
			     (:bold t)))
  "Citation face.")

(defface gnus-cite-face-6 '((((class color)
			      (background dark))
			     (:foreground "cyan"))
			    (((class color)
			      (background light))
			     (:foreground "cyan"))
			    (t
			     (:bold t)))
  "Citation face.")

(defface gnus-cite-face-7 '((((class color)
			      (background dark))
			     (:foreground "cyan"))
			    (((class color)
			      (background light))
			     (:foreground "cyan"))
			    (t
			     (:bold t)))
  "Citation face.")

(defface gnus-cite-face-8 '((((class color)
			      (background dark))
			     (:foreground "cyan"))
			    (((class color)
			      (background light))
			     (:foreground "cyan"))
			    (t
			     (:bold t)))
  "Citation face.")

(defface gnus-cite-face-9 '((((class color)
			      (background dark))
			     (:foreground "cyan"))
			    (((class color)
			      (background light))
			     (:foreground "cyan"))
			    (t
			     (:bold t)))
  "Citation face.")

(defface gnus-cite-face-10 '((((class color)
			       (background dark))
			      (:foreground "cyan"))
			     (((class color)
			       (background light))
			      (:foreground "cyan"))
			     (t
			      (:bold t)))
  "Citation face.")

(defface gnus-cite-face-11 '((((class color)
			       (background dark))
			      (:foreground "cyan"))
			     (((class color)
			       (background light))
			      (:foreground "cyan"))
			     (t
			      (:bold t)))
  "Citation face.")

(defface gnus-cite-face-12 '((((class color)
			       (background dark))
			      (:foreground "cyan"))
			     (((class color)
			       (background light))
			      (:foreground "cyan"))
			     (t
			      (:bold t)))
  "Citation face.")

(defcustom gnus-signature-face 'gnus-signature-face
  "Face used for highlighting a signature in the article buffer.
Obsolete; use the face `gnus-signature-face' for customizations instead."
  :type 'face
  :group 'gnus-article-highlight
  :group 'gnus-article-signature)

(defface gnus-signature-face
  '((((class color)
      (background dark))
     (:foreground "brightyellow"))
    (((class color)
      (background light))
     (:foreground "brightyellow"))
    (t
     (:bold t)))
"Signature face.")

(defface gnus-header-from-face
  '((((class color)
      (background dark))
     (:foreground "brightwhite"))
    (((class color)
      (background light))
     (:foreground "brightwhite"))
    (t
     (:bold t)))
  "Face used for displaying from headers.")

(defface gnus-header-subject-face
  '((((class color)
      (background dark))
     (:foreground "brightyellow"))
    (((class color)
      (background light))
     (:foreground "brightyellow"))
    (t
     (:bold t)))
  "Face used for displaying subject headers.")

(defface gnus-header-name-face
  '((((class color)
      (background dark))
     (:foreground "red"))
    (((class color)
      (background light))
     (:foreground "red"))
    (t
     (:bold t)))
  "Face used for displaying header names."
  :group 'gnus-article-headers
  :group 'gnus-article-highlight)

;;;; End Colors
;##--> ;; ========================== Mail Scoring =======================================
;##--> ;;(setq gnus-summary-mark-below -1024)
;##--> ;;(setq gnus-adaptive-word-minimum -2048)
;##--> ;;(setq gnus-score-interactive-default-score 256)
;##--> ;;(setq gnus-score-expiry-days 21)
;##--> ;;(setq gnus-decay-scores t)
;##--> ;;(setq gnus-score-decay-constant 32)
;##--> ;;(require 'gnus-sum)
;##--> (setq gnus-use-adaptive-scoring '(word line))
;##--> ;;(setq gnus-use-adaptive-scoring t)
;##--> (setq gnus-default-adaptive-score-alist
;##-->       '((gnus-unread-mark)
;##--> 	(gnus-ticked-mark
;##--> 	 (from 4)
;##--> 	 (followup 3))
;##--> ;;	(gnus-dormant-mark
;##--> ;;	 (from 5))
;##--> ;;;;	 (subject 1024))
;##--> 	(gnus-read-mark
;##--> 	 (from 0)
;##--> 	 (subject 1))
;##--> 	(gnus-killed-mark
;##--> 	 (from 0)
;##--> 	 (subject -3))
;##--> ;;	(gnus-catchup-mark
;##--> ;;	 (from -1)
;##--> ;;	 (subject -4))))
;##--> 	)
;##--> )
;##--> ;;(setq gnus-adaptive-word-length-limit 3)
;##--> 
;##--> (defun gnus-group-in-and-out ()
;##-->   "Enter and exit every group, so that any articles that
;##-->    have been adaptive-scored low enough to have a 'Y' mark 
;##-->    won't be there when I enter manually, plus the article
;##-->    count will reflect the # of articles I actually might
;##-->    want to read."
;##-->   (interactive)
;##-->   (dolist (group gnus-newsrc-alist)  ; loop over all groups
;##-->     (when (and 
;##-->            (gnus-message 7 (gnus-info-group group))
;##--> 	   ;; Only apply to NNTP groups (only ones which do scoring)
;##--> 	   (string-match "^nntp\\+" (gnus-info-group group))
;##--> 	   ;; Is this group really subscribed?
;##--> 	   (< (gnus-group-level (gnus-info-group group)) 5)
;##--> 	   ;; Are there any unread messages?
;##--> 	   (> (gnus-group-unread (gnus-info-group group)) 0)
;##-->            )
;##-->       ;; Enter the group (must create the summary buffer or else
;##-->       ;; on exit the counts aren't updated
;##-->       (gnus-summary-read-group (gnus-info-group group) nil t nil nil)
;##-->       ;; and exit
;##-->       (gnus-summary-exit))))
;##--> 
;##--> ;; =====================  End of Mail Scoring ====================================
