1 ;;; nndejagnus.el --- retrieving articles via DejaNews
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; Note: You need to have `url' and `w3' installed for this
37 (nnoo-declare nndejagnus)
39 (defvoo nndejagnus-address "http://www.dejagnus.com/"
40 "Base URL of the DejaNews search engine.")
42 (defvoo nndejagnus-search nil
43 "Search string to feed to DejaNews.")
45 (defvoo nndejagnus-max-hits 100
46 "Maximum number of hits to display.")
48 ;;; Internal variables
50 (defvoo nndejagnus-articles nil)
51 (defvoo nndejagnus-buffer nil)
52 (defvoo nndejagnus-async-buffer nil)
53 (defvar nndejagnus-callback-function nil)
54 (defvar nndejagnus-to-buffer nil)
55 (defvar nndejagnus-start-point nil)
56 (defvar nndejagnus-inside-change-function nil)
58 ;;; Interface functions
60 (nnoo-define-basics nndejagnus)
62 (deffoo nndejagnus-retrieve-headers (articles &optional group server fetch-old)
63 (nndejagnus-possibly-change-server server)
65 (set-buffer nntp-server-buffer)
68 (while (setq article (pop articles))
69 (when (setq header (cadr (assq article nndejagnus-articles)))
70 (nnheader-insert-nov header)))
73 (deffoo nndejagnus-request-group (group &optional server dont-check)
74 (nndejagnus-possibly-change-server server)
75 (when (or (not dont-check)
76 (not nndejagnus-articles))
77 (nndejagnus-create-mapping group))
79 ((not nndejagnus-articles)
80 (nnheader-report 'nndejagnus "Couldn't request search"))
82 (nnheader-report 'nndejagnus "Opened group %s" group)
84 "211 %d %d %d %s\n" (length nndejagnus-articles)
85 (caar nndejagnus-articles) (caar (last nndejagnus-articles))
88 (deffoo nndejagnus-request-article (article &optional group server buffer)
89 (nndejagnus-possibly-change-server server)
91 (set-buffer (or buffer nntp-server-buffer))
92 (let ((url (caddr (assq article nndejagnus-articles))))
94 (nndejagnus-fetch-url url))
95 (unless nnheader-callback-function
96 (nndejagnus-decode-article)
97 (nndejagnus-decode-entities))
98 (nnheader-report 'nndejagnus "Fetched article %s" article)
101 (deffoo nndejagnus-close-server (&optional server)
102 (when (nndejagnus-server-opened server)
103 (gnus-kill-buffer nndejagnus-buffer))
104 (nnoo-close-server 'nndejagnus server))
106 (deffoo nndejagnus-request-update-info (group info &optional server)
107 (nndejagnus-possibly-change-server server)
108 (setcar (cddr info) nil))
110 (deffoo nndejagnus-asynchronous-p ()
113 (nnoo-define-skeleton nndejagnus)
115 ;;; Internal functions
117 (defun nndejagnus-possibly-change-server (&optional server)
118 (nndejagnus-init server)
120 (unless (nndejagnus-server-opened server)
121 (nndejagnus-open-server server))))
123 (defun nndejagnus-init (server)
124 "Initialize buffers and such."
125 (unless (gnus-buffer-live-p nndejagnus-buffer)
126 (setq nndejagnus-buffer
128 (nnheader-set-temp-buffer
129 (format " *nndejagnus %s*" server))))))
131 (defun nndejagnus-create-mapping (group)
132 "Perform the search and create an number-to-url alist."
134 (set-buffer nndejagnus-buffer)
136 (when (nndejagnus-fetch-search nndejagnus-search)
139 Subject Score Date Newsgroup Author
142 ;; Go through all the article hits on this page.
143 (goto-char (point-min))
144 (nndejagnus-decode-entities)
145 (goto-char (point-min))
146 (while (re-search-forward "^ +[0-9]+\\." nil t)
149 (if (re-search-forward "^ +[0-9]+\\." nil t)
152 (goto-char (point-min))
153 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
154 (setq url (match-string 1)))
155 (while (re-search-forward "<[^>]+>" nil t)
156 (replace-match "" t t))
157 (goto-char (point-min))
158 (while (search-forward "\t" nil t)
160 (goto-char (point-min))
161 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
162 (set (intern (match-string 1)) (match-string 2)))
164 (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
165 (setq Subject (substring Subject 0 (match-beginning 0))))
169 (make-full-mail-header
170 i (concat "(" Newsgroup ") " Subject) Author Date
171 (concat "<" (message-unique-id) "-" (int-to-string i)
173 nil 0 (string-to-int Score) nil)
176 ;; See whether there is a "Get next 20 hits" button here.
177 (if (or (not (re-search-forward
178 "HREF=\"\\([^\"]+\\)\">Get next" nil t))
179 (> i nndejagnus-max-hits))
182 (setq more (match-string 1))
184 (url-insert-file-contents more)))
185 ;; Return the articles in the right order.
186 (setq nndejagnus-articles (nreverse map))))))
188 (defun nndejagnus-fetch-url (url)
190 (if (not nnheader-callback-function)
191 (let ((buf (current-buffer)))
193 (set-buffer nndejagnus-buffer)
196 (url-insert-file-contents url)
197 (copy-to-buffer buf (point-min) (point-max)))))
198 (nndejagnus-url-retrieve-asynch
199 url 'nndejanews-callback (current-buffer) nnheader-callback-function)
202 (defun nndejanews-callback (buffer callback)
204 (set-buffer url-working-buffer)
205 (nndejagnus-decode-article)
206 (nndejagnus-decode-entities)
208 (goto-char (point-max))
209 (insert-buffer-substring url-working-buffer))
211 (gnus-kill-buffer url-working-buffer))
213 (defun nndejagnus-url-retrieve-asynch (url callback &rest data)
214 (let ((url-request-method "GET")
215 (old-asynch url-be-asynchronous)
216 (url-request-data nil)
217 (url-request-extra-headers nil)
218 (url-working-buffer (generate-new-buffer-name " *dejanews*")))
219 (setq-default url-be-asynchronous t)
221 (set-buffer (get-buffer-create url-working-buffer))
222 (setq url-current-callback-data data
223 url-be-asynchronous t
224 url-current-callback-func callback)
226 (setq-default url-be-asynchronous old-asynch)))
228 (defun nndejagnus-decode-article ()
229 (goto-char (point-min))
230 (re-search-forward "<PRE>" nil t)
231 (delete-region (point-min) (point))
232 (re-search-forward "</PRE>" nil t)
233 (delete-region (point) (point-max))
234 (goto-char (point-min))
235 (while (re-search-forward "<[^>]+>" nil t)
236 (replace-match "" t t))
237 (goto-char (point-min))
238 (while (looking-at " *$")
240 (while (looking-at "\\(^[^ ]+:\\) *")
241 (replace-match "\\1 " t)
243 (when (re-search-forward "\n\n+" nil t)
244 (replace-match "\n" t t)))
246 (defun nndejagnus-encode-www-form-urlencoded (pairs)
247 "Return PAIRS encoded for forms."
251 (concat (w3-form-encode-xwfu (car data)) "="
252 (w3-form-encode-xwfu (cdr data))))) pairs "&"))
254 (defun nndejagnus-fetch-form (url pairs)
255 (let ((url-request-data (nndejagnus-encode-www-form-urlencoded pairs))
256 (url-request-method 'POST)
257 (url-request-extra-headers
258 '(("Content-type" . "application/x-www-form-urlencoded"))))
259 (url-insert-file-contents url)))
261 (defun nndejagnus-fetch-search (search)
262 (nndejagnus-fetch-form
263 "http://xp6.dejanews.com/dnquery.xp"
264 `(("query" . ,search)
265 ("defaultOp" . "AND")
266 ("svcclass" . "dncurrent")
268 ("format" . "verbose")
270 ("showsort" . "score")
272 ("ageweight" . "1"))))
274 (defun nndejagnus-decode-entities ()
275 (goto-char (point-min))
276 (while (re-search-forward "&\\([a-z]+\\);" nil t)
277 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
282 ; "^ +\\([0-9]+\\)\\. +\\([0-9]+\\)/+\\([0-9]+\\)/+\\([0-9]+\\) +\\([0-9]+\\).+HREF=\"\\([^\"]+\\)\">\\([^<]+\\)<.*<B> *\\(.*\\)</B>.+>\\([^<>]+\\)</A> *$"
284 (provide 'nndejagnus)
286 ;;; nndejagnus.el ends here