Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / nnweb.el
1 ;;; nnweb.el --- retrieving articles via web search engines
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Note: You need to have `w3' installed for some functions to work.
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'nnoo)
35 (require 'message)
36 (require 'gnus-util)
37 (require 'gnus)
38 (require 'nnmail)
39 (require 'mm-util)
40 (require 'mm-url)
41 (eval-and-compile
42   (ignore-errors
43     (require 'url)))
44 (autoload 'w3-parse-buffer "w3-parse")
45
46 (nnoo-declare nnweb)
47
48 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
49   "Where nnweb will save its files.")
50
51 (defvoo nnweb-type 'google
52   "What search engine type is being used.
53 Valid types include `google', `dejanews', `reference', and `altavista'.")
54
55 (defvar nnweb-type-definition
56   '((google
57      (article . ignore)
58      (id . "http://groups.google.com/groups?selm=%s&output=gplain")
59      (reference . identity)
60      (map . nnweb-google-create-mapping)
61      (search . nnweb-google-search)
62      (address . "http://groups.google.com/groups")
63      (identifier . nnweb-google-identity))
64     (dejanews ;; alias of google
65      ;;(article . nnweb-google-wash-article)
66      ;;(id . "http://groups.google.com/groups?as_umsgid=%s")
67      (article . ignore)
68      (id . "http://groups.google.com/groups?selm=%s&output=gplain")
69      ;;(reference . nnweb-google-reference)
70      (reference . identity)
71      (map . nnweb-google-create-mapping)
72      (search . nnweb-google-search)
73      (address . "http://groups.google.com/groups")
74      (identifier . nnweb-google-identity))
75     (reference
76      (article . nnweb-reference-wash-article)
77      (map . nnweb-reference-create-mapping)
78      (search . nnweb-reference-search)
79      (address . "http://www.reference.com/cgi-bin/pn/go")
80      (identifier . identity))
81     (altavista
82      (article . nnweb-altavista-wash-article)
83      (map . nnweb-altavista-create-mapping)
84      (search . nnweb-altavista-search)
85      (address . "http://www.altavista.digital.com/cgi-bin/query")
86      (id . "/cgi-bin/news?id@%s")
87      (identifier . identity)))
88   "Type-definition alist.")
89
90 (defvoo nnweb-search nil
91   "Search string to feed to DejaNews.")
92
93 (defvoo nnweb-max-hits 999
94   "Maximum number of hits to display.")
95
96 (defvoo nnweb-ephemeral-p nil
97   "Whether this nnweb server is ephemeral.")
98
99 ;;; Internal variables
100
101 (defvoo nnweb-articles nil)
102 (defvoo nnweb-buffer nil)
103 (defvoo nnweb-group-alist nil)
104 (defvoo nnweb-group nil)
105 (defvoo nnweb-hashtb nil)
106
107 ;;; Interface functions
108
109 (nnoo-define-basics nnweb)
110
111 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
112   (nnweb-possibly-change-server group server)
113   (save-excursion
114     (set-buffer nntp-server-buffer)
115     (erase-buffer)
116     (let (article header)
117       (mm-with-unibyte-current-buffer
118         (while (setq article (pop articles))
119           (when (setq header (cadr (assq article nnweb-articles)))
120             (nnheader-insert-nov header))))
121       'nov)))
122
123 (deffoo nnweb-request-scan (&optional group server)
124   (nnweb-possibly-change-server group server)
125   (if nnweb-ephemeral-p
126       (setq nnweb-hashtb (gnus-make-hashtable 4095)))
127   (funcall (nnweb-definition 'map))
128   (unless nnweb-ephemeral-p
129     (nnweb-write-active)
130     (nnweb-write-overview group)))
131
132 (deffoo nnweb-request-group (group &optional server dont-check)
133   (nnweb-possibly-change-server nil server)
134   (when (and group
135              (not (equal group nnweb-group))
136              (not nnweb-ephemeral-p))
137     (setq nnweb-group group
138           nnweb-articles nil)
139     (let ((info (assoc group nnweb-group-alist)))
140       (when info
141         (setq nnweb-type (nth 2 info))
142         (setq nnweb-search (nth 3 info))
143         (unless dont-check
144           (nnweb-read-overview group)))))
145   (cond
146    ((not nnweb-articles)
147     (nnheader-report 'nnweb "No matching articles"))
148    (t
149     (let ((active (if nnweb-ephemeral-p
150                       (cons (caar nnweb-articles)
151                             (caar (last nnweb-articles)))
152                     (cadr (assoc group nnweb-group-alist)))))
153       (nnheader-report 'nnweb "Opened group %s" group)
154       (nnheader-insert
155        "211 %d %d %d %s\n" (length nnweb-articles)
156        (car active) (cdr active) group)))))
157
158 (deffoo nnweb-close-group (group &optional server)
159   (nnweb-possibly-change-server group server)
160   (when (gnus-buffer-live-p nnweb-buffer)
161     (save-excursion
162       (set-buffer nnweb-buffer)
163       (set-buffer-modified-p nil)
164       (kill-buffer nnweb-buffer)))
165   t)
166
167 (deffoo nnweb-request-article (article &optional group server buffer)
168   (nnweb-possibly-change-server group server)
169   (save-excursion
170     (set-buffer (or buffer nntp-server-buffer))
171     (let* ((header (cadr (assq article nnweb-articles)))
172            (url (and header (mail-header-xref header))))
173       (when (or (and url
174                      (mm-with-unibyte-current-buffer
175                        (mm-url-insert url)))
176                 (and (stringp article)
177                      (nnweb-definition 'id t)
178                      (let ((fetch (nnweb-definition 'id))
179                            art active)
180                        (when (string-match "^<\\(.*\\)>$" article)
181                          (setq art (match-string 1 article)))
182                        (when (and fetch art)
183                          (setq url (format fetch art))
184                          (mm-with-unibyte-current-buffer
185                            (mm-url-insert url))
186                          (if (nnweb-definition 'reference t)
187                              (setq article
188                                    (funcall (nnweb-definition
189                                              'reference) article)))))))
190         (unless nnheader-callback-function
191           (funcall (nnweb-definition 'article)))
192         (nnheader-report 'nnweb "Fetched article %s" article)
193         (cons group (and (numberp article) article))))))
194
195 (deffoo nnweb-close-server (&optional server)
196   (when (and (nnweb-server-opened server)
197              (gnus-buffer-live-p nnweb-buffer))
198     (save-excursion
199       (set-buffer nnweb-buffer)
200       (set-buffer-modified-p nil)
201       (kill-buffer nnweb-buffer)))
202   (nnoo-close-server 'nnweb server))
203
204 (deffoo nnweb-request-list (&optional server)
205   (nnweb-possibly-change-server nil server)
206   (save-excursion
207     (set-buffer nntp-server-buffer)
208     (nnmail-generate-active nnweb-group-alist)
209     t))
210
211 (deffoo nnweb-request-update-info (group info &optional server)
212   (nnweb-possibly-change-server group server))
213
214 (deffoo nnweb-asynchronous-p ()
215   t)
216
217 (deffoo nnweb-request-create-group (group &optional server args)
218   (nnweb-possibly-change-server nil server)
219   (nnweb-request-delete-group group)
220   (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
221   (nnweb-write-active)
222   t)
223
224 (deffoo nnweb-request-delete-group (group &optional force server)
225   (nnweb-possibly-change-server group server)
226   (gnus-pull group nnweb-group-alist t)
227   (nnweb-write-active)
228   (gnus-delete-file (nnweb-overview-file group))
229   t)
230
231 (nnoo-define-skeleton nnweb)
232
233 ;;; Internal functions
234
235 (defun nnweb-read-overview (group)
236   "Read the overview of GROUP and build the map."
237   (when (file-exists-p (nnweb-overview-file group))
238     (mm-with-unibyte-buffer
239       (nnheader-insert-file-contents (nnweb-overview-file group))
240       (goto-char (point-min))
241       (let (header)
242         (while (not (eobp))
243           (setq header (nnheader-parse-nov))
244           (forward-line 1)
245           (push (list (mail-header-number header)
246                       header (mail-header-xref header))
247                 nnweb-articles)
248           (nnweb-set-hashtb header (car nnweb-articles)))))))
249
250 (defun nnweb-write-overview (group)
251   "Write the overview file for GROUP."
252   (with-temp-file (nnweb-overview-file group)
253     (let ((articles nnweb-articles))
254       (while articles
255         (nnheader-insert-nov (cadr (pop articles)))))))
256
257 (defun nnweb-set-hashtb (header data)
258   (gnus-sethash (nnweb-identifier (mail-header-xref header))
259                 data nnweb-hashtb))
260
261 (defun nnweb-get-hashtb (url)
262   (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
263
264 (defun nnweb-identifier (ident)
265   (funcall (nnweb-definition 'identifier) ident))
266
267 (defun nnweb-overview-file (group)
268   "Return the name of the overview file of GROUP."
269   (nnheader-concat nnweb-directory group ".overview"))
270
271 (defun nnweb-write-active ()
272   "Save the active file."
273   (gnus-make-directory nnweb-directory)
274   (with-temp-file (nnheader-concat nnweb-directory "active")
275     (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
276
277 (defun nnweb-read-active ()
278   "Read the active file."
279   (load (nnheader-concat nnweb-directory "active") t t t))
280
281 (defun nnweb-definition (type &optional noerror)
282   "Return the definition of TYPE."
283   (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
284     (when (and (not def)
285                (not noerror))
286       (error "Undefined definition %s" type))
287     def))
288
289 (defun nnweb-possibly-change-server (&optional group server)
290   (nnweb-init server)
291   (when server
292     (unless (nnweb-server-opened server)
293       (nnweb-open-server server)))
294   (unless nnweb-group-alist
295     (nnweb-read-active))
296   (unless nnweb-hashtb
297     (setq nnweb-hashtb (gnus-make-hashtable 4095)))
298   (when group
299     (when (and (not nnweb-ephemeral-p)
300                (equal group nnweb-group))
301       (nnweb-request-group group nil t))))
302
303 (defun nnweb-init (server)
304   "Initialize buffers and such."
305   (unless (gnus-buffer-live-p nnweb-buffer)
306     (setq nnweb-buffer
307           (save-excursion
308             (mm-with-unibyte
309               (nnheader-set-temp-buffer
310                (format " *nnweb %s %s %s*"
311                        nnweb-type nnweb-search server))
312               (current-buffer))))))
313
314 ;; (defun nnweb-fetch-url (url)
315 ;;   (let (buf)
316 ;;     (save-excursion
317 ;;       (if (not nnheader-callback-function)
318 ;;        (progn
319 ;;          (with-temp-buffer
320 ;;            (mm-enable-multibyte)
321 ;;            (let ((coding-system-for-read 'binary)
322 ;;                  (coding-system-for-write 'binary)
323 ;;                  (input-coding-system 'binary)
324 ;;                  (output-coding-system 'binary)
325 ;;                  (default-process-coding-system 'binary))
326 ;;              (nnweb-insert url))
327 ;;            (setq buf (buffer-string)))
328 ;;          (erase-buffer)
329 ;;          (insert buf)
330 ;;          t)
331 ;;      (nnweb-url-retrieve-asynch
332 ;;       url 'nnweb-callback (current-buffer) nnheader-callback-function)
333 ;;      t))))
334
335 ;; (defun nnweb-callback (buffer callback)
336 ;;   (when (gnus-buffer-live-p url-working-buffer)
337 ;;     (save-excursion
338 ;;       (set-buffer url-working-buffer)
339 ;;       (funcall (nnweb-definition 'article))
340 ;;       (nnweb-decode-entities)
341 ;;       (set-buffer buffer)
342 ;;       (goto-char (point-max))
343 ;;       (insert-buffer-substring url-working-buffer))
344 ;;     (funcall callback t)
345 ;;     (gnus-kill-buffer url-working-buffer)))
346
347 ;; (defun nnweb-url-retrieve-asynch (url callback &rest data)
348 ;;   (let ((url-request-method "GET")
349 ;;      (old-asynch url-be-asynchronous)
350 ;;      (url-request-data nil)
351 ;;      (url-request-extra-headers nil)
352 ;;      (url-working-buffer (generate-new-buffer-name " *nnweb*")))
353 ;;     (setq-default url-be-asynchronous t)
354 ;;     (save-excursion
355 ;;       (set-buffer (get-buffer-create url-working-buffer))
356 ;;       (setq url-current-callback-data data
357 ;;          url-be-asynchronous t
358 ;;          url-current-callback-func callback)
359 ;;       (url-retrieve url nil))
360 ;;     (setq-default url-be-asynchronous old-asynch)))
361
362 ;; (if (fboundp 'url-retrieve-synchronously)
363 ;;     (defun nnweb-url-retrieve-asynch (url callback &rest data)
364 ;;       (url-retrieve url callback data)))
365
366 ;;;
367 ;;; DejaNews functions.
368 ;;;
369
370 (defun nnweb-dejanews-create-mapping ()
371   "Perform the search and create an number-to-url alist."
372   (save-excursion
373     (set-buffer nnweb-buffer)
374     (erase-buffer)
375     (when (funcall (nnweb-definition 'search) nnweb-search)
376       (let ((i 0)
377             (more t)
378             (case-fold-search t)
379             (active (or (cadr (assoc nnweb-group nnweb-group-alist))
380                         (cons 1 0)))
381             subject date from
382             map url parse a table group text)
383         (while more
384           ;; Go through all the article hits on this page.
385           (goto-char (point-min))
386           (setq parse (w3-parse-buffer (current-buffer))
387                 table (nth 1 (nnweb-parse-find-all 'table parse)))
388           (dolist (row (nth 2 (car (nth 2 table))))
389             (setq a (nnweb-parse-find 'a row)
390                   url (cdr (assq 'href (nth 1 a)))
391                   text (nreverse (nnweb-text row)))
392             (when a
393               (setq subject (nth 4 text)
394                     group (nth 2 text)
395                     date (nth 1 text)
396                     from (nth 0 text))
397               (if (string-match "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)" date)
398                   (setq date (format "%s %s 00:00:00 %s"
399                                      (car (rassq (string-to-number
400                                                   (match-string 2 date))
401                                                  parse-time-months))
402                                      (match-string 3 date)
403                                      (match-string 1 date)))
404                 (setq date "Jan 1 00:00:00 0000"))
405               (incf i)
406               (setq url (concat url "&fmt=text"))
407               (when (string-match "&context=[^&]+" url)
408                 (setq url (replace-match "" t t url)))
409               (unless (nnweb-get-hashtb url)
410                 (push
411                  (list
412                   (incf (cdr active))
413                   (make-full-mail-header
414                    (cdr active) (concat subject " (" group ")") from date
415                    (concat "<" (nnweb-identifier url) "@dejanews>")
416                    nil 0 0 url))
417                  map)
418                 (nnweb-set-hashtb (cadar map) (car map)))))
419           ;; See whether there is a "Get next 20 hits" button here.
420           (goto-char (point-min))
421           (if (or (not (re-search-forward
422                         "HREF=\"\\([^\"]+\\)\"[<>b]+Next result" nil t))
423                   (>= i nnweb-max-hits))
424               (setq more nil)
425             ;; Yup -- fetch it.
426             (setq more (match-string 1))
427             (erase-buffer)
428             (mm-url-insert more)))
429         ;; Return the articles in the right order.
430         (setq nnweb-articles
431               (sort (nconc nnweb-articles map) 'car-less-than-car))))))
432
433 (defun nnweb-dejanews-search (search)
434   (mm-url-insert
435    (concat
436     (nnweb-definition 'address)
437     "?"
438     (mm-url-encode-www-form-urlencoded
439      `(("ST" . "PS")
440        ("svcclass" . "dnyr")
441        ("QRY" . ,search)
442        ("defaultOp" . "AND")
443        ("DBS" . "1")
444        ("OP" . "dnquery.xp")
445        ("LNG" . "ALL")
446        ("maxhits" . "100")
447        ("threaded" . "0")
448        ("format" . "verbose2")
449        ("showsort" . "date")
450        ("agesign" . "1")
451        ("ageweight" . "1")))))
452   t)
453
454 ;; (defun nnweb-dejanewsold-search (search)
455 ;;   (nnweb-fetch-form
456 ;;    (nnweb-definition 'address)
457 ;;    `(("query" . ,search)
458 ;;      ("defaultOp" . "AND")
459 ;;      ("svcclass" . "dnold")
460 ;;      ("maxhits" . "100")
461 ;;      ("format" . "verbose2")
462 ;;      ("threaded" . "0")
463 ;;      ("showsort" . "date")
464 ;;      ("agesign" . "1")
465 ;;      ("ageweight" . "1")))
466 ;;   t)
467
468 (defun nnweb-dejanews-identity (url)
469   "Return an unique identifier based on URL."
470   (if (string-match "AN=\\([0-9]+\\)" url)
471       (match-string 1 url)
472     url))
473
474 ;;;
475 ;;; InReference
476 ;;;
477
478 (defun nnweb-reference-create-mapping ()
479   "Perform the search and create an number-to-url alist."
480   (save-excursion
481     (set-buffer nnweb-buffer)
482     (erase-buffer)
483     (when (funcall (nnweb-definition 'search) nnweb-search)
484       (let ((i 0)
485             (more t)
486             (case-fold-search t)
487             (active (or (cadr (assoc nnweb-group nnweb-group-alist))
488                         (cons 1 0)))
489             Subject Score Date Newsgroups From Message-ID
490             map url)
491         (while more
492           ;; Go through all the article hits on this page.
493           (goto-char (point-min))
494           (search-forward "</pre><hr>" nil t)
495           (delete-region (point-min) (point))
496           (goto-char (point-min))
497           (while (re-search-forward "^ +[0-9]+\\." nil t)
498             (narrow-to-region
499              (point)
500              (if (re-search-forward "^$" nil t)
501                  (match-beginning 0)
502                (point-max)))
503             (goto-char (point-min))
504             (when (looking-at ".*href=\"\\([^\"]+\\)\"")
505               (setq url (match-string 1)))
506             (mm-url-remove-markup)
507             (goto-char (point-min))
508             (while (search-forward "\t" nil t)
509               (replace-match " "))
510             (goto-char (point-min))
511             (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
512               (set (intern (match-string 1)) (match-string 2)))
513             (widen)
514             (search-forward "</pre>" nil t)
515             (incf i)
516             (unless (nnweb-get-hashtb url)
517               (push
518                (list
519                 (incf (cdr active))
520                 (make-full-mail-header
521                  (cdr active) (concat  "(" Newsgroups ") " Subject) From Date
522                  Message-ID
523                  nil 0 (string-to-int Score) url))
524                map)
525               (nnweb-set-hashtb (cadar map) (car map))))
526           (setq more nil))
527         ;; Return the articles in the right order.
528         (setq nnweb-articles
529               (sort (nconc nnweb-articles map) 'car-less-than-car))))))
530
531 (defun nnweb-reference-wash-article ()
532   (let ((case-fold-search t))
533     (goto-char (point-min))
534     (re-search-forward "^</center><hr>" nil t)
535     (delete-region (point-min) (point))
536     (search-forward "<pre>" nil t)
537     (forward-line -1)
538     (let ((body (point-marker)))
539       (search-forward "</pre>" nil t)
540       (delete-region (point) (point-max))
541       (mm-url-remove-markup)
542       (goto-char (point-min))
543       (while (looking-at " *$")
544         (gnus-delete-line))
545       (narrow-to-region (point-min) body)
546       (while (and (re-search-forward "^$" nil t)
547                   (not (eobp)))
548         (gnus-delete-line))
549       (goto-char (point-min))
550       (while (looking-at "\\(^[^ ]+:\\) *")
551         (replace-match "\\1 " t)
552         (forward-line 1))
553       (goto-char (point-min))
554       (when (re-search-forward "^References:" nil t)
555         (narrow-to-region
556          (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
557                      (match-beginning 0)
558                    (point-max)))
559         (goto-char (point-min))
560         (while (not (eobp))
561           (unless (looking-at "References")
562             (insert "\t")
563             (forward-line 1)))
564         (goto-char (point-min))
565         (while (search-forward "," nil t)
566           (replace-match " " t t)))
567       (widen)
568       (mm-url-decode-entities)
569       (set-marker body nil))))
570
571 (defun nnweb-reference-search (search)
572   (mm-url-insert
573    (concat
574     (nnweb-definition 'address)
575     "?"
576     (mm-url-encode-www-form-urlencoded
577      `(("search" . "advanced")
578        ("querytext" . ,search)
579        ("subj" . "")
580        ("name" . "")
581        ("login" . "")
582        ("host" . "")
583        ("organization" . "")
584        ("groups" . "")
585        ("keywords" . "")
586        ("choice" . "Search")
587        ("startmonth" . "Jul")
588        ("startday" . "25")
589        ("startyear" . "1996")
590        ("endmonth" . "Aug")
591        ("endday" . "24")
592        ("endyear" . "1996")
593        ("mode" . "Quick")
594        ("verbosity" . "Verbose")
595        ("ranking" . "Relevance")
596        ("first" . "1")
597        ("last" . "25")
598        ("score" . "50")))))
599   (setq buffer-file-name nil)
600   t)
601
602 ;;;
603 ;;; Alta Vista
604 ;;;
605
606 (defun nnweb-altavista-create-mapping ()
607   "Perform the search and create an number-to-url alist."
608   (save-excursion
609     (set-buffer nnweb-buffer)
610     (erase-buffer)
611     (let ((part 0))
612       (when (funcall (nnweb-definition 'search) nnweb-search part)
613         (let ((i 0)
614               (more t)
615               (case-fold-search t)
616               (active (or (cadr (assoc nnweb-group nnweb-group-alist))
617                           (cons 1 0)))
618               subject date from id group
619               map url)
620           (while more
621             ;; Go through all the article hits on this page.
622             (goto-char (point-min))
623             (search-forward "<dt>" nil t)
624             (delete-region (point-min) (match-beginning 0))
625             (goto-char (point-min))
626             (while (search-forward "<dt>" nil t)
627               (replace-match "\n<blubb>"))
628             (mm-url-decode-entities)
629             (goto-char (point-min))
630             (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
631                                       nil t)
632               (setq url (match-string 1)
633                     subject (match-string 2)
634                     date (match-string 3)
635                     group (match-string 4)
636                     id (concat "<" (match-string 5) ">")
637                     from (match-string 6))
638               (incf i)
639               (unless (nnweb-get-hashtb url)
640                 (push
641                  (list
642                   (incf (cdr active))
643                   (make-full-mail-header
644                    (cdr active) (concat  "(" group ") " subject) from date
645                    id nil 0 0 url))
646                  map)
647                 (nnweb-set-hashtb (cadar map) (car map))))
648             ;; See if we want more.
649             (when (or (not nnweb-articles)
650                       (>= i nnweb-max-hits)
651                       (not (funcall (nnweb-definition 'search)
652                                     nnweb-search (incf part))))
653               (setq more nil)))
654           ;; Return the articles in the right order.
655           (setq nnweb-articles
656                 (sort (nconc nnweb-articles map) 'car-less-than-car)))))))
657
658 (defun nnweb-altavista-wash-article ()
659   (goto-char (point-min))
660   (let ((case-fold-search t))
661     (when (re-search-forward "^<strong>" nil t)
662       (delete-region (point-min) (match-beginning 0)))
663     (goto-char (point-min))
664     (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
665       (replace-match "\\1: \\2" t)
666       (forward-line 1))
667     (when (re-search-backward "^References:" nil t)
668       (narrow-to-region (point) (progn (forward-line 1) (point)))
669       (goto-char (point-min))
670       (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
671         (replace-match "&lt;\\1&gt; " t)))
672     (widen)
673     (mm-url-remove-markup)
674     (mm-url-decode-entities)))
675
676 (defun nnweb-altavista-search (search &optional part)
677   (mm-url-insert
678    (concat
679     (nnweb-definition 'address)
680     "?"
681     (mm-url-encode-www-form-urlencoded
682      `(("pg" . "aq")
683        ("what" . "news")
684        ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
685        ("fmt" . "d")
686        ("q" . ,search)
687        ("r" . "")
688        ("d0" . "")
689        ("d1" . "")))))
690   (setq buffer-file-name nil)
691   t)
692
693 ;;;
694 ;;; Deja bought by google.com
695 ;;;
696
697 (defun nnweb-google-wash-article ()
698   (let ((case-fold-search t) url)
699     (goto-char (point-min))
700     (re-search-forward "^<pre>" nil t)
701     (narrow-to-region (point-min) (point))
702     (search-backward "<table " nil t 2)
703     (delete-region (point-min) (point))
704     (if (re-search-forward "Search Result [0-9]+" nil t)
705         (replace-match ""))
706     (if (re-search-forward "View complete thread ([0-9]+ articles?)" nil t)
707         (replace-match ""))
708     (goto-char (point-min))
709     (while (search-forward "<br>" nil t)
710       (replace-match "\n"))
711     (mm-url-remove-markup)
712     (goto-char (point-min))
713     (while (re-search-forward "^[ \t]*\n" nil t)
714       (replace-match ""))
715     (goto-char (point-max))
716     (insert "\n")
717     (widen)
718     (narrow-to-region (point) (point-max))
719     (search-forward "</pre>" nil t)
720     (delete-region (point) (point-max))
721     (mm-url-remove-markup)
722     (widen)))
723
724 (defun nnweb-google-parse-1 (&optional Message-ID)
725   (let ((i 0)
726         (case-fold-search t)
727         (active (cadr (assoc nnweb-group nnweb-group-alist)))
728         Subject Score Date Newsgroups From
729         map url mid)
730     (unless active
731       (push (list nnweb-group (setq active (cons 1 0))
732                   nnweb-type nnweb-search)
733             nnweb-group-alist))
734     ;; Go through all the article hits on this page.
735     (goto-char (point-min))
736     (while (re-search-forward
737             "a href=/groups\\(\\?[^ \">]*selm=\\([^ &\">]+\\)\\)" nil t)
738       (setq mid (match-string 2)
739             url (format
740                  "http://groups.google.com/groups?selm=%s&output=gplain" mid))
741       (narrow-to-region (search-forward ">" nil t)
742                         (search-forward "</a>" nil t))
743       (mm-url-remove-markup)
744       (mm-url-decode-entities)
745       (setq Subject (buffer-string))
746       (goto-char (point-max))
747       (widen)
748       (forward-line 1)
749       (when (looking-at "<br><font[^>]+>")
750         (goto-char (match-end 0)))
751       (if (not (looking-at "<a[^>]+>"))
752           (skip-chars-forward " \t")
753         (narrow-to-region (point)
754                           (search-forward "</a>" nil t))
755         (mm-url-remove-markup)
756         (mm-url-decode-entities)
757         (setq Newsgroups (buffer-string))
758         (goto-char (point-max))
759         (widen)
760         (skip-chars-forward "- \t"))
761       (when (looking-at
762              "\\([0-9]+\\)[/ ]\\([A-Za-z]+\\)[/ ]\\([0-9]+\\)[ \t]*by[ \t]*\\([^<]*\\) - <a")
763         (setq From (match-string 4)
764               Date (format "%s %s 00:00:00 %s"
765                            (match-string 2) (match-string 1)
766                            (match-string 3))))
767       (forward-line 1)
768       (incf i)
769       (unless (nnweb-get-hashtb url)
770         (push
771          (list
772           (incf (cdr active))
773           (make-full-mail-header
774            (cdr active) (if Newsgroups
775                             (concat  "(" Newsgroups ") " Subject)
776                           Subject)
777            From Date (or Message-ID mid)
778            nil 0 0 url))
779          map)
780         (nnweb-set-hashtb (cadar map) (car map))))
781     map))
782
783 (defun nnweb-google-reference (id)
784   (let ((map (nnweb-google-parse-1 id)) header)
785     (setq nnweb-articles
786           (nconc nnweb-articles map))
787     (when (setq header (cadar map))
788       (mm-with-unibyte-current-buffer
789         (mm-url-insert (mail-header-xref header)))
790       (caar map))))
791
792 (defun nnweb-google-create-mapping ()
793   "Perform the search and create an number-to-url alist."
794   (save-excursion
795     (set-buffer nnweb-buffer)
796     (erase-buffer)
797     (when (funcall (nnweb-definition 'search) nnweb-search)
798       (let ((more t)
799             (i 0))
800         (while more
801           (setq nnweb-articles
802                 (nconc nnweb-articles (nnweb-google-parse-1)))
803           ;; Check if there are more articles to fetch
804           (goto-char (point-min))
805           (incf i 100)
806           (if (or (not (re-search-forward
807                         "<td nowrap><a href=\\([^>]+\\).*<span class=b>Next</span>" nil t))
808                   (>= i nnweb-max-hits))
809               (setq more nil)
810             ;; Yup, there are more articles
811             (setq more (concat "http://groups.google.com" (match-string 1)))
812             (when more
813               (erase-buffer)
814               (mm-url-insert more))))
815         ;; Return the articles in the right order.
816         (setq nnweb-articles
817               (sort nnweb-articles 'car-less-than-car))))))
818
819 (defun nnweb-google-search (search)
820   (mm-url-insert
821    (concat
822     (nnweb-definition 'address)
823     "?"
824     (mm-url-encode-www-form-urlencoded
825      `(("q" . ,search)
826        ("num". "100")
827        ("hq" . "")
828        ("hl" . "")
829        ("lr" . "")
830        ("safe" . "off")
831        ("sites" . "groups")))))
832   t)
833
834 (defun nnweb-google-identity (url)
835   "Return an unique identifier based on URL."
836   (if (string-match "selm=\\([^ &>]+\\)" url)
837       (match-string 1 url)
838     url))
839
840 ;;;
841 ;;; General web/w3 interface utility functions
842 ;;;
843
844 (defun nnweb-insert-html (parse)
845   "Insert HTML based on a w3 parse tree."
846   (if (stringp parse)
847       (insert (nnheader-string-as-multibyte parse))
848     (insert "<" (symbol-name (car parse)) " ")
849     (insert (mapconcat
850              (lambda (param)
851                (concat (symbol-name (car param)) "="
852                        (prin1-to-string
853                         (if (consp (cdr param))
854                             (cadr param)
855                           (cdr param)))))
856              (nth 1 parse)
857              " "))
858     (insert ">\n")
859     (mapcar 'nnweb-insert-html (nth 2 parse))
860     (insert "</" (symbol-name (car parse)) ">\n")))
861
862 (defun nnweb-parse-find (type parse &optional maxdepth)
863   "Find the element of TYPE in PARSE."
864   (catch 'found
865     (nnweb-parse-find-1 type parse maxdepth)))
866
867 (defun nnweb-parse-find-1 (type contents maxdepth)
868   (when (or (null maxdepth)
869             (not (zerop maxdepth)))
870     (when (consp contents)
871       (when (eq (car contents) type)
872         (throw 'found contents))
873       (when (listp (cdr contents))
874         (dolist (element contents)
875           (when (consp element)
876             (nnweb-parse-find-1 type element
877                                 (and maxdepth (1- maxdepth)))))))))
878
879 (defun nnweb-parse-find-all (type parse)
880   "Find all elements of TYPE in PARSE."
881   (catch 'found
882     (nnweb-parse-find-all-1 type parse)))
883
884 (defun nnweb-parse-find-all-1 (type contents)
885   (let (result)
886     (when (consp contents)
887       (if (eq (car contents) type)
888           (push contents result)
889         (when (listp (cdr contents))
890           (dolist (element contents)
891             (when (consp element)
892               (setq result
893                     (nconc result (nnweb-parse-find-all-1 type element))))))))
894     result))
895
896 (defvar nnweb-text)
897 (defun nnweb-text (parse)
898   "Return a list of text contents in PARSE."
899   (let ((nnweb-text nil))
900     (nnweb-text-1 parse)
901     (nreverse nnweb-text)))
902
903 (defun nnweb-text-1 (contents)
904   (dolist (element contents)
905     (if (stringp element)
906         (push element nnweb-text)
907       (when (and (consp element)
908                  (listp (cdr element)))
909         (nnweb-text-1 element)))))
910
911 (provide 'nnweb)
912
913 ;;; nnweb.el ends here