* spam-report.el (spam-report-gmane-ham): Renamed from
[gnus] / lisp / nnweb.el
1 ;;; nnweb.el --- retrieving articles via web search engines
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;;   2004, 2005 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; Note: You need to have `w3' installed for some functions to work.
29
30 ;; FIXME: Due to changes in the HTML output of Google Groups and Gmane, stuff
31 ;; related to web groups (gnus-group-make-web-group) doesn't work anymore.
32
33 ;; Fetching an article by MID (cf. gnus-refer-article-method) over Google
34 ;; Groups should work.
35
36 ;;; Code:
37
38 (eval-when-compile (require 'cl))
39
40 (require 'nnoo)
41 (require 'message)
42 (require 'gnus-util)
43 (require 'gnus)
44 (require 'nnmail)
45 (require 'mm-util)
46 (require 'mm-url)
47 (eval-and-compile
48   (ignore-errors
49     (require 'url)))
50 (autoload 'w3-parse-buffer "w3-parse")
51
52 (nnoo-declare nnweb)
53
54 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
55   "Where nnweb will save its files.")
56
57 (defvoo nnweb-type 'google
58   "What search engine type is being used.
59 Valid types include `google', `dejanews', and `gmane'.")
60
61 (defvar nnweb-type-definition
62   '((google
63      (id . "http://www.google.com/groups?as_umsgid=%s&hl=en&dmode=source")
64      (article . nnweb-google-wash-article)
65      (reference . identity)
66      (map . nnweb-google-create-mapping)
67      (search . nnweb-google-search)
68      (address . "http://groups.google.com/groups")
69      (base    . "http://groups.google.com")
70      (identifier . nnweb-google-identity))
71     (dejanews ;; alias of google
72      (article . ignore)
73      (id . "http://groups.google.com/groups?selm=%s&output=gplain")
74      (reference . identity)
75      (map . nnweb-google-create-mapping)
76      (search . nnweb-google-search)
77      (address . "http://groups.google.com/groups")
78      (base    . "http://groups.google.com")
79      (identifier . nnweb-google-identity))
80     (gmane
81      (article . nnweb-gmane-wash-article)
82      (id . "http://gmane.org/view.php?group=%s")
83      (reference . identity)
84      (map . nnweb-gmane-create-mapping)
85      (search . nnweb-gmane-search)
86      (address . "http://gmane.org/")
87      (identifier . nnweb-gmane-identity)))
88   "Type-definition alist.")
89
90 (defvoo nnweb-search nil
91   "Search string to feed to Google.")
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   nil)
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 ;;;
315 ;;; groups.google.com
316 ;;;
317
318 (defun nnweb-google-wash-article ()
319   ;; We have Google's masked e-mail addresses here.  :-/
320   (let ((case-fold-search t))
321     (goto-char (point-min))
322     (if (save-excursion
323           (or (re-search-forward "The requested message.*could not be found."
324                                  nil t)
325               (not (and (re-search-forward "^<pre>" nil t)
326                         (re-search-forward "^</pre>" nil t)))))
327         ;; FIXME: Don't know how to indicate "not found".
328         ;; Should this function throw an error?  --rsteib
329         (progn
330           (gnus-message 3 "Requested article not found")
331           (erase-buffer))
332       (delete-region (point-min)
333                      (1+ (re-search-forward "^<pre>" nil t)))
334       (goto-char (point-min))
335       (delete-region (- (re-search-forward "^</pre>" nil t) (length "</pre>"))
336                      (point-max))
337       (mm-url-decode-entities))))
338
339 (defun nnweb-google-parse-1 (&optional Message-ID)
340   (let ((i 0)
341         (case-fold-search t)
342         (active (cadr (assoc nnweb-group nnweb-group-alist)))
343         Subject Score Date Newsgroups From
344         map url mid)
345     (unless active
346       (push (list nnweb-group (setq active (cons 1 0))
347                   nnweb-type nnweb-search)
348             nnweb-group-alist))
349     ;; Go through all the article hits on this page.
350     (goto-char (point-min))
351     (while (re-search-forward
352             "a href=/groups\\(\\?[^ \">]*selm=\\([^ &\">]+\\)\\)" nil t)
353       (setq mid (match-string 2)
354             url (format
355                  (nnweb-definition 'id) mid))
356       (narrow-to-region (search-forward ">" nil t)
357                         (search-forward "</a>" nil t))
358       (mm-url-remove-markup)
359       (mm-url-decode-entities)
360       (setq Subject (buffer-string))
361       (goto-char (point-max))
362       (widen)
363       (forward-line 2)
364       (when (looking-at "<br><font[^>]+>")
365         (goto-char (match-end 0)))
366       (if (not (looking-at "<a[^>]+>"))
367           (skip-chars-forward " \t")
368         (narrow-to-region (point)
369                           (search-forward "</a>" nil t))
370         (mm-url-remove-markup)
371         (mm-url-decode-entities)
372         (setq Newsgroups (buffer-string))
373         (goto-char (point-max))
374         (widen)
375         (skip-chars-forward "- \t"))
376       (when (looking-at
377              "\\([0-9]+\\)[/ ]\\([A-Za-z]+\\)[/ ]\\([0-9]+\\)[ \t]*by[ \t]*\\([^<]*\\) - <a")
378         (setq From (match-string 4)
379               Date (format "%s %s 00:00:00 %s"
380                            (match-string 2) (match-string 1)
381                            (match-string 3))))
382       (forward-line 1)
383       (incf i)
384       (unless (nnweb-get-hashtb url)
385         (push
386          (list
387           (incf (cdr active))
388           (make-full-mail-header
389            (cdr active) (if Newsgroups
390                             (concat  "(" Newsgroups ") " Subject)
391                           Subject)
392            From Date (or Message-ID mid)
393            nil 0 0 url))
394          map)
395         (nnweb-set-hashtb (cadar map) (car map))))
396     map))
397
398 (defun nnweb-google-reference (id)
399   (let ((map (nnweb-google-parse-1 id)) header)
400     (setq nnweb-articles
401           (nconc nnweb-articles map))
402     (when (setq header (cadar map))
403       (mm-with-unibyte-current-buffer
404         (mm-url-insert (mail-header-xref header)))
405       (caar map))))
406
407 (defun nnweb-google-create-mapping ()
408   "Perform the search and create a number-to-url alist."
409   (save-excursion
410     (set-buffer nnweb-buffer)
411     (erase-buffer)
412     (when (funcall (nnweb-definition 'search) nnweb-search)
413         (let ((more t)
414               (i 0))
415           (while more
416             (setq nnweb-articles
417                   (nconc nnweb-articles (nnweb-google-parse-1)))
418             ;; Check if there are more articles to fetch
419             (goto-char (point-min))
420             (incf i 100)
421             (if (or (not (re-search-forward
422                           "<td nowrap><a href=\\([^>]+\\).*<span class=b>Next</span>" nil t))
423                     (>= i nnweb-max-hits))
424                 (setq more nil)
425               ;; Yup, there are more articles
426               (setq more (concat (nnweb-definition 'base) (match-string 1)))
427             (when more
428               (erase-buffer)
429               (mm-url-insert more))))
430           ;; Return the articles in the right order.
431           (setq nnweb-articles
432                 (sort nnweb-articles 'car-less-than-car))))))
433
434 (defun nnweb-google-search (search)
435   (mm-url-insert
436    (concat
437     (nnweb-definition 'address)
438     "?"
439     (mm-url-encode-www-form-urlencoded
440      `(("q" . ,search)
441        ("num" . "100")
442        ("hq" . "")
443        ("hl" . "en")
444        ("lr" . "")
445        ("safe" . "off")
446        ("sites" . "groups")))))
447   t)
448
449 (defun nnweb-google-identity (url)
450   "Return an unique identifier based on URL."
451   (if (string-match "selm=\\([^ &>]+\\)" url)
452       (match-string 1 url)
453     url))
454
455 ;;;
456 ;;; gmane.org
457 ;;;
458 (defun nnweb-gmane-create-mapping ()
459   "Perform the search and create a number-to-url alist."
460   (save-excursion
461     (set-buffer nnweb-buffer)
462     (erase-buffer)
463     (when (funcall (nnweb-definition 'search) nnweb-search)
464       (let ((more t)
465             (case-fold-search t)
466             (active (or (cadr (assoc nnweb-group nnweb-group-alist))
467                         (cons 1 0)))
468             subject group url
469             map)
470           ;; Remove stuff from the beginning of results
471         (goto-char (point-min))
472         (search-forward "Search Results</h1><ul>" nil t)
473         (delete-region (point-min) (point))
474         (goto-char (point-min))
475         ;; Iterate over the actual hits
476         (while (re-search-forward ".*href=\"\\([^\"]+\\)\">\\(.*\\)" nil t)
477             (setq url (concat "http://gmane.org/" (match-string 1)))
478             (setq subject (match-string 2))
479           (unless (nnweb-get-hashtb url)
480             (push
481              (list
482               (incf (cdr active))
483               (make-full-mail-header
484                (cdr active) (concat  "(" group ") " subject) nil nil
485                nil nil 0 0 url))
486              map)
487             (nnweb-set-hashtb (cadar map) (car map))))
488         ;; Return the articles in the right order.
489         (setq nnweb-articles
490               (sort (nconc nnweb-articles map) 'car-less-than-car))))))
491
492 (defun nnweb-gmane-wash-article ()
493   (let ((case-fold-search t))
494     (goto-char (point-min))
495     (search-forward "<!--X-Head-of-Message-->" nil t)
496     (delete-region (point-min) (point))
497     (goto-char (point-min))
498     (while (looking-at "^<li><em>\\([^ ]+\\)</em>.*</li>")
499       (replace-match "\\1\\2" t)
500       (forward-line 1))
501     (mm-url-remove-markup)))
502
503 (defun nnweb-gmane-search (search)
504   (mm-url-insert
505    (concat
506     (nnweb-definition 'address)
507     "?"
508     (mm-url-encode-www-form-urlencoded
509      `(("query" . ,search)))))
510   (setq buffer-file-name nil)
511   t)
512
513
514 (defun nnweb-gmane-identity (url)
515   "Return a unique identifier based on URL."
516   (if (string-match "group=\\(.+\\)" url)
517       (match-string 1 url)
518     url))
519
520 ;;;
521 ;;; General web/w3 interface utility functions
522 ;;;
523
524 (defun nnweb-insert-html (parse)
525   "Insert HTML based on a w3 parse tree."
526   (if (stringp parse)
527       (insert (nnheader-string-as-multibyte parse))
528     (insert "<" (symbol-name (car parse)) " ")
529     (insert (mapconcat
530              (lambda (param)
531                (concat (symbol-name (car param)) "="
532                        (prin1-to-string
533                         (if (consp (cdr param))
534                             (cadr param)
535                           (cdr param)))))
536              (nth 1 parse)
537              " "))
538     (insert ">\n")
539     (mapc 'nnweb-insert-html (nth 2 parse))
540     (insert "</" (symbol-name (car parse)) ">\n")))
541
542 (defun nnweb-parse-find (type parse &optional maxdepth)
543   "Find the element of TYPE in PARSE."
544   (catch 'found
545     (nnweb-parse-find-1 type parse maxdepth)))
546
547 (defun nnweb-parse-find-1 (type contents maxdepth)
548   (when (or (null maxdepth)
549             (not (zerop maxdepth)))
550     (when (consp contents)
551       (when (eq (car contents) type)
552         (throw 'found contents))
553       (when (listp (cdr contents))
554         (dolist (element contents)
555           (when (consp element)
556             (nnweb-parse-find-1 type element
557                                 (and maxdepth (1- maxdepth)))))))))
558
559 (defun nnweb-parse-find-all (type parse)
560   "Find all elements of TYPE in PARSE."
561   (catch 'found
562     (nnweb-parse-find-all-1 type parse)))
563
564 (defun nnweb-parse-find-all-1 (type contents)
565   (let (result)
566     (when (consp contents)
567       (if (eq (car contents) type)
568           (push contents result)
569         (when (listp (cdr contents))
570           (dolist (element contents)
571             (when (consp element)
572               (setq result
573                     (nconc result (nnweb-parse-find-all-1 type element))))))))
574     result))
575
576 (defvar nnweb-text)
577 (defun nnweb-text (parse)
578   "Return a list of text contents in PARSE."
579   (let ((nnweb-text nil))
580     (nnweb-text-1 parse)
581     (nreverse nnweb-text)))
582
583 (defun nnweb-text-1 (contents)
584   (dolist (element contents)
585     (if (stringp element)
586         (push element nnweb-text)
587       (when (and (consp element)
588                  (listp (cdr element)))
589         (nnweb-text-1 element)))))
590
591 (provide 'nnweb)
592
593 ;;; arch-tag: f59307eb-c90f-479f-b7d2-dbd8bf51b697
594 ;;; nnweb.el ends here