Revert my change to gnus-art.el
[gnus] / lisp / nngateway.el
1 ;;; nngateway.el --- posting news via mail gateways
2
3 ;; Copyright (C) 1996-2015 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news, mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28 (require 'nnoo)
29 (require 'message)
30
31 (nnoo-declare nngateway)
32
33 (defvoo nngateway-address nil
34   "Address of the mail-to-news gateway.")
35
36 (defvoo nngateway-header-transformation 'nngateway-simple-header-transformation
37   "Function to be called to rewrite the news headers into mail headers.
38 It is called narrowed to the headers to be transformed with one
39 parameter -- the gateway address.")
40
41 ;;; Interface functions
42
43 (nnoo-define-basics nngateway)
44
45 (deffoo nngateway-open-server (server &optional defs)
46   (if (nngateway-server-opened server)
47       t
48     (unless (assq 'nngateway-address defs)
49       (setq defs (append defs (list (list 'nngateway-address server)))))
50     (nnoo-change-server 'nngateway server defs)))
51
52 (deffoo nngateway-request-post (&optional server)
53   (when (or (nngateway-server-opened server)
54             (nngateway-open-server server))
55     ;; Rewrite the header.
56     (let ((buf (current-buffer)))
57       (with-temp-buffer
58         (insert-buffer-substring buf)
59         (message-narrow-to-head)
60         (funcall nngateway-header-transformation nngateway-address)
61         (goto-char (point-max))
62         (insert mail-header-separator "\n")
63         (widen)
64         (let (message-required-mail-headers)
65           (funcall (or message-send-mail-real-function
66                        message-send-mail-function)))
67         t))))
68
69 ;;; Internal functions
70
71 (defun nngateway-simple-header-transformation (gateway)
72   "Transform the headers to use GATEWAY."
73   (let ((newsgroups (mail-fetch-field "newsgroups")))
74     (message-remove-header "to")
75     (message-remove-header "cc")
76     (goto-char (point-min))
77     (insert "To: " (nnheader-replace-chars-in-string newsgroups ?. ?-)
78             "@" gateway "\n")))
79
80 (defun nngateway-mail2news-header-transformation (gateway)
81   "Transform the headers for sending to a mail2news gateway."
82   (message-remove-header "to")
83   (message-remove-header "cc")
84   (goto-char (point-min))
85   (insert "To: " gateway "\n"))
86
87 (nnoo-define-skeleton nngateway)
88
89 (provide 'nngateway)
90
91 ;;; nngateway.el ends here