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
43 (defvoo nnweb-directory
(nnheader-concat gnus-directory
"nnweb/")
44 "Where nnweb will save its files.")
46 (defvoo nnweb-type
'dejanews
47 "What search engine type is being used.")
49 (defvar nnweb-type-definition
51 (article . nnweb-dejanews-wash-article
)
52 (map . nnweb-dejanews-create-mapping
)
53 (search . nnweb-dejanews-search
)
54 (address .
"http://xp9.dejanews.com/dnquery.xp")
55 (identifier . nnweb-dejanews-identity
))
57 (article . nnweb-reference-wash-article
)
58 (map . nnweb-reference-create-mapping
)
59 (search . nnweb-reference-search
)
60 (address .
"http://www.reference.com/cgi-bin/pn/go")
61 (identifier . identity
))
63 (article . nnweb-altavista-wash-article
)
64 (map . nnweb-altavista-create-mapping
)
65 (search . nnweb-altavista-search
)
66 (address .
"http://www.altavista.digital.com/cgi-bin/query")
67 (id .
"/cgi-bin/news?id@%s")
68 (identifier . identity
)))
69 "Type-definition alist.")
71 (defvoo nnweb-search nil
72 "Search string to feed to DejaNews.")
74 (defvoo nnweb-max-hits
100
75 "Maximum number of hits to display.")
77 (defvoo nnweb-ephemeral-p nil
78 "Whether this nnweb server is ephemeral.")
80 ;;; Internal variables
82 (defvoo nnweb-articles nil
)
83 (defvoo nnweb-buffer nil
)
84 (defvoo nnweb-group-alist nil
)
85 (defvoo nnweb-group nil
)
86 (defvoo nnweb-hashtb nil
)
88 ;;; Interface functions
90 (nnoo-define-basics nnweb
)
92 (deffoo nnweb-retrieve-headers
(articles &optional group server fetch-old
)
93 (nnweb-possibly-change-server group server
)
95 (set-buffer nntp-server-buffer
)
98 (while (setq article
(pop articles
))
99 (when (setq header
(cadr (assq article nnweb-articles
)))
100 (nnheader-insert-nov header
)))
103 (deffoo nnweb-request-scan
(&optional group server
)
104 (nnweb-possibly-change-server group server
)
105 (setq nnweb-hashtb
(gnus-make-hashtable 4095))
106 (funcall (nnweb-definition 'map
))
107 (unless nnweb-ephemeral-p
109 (nnweb-write-overview group
)))
111 (deffoo nnweb-request-group
(group &optional server dont-check
)
112 (nnweb-possibly-change-server nil server
)
114 (not (equal group nnweb-group
))
115 (not nnweb-ephemeral-p
))
116 (let ((info (assoc group nnweb-group-alist
)))
117 (setq nnweb-group group
)
118 (setq nnweb-type
(nth 2 info
))
119 (setq nnweb-search
(nth 3 info
))
121 (nnweb-read-overview group
))))
123 ((not nnweb-articles
)
124 (nnheader-report 'nnweb
"No matching articles"))
126 (let ((active (if nnweb-ephemeral-p
127 (cons (caar nnweb-articles
)
128 (caar (last nnweb-articles
)))
129 (cadr (assoc group nnweb-group-alist
)))))
130 (nnheader-report 'nnweb
"Opened group %s" group
)
132 "211 %d %d %d %s\n" (length nnweb-articles
)
133 (car active
) (cdr active
) group
)))))
135 (deffoo nnweb-close-group
(group &optional server
)
136 (nnweb-possibly-change-server group server
)
137 (when (gnus-buffer-live-p nnweb-buffer
)
139 (set-buffer nnweb-buffer
)
140 (set-buffer-modified-p nil
)
141 (kill-buffer nnweb-buffer
)))
144 (deffoo nnweb-request-article
(article &optional group server buffer
)
145 (nnweb-possibly-change-server group server
)
147 (set-buffer (or buffer nntp-server-buffer
))
148 (let* ((header (cadr (assq article nnweb-articles
)))
149 (url (and header
(mail-header-xref header
))))
151 (nnweb-fetch-url url
))
152 (and (stringp article
)
153 (nnweb-definition 'id t
)
154 (let ((fetch (nnweb-definition 'id
))
156 (when (string-match "^<\\(.*\\)>$" article
)
157 (setq art
(match-string 1 article
)))
161 (format fetch article
))))))
162 (unless nnheader-callback-function
163 (funcall (nnweb-definition 'article
))
164 (nnweb-decode-entities))
165 (nnheader-report 'nnweb
"Fetched article %s" article
)
168 (deffoo nnweb-close-server
(&optional server
)
169 (when (and (nnweb-server-opened server
)
170 (gnus-buffer-live-p nnweb-buffer
))
172 (set-buffer nnweb-buffer
)
173 (set-buffer-modified-p nil
)
174 (kill-buffer nnweb-buffer
)))
175 (nnoo-close-server 'nnweb server
))
177 (deffoo nnweb-request-list
(&optional server
)
178 (nnweb-possibly-change-server nil server
)
180 (set-buffer nntp-server-buffer
)
181 (nnmail-generate-active nnweb-group-alist
)
184 (deffoo nnweb-request-update-info
(group info
&optional server
)
185 (nnweb-possibly-change-server group server
)
186 ;;(setcar (cddr info) nil)
189 (deffoo nnweb-asynchronous-p
()
192 (deffoo nnweb-request-create-group
(group &optional server args
)
193 (nnweb-possibly-change-server nil server
)
194 (nnweb-request-delete-group group
)
195 (push `(,group
,(cons 1 0) ,@args
) nnweb-group-alist
)
199 (deffoo nnweb-request-delete-group
(group &optional force server
)
200 (nnweb-possibly-change-server group server
)
201 (gnus-delete-assoc group nnweb-group-alist
)
202 (gnus-delete-file (nnweb-overview-file group
))
205 (nnoo-define-skeleton nnweb
)
207 ;;; Internal functions
209 (defun nnweb-read-overview (group)
210 "Read the overview of GROUP and build the map."
211 (when (file-exists-p (nnweb-overview-file group
))
212 (nnheader-temp-write nil
213 (nnheader-insert-file-contents (nnweb-overview-file group
))
214 (goto-char (point-min))
217 (setq header
(nnheader-parse-nov))
219 (push (list (mail-header-number header
)
220 header
(mail-header-xref header
))
222 (nnweb-set-hashtb header
(car nnweb-articles
)))))))
224 (defun nnweb-write-overview (group)
225 "Write the overview file for GROUP."
226 (nnheader-temp-write (nnweb-overview-file group
)
227 (let ((articles nnweb-articles
))
229 (nnheader-insert-nov (cadr (pop articles
)))))))
231 (defun nnweb-set-hashtb (header data
)
232 (gnus-sethash (nnweb-identifier (mail-header-xref header
))
235 (defun nnweb-get-hashtb (url)
236 (gnus-gethash (nnweb-identifier url
) nnweb-hashtb
))
238 (defun nnweb-identifier (ident)
239 (funcall (nnweb-definition 'identifier
) ident
))
241 (defun nnweb-overview-file (group)
242 "Return the name of the overview file of GROUP."
243 (nnheader-concat nnweb-directory group
".overview"))
245 (defun nnweb-write-active ()
246 "Save the active file."
247 (nnheader-temp-write (nnheader-concat nnweb-directory
"active")
248 (prin1 `(setq nnweb-group-alist
',nnweb-group-alist
) (current-buffer))))
250 (defun nnweb-read-active ()
251 "Read the active file."
252 (load (nnheader-concat nnweb-directory
"active") t t t
))
254 (defun nnweb-definition (type &optional noerror
)
255 "Return the definition of TYPE."
256 (let ((def (cdr (assq type
(assq nnweb-type nnweb-type-definition
)))))
259 (error "Undefined definition %s" type
))
262 (defun nnweb-possibly-change-server (&optional group server
)
265 (unless (nnweb-server-opened server
)
266 (nnweb-open-server server
)))
267 (unless nnweb-group-alist
270 (when (and (not nnweb-ephemeral-p
)
271 (not (equal group nnweb-group
)))
272 (nnweb-request-group group nil t
))))
274 (defun nnweb-init (server)
275 "Initialize buffers and such."
276 (unless (gnus-buffer-live-p nnweb-buffer
)
279 (nnheader-set-temp-buffer
280 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server
))))))
282 (defun nnweb-fetch-url (url)
284 (if (not nnheader-callback-function
)
285 (let ((buf (current-buffer)))
287 (set-buffer nnweb-buffer
)
290 (url-insert-file-contents url
)
291 (copy-to-buffer buf
(point-min) (point-max)))))
292 (nnweb-url-retrieve-asynch
293 url
'nnweb-callback
(current-buffer) nnheader-callback-function
)
296 (defun nnweb-callback (buffer callback
)
297 (when (gnus-buffer-live-p url-working-buffer
)
299 (set-buffer url-working-buffer
)
300 (funcall (nnweb-definition 'article
))
301 (nnweb-decode-entities)
303 (goto-char (point-max))
304 (insert-buffer-substring url-working-buffer
))
306 (gnus-kill-buffer url-working-buffer
)))
308 (defun nnweb-url-retrieve-asynch (url callback
&rest data
)
309 (let ((url-request-method "GET")
310 (old-asynch url-be-asynchronous
)
311 (url-request-data nil
)
312 (url-request-extra-headers nil
)
313 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
314 (setq-default url-be-asynchronous t
)
316 (set-buffer (get-buffer-create url-working-buffer
))
317 (setq url-current-callback-data data
318 url-be-asynchronous t
319 url-current-callback-func callback
)
321 (setq-default url-be-asynchronous old-asynch
)))
323 (defun nnweb-encode-www-form-urlencoded (pairs)
324 "Return PAIRS encoded for forms."
328 (concat (w3-form-encode-xwfu (car data
)) "="
329 (w3-form-encode-xwfu (cdr data
)))))
332 (defun nnweb-fetch-form (url pairs
)
333 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs
))
334 (url-request-method "POST")
335 (url-request-extra-headers
336 '(("Content-type" .
"application/x-www-form-urlencoded"))))
337 (url-insert-file-contents url
)
338 (setq buffer-file-name nil
))
341 (defun nnweb-decode-entities ()
342 (goto-char (point-min))
343 (while (re-search-forward "&\\([a-z]+\\);" nil t
)
344 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
349 (defun nnweb-remove-markup ()
350 (goto-char (point-min))
351 (while (search-forward "<!--" nil t
)
352 (delete-region (match-beginning 0)
353 (or (search-forward "-->" nil t
)
355 (goto-char (point-min))
356 (while (re-search-forward "<[^>]+>" nil t
)
357 (replace-match "" t t
)))
360 ;;; DejaNews functions.
363 (defun nnweb-dejanews-create-mapping ()
364 "Perform the search and create an number-to-url alist."
366 (set-buffer nnweb-buffer
)
368 (when (funcall (nnweb-definition 'search
) nnweb-search
)
372 (active (or (cadr (assoc nnweb-group nnweb-group-alist
))
374 Subject Score Date Newsgroup Author
377 ;; Go through all the article hits on this page.
378 (goto-char (point-min))
379 (nnweb-decode-entities)
380 (goto-char (point-min))
381 (while (re-search-forward "^ +[0-9]+\\." nil t
)
384 (cond ((re-search-forward "^ +[0-9]+\\." nil t
)
386 ((search-forward "\n\n" nil t
)
390 (goto-char (point-min))
391 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
392 (setq url
(match-string 1)))
393 (nnweb-remove-markup)
394 (goto-char (point-min))
395 (while (search-forward "\t" nil t
)
397 (goto-char (point-min))
398 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t
)
399 (set (intern (match-string 1)) (match-string 2)))
401 (when (string-match "#[0-9]+/[0-9]+ *$" Subject
)
402 (setq Subject
(substring Subject
0 (match-beginning 0))))
404 (unless (nnweb-get-hashtb url
)
408 (make-full-mail-header
409 (cdr active
) (concat "(" Newsgroup
") " Subject
) Author Date
410 (concat "<" (nnweb-identifier url
) "@dejanews>")
411 nil
0 (string-to-int Score
) url
))
413 (nnweb-set-hashtb (cadar map
) (car map
))))
414 ;; See whether there is a "Get next 20 hits" button here.
415 (if (or (not (re-search-forward
416 "HREF=\"\\([^\"]+\\)\">Get next" nil t
))
417 (>= i nnweb-max-hits
))
420 (setq more
(match-string 1))
422 (url-insert-file-contents more
)))
423 ;; Return the articles in the right order.
425 (sort (nconc nnweb-articles map
)
426 (lambda (s1 s2
) (< (car s1
) (car s2
)))))))))
428 (defun nnweb-dejanews-wash-article ()
429 (let ((case-fold-search t
))
430 (goto-char (point-min))
431 (re-search-forward "<PRE>" nil t
)
432 (delete-region (point-min) (point))
433 (re-search-forward "</PRE>" nil t
)
434 (delete-region (point) (point-max))
435 (nnweb-remove-markup)
436 (goto-char (point-min))
437 (while (and (looking-at " *$")
440 (while (looking-at "\\(^[^ ]+:\\) *")
441 (replace-match "\\1 " t
)
443 (when (re-search-forward "\n\n+" nil t
)
444 (replace-match "\n" t t
))))
446 (defun nnweb-dejanews-search (search)
448 (nnweb-definition 'address
)
449 `(("query" .
,search
)
450 ("defaultOp" .
"AND")
451 ("svcclass" .
"dncurrent")
453 ("format" .
"verbose")
455 ("showsort" .
"score")
457 ("ageweight" .
"1")))
460 (defun nnweb-dejanews-identity (url)
461 "Return an unique identifier based on URL."
462 (if (string-match "recnum=\\([0-9]+\\)" url
)
470 (defun nnweb-reference-create-mapping ()
471 "Perform the search and create an number-to-url alist."
473 (set-buffer nnweb-buffer
)
475 (when (funcall (nnweb-definition 'search
) nnweb-search
)
479 (active (or (cadr (assoc nnweb-group nnweb-group-alist
))
481 Subject Score Date Newsgroups From Message-ID
484 ;; Go through all the article hits on this page.
485 (goto-char (point-min))
486 (search-forward "</pre><hr>" nil t
)
487 (delete-region (point-min) (point))
488 ;(nnweb-decode-entities)
489 (goto-char (point-min))
490 (while (re-search-forward "^ +[0-9]+\\." nil t
)
493 (if (re-search-forward "^$" nil t
)
496 (goto-char (point-min))
497 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
498 (setq url
(match-string 1)))
499 (nnweb-remove-markup)
500 (goto-char (point-min))
501 (while (search-forward "\t" nil t
)
503 (goto-char (point-min))
504 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t
)
505 (set (intern (match-string 1)) (match-string 2)))
507 (search-forward "</pre>" nil t
)
509 (unless (nnweb-get-hashtb url
)
513 (make-full-mail-header
514 (cdr active
) (concat "(" Newsgroups
") " Subject
) From Date
516 nil
0 (string-to-int Score
) url
))
518 (nnweb-set-hashtb (cadar map
) (car map
))))
520 ;; Return the articles in the right order.
522 (sort (nconc nnweb-articles map
)
523 (lambda (s1 s2
) (< (car s1
) (car s2
)))))))))
525 (defun nnweb-reference-wash-article ()
526 (let ((case-fold-search t
))
527 (goto-char (point-min))
528 (re-search-forward "^</center><hr>" nil t
)
529 (delete-region (point-min) (point))
530 (search-forward "<pre>" nil t
)
532 (let ((body (point-marker)))
533 (search-forward "</pre>" nil t
)
534 (delete-region (point) (point-max))
535 (nnweb-remove-markup)
536 (goto-char (point-min))
537 (while (looking-at " *$")
539 (narrow-to-region (point-min) body
)
540 (while (and (re-search-forward "^$" nil t
)
543 (goto-char (point-min))
544 (while (looking-at "\\(^[^ ]+:\\) *")
545 (replace-match "\\1 " t
)
547 (goto-char (point-min))
548 (when (re-search-forward "^References:" nil t
)
550 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t
)
553 (goto-char (point-min))
555 (unless (looking-at "References")
558 (goto-char (point-min))
559 (while (search-forward "," nil t
)
560 (replace-match " " t t
)))
562 (set-marker body nil
))))
564 (defun nnweb-reference-search (search)
566 (url-insert-file-contents
568 (nnweb-definition 'address
)
570 (nnweb-encode-www-form-urlencoded
571 `(("search" .
"advanced")
572 ("querytext" .
,search
)
577 ("organization" .
"")
580 ("choice" .
"Search")
581 ("startmonth" .
"Jul")
583 ("startyear" .
"1996")
588 ("verbosity" .
"Verbose")
589 ("ranking" .
"Relevance")
593 (setq buffer-file-name nil
))
600 (defun nnweb-altavista-create-mapping ()
601 "Perform the search and create an number-to-url alist."
603 (set-buffer nnweb-buffer
)
606 (when (funcall (nnweb-definition 'search
) nnweb-search part
)
610 (active (or (cadr (assoc nnweb-group nnweb-group-alist
))
612 subject date from id group
615 ;; Go through all the article hits on this page.
616 (goto-char (point-min))
617 (search-forward "<dt>" nil t
)
618 (delete-region (point-min) (match-beginning 0))
619 (goto-char (point-min))
620 (while (search-forward "<dt>" nil t
)
621 (replace-match "\n<blubb>"))
622 (nnweb-decode-entities)
623 (goto-char (point-min))
624 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
626 (setq url
(match-string 1)
627 subject
(match-string 2)
628 date
(match-string 3)
629 group
(match-string 4)
630 id
(concat "<" (match-string 5) ">")
631 from
(match-string 6))
633 (unless (nnweb-get-hashtb url
)
637 (make-full-mail-header
638 (cdr active
) (concat "(" group
") " subject
) from date
641 (nnweb-set-hashtb (cadar map
) (car map
))))
642 ;; See if we want more.
643 (when (or (not nnweb-articles
)
644 (>= i nnweb-max-hits
)
645 (not (funcall (nnweb-definition 'search
)
646 nnweb-search
(incf part
))))
648 ;; Return the articles in the right order.
650 (sort (nconc nnweb-articles map
)
651 (lambda (s1 s2
) (< (car s1
) (car s2
))))))))))
653 (defun nnweb-altavista-wash-article ()
654 (goto-char (point-min))
655 (let ((case-fold-search t
))
656 (when (re-search-forward "^<strong>" nil t
)
657 (delete-region (point-min) (match-beginning 0)))
658 (goto-char (point-min))
659 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
660 (replace-match "\\1: \\2" t
)
662 (when (re-search-backward "^References:" nil t
)
663 (narrow-to-region (point) (progn (forward-line 1) (point)))
664 (goto-char (point-min))
665 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t
)
666 (replace-match "<\\1> " t
)))
668 (nnweb-remove-markup)))
670 (defun nnweb-altavista-search (search &optional part
)
672 (url-insert-file-contents
674 (nnweb-definition 'address
)
676 (nnweb-encode-www-form-urlencoded
679 ,@(when part
`(("stq" .
,(int-to-string (* part
30)))))
685 (setq buffer-file-name nil
)))
689 ;;; nnweb.el ends here