1 ;;; nnweb.el --- retrieving articles via web search engines
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
42 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
43 "Where nnweb will save its files.")
45 (defvoo nnweb-type 'dejanews
46 "What search engine type is being used.")
48 (defvar nnweb-type-definition
50 (article . nnweb-dejanews-wash-article)
51 (map . nnweb-dejanews-create-mapping)
52 (search . nnweb-dejanews-search)
53 (address . "http://xp9.dejanews.com/dnquery.xp"))
55 (article . nnweb-reference-wash-article)
56 (map . nnweb-reference-create-mapping)
57 (search . nnweb-reference-search)
58 (address . "http://www.reference.com/cgi-bin/pn/go"))
60 (article . nnweb-altavista-wash-article)
61 (map . nnweb-altavista-create-mapping)
62 (search . nnweb-altavista-search)
63 (address . "http://www.altavista.digital.com/cgi-bin/query")
64 (id . "/cgi-bin/news?id@%s")))
65 "Type-definition alist.")
67 (defvoo nnweb-search nil
68 "Search string to feed to DejaNews.")
70 (defvoo nnweb-max-hits 100
71 "Maximum number of hits to display.")
73 (defvoo nnweb-ephemeral-p nil
74 "Whether this nnweb server is ephemeral.")
76 ;;; Internal variables
78 (defvoo nnweb-articles nil)
79 (defvoo nnweb-buffer nil)
80 (defvoo nnweb-group-alist nil)
81 (defvoo nnweb-group nil)
82 (defvoo nnweb-hashtb nil)
84 ;;; Interface functions
86 (nnoo-define-basics nnweb)
88 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
89 (nnweb-possibly-change-server group server)
91 (set-buffer nntp-server-buffer)
94 (while (setq article (pop articles))
95 (when (setq header (cadr (assq article nnweb-articles)))
96 (nnheader-insert-nov header)))
99 (deffoo nnweb-request-scan (&optional group server)
100 (nnweb-possibly-change-server group server)
101 (funcall (nnweb-definition 'map))
102 (unless nnweb-ephemeral-p
104 (nnweb-write-overview group)))
106 (deffoo nnweb-request-group (group &optional server dont-check)
107 (nnweb-possibly-change-server nil server)
109 (not (equal group nnweb-group))
110 (not nnweb-ephemeral-p))
111 (let ((info (assoc group nnweb-group-alist)))
112 (setq nnweb-group group)
113 (setq nnweb-type (nth 2 info))
114 (setq nnweb-search (nth 3 info))
116 (nnweb-read-overview group))))
118 ((not nnweb-articles)
119 (nnheader-report 'nnweb "No matching articles"))
121 (let ((active (if nnweb-ephemeral-p
122 (cons (caar nnweb-articles)
123 (caar (last nnweb-articles)))
124 (cadr (assoc group nnweb-group-alist)))))
125 (nnheader-report 'nnweb "Opened group %s" group)
127 "211 %d %d %d %s\n" (length nnweb-articles)
128 (car active) (cdr active) group)))))
130 (deffoo nnweb-close-group (group &optional server)
131 (nnweb-possibly-change-server group server)
132 (when (gnus-buffer-live-p nnweb-buffer)
134 (set-buffer nnweb-buffer)
135 (set-buffer-modified-p nil)
136 (kill-buffer nnweb-buffer)))
139 (deffoo nnweb-request-article (article &optional group server buffer)
140 (nnweb-possibly-change-server group server)
142 (set-buffer (or buffer nntp-server-buffer))
143 (let ((url (caddr (assq article nnweb-articles))))
145 (nnweb-fetch-url url))
146 (and (stringp article)
147 (let ((fetch (nnweb-definition 'id))
149 (when (string-match "^<\\(.*\\)>$" article)
150 (setq art (match-string 1 article)))
154 (format fetch article))))))
155 (unless nnheader-callback-function
156 (funcall (nnweb-definition 'article))
157 (nnweb-decode-entities))
158 (nnheader-report 'nnweb "Fetched article %s" article)
161 (deffoo nnweb-close-server (&optional server)
162 (when (and (nnweb-server-opened server)
163 (gnus-buffer-live-p nnweb-buffer))
165 (set-buffer nnweb-buffer)
166 (set-buffer-modified-p nil)
167 (kill-buffer nnweb-buffer)))
168 (nnoo-close-server 'nnweb server))
170 (deffoo nnweb-request-list (&optional server)
171 (nnweb-possibly-change-server nil server)
173 (set-buffer nntp-server-buffer)
174 (nnmail-generate-active nnweb-group-alist)
177 (deffoo nnweb-request-update-info (group info &optional server)
178 (nnweb-possibly-change-server group server)
179 ;;(setcar (cddr info) nil)
182 (deffoo nnweb-asynchronous-p ()
185 (deffoo nnweb-request-create-group (group &optional server args)
186 (nnweb-possibly-change-server nil server)
187 (nnweb-request-delete-group group)
188 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
192 (deffoo nnweb-request-delete-group (group &optional force server)
193 (nnweb-possibly-change-server group server)
194 (gnus-delete-assoc group nnweb-group-alist)
195 (gnus-delete-file (nnweb-overview-file group))
198 (nnoo-define-skeleton nnweb)
200 ;;; Internal functions
202 (defun nnweb-read-overview (group)
203 "Read the overview of GROUP and build the map."
204 (when (file-exists-p (nnweb-overview-file group))
205 (nnheader-temp-write nil
206 (nnheader-insert-file-contents (nnweb-overview-file group))
207 (goto-char (point-min))
208 (setq nnweb-hashtb (gnus-make-hashtable
209 (count-lines (point-min) (point-max))))
212 (setq header (nnheader-parse-nov))
214 (push (list (mail-header-number header)
215 header (mail-header-xref header))
217 (nnweb-set-hashtb header (car nnweb-articles)))))))
219 (defun nnweb-write-overview (group)
220 "Write the overview file for GROUP."
221 (nnheader-temp-write (nnweb-overview-file group)
222 (let ((articles nnweb-articles))
224 (nnheader-insert-nov (cadr (pop articles)))))))
226 (defun nnweb-set-hashtb (header data)
227 (gnus-sethash (nnweb-identifier (mail-header-xref header))
230 (defun nnweb-get-hashtb (url)
231 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
233 (defun nnweb-identifier (ident)
236 (defun nnweb-overview-file (group)
237 "Return the name of the overview file of GROUP."
238 (nnheader-concat nnweb-directory group ".overview"))
240 (defun nnweb-write-active ()
241 "Save the active file."
242 (nnheader-temp-write (nnheader-concat nnweb-directory "active")
243 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
245 (defun nnweb-read-active ()
246 "Read the active file."
247 (load (nnheader-concat nnweb-directory "active") t t t))
249 (defun nnweb-definition (type)
250 "Return the definition of TYPE."
251 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
253 (error "Undefined definition %s" type))
256 (defun nnweb-possibly-change-server (&optional group server)
259 (unless (nnweb-server-opened server)
260 (nnweb-open-server server)))
262 (when (and (not nnweb-ephemeral-p)
263 (not (equal group nnweb-group)))
264 (nnweb-request-group group nil t))))
266 (defun nnweb-init (server)
267 "Initialize buffers and such."
268 (unless (gnus-buffer-live-p nnweb-buffer)
271 (nnheader-set-temp-buffer
272 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
274 (defun nnweb-fetch-url (url)
276 (if (not nnheader-callback-function)
277 (let ((buf (current-buffer)))
279 (set-buffer nnweb-buffer)
282 (url-insert-file-contents url)
283 (copy-to-buffer buf (point-min) (point-max)))))
284 (nnweb-url-retrieve-asynch
285 url 'nnweb-callback (current-buffer) nnheader-callback-function)
288 (defun nnweb-callback (buffer callback)
289 (when (gnus-buffer-live-p url-working-buffer)
291 (set-buffer url-working-buffer)
292 (funcall (nnweb-definition 'article))
293 (nnweb-decode-entities)
295 (goto-char (point-max))
296 (insert-buffer-substring url-working-buffer))
298 (gnus-kill-buffer url-working-buffer)))
300 (defun nnweb-url-retrieve-asynch (url callback &rest data)
301 (let ((url-request-method "GET")
302 (old-asynch url-be-asynchronous)
303 (url-request-data nil)
304 (url-request-extra-headers nil)
305 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
306 (setq-default url-be-asynchronous t)
308 (set-buffer (get-buffer-create url-working-buffer))
309 (setq url-current-callback-data data
310 url-be-asynchronous t
311 url-current-callback-func callback)
313 (setq-default url-be-asynchronous old-asynch)))
315 (defun nnweb-encode-www-form-urlencoded (pairs)
316 "Return PAIRS encoded for forms."
320 (concat (w3-form-encode-xwfu (car data)) "="
321 (w3-form-encode-xwfu (cdr data)))))
324 (defun nnweb-fetch-form (url pairs)
325 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
326 (url-request-method "POST")
327 (url-request-extra-headers
328 '(("Content-type" . "application/x-www-form-urlencoded"))))
329 (url-insert-file-contents url)
330 (setq buffer-file-name nil))
333 (defun nnweb-decode-entities ()
334 (goto-char (point-min))
335 (while (re-search-forward "&\\([a-z]+\\);" nil t)
336 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
341 (defun nnweb-remove-markup ()
342 (goto-char (point-min))
343 (while (search-forward "<!--" nil t)
344 (delete-region (match-beginning 0)
345 (or (search-forward "-->" nil t)
347 (goto-char (point-min))
348 (while (re-search-forward "<[^>]+>" nil t)
349 (replace-match "" t t)))
352 ;;; DejaNews functions.
355 (defun nnweb-dejanews-create-mapping ()
356 "Perform the search and create an number-to-url alist."
358 (set-buffer nnweb-buffer)
360 (when (funcall (nnweb-definition 'search) nnweb-search)
364 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
366 Subject Score Date Newsgroup Author
369 ;; Go through all the article hits on this page.
370 (goto-char (point-min))
371 (nnweb-decode-entities)
372 (goto-char (point-min))
373 (while (re-search-forward "^ +[0-9]+\\." nil t)
376 (cond ((re-search-forward "^ +[0-9]+\\." nil t)
378 ((search-forward "\n\n" nil t)
382 (goto-char (point-min))
383 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
384 (setq url (match-string 1)))
385 (nnweb-remove-markup)
386 (goto-char (point-min))
387 (while (search-forward "\t" nil t)
389 (goto-char (point-min))
390 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
391 (set (intern (match-string 1)) (match-string 2)))
393 (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
394 (setq Subject (substring Subject 0 (match-beginning 0))))
395 (unless (nnweb-get-hashtb url)
400 (make-full-mail-header
401 (cdr active) (concat "(" Newsgroup ") " Subject) Author Date
402 (concat "<" (message-unique-id) "-" (int-to-string i)
404 nil 0 (string-to-int Score) nil)
407 ;; See whether there is a "Get next 20 hits" button here.
408 (if (or (not (re-search-forward
409 "HREF=\"\\([^\"]+\\)\">Get next" nil t))
410 (>= i nnweb-max-hits))
413 (setq more (match-string 1))
415 (url-insert-file-contents more)))
416 ;; Return the articles in the right order.
418 (sort map (lambda (s1 s2) (< (car s1) (car s2)))))))))
420 (defun nnweb-dejanews-wash-article ()
421 (let ((case-fold-search t))
422 (goto-char (point-min))
423 (re-search-forward "<PRE>" nil t)
424 (delete-region (point-min) (point))
425 (re-search-forward "</PRE>" nil t)
426 (delete-region (point) (point-max))
427 (nnweb-remove-markup)
428 (goto-char (point-min))
429 (while (and (looking-at " *$")
432 (while (looking-at "\\(^[^ ]+:\\) *")
433 (replace-match "\\1 " t)
435 (when (re-search-forward "\n\n+" nil t)
436 (replace-match "\n" t t))))
438 (defun nnweb-dejanews-search (search)
440 (nnweb-definition 'address)
441 `(("query" . ,search)
442 ("defaultOp" . "AND")
443 ("svcclass" . "dncurrent")
445 ("format" . "verbose")
447 ("showsort" . "score")
449 ("ageweight" . "1")))
456 (defun nnweb-reference-create-mapping ()
457 "Perform the search and create an number-to-url alist."
459 (set-buffer nnweb-buffer)
461 (when (funcall (nnweb-definition 'search) nnweb-search)
465 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
467 Subject Score Date Newsgroups From Message-ID
470 ;; Go through all the article hits on this page.
471 (goto-char (point-min))
472 (search-forward "</pre><hr>" nil t)
473 (delete-region (point-min) (point))
474 ;(nnweb-decode-entities)
475 (goto-char (point-min))
476 (while (re-search-forward "^ +[0-9]+\\." nil t)
479 (if (re-search-forward "^$" nil t)
482 (goto-char (point-min))
483 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
484 (setq url (match-string 1)))
485 (nnweb-remove-markup)
486 (goto-char (point-min))
487 (while (search-forward "\t" nil t)
489 (goto-char (point-min))
490 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
491 (set (intern (match-string 1)) (match-string 2)))
493 (search-forward "</pre>" nil t)
494 (unless (nnweb-get-hashtb url)
499 (make-full-mail-header
500 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
502 nil 0 (string-to-int Score) nil)
506 ;; Return the articles in the right order.
508 (sort map (lambda (s1 s2) (< (car s1) (car s2)))))))))
510 (defun nnweb-reference-wash-article ()
511 (let ((case-fold-search t))
512 (goto-char (point-min))
513 (re-search-forward "^</center><hr>" nil t)
514 (delete-region (point-min) (point))
515 (search-forward "<pre>" nil t)
517 (let ((body (point-marker)))
518 (search-forward "</pre>" nil t)
519 (delete-region (point) (point-max))
520 (nnweb-remove-markup)
521 (goto-char (point-min))
522 (while (looking-at " *$")
524 (narrow-to-region (point-min) body)
525 (while (and (re-search-forward "^$" nil t)
528 (goto-char (point-min))
529 (while (looking-at "\\(^[^ ]+:\\) *")
530 (replace-match "\\1 " t)
532 (goto-char (point-min))
533 (when (re-search-forward "^References:" nil t)
535 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
538 (goto-char (point-min))
540 (unless (looking-at "References")
543 (goto-char (point-min))
544 (while (search-forward "," nil t)
545 (replace-match " " t t)))
547 (set-marker body nil))))
549 (defun nnweb-reference-search (search)
551 (url-insert-file-contents
553 (nnweb-definition 'address)
555 (nnweb-encode-www-form-urlencoded
556 `(("search" . "advanced")
557 ("querytext" . ,search)
562 ("organization" . "")
565 ("choice" . "Search")
566 ("startmonth" . "Jul")
568 ("startyear" . "1996")
573 ("verbosity" . "Verbose")
574 ("ranking" . "Relevance")
578 (setq buffer-file-name nil))
585 (defun nnweb-altavista-create-mapping ()
586 "Perform the search and create an number-to-url alist."
588 (set-buffer nnweb-buffer)
591 (when (funcall (nnweb-definition 'search) nnweb-search part)
595 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
597 subject date from id group
600 ;; Go through all the article hits on this page.
601 (goto-char (point-min))
602 (search-forward "<dt>" nil t)
603 (delete-region (point-min) (match-beginning 0))
604 (goto-char (point-min))
605 (while (search-forward "<dt>" nil t)
606 (replace-match "\n<blubb>"))
607 (nnweb-decode-entities)
608 (goto-char (point-min))
609 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
611 (setq url (match-string 1)
612 subject (match-string 2)
613 date (match-string 3)
614 group (match-string 4)
615 id (concat "<" (match-string 5) ">")
616 from (match-string 6))
617 (unless (nnweb-get-hashtb url)
622 (make-full-mail-header
623 (cdr active) (concat "(" group ") " subject) from date
627 ;; See if we want more.
628 (when (or (not nnweb-articles)
629 (>= i nnweb-max-hits)
630 (not (funcall (nnweb-definition 'search)
631 nnweb-search (incf part))))
633 ;; Return the articles in the right order.
635 (sort map (lambda (s1 s2) (< (car s1) (car s2))))))))))
637 (defun nnweb-altavista-wash-article ()
638 (goto-char (point-min))
639 (let ((case-fold-search t))
640 (when (re-search-forward "^<strong>" nil t)
641 (delete-region (point-min) (match-beginning 0)))
642 (goto-char (point-min))
643 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
644 (replace-match "\\1: \\2" t)
646 (when (re-search-backward "^References:" nil t)
647 (narrow-to-region (point) (progn (forward-line 1) (point)))
648 (goto-char (point-min))
649 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
650 (replace-match "<\\1> " t)))
652 (nnweb-remove-markup)))
654 (defun nnweb-altavista-search (search &optional part)
656 (url-insert-file-contents
658 (nnweb-definition 'address)
660 (nnweb-encode-www-form-urlencoded
663 ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
669 (setq buffer-file-name nil)))
673 ;;; nnweb.el ends here