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
40 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
41 "Where nnweb will save its files.")
43 (defvoo nnweb-type 'dejanews
44 "What search engine type is being used.")
46 (defvar nnweb-type-definition
48 (article . nnweb-dejanews-wash-article)
49 (map . nnweb-dejanews-create-mapping)
50 (search . nnweb-dejanews-search)
51 (address . "http://search.dejanews.com/dnquery.xp"))
53 (article . nnweb-reference-wash-article)
54 (map . nnweb-reference-create-mapping)
55 (search . nnweb-reference-search)
56 (address . "http://www.reference.com/cgi-bin/pn/go"))
58 (article . nnweb-altavista-wash-article)
59 (map . nnweb-altavista-create-mapping)
60 (search . nnweb-altavista-search)
61 (address . "http://www.altavista.digital.com/cgi-bin/query")
62 (id . "/cgi-bin/news?id@%s")))
63 "Type-definition alist.")
65 (defvoo nnweb-search nil
66 "Search string to feed to DejaNews.")
68 (defvoo nnweb-max-hits 100
69 "Maximum number of hits to display.")
71 (defvoo nnweb-ephemeral-p nil
72 "Whether this nnweb server is ephemeral.")
74 ;;; Internal variables
76 (defvoo nnweb-articles nil)
77 (defvoo nnweb-buffer nil)
78 (defvoo nnweb-group-alist nil)
79 (defvoo nnweb-group nil)
80 (defvoo nnweb-hashtb nil)
82 ;;; Interface functions
84 (nnoo-define-basics nnweb)
86 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
87 (nnweb-possibly-change-server group server)
89 (set-buffer nntp-server-buffer)
92 (while (setq article (pop articles))
93 (when (setq header (cadr (assq article nnweb-articles)))
94 (nnheader-insert-nov header)))
97 (deffoo nnweb-request-scan (&optional group server)
98 (nnweb-possibly-change-server group server)
99 (funcall (nnweb-definition 'map))
100 (unless nnweb-ephemeral-p
102 (nnweb-write-overview group)))
104 (deffoo nnweb-request-group (group &optional server dont-check)
105 (nnweb-possibly-change-server nil server)
106 (when (and (not (equal group nnweb-group))
107 (not nnweb-ephemeral-p))
108 (let ((info (assoc group nnweb-group-alist)))
109 (setq nnweb-group group)
110 (setq nnweb-type (nth 2 info))
111 (setq nnweb-search (nth 3 info))
113 (nnweb-read-overview group))))
115 ((not nnweb-articles)
116 (nnheader-report 'nnweb "No matching articles"))
118 (let ((active (if nnweb-ephemeral-p
119 (cons (caar nnweb-articles)
120 (caar (last nnweb-articles)))
121 (cadr (assoc group nnweb-group-alist)))))
122 (nnheader-report 'nnweb "Opened group %s" group)
124 "211 %d %d %d %s\n" (length nnweb-articles)
125 (car active) (cdr active) group)))))
127 (deffoo nnweb-close-group (group &optional server)
128 (nnweb-possibly-change-server group server)
129 (when (gnus-buffer-live-p nnweb-buffer)
131 (set-buffer nnweb-buffer)
132 (set-buffer-modified-p nil)
133 (kill-buffer nnweb-buffer)))
136 (deffoo nnweb-request-article (article &optional group server buffer)
137 (nnweb-possibly-change-server group server)
139 (set-buffer (or buffer nntp-server-buffer))
140 (let ((url (caddr (assq article nnweb-articles))))
142 (nnweb-fetch-url url))
143 (and (stringp article)
144 (let ((fetch (nnweb-definition 'id))
146 (when (string-match "^<\\(.*\\)>$" article)
147 (setq art (match-string 1 article)))
151 (format fetch article))))))
152 (unless nnheader-callback-function
153 (funcall (nnweb-definition 'article))
154 (nnweb-decode-entities))
155 (nnheader-report 'nnweb "Fetched article %s" article)
158 (deffoo nnweb-close-server (&optional server)
159 (when (and (nnweb-server-opened server)
160 (gnus-buffer-live-p nnweb-buffer))
162 (set-buffer nnweb-buffer)
163 (set-buffer-modified-p nil)
164 (kill-buffer nnweb-buffer)))
165 (nnoo-close-server 'nnweb server))
167 (deffoo nnweb-request-list (&optional server)
168 (nnweb-possibly-change-server nil server)
170 (set-buffer nntp-server-buffer)
171 (nnmail-generate-active nnweb-group-alist)
174 (deffoo nnweb-request-update-info (group info &optional server)
175 (nnweb-possibly-change-server group server)
176 ;;(setcar (cddr info) nil)
179 (deffoo nnweb-asynchronous-p ()
182 (deffoo nnweb-request-create-group (group &optional server args)
183 (nnweb-possibly-change-server nil server)
184 (nnweb-request-delete-group group)
185 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
189 (deffoo nnweb-request-delete-group (group &optional force server)
190 (nnweb-possibly-change-server group server)
191 (gnus-delete-assoc group nnweb-group-alist)
192 (gnus-delete-file (nnweb-overview-file group))
195 (nnoo-define-skeleton nnweb)
197 ;;; Internal functions
199 (defun nnweb-read-overview (group)
200 "Read the overview of GROUP and build the map."
201 (when (file-exists-p (nnweb-overview-file group))
202 (nnheader-temp-write nil
203 (insert-file-contents (nnweb-overview-file group))
204 (goto-char (point-min))
205 (setq nnweb-hashtb (gnus-make-hashtable
206 (count-lines (point-min) (point-max))))
209 (setq header (nnheader-parse-nov))
211 (push (list (mail-header-number header)
212 header (mail-header-xrefs header))
214 (nnweb-set-hashtb header (car nnweb-articles)))))))
216 (defun nnweb-write-overview (group)
217 "Write the overview file for GROUP."
218 (nnheader-temp-write (nnweb-overview-file group)
219 (let ((articles nnweb-articles))
221 (nnheader-insert-nov (cadr (pop articles)))))))
223 (defun nnweb-set-hashtb (header data)
224 (gnus-sethasb (nnweb-identifier (mail-header-xrefs header))
227 (defun nnweb-get-hashtb (url)
228 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
230 (defun nnweb-identifier (ident)
233 (defun nnweb-overview-file (group)
234 "Return the name of the overview file of GROUP."
235 (nnheader-concat nnweb-directory group ".overview"))
237 (defun nnweb-write-active ()
238 "Save the active file."
239 (nnheader-temp-write (nnheader-concat nnweb-directory "active")
240 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
242 (defun nnweb-read-active ()
243 "Read the active file."
244 (load (nnheader-concat nnweb-directory "active") t t t))
246 (defun nnweb-definition (type)
247 "Return the definition of TYPE."
248 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
250 (error "Undefined definition %s" type))
253 (defun nnweb-possibly-change-server (&optional group server)
256 (unless (nnweb-server-opened server)
257 (nnweb-open-server server)))
259 (when (and (not nnweb-ephemeral-p)
260 (not (equal group nnweb-group)))
261 (nnweb-request-group group nil t))))
263 (defun nnweb-init (server)
264 "Initialize buffers and such."
265 (unless (gnus-buffer-live-p nnweb-buffer)
268 (nnheader-set-temp-buffer
269 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
271 (defun nnweb-fetch-url (url)
273 (if (not nnheader-callback-function)
274 (let ((buf (current-buffer)))
276 (set-buffer nnweb-buffer)
279 (url-insert-file-contents url)
280 (copy-to-buffer buf (point-min) (point-max)))))
281 (nnweb-url-retrieve-asynch
282 url 'nnweb-callback (current-buffer) nnheader-callback-function)
285 (defun nnweb-callback (buffer callback)
286 (when (gnus-buffer-live-p url-working-buffer)
288 (set-buffer url-working-buffer)
289 (funcall (nnweb-definition 'article))
290 (nnweb-decode-entities)
292 (goto-char (point-max))
293 (insert-buffer-substring url-working-buffer))
295 (gnus-kill-buffer url-working-buffer)))
297 (defun nnweb-url-retrieve-asynch (url callback &rest data)
298 (let ((url-request-method "GET")
299 (old-asynch url-be-asynchronous)
300 (url-request-data nil)
301 (url-request-extra-headers nil)
302 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
303 (setq-default url-be-asynchronous t)
305 (set-buffer (get-buffer-create url-working-buffer))
306 (setq url-current-callback-data data
307 url-be-asynchronous t
308 url-current-callback-func callback)
310 (setq-default url-be-asynchronous old-asynch)))
312 (defun nnweb-encode-www-form-urlencoded (pairs)
313 "Return PAIRS encoded for forms."
317 (concat (w3-form-encode-xwfu (car data)) "="
318 (w3-form-encode-xwfu (cdr data))))) pairs "&"))
320 (defun nnweb-fetch-form (url pairs)
321 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
322 (url-request-method 'POST)
323 (url-request-extra-headers
324 '(("Content-type" . "application/x-www-form-urlencoded"))))
325 (url-insert-file-contents url)
326 (setq buffer-file-name nil))
329 (defun nnweb-decode-entities ()
330 (goto-char (point-min))
331 (while (re-search-forward "&\\([a-z]+\\);" nil t)
332 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
337 (defun nnweb-remove-markup ()
338 (goto-char (point-min))
339 (while (search-forward "<!--" nil t)
340 (delete-region (match-beginning 0)
341 (or (search-forward "-->" nil t)
343 (goto-char (point-min))
344 (while (re-search-forward "<[^>]+>" nil t)
345 (replace-match "" t t)))
348 ;;; DejaNews functions.
351 (defun nnweb-dejanews-create-mapping ()
352 "Perform the search and create an number-to-url alist."
354 (set-buffer nnweb-buffer)
356 (when (funcall (nnweb-definition 'search) nnweb-search)
360 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
362 Subject Score Date Newsgroup Author
365 ;; Go through all the article hits on this page.
366 (goto-char (point-min))
367 (nnweb-decode-entities)
368 (goto-char (point-min))
369 (while (re-search-forward "^ +[0-9]+\\." nil t)
372 (cond ((re-search-forward "^ +[0-9]+\\." nil t)
374 ((search-forward "\n\n" nil t)
378 (goto-char (point-min))
379 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
380 (setq url (match-string 1)))
381 (nnweb-remove-markup)
382 (goto-char (point-min))
383 (while (search-forward "\t" nil t)
385 (goto-char (point-min))
386 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
387 (set (intern (match-string 1)) (match-string 2)))
389 (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
390 (setq Subject (substring Subject 0 (match-beginning 0))))
391 (unless (nnweb-get-hashtb url)
396 (make-full-mail-header
397 (cdr active) (concat "(" Newsgroup ") " Subject) Author Date
398 (concat "<" (message-unique-id) "-" (int-to-string i)
400 nil 0 (string-to-int Score) nil)
403 ;; See whether there is a "Get next 20 hits" button here.
404 (if (or (not (re-search-forward
405 "HREF=\"\\([^\"]+\\)\">Get next" nil t))
406 (>= i nnweb-max-hits))
409 (setq more (match-string 1))
411 (url-insert-file-contents more)))
412 ;; Return the articles in the right order.
414 (sort map (lambda (s1 s2) (< (car s1) (car s2)))))))))
416 (defun nnweb-dejanews-wash-article ()
417 (let ((case-fold-search t))
418 (goto-char (point-min))
419 (re-search-forward "<PRE>" nil t)
420 (delete-region (point-min) (point))
421 (re-search-forward "</PRE>" nil t)
422 (delete-region (point) (point-max))
423 (nnweb-remove-markup)
424 (goto-char (point-min))
425 (while (and (looking-at " *$")
428 (while (looking-at "\\(^[^ ]+:\\) *")
429 (replace-match "\\1 " t)
431 (when (re-search-forward "\n\n+" nil t)
432 (replace-match "\n" t t))))
434 (defun nnweb-dejanews-search (search)
436 (nnweb-definition 'address)
437 `(("query" . ,search)
438 ("defaultOp" . "AND")
439 ("svcclass" . "dncurrent")
441 ("format" . "verbose")
443 ("showsort" . "score")
445 ("ageweight" . "1")))
452 (defun nnweb-reference-create-mapping ()
453 "Perform the search and create an number-to-url alist."
455 (set-buffer nnweb-buffer)
457 (when (funcall (nnweb-definition 'search) nnweb-search)
461 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
463 Subject Score Date Newsgroups From Message-ID
466 ;; Go through all the article hits on this page.
467 (goto-char (point-min))
468 (search-forward "</pre><hr>" nil t)
469 (delete-region (point-min) (point))
470 ;(nnweb-decode-entities)
471 (goto-char (point-min))
472 (while (re-search-forward "^ +[0-9]+\\." nil t)
475 (if (re-search-forward "^$" nil t)
478 (goto-char (point-min))
479 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
480 (setq url (match-string 1)))
481 (nnweb-remove-markup)
482 (goto-char (point-min))
483 (while (search-forward "\t" nil t)
485 (goto-char (point-min))
486 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
487 (set (intern (match-string 1)) (match-string 2)))
489 (search-forward "</pre>" nil t)
490 (unless (nnweb-get-hashtb url)
495 (make-full-mail-header
496 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
498 nil 0 (string-to-int Score) nil)
502 ;; Return the articles in the right order.
504 (sort map (lambda (s1 s2) (< (car s1) (car s2)))))))))
506 (defun nnweb-reference-wash-article ()
507 (let ((case-fold-search t))
508 (goto-char (point-min))
509 (re-search-forward "^</center><hr>" nil t)
510 (delete-region (point-min) (point))
511 (search-forward "<pre>" nil t)
513 (let ((body (point-marker)))
514 (search-forward "</pre>" nil t)
515 (delete-region (point) (point-max))
516 (nnweb-remove-markup)
517 (goto-char (point-min))
518 (while (looking-at " *$")
520 (narrow-to-region (point-min) body)
521 (while (and (re-search-forward "^$" nil t)
524 (goto-char (point-min))
525 (while (looking-at "\\(^[^ ]+:\\) *")
526 (replace-match "\\1 " t)
528 (goto-char (point-min))
529 (when (re-search-forward "^References:" nil t)
531 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
534 (goto-char (point-min))
536 (unless (looking-at "References")
539 (goto-char (point-min))
540 (while (search-forward "," nil t)
541 (replace-match " " t t)))
543 (set-marker body nil))))
545 (defun nnweb-reference-search (search)
547 (url-insert-file-contents
549 (nnweb-definition 'address)
551 (nnweb-encode-www-form-urlencoded
552 `(("search" . "advanced")
553 ("querytext" . ,search)
558 ("organization" . "")
561 ("choice" . "Search")
562 ("startmonth" . "Jul")
564 ("startyear" . "1996")
569 ("verbosity" . "Verbose")
570 ("ranking" . "Relevance")
574 (setq buffer-file-name nil))
581 (defun nnweb-altavista-create-mapping ()
582 "Perform the search and create an number-to-url alist."
584 (set-buffer nnweb-buffer)
587 (when (funcall (nnweb-definition 'search) nnweb-search part)
591 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
593 subject date from id group
596 ;; Go through all the article hits on this page.
597 (goto-char (point-min))
598 (search-forward "<dt>" nil t)
599 (delete-region (point-min) (match-beginning 0))
600 (goto-char (point-min))
601 (while (search-forward "<dt>" nil t)
602 (replace-match "\n<blubb>"))
603 (nnweb-decode-entities)
604 (goto-char (point-min))
605 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
607 (setq url (match-string 1)
608 subject (match-string 2)
609 date (match-string 3)
610 group (match-string 4)
611 id (concat "<" (match-string 5) ">")
612 from (match-string 6))
613 (unless (nnweb-get-hashtb url)
618 (make-full-mail-header
619 (cdr active) (concat "(" group ") " subject) from date
623 ;; See if we want more.
624 (when (or (not nnweb-articles)
625 (>= i nnweb-max-hits)
626 (not (funcall (nnweb-definition 'search)
627 nnweb-search (incf part))))
629 ;; Return the articles in the right order.
631 (sort map (lambda (s1 s2) (< (car s1) (car s2))))))))))
633 (defun nnweb-altavista-wash-article ()
634 (goto-char (point-min))
635 (let ((case-fold-search t))
636 (re-search-forward "^<strong>" nil t)
637 (delete-region (point-min) (match-beginning 0))
638 (goto-char (point-min))
639 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
640 (replace-match "\\1: \\2" t)
642 (when (re-search-backward "^References:" nil t)
643 (narrow-to-region (point) (progn (forward-line 1) (point)))
644 (goto-char (point-min))
645 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
646 (replace-match "<\\1> " t)))
648 (nnweb-remove-markup)))
650 (defun nnweb-altavista-search (search &optional part)
652 (url-insert-file-contents
654 (nnweb-definition 'address)
656 (nnweb-encode-www-form-urlencoded
659 ,@(if part `(("stq" . ,(int-to-string (* part 30)))))
665 (setq buffer-file-name nil)))
669 ;;; nnweb.el ends here