1 ;;; nnweb.el --- retrieving articles via web search engines
2 ;; Copyright (C) 1996,97 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 (nnweb-definition 'id t)
148 (let ((fetch (nnweb-definition 'id))
150 (when (string-match "^<\\(.*\\)>$" article)
151 (setq art (match-string 1 article)))
155 (format fetch article))))))
156 (unless nnheader-callback-function
157 (funcall (nnweb-definition 'article))
158 (nnweb-decode-entities))
159 (nnheader-report 'nnweb "Fetched article %s" article)
162 (deffoo nnweb-close-server (&optional server)
163 (when (and (nnweb-server-opened server)
164 (gnus-buffer-live-p nnweb-buffer))
166 (set-buffer nnweb-buffer)
167 (set-buffer-modified-p nil)
168 (kill-buffer nnweb-buffer)))
169 (nnoo-close-server 'nnweb server))
171 (deffoo nnweb-request-list (&optional server)
172 (nnweb-possibly-change-server nil server)
174 (set-buffer nntp-server-buffer)
175 (nnmail-generate-active nnweb-group-alist)
178 (deffoo nnweb-request-update-info (group info &optional server)
179 (nnweb-possibly-change-server group server)
180 ;;(setcar (cddr info) nil)
183 (deffoo nnweb-asynchronous-p ()
186 (deffoo nnweb-request-create-group (group &optional server args)
187 (nnweb-possibly-change-server nil server)
188 (nnweb-request-delete-group group)
189 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
193 (deffoo nnweb-request-delete-group (group &optional force server)
194 (nnweb-possibly-change-server group server)
195 (gnus-delete-assoc group nnweb-group-alist)
196 (gnus-delete-file (nnweb-overview-file group))
199 (nnoo-define-skeleton nnweb)
201 ;;; Internal functions
203 (defun nnweb-read-overview (group)
204 "Read the overview of GROUP and build the map."
205 (when (file-exists-p (nnweb-overview-file group))
206 (nnheader-temp-write nil
207 (nnheader-insert-file-contents (nnweb-overview-file group))
208 (goto-char (point-min))
209 (setq nnweb-hashtb (gnus-make-hashtable
210 (count-lines (point-min) (point-max))))
213 (setq header (nnheader-parse-nov))
215 (push (list (mail-header-number header)
216 header (mail-header-xref header))
218 (nnweb-set-hashtb header (car nnweb-articles)))))))
220 (defun nnweb-write-overview (group)
221 "Write the overview file for GROUP."
222 (nnheader-temp-write (nnweb-overview-file group)
223 (let ((articles nnweb-articles))
225 (nnheader-insert-nov (cadr (pop articles)))))))
227 (defun nnweb-set-hashtb (header data)
228 (gnus-sethash (nnweb-identifier (mail-header-xref header))
231 (defun nnweb-get-hashtb (url)
232 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
234 (defun nnweb-identifier (ident)
237 (defun nnweb-overview-file (group)
238 "Return the name of the overview file of GROUP."
239 (nnheader-concat nnweb-directory group ".overview"))
241 (defun nnweb-write-active ()
242 "Save the active file."
243 (nnheader-temp-write (nnheader-concat nnweb-directory "active")
244 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
246 (defun nnweb-read-active ()
247 "Read the active file."
248 (load (nnheader-concat nnweb-directory "active") t t t))
250 (defun nnweb-definition (type &optional noerror)
251 "Return the definition of TYPE."
252 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
255 (error "Undefined definition %s" type))
258 (defun nnweb-possibly-change-server (&optional group server)
261 (unless (nnweb-server-opened server)
262 (nnweb-open-server server)))
264 (when (and (not nnweb-ephemeral-p)
265 (not (equal group nnweb-group)))
266 (nnweb-request-group group nil t))))
268 (defun nnweb-init (server)
269 "Initialize buffers and such."
270 (unless (gnus-buffer-live-p nnweb-buffer)
273 (nnheader-set-temp-buffer
274 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
276 (defun nnweb-fetch-url (url)
278 (if (not nnheader-callback-function)
279 (let ((buf (current-buffer)))
281 (set-buffer nnweb-buffer)
284 (url-insert-file-contents url)
285 (copy-to-buffer buf (point-min) (point-max)))))
286 (nnweb-url-retrieve-asynch
287 url 'nnweb-callback (current-buffer) nnheader-callback-function)
290 (defun nnweb-callback (buffer callback)
291 (when (gnus-buffer-live-p url-working-buffer)
293 (set-buffer url-working-buffer)
294 (funcall (nnweb-definition 'article))
295 (nnweb-decode-entities)
297 (goto-char (point-max))
298 (insert-buffer-substring url-working-buffer))
300 (gnus-kill-buffer url-working-buffer)))
302 (defun nnweb-url-retrieve-asynch (url callback &rest data)
303 (let ((url-request-method "GET")
304 (old-asynch url-be-asynchronous)
305 (url-request-data nil)
306 (url-request-extra-headers nil)
307 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
308 (setq-default url-be-asynchronous t)
310 (set-buffer (get-buffer-create url-working-buffer))
311 (setq url-current-callback-data data
312 url-be-asynchronous t
313 url-current-callback-func callback)
315 (setq-default url-be-asynchronous old-asynch)))
317 (defun nnweb-encode-www-form-urlencoded (pairs)
318 "Return PAIRS encoded for forms."
322 (concat (w3-form-encode-xwfu (car data)) "="
323 (w3-form-encode-xwfu (cdr data)))))
326 (defun nnweb-fetch-form (url pairs)
327 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
328 (url-request-method "POST")
329 (url-request-extra-headers
330 '(("Content-type" . "application/x-www-form-urlencoded"))))
331 (url-insert-file-contents url)
332 (setq buffer-file-name nil))
335 (defun nnweb-decode-entities ()
336 (goto-char (point-min))
337 (while (re-search-forward "&\\([a-z]+\\);" nil t)
338 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
343 (defun nnweb-remove-markup ()
344 (goto-char (point-min))
345 (while (search-forward "<!--" nil t)
346 (delete-region (match-beginning 0)
347 (or (search-forward "-->" nil t)
349 (goto-char (point-min))
350 (while (re-search-forward "<[^>]+>" nil t)
351 (replace-match "" t t)))
354 ;;; DejaNews functions.
357 (defun nnweb-dejanews-create-mapping ()
358 "Perform the search and create an number-to-url alist."
360 (set-buffer nnweb-buffer)
362 (when (funcall (nnweb-definition 'search) nnweb-search)
366 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
368 Subject Score Date Newsgroup Author
371 ;; Go through all the article hits on this page.
372 (goto-char (point-min))
373 (nnweb-decode-entities)
374 (goto-char (point-min))
375 (while (re-search-forward "^ +[0-9]+\\." nil t)
378 (cond ((re-search-forward "^ +[0-9]+\\." nil t)
380 ((search-forward "\n\n" nil t)
384 (goto-char (point-min))
385 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
386 (setq url (match-string 1)))
387 (nnweb-remove-markup)
388 (goto-char (point-min))
389 (while (search-forward "\t" nil t)
391 (goto-char (point-min))
392 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
393 (set (intern (match-string 1)) (match-string 2)))
395 (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
396 (setq Subject (substring Subject 0 (match-beginning 0))))
397 (unless (nnweb-get-hashtb url)
402 (make-full-mail-header
403 (cdr active) (concat "(" Newsgroup ") " Subject) Author Date
404 (concat "<" (message-unique-id) "-" (int-to-string i)
406 nil 0 (string-to-int Score) nil)
409 ;; See whether there is a "Get next 20 hits" button here.
410 (if (or (not (re-search-forward
411 "HREF=\"\\([^\"]+\\)\">Get next" nil t))
412 (>= i nnweb-max-hits))
415 (setq more (match-string 1))
417 (url-insert-file-contents more)))
418 ;; Return the articles in the right order.
420 (sort map (lambda (s1 s2) (< (car s1) (car s2)))))))))
422 (defun nnweb-dejanews-wash-article ()
423 (let ((case-fold-search t))
424 (goto-char (point-min))
425 (re-search-forward "<PRE>" nil t)
426 (delete-region (point-min) (point))
427 (re-search-forward "</PRE>" nil t)
428 (delete-region (point) (point-max))
429 (nnweb-remove-markup)
430 (goto-char (point-min))
431 (while (and (looking-at " *$")
434 (while (looking-at "\\(^[^ ]+:\\) *")
435 (replace-match "\\1 " t)
437 (when (re-search-forward "\n\n+" nil t)
438 (replace-match "\n" t t))))
440 (defun nnweb-dejanews-search (search)
442 (nnweb-definition 'address)
443 `(("query" . ,search)
444 ("defaultOp" . "AND")
445 ("svcclass" . "dncurrent")
447 ("format" . "verbose")
449 ("showsort" . "score")
451 ("ageweight" . "1")))
458 (defun nnweb-reference-create-mapping ()
459 "Perform the search and create an number-to-url alist."
461 (set-buffer nnweb-buffer)
463 (when (funcall (nnweb-definition 'search) nnweb-search)
467 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
469 Subject Score Date Newsgroups From Message-ID
472 ;; Go through all the article hits on this page.
473 (goto-char (point-min))
474 (search-forward "</pre><hr>" nil t)
475 (delete-region (point-min) (point))
476 ;(nnweb-decode-entities)
477 (goto-char (point-min))
478 (while (re-search-forward "^ +[0-9]+\\." nil t)
481 (if (re-search-forward "^$" nil t)
484 (goto-char (point-min))
485 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
486 (setq url (match-string 1)))
487 (nnweb-remove-markup)
488 (goto-char (point-min))
489 (while (search-forward "\t" nil t)
491 (goto-char (point-min))
492 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
493 (set (intern (match-string 1)) (match-string 2)))
495 (search-forward "</pre>" nil t)
496 (unless (nnweb-get-hashtb url)
501 (make-full-mail-header
502 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
504 nil 0 (string-to-int Score) nil)
508 ;; Return the articles in the right order.
510 (sort map (lambda (s1 s2) (< (car s1) (car s2)))))))))
512 (defun nnweb-reference-wash-article ()
513 (let ((case-fold-search t))
514 (goto-char (point-min))
515 (re-search-forward "^</center><hr>" nil t)
516 (delete-region (point-min) (point))
517 (search-forward "<pre>" nil t)
519 (let ((body (point-marker)))
520 (search-forward "</pre>" nil t)
521 (delete-region (point) (point-max))
522 (nnweb-remove-markup)
523 (goto-char (point-min))
524 (while (looking-at " *$")
526 (narrow-to-region (point-min) body)
527 (while (and (re-search-forward "^$" nil t)
530 (goto-char (point-min))
531 (while (looking-at "\\(^[^ ]+:\\) *")
532 (replace-match "\\1 " t)
534 (goto-char (point-min))
535 (when (re-search-forward "^References:" nil t)
537 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
540 (goto-char (point-min))
542 (unless (looking-at "References")
545 (goto-char (point-min))
546 (while (search-forward "," nil t)
547 (replace-match " " t t)))
549 (set-marker body nil))))
551 (defun nnweb-reference-search (search)
553 (url-insert-file-contents
555 (nnweb-definition 'address)
557 (nnweb-encode-www-form-urlencoded
558 `(("search" . "advanced")
559 ("querytext" . ,search)
564 ("organization" . "")
567 ("choice" . "Search")
568 ("startmonth" . "Jul")
570 ("startyear" . "1996")
575 ("verbosity" . "Verbose")
576 ("ranking" . "Relevance")
580 (setq buffer-file-name nil))
587 (defun nnweb-altavista-create-mapping ()
588 "Perform the search and create an number-to-url alist."
590 (set-buffer nnweb-buffer)
593 (when (funcall (nnweb-definition 'search) nnweb-search part)
597 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
599 subject date from id group
602 ;; Go through all the article hits on this page.
603 (goto-char (point-min))
604 (search-forward "<dt>" nil t)
605 (delete-region (point-min) (match-beginning 0))
606 (goto-char (point-min))
607 (while (search-forward "<dt>" nil t)
608 (replace-match "\n<blubb>"))
609 (nnweb-decode-entities)
610 (goto-char (point-min))
611 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
613 (setq url (match-string 1)
614 subject (match-string 2)
615 date (match-string 3)
616 group (match-string 4)
617 id (concat "<" (match-string 5) ">")
618 from (match-string 6))
619 (unless (nnweb-get-hashtb url)
624 (make-full-mail-header
625 (cdr active) (concat "(" group ") " subject) from date
629 ;; See if we want more.
630 (when (or (not nnweb-articles)
631 (>= i nnweb-max-hits)
632 (not (funcall (nnweb-definition 'search)
633 nnweb-search (incf part))))
635 ;; Return the articles in the right order.
637 (sort map (lambda (s1 s2) (< (car s1) (car s2))))))))))
639 (defun nnweb-altavista-wash-article ()
640 (goto-char (point-min))
641 (let ((case-fold-search t))
642 (when (re-search-forward "^<strong>" nil t)
643 (delete-region (point-min) (match-beginning 0)))
644 (goto-char (point-min))
645 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
646 (replace-match "\\1: \\2" t)
648 (when (re-search-backward "^References:" nil t)
649 (narrow-to-region (point) (progn (forward-line 1) (point)))
650 (goto-char (point-min))
651 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
652 (replace-match "<\\1> " t)))
654 (nnweb-remove-markup)))
656 (defun nnweb-altavista-search (search &optional part)
658 (url-insert-file-contents
660 (nnweb-definition 'address)
662 (nnweb-encode-www-form-urlencoded
665 ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
671 (setq buffer-file-name nil)))
675 ;;; nnweb.el ends here