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)
107 (not (equal group nnweb-group))
108 (not nnweb-ephemeral-p))
109 (let ((info (assoc group nnweb-group-alist)))
110 (setq nnweb-group group)
111 (setq nnweb-type (nth 2 info))
112 (setq nnweb-search (nth 3 info))
114 (nnweb-read-overview group))))
116 ((not nnweb-articles)
117 (nnheader-report 'nnweb "No matching articles"))
119 (let ((active (if nnweb-ephemeral-p
120 (cons (caar nnweb-articles)
121 (caar (last nnweb-articles)))
122 (cadr (assoc group nnweb-group-alist)))))
123 (nnheader-report 'nnweb "Opened group %s" group)
125 "211 %d %d %d %s\n" (length nnweb-articles)
126 (car active) (cdr active) group)))))
128 (deffoo nnweb-close-group (group &optional server)
129 (nnweb-possibly-change-server group server)
130 (when (gnus-buffer-live-p nnweb-buffer)
132 (set-buffer nnweb-buffer)
133 (set-buffer-modified-p nil)
134 (kill-buffer nnweb-buffer)))
137 (deffoo nnweb-request-article (article &optional group server buffer)
138 (nnweb-possibly-change-server group server)
140 (set-buffer (or buffer nntp-server-buffer))
141 (let ((url (caddr (assq article nnweb-articles))))
143 (nnweb-fetch-url url))
144 (and (stringp article)
145 (let ((fetch (nnweb-definition 'id))
147 (when (string-match "^<\\(.*\\)>$" article)
148 (setq art (match-string 1 article)))
152 (format fetch article))))))
153 (unless nnheader-callback-function
154 (funcall (nnweb-definition 'article))
155 (nnweb-decode-entities))
156 (nnheader-report 'nnweb "Fetched article %s" article)
159 (deffoo nnweb-close-server (&optional server)
160 (when (and (nnweb-server-opened server)
161 (gnus-buffer-live-p nnweb-buffer))
163 (set-buffer nnweb-buffer)
164 (set-buffer-modified-p nil)
165 (kill-buffer nnweb-buffer)))
166 (nnoo-close-server 'nnweb server))
168 (deffoo nnweb-request-list (&optional server)
169 (nnweb-possibly-change-server nil server)
171 (set-buffer nntp-server-buffer)
172 (nnmail-generate-active nnweb-group-alist)
175 (deffoo nnweb-request-update-info (group info &optional server)
176 (nnweb-possibly-change-server group server)
177 ;;(setcar (cddr info) nil)
180 (deffoo nnweb-asynchronous-p ()
183 (deffoo nnweb-request-create-group (group &optional server args)
184 (nnweb-possibly-change-server nil server)
185 (nnweb-request-delete-group group)
186 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
190 (deffoo nnweb-request-delete-group (group &optional force server)
191 (nnweb-possibly-change-server group server)
192 (gnus-delete-assoc group nnweb-group-alist)
193 (gnus-delete-file (nnweb-overview-file group))
196 (nnoo-define-skeleton nnweb)
198 ;;; Internal functions
200 (defun nnweb-read-overview (group)
201 "Read the overview of GROUP and build the map."
202 (when (file-exists-p (nnweb-overview-file group))
203 (nnheader-temp-write nil
204 (insert-file-contents (nnweb-overview-file group))
205 (goto-char (point-min))
206 (setq nnweb-hashtb (gnus-make-hashtable
207 (count-lines (point-min) (point-max))))
210 (setq header (nnheader-parse-nov))
212 (push (list (mail-header-number header)
213 header (mail-header-xrefs header))
215 (nnweb-set-hashtb header (car nnweb-articles)))))))
217 (defun nnweb-write-overview (group)
218 "Write the overview file for GROUP."
219 (nnheader-temp-write (nnweb-overview-file group)
220 (let ((articles nnweb-articles))
222 (nnheader-insert-nov (cadr (pop articles)))))))
224 (defun nnweb-set-hashtb (header data)
225 (gnus-sethasb (nnweb-identifier (mail-header-xrefs header))
228 (defun nnweb-get-hashtb (url)
229 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
231 (defun nnweb-identifier (ident)
234 (defun nnweb-overview-file (group)
235 "Return the name of the overview file of GROUP."
236 (nnheader-concat nnweb-directory group ".overview"))
238 (defun nnweb-write-active ()
239 "Save the active file."
240 (nnheader-temp-write (nnheader-concat nnweb-directory "active")
241 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
243 (defun nnweb-read-active ()
244 "Read the active file."
245 (load (nnheader-concat nnweb-directory "active") t t t))
247 (defun nnweb-definition (type)
248 "Return the definition of TYPE."
249 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
251 (error "Undefined definition %s" type))
254 (defun nnweb-possibly-change-server (&optional group server)
257 (unless (nnweb-server-opened server)
258 (nnweb-open-server server)))
260 (when (and (not nnweb-ephemeral-p)
261 (not (equal group nnweb-group)))
262 (nnweb-request-group group nil t))))
264 (defun nnweb-init (server)
265 "Initialize buffers and such."
266 (unless (gnus-buffer-live-p nnweb-buffer)
269 (nnheader-set-temp-buffer
270 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
272 (defun nnweb-fetch-url (url)
274 (if (not nnheader-callback-function)
275 (let ((buf (current-buffer)))
277 (set-buffer nnweb-buffer)
280 (url-insert-file-contents url)
281 (copy-to-buffer buf (point-min) (point-max)))))
282 (nnweb-url-retrieve-asynch
283 url 'nnweb-callback (current-buffer) nnheader-callback-function)
286 (defun nnweb-callback (buffer callback)
287 (when (gnus-buffer-live-p url-working-buffer)
289 (set-buffer url-working-buffer)
290 (funcall (nnweb-definition 'article))
291 (nnweb-decode-entities)
293 (goto-char (point-max))
294 (insert-buffer-substring url-working-buffer))
296 (gnus-kill-buffer url-working-buffer)))
298 (defun nnweb-url-retrieve-asynch (url callback &rest data)
299 (let ((url-request-method "GET")
300 (old-asynch url-be-asynchronous)
301 (url-request-data nil)
302 (url-request-extra-headers nil)
303 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
304 (setq-default url-be-asynchronous t)
306 (set-buffer (get-buffer-create url-working-buffer))
307 (setq url-current-callback-data data
308 url-be-asynchronous t
309 url-current-callback-func callback)
311 (setq-default url-be-asynchronous old-asynch)))
313 (defun nnweb-encode-www-form-urlencoded (pairs)
314 "Return PAIRS encoded for forms."
318 (concat (w3-form-encode-xwfu (car data)) "="
319 (w3-form-encode-xwfu (cdr data)))))
322 (defun nnweb-fetch-form (url pairs)
323 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
324 (url-request-method 'POST)
325 (url-request-extra-headers
326 '(("Content-type" . "application/x-www-form-urlencoded"))))
327 (url-insert-file-contents url)
328 (setq buffer-file-name nil))
331 (defun nnweb-decode-entities ()
332 (goto-char (point-min))
333 (while (re-search-forward "&\\([a-z]+\\);" nil t)
334 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
339 (defun nnweb-remove-markup ()
340 (goto-char (point-min))
341 (while (search-forward "<!--" nil t)
342 (delete-region (match-beginning 0)
343 (or (search-forward "-->" nil t)
345 (goto-char (point-min))
346 (while (re-search-forward "<[^>]+>" nil t)
347 (replace-match "" t t)))
350 ;;; DejaNews functions.
353 (defun nnweb-dejanews-create-mapping ()
354 "Perform the search and create an number-to-url alist."
356 (set-buffer nnweb-buffer)
358 (when (funcall (nnweb-definition 'search) nnweb-search)
362 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
364 Subject Score Date Newsgroup Author
367 ;; Go through all the article hits on this page.
368 (goto-char (point-min))
369 (nnweb-decode-entities)
370 (goto-char (point-min))
371 (while (re-search-forward "^ +[0-9]+\\." nil t)
374 (cond ((re-search-forward "^ +[0-9]+\\." nil t)
376 ((search-forward "\n\n" nil t)
380 (goto-char (point-min))
381 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
382 (setq url (match-string 1)))
383 (nnweb-remove-markup)
384 (goto-char (point-min))
385 (while (search-forward "\t" nil t)
387 (goto-char (point-min))
388 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
389 (set (intern (match-string 1)) (match-string 2)))
391 (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
392 (setq Subject (substring Subject 0 (match-beginning 0))))
393 (unless (nnweb-get-hashtb url)
398 (make-full-mail-header
399 (cdr active) (concat "(" Newsgroup ") " Subject) Author Date
400 (concat "<" (message-unique-id) "-" (int-to-string i)
402 nil 0 (string-to-int Score) nil)
405 ;; See whether there is a "Get next 20 hits" button here.
406 (if (or (not (re-search-forward
407 "HREF=\"\\([^\"]+\\)\">Get next" nil t))
408 (>= i nnweb-max-hits))
411 (setq more (match-string 1))
413 (url-insert-file-contents more)))
414 ;; Return the articles in the right order.
416 (sort map (lambda (s1 s2) (< (car s1) (car s2)))))))))
418 (defun nnweb-dejanews-wash-article ()
419 (let ((case-fold-search t))
420 (goto-char (point-min))
421 (re-search-forward "<PRE>" nil t)
422 (delete-region (point-min) (point))
423 (re-search-forward "</PRE>" nil t)
424 (delete-region (point) (point-max))
425 (nnweb-remove-markup)
426 (goto-char (point-min))
427 (while (and (looking-at " *$")
430 (while (looking-at "\\(^[^ ]+:\\) *")
431 (replace-match "\\1 " t)
433 (when (re-search-forward "\n\n+" nil t)
434 (replace-match "\n" t t))))
436 (defun nnweb-dejanews-search (search)
438 (nnweb-definition 'address)
439 `(("query" . ,search)
440 ("defaultOp" . "AND")
441 ("svcclass" . "dncurrent")
443 ("format" . "verbose")
445 ("showsort" . "score")
447 ("ageweight" . "1")))
454 (defun nnweb-reference-create-mapping ()
455 "Perform the search and create an number-to-url alist."
457 (set-buffer nnweb-buffer)
459 (when (funcall (nnweb-definition 'search) nnweb-search)
463 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
465 Subject Score Date Newsgroups From Message-ID
468 ;; Go through all the article hits on this page.
469 (goto-char (point-min))
470 (search-forward "</pre><hr>" nil t)
471 (delete-region (point-min) (point))
472 ;(nnweb-decode-entities)
473 (goto-char (point-min))
474 (while (re-search-forward "^ +[0-9]+\\." nil t)
477 (if (re-search-forward "^$" nil t)
480 (goto-char (point-min))
481 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
482 (setq url (match-string 1)))
483 (nnweb-remove-markup)
484 (goto-char (point-min))
485 (while (search-forward "\t" nil t)
487 (goto-char (point-min))
488 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
489 (set (intern (match-string 1)) (match-string 2)))
491 (search-forward "</pre>" nil t)
492 (unless (nnweb-get-hashtb url)
497 (make-full-mail-header
498 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
500 nil 0 (string-to-int Score) nil)
504 ;; Return the articles in the right order.
506 (sort map (lambda (s1 s2) (< (car s1) (car s2)))))))))
508 (defun nnweb-reference-wash-article ()
509 (let ((case-fold-search t))
510 (goto-char (point-min))
511 (re-search-forward "^</center><hr>" nil t)
512 (delete-region (point-min) (point))
513 (search-forward "<pre>" nil t)
515 (let ((body (point-marker)))
516 (search-forward "</pre>" nil t)
517 (delete-region (point) (point-max))
518 (nnweb-remove-markup)
519 (goto-char (point-min))
520 (while (looking-at " *$")
522 (narrow-to-region (point-min) body)
523 (while (and (re-search-forward "^$" nil t)
526 (goto-char (point-min))
527 (while (looking-at "\\(^[^ ]+:\\) *")
528 (replace-match "\\1 " t)
530 (goto-char (point-min))
531 (when (re-search-forward "^References:" nil t)
533 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
536 (goto-char (point-min))
538 (unless (looking-at "References")
541 (goto-char (point-min))
542 (while (search-forward "," nil t)
543 (replace-match " " t t)))
545 (set-marker body nil))))
547 (defun nnweb-reference-search (search)
549 (url-insert-file-contents
551 (nnweb-definition 'address)
553 (nnweb-encode-www-form-urlencoded
554 `(("search" . "advanced")
555 ("querytext" . ,search)
560 ("organization" . "")
563 ("choice" . "Search")
564 ("startmonth" . "Jul")
566 ("startyear" . "1996")
571 ("verbosity" . "Verbose")
572 ("ranking" . "Relevance")
576 (setq buffer-file-name nil))
583 (defun nnweb-altavista-create-mapping ()
584 "Perform the search and create an number-to-url alist."
586 (set-buffer nnweb-buffer)
589 (when (funcall (nnweb-definition 'search) nnweb-search part)
593 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
595 subject date from id group
598 ;; Go through all the article hits on this page.
599 (goto-char (point-min))
600 (search-forward "<dt>" nil t)
601 (delete-region (point-min) (match-beginning 0))
602 (goto-char (point-min))
603 (while (search-forward "<dt>" nil t)
604 (replace-match "\n<blubb>"))
605 (nnweb-decode-entities)
606 (goto-char (point-min))
607 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
609 (setq url (match-string 1)
610 subject (match-string 2)
611 date (match-string 3)
612 group (match-string 4)
613 id (concat "<" (match-string 5) ">")
614 from (match-string 6))
615 (unless (nnweb-get-hashtb url)
620 (make-full-mail-header
621 (cdr active) (concat "(" group ") " subject) from date
625 ;; See if we want more.
626 (when (or (not nnweb-articles)
627 (>= i nnweb-max-hits)
628 (not (funcall (nnweb-definition 'search)
629 nnweb-search (incf part))))
631 ;; Return the articles in the right order.
633 (sort map (lambda (s1 s2) (< (car s1) (car s2))))))))))
635 (defun nnweb-altavista-wash-article ()
636 (goto-char (point-min))
637 (let ((case-fold-search t))
638 (re-search-forward "^<strong>" nil t)
639 (delete-region (point-min) (match-beginning 0))
640 (goto-char (point-min))
641 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
642 (replace-match "\\1: \\2" t)
644 (when (re-search-backward "^References:" nil t)
645 (narrow-to-region (point) (progn (forward-line 1) (point)))
646 (goto-char (point-min))
647 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
648 (replace-match "<\\1> " t)))
650 (nnweb-remove-markup)))
652 (defun nnweb-altavista-search (search &optional part)
654 (url-insert-file-contents
656 (nnweb-definition 'address)
658 (nnweb-encode-www-form-urlencoded
661 ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
667 (setq buffer-file-name nil)))
671 ;;; nnweb.el ends here