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