Remove arch-tags from all files, since these are no longer needed.
[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, 2006, 2007, 2008, 2009, 2010 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 3 of the License, or
14 ;; (at your option) 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.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Note: You need to have `w3' installed for some functions to work.
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'nnoo)
33 (require 'message)
34 (require 'gnus-util)
35 (require 'gnus)
36 (require 'nnmail)
37 (require 'mm-util)
38 (require 'mm-url)
39 (eval-and-compile
40   (ignore-errors
41     (require 'url)))
42 (autoload 'w3-parse-buffer "w3-parse")
43
44 (nnoo-declare nnweb)
45
46 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
47   "Where nnweb will save its files.")
48
49 (defvoo nnweb-type 'google
50   "What search engine type is being used.
51 Valid types include `google', `dejanews', and `gmane'.")
52
53 (defvar nnweb-type-definition
54   '((google
55      (id . "http://www.google.com/groups?as_umsgid=%s&hl=en&dmode=source")
56      (result . "http://groups.google.com/group/%s/msg/%s?dmode=source")
57      (article . nnweb-google-wash-article)
58      (reference . identity)
59      (map . nnweb-google-create-mapping)
60      (search . nnweb-google-search)
61      (address . "http://groups.google.com/groups")
62      (base    . "http://groups.google.com")
63      (identifier . nnweb-google-identity))
64     (dejanews ;; alias of google
65      (id . "http://www.google.com/groups?as_umsgid=%s&hl=en&dmode=source")
66      (result . "http://groups.google.com/group/%s/msg/%s?dmode=source")
67      (article . nnweb-google-wash-article)
68      (reference . identity)
69      (map . nnweb-google-create-mapping)
70      (search . nnweb-google-search)
71      (address . "http://groups.google.com/groups")
72      (base    . "http://groups.google.com")
73      (identifier . nnweb-google-identity))
74     (gmane
75      (article . nnweb-gmane-wash-article)
76      (id . "http://gmane.org/view.php?group=%s")
77      (reference . identity)
78      (map . nnweb-gmane-create-mapping)
79      (search . nnweb-gmane-search)
80      (address . "http://search.gmane.org/nov.php")
81      (identifier . nnweb-gmane-identity)))
82   "Type-definition alist.")
83
84 (defvoo nnweb-search nil
85   "Search string to feed to Google.")
86
87 (defvoo nnweb-max-hits 999
88   "Maximum number of hits to display.")
89
90 (defvoo nnweb-ephemeral-p nil
91   "Whether this nnweb server is ephemeral.")
92
93 ;;; Internal variables
94
95 (defvoo nnweb-articles nil)
96 (defvoo nnweb-buffer nil)
97 (defvoo nnweb-group-alist nil)
98 (defvoo nnweb-group nil)
99 (defvoo nnweb-hashtb nil)
100
101 ;;; Interface functions
102
103 (nnoo-define-basics nnweb)
104
105 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
106   (nnweb-possibly-change-server group server)
107   (save-excursion
108     (set-buffer nntp-server-buffer)
109     (erase-buffer)
110     (let (article header)
111       (mm-with-unibyte-current-buffer
112         (while (setq article (pop articles))
113           (when (setq header (cadr (assq article nnweb-articles)))
114             (nnheader-insert-nov header))))
115       'nov)))
116
117 (deffoo nnweb-request-scan (&optional group server)
118   (nnweb-possibly-change-server group server)
119   (if nnweb-ephemeral-p
120       (setq nnweb-hashtb (gnus-make-hashtable 4095))
121     (unless nnweb-articles
122       (nnweb-read-overview group)))
123   (funcall (nnweb-definition 'map))
124   (unless nnweb-ephemeral-p
125     (nnweb-write-active)
126     (nnweb-write-overview group)))
127
128 (deffoo nnweb-request-group (group &optional server dont-check)
129   (nnweb-possibly-change-server group server)
130   (unless (or nnweb-ephemeral-p
131               dont-check
132               nnweb-articles)
133     (nnweb-read-overview group))
134   (cond
135    ((not nnweb-articles)
136     (nnheader-report 'nnweb "No matching articles"))
137    (t
138     (let ((active (if nnweb-ephemeral-p
139                       (cons (caar nnweb-articles)
140                             (caar (last nnweb-articles)))
141                     (cadr (assoc group nnweb-group-alist)))))
142       (nnheader-report 'nnweb "Opened group %s" group)
143       (nnheader-insert
144        "211 %d %d %d %s\n" (length nnweb-articles)
145        (car active) (cdr active) group)))))
146
147 (deffoo nnweb-close-group (group &optional server)
148   (nnweb-possibly-change-server group server)
149   (when (gnus-buffer-live-p nnweb-buffer)
150     (save-excursion
151       (set-buffer nnweb-buffer)
152       (set-buffer-modified-p nil)
153       (kill-buffer nnweb-buffer)))
154   t)
155
156 (deffoo nnweb-request-article (article &optional group server buffer)
157   (nnweb-possibly-change-server group server)
158   (save-excursion
159     (set-buffer (or buffer nntp-server-buffer))
160     (let* ((header (cadr (assq article nnweb-articles)))
161            (url (and header (mail-header-xref header))))
162       (when (or (and url
163                      (mm-with-unibyte-current-buffer
164                        (mm-url-insert url)))
165                 (and (stringp article)
166                      (nnweb-definition 'id t)
167                      (let ((fetch (nnweb-definition 'id))
168                            art active)
169                        (when (string-match "^<\\(.*\\)>$" article)
170                          (setq art (match-string 1 article)))
171                        (when (and fetch art)
172                          (setq url (format fetch
173                                            (mm-url-form-encode-xwfu art)))
174                          (mm-with-unibyte-current-buffer
175                            (mm-url-insert url))
176                          (if (nnweb-definition 'reference t)
177                              (setq article
178                                    (funcall (nnweb-definition
179                                              'reference) article)))))))
180         (unless nnheader-callback-function
181           (funcall (nnweb-definition 'article)))
182         (nnheader-report 'nnweb "Fetched article %s" article)
183         (cons group (and (numberp article) article))))))
184
185 (deffoo nnweb-close-server (&optional server)
186   (when (and (nnweb-server-opened server)
187              (gnus-buffer-live-p nnweb-buffer))
188     (save-excursion
189       (set-buffer nnweb-buffer)
190       (set-buffer-modified-p nil)
191       (kill-buffer nnweb-buffer)))
192   (nnoo-close-server 'nnweb server))
193
194 (deffoo nnweb-request-list (&optional server)
195   (nnweb-possibly-change-server nil server)
196   (save-excursion
197     (set-buffer nntp-server-buffer)
198     (nnmail-generate-active (list (assoc server nnweb-group-alist)))
199     t))
200
201 (deffoo nnweb-request-update-info (group info &optional server)
202   (nnweb-possibly-change-server group server))
203
204 (deffoo nnweb-asynchronous-p ()
205   nil)
206
207 (deffoo nnweb-request-create-group (group &optional server args)
208   (nnweb-possibly-change-server nil server)
209   (nnweb-request-delete-group group)
210   (push `(,group ,(cons 1 0)) nnweb-group-alist)
211   (nnweb-write-active)
212   t)
213
214 (deffoo nnweb-request-delete-group (group &optional force server)
215   (nnweb-possibly-change-server group server)
216   (gnus-pull group nnweb-group-alist t)
217   (nnweb-write-active)
218   (gnus-delete-file (nnweb-overview-file group))
219   t)
220
221 (nnoo-define-skeleton nnweb)
222
223 ;;; Internal functions
224
225 (defun nnweb-read-overview (group)
226   "Read the overview of GROUP and build the map."
227   (when (file-exists-p (nnweb-overview-file group))
228     (mm-with-unibyte-buffer
229       (nnheader-insert-file-contents (nnweb-overview-file group))
230       (goto-char (point-min))
231       (let (header)
232         (while (not (eobp))
233           (setq header (nnheader-parse-nov))
234           (forward-line 1)
235           (push (list (mail-header-number header)
236                       header (mail-header-xref header))
237                 nnweb-articles)
238           (nnweb-set-hashtb header (car nnweb-articles)))))))
239
240 (defun nnweb-write-overview (group)
241   "Write the overview file for GROUP."
242   (with-temp-file (nnweb-overview-file group)
243     (let ((articles nnweb-articles))
244       (while articles
245         (nnheader-insert-nov (cadr (pop articles)))))))
246
247 (defun nnweb-set-hashtb (header data)
248   (gnus-sethash (nnweb-identifier (mail-header-xref header))
249                 data nnweb-hashtb))
250
251 (defun nnweb-get-hashtb (url)
252   (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
253
254 (defun nnweb-identifier (ident)
255   (funcall (nnweb-definition 'identifier) ident))
256
257 (defun nnweb-overview-file (group)
258   "Return the name of the overview file of GROUP."
259   (nnheader-concat nnweb-directory group ".overview"))
260
261 (defun nnweb-write-active ()
262   "Save the active file."
263   (gnus-make-directory nnweb-directory)
264   (with-temp-file (nnheader-concat nnweb-directory "active")
265     (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
266
267 (defun nnweb-read-active ()
268   "Read the active file."
269   (load (nnheader-concat nnweb-directory "active") t t t))
270
271 (defun nnweb-definition (type &optional noerror)
272   "Return the definition of TYPE."
273   (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
274     (when (and (not def)
275                (not noerror))
276       (error "Undefined definition %s" type))
277     def))
278
279 (defun nnweb-possibly-change-server (&optional group server)
280   (when server
281     (unless (nnweb-server-opened server)
282       (nnweb-open-server server))
283     (nnweb-init server))
284   (unless nnweb-group-alist
285     (nnweb-read-active))
286   (unless nnweb-hashtb
287     (setq nnweb-hashtb (gnus-make-hashtable 4095)))
288   (when group
289     (setq nnweb-group group)))
290
291 (defun nnweb-init (server)
292   "Initialize buffers and such."
293   (unless (gnus-buffer-live-p nnweb-buffer)
294     (setq nnweb-buffer
295           (save-current-buffer
296             (nnheader-set-temp-buffer
297              (format " *nnweb %s %s %s*"
298                      nnweb-type nnweb-search server))
299             (mm-disable-multibyte)
300             (current-buffer)))))
301
302 ;;;
303 ;;; groups.google.com
304 ;;;
305
306 (defun nnweb-google-wash-article ()
307   ;; We have Google's masked e-mail addresses here.  :-/
308   (let ((case-fold-search t)
309         (start-re "<pre>[\r\n ]*")
310         (end-re "[\r\n ]*</pre>"))
311     (goto-char (point-min))
312     (if (save-excursion
313           (or (re-search-forward "The requested message.*could not be found."
314                                  nil t)
315               (not (and (re-search-forward start-re nil t)
316                         (re-search-forward end-re nil t)))))
317         ;; FIXME: Don't know how to indicate "not found".
318         ;; Should this function throw an error?  --rsteib
319         (progn
320           (gnus-message 3 "Requested article not found")
321           (erase-buffer))
322       (delete-region (point-min)
323                      (re-search-forward start-re))
324       (goto-char (point-min))
325       (delete-region (progn
326                        (re-search-forward end-re)
327                        (match-beginning 0))
328                      (point-max))
329       (mm-url-decode-entities))))
330
331 (defun nnweb-google-parse-1 (&optional Message-ID)
332   "Parse search result in current buffer."
333   (let ((i 0)
334         (case-fold-search t)
335         (active (cadr (assoc nnweb-group nnweb-group-alist)))
336         Subject Score Date Newsgroups From
337         map url mid)
338     (unless active
339       (push (list nnweb-group (setq active (cons 1 0)))
340             nnweb-group-alist))
341     ;; Go through all the article hits on this page.
342     (goto-char (point-min))
343     (while
344         (re-search-forward
345          "a +href=\"/group/\\([^>\"]+\\)/browse_thread/[^>]+#\\([0-9a-f]+\\)"
346          nil t)
347       (setq Newsgroups (match-string-no-properties 1)
348             ;; Note: Starting with Google Groups 2, `mid' is a Google-internal
349             ;; ID, not a proper Message-ID.
350             mid (match-string-no-properties 2)
351             url (format
352                  (nnweb-definition 'result) Newsgroups mid))
353       (narrow-to-region (search-forward ">" nil t)
354                         (search-forward "</a>" nil t))
355       (mm-url-remove-markup)
356       (mm-url-decode-entities)
357       (setq Subject (buffer-string))
358       (goto-char (point-max))
359       (widen)
360       (narrow-to-region (point)
361                         (search-forward "</table" nil t))
362
363       (mm-url-remove-markup)
364       (mm-url-decode-entities)
365       (goto-char (point-max))
366       (when
367           (re-search-backward
368            "^\\(?:\\(\\w+\\) \\([0-9]+\\)\\|\\S-+\\)\\(?: \\([0-9]\\{4\\}\\)\\)? by ?\\(.*\\)"
369            nil t)
370         (setq Date (if (match-string 1)
371                        (format "%s %s 00:00:00 %s"
372                                (match-string 1)
373                                (match-string 2)
374                                (or (match-string 3)
375                                    (substring (current-time-string) -4)))
376                      (current-time-string)))
377         (setq From (match-string 4)))
378       (widen)
379       (incf i)
380       (unless (nnweb-get-hashtb url)
381         (push
382          (list
383           (incf (cdr active))
384           (make-full-mail-header
385            (cdr active) (if Newsgroups
386                             (concat  "(" Newsgroups ") " Subject)
387                           Subject)
388            From Date (or Message-ID mid)
389            nil 0 0 url))
390          map)
391         (nnweb-set-hashtb (cadar map) (car map))))
392     map))
393
394 (defun nnweb-google-reference (id)
395   (let ((map (nnweb-google-parse-1 id)) header)
396     (setq nnweb-articles
397           (nconc nnweb-articles map))
398     (when (setq header (cadar map))
399       (mm-with-unibyte-current-buffer
400         (mm-url-insert (mail-header-xref header)))
401       (caar map))))
402
403 (defun nnweb-google-create-mapping ()
404   "Perform the search and create a number-to-url alist."
405   (save-excursion
406     (set-buffer nnweb-buffer)
407     (erase-buffer)
408     (nnheader-message 7 "Searching google...")
409     (when (funcall (nnweb-definition 'search) nnweb-search)
410         (let ((more t)
411               (i 0))
412           (while more
413             (setq nnweb-articles
414                   (nconc nnweb-articles (nnweb-google-parse-1)))
415             ;; Check if there are more articles to fetch
416             (goto-char (point-min))
417             (incf i 100)
418             (if (or (not (re-search-forward
419                           "<a [^>]+href=\"\n?\\([^>\" \n\t]+\\)[^<]*<img[^>]+src=[^>]+next"
420                           nil t))
421                     (>= i nnweb-max-hits))
422                 (setq more nil)
423               ;; Yup, there are more articles
424               (setq more (concat (nnweb-definition 'base) (match-string 1)))
425             (when more
426               (erase-buffer)
427               (nnheader-message 7 "Searching google...(%d)" i)
428               (mm-url-insert more))))
429           ;; Return the articles in the right order.
430           (nnheader-message 7 "Searching google...done")
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" . ,(number-to-string
442                   (min 100 nnweb-max-hits)))
443        ("hq" . "")
444        ("hl" . "en")
445        ("lr" . "")
446        ("safe" . "off")
447        ("sites" . "groups")
448        ("filter" . "0")))))
449   t)
450
451 (defun nnweb-google-identity (url)
452   "Return an unique identifier based on URL."
453   (if (string-match "selm=\\([^ &>]+\\)" url)
454       (match-string 1 url)
455     url))
456
457 ;;;
458 ;;; gmane.org
459 ;;;
460 (defun nnweb-gmane-create-mapping ()
461   "Perform the search and create a number-to-url alist."
462   (save-excursion
463     (set-buffer nnweb-buffer)
464     (let ((case-fold-search t)
465           (active (or (cadr (assoc nnweb-group nnweb-group-alist))
466                       (cons 1 0)))
467           map)
468       (erase-buffer)
469       (nnheader-message 7 "Searching Gmane..." )
470       (when (funcall (nnweb-definition 'search) nnweb-search)
471         (goto-char (point-min))
472         ;; Skip the status line
473         (forward-line 1)
474         ;; Thanks to Olly Betts we now have NOV lines in our buffer!
475         (while (not (eobp))
476           (unless (or (eolp) (looking-at "\x0d"))
477             (let ((header (nnheader-parse-nov)))
478               (let ((xref (mail-header-xref header))
479                     (from (mail-header-from header))
480                     (subject (mail-header-subject header))
481                     (rfc2047-encoding-type 'mime))
482                 (when (string-match " \\([^:]+\\)[:/]\\([0-9]+\\)" xref)
483                   (mail-header-set-xref
484                    header
485                    (format "http://article.gmane.org/%s/%s/raw"
486                            (match-string 1 xref)
487                            (match-string 2 xref))))
488
489                 ;; Add host part to gmane-encrypted addresses
490                 (when (string-match "@$" from)
491                   (mail-header-set-from header
492                                         (concat from "public.gmane.org")))
493
494                 (mail-header-set-subject header
495                                          (rfc2047-encode-string subject))
496
497                 (unless (nnweb-get-hashtb (mail-header-xref header))
498                   (mail-header-set-number header (incf (cdr active)))
499                   (push (list (mail-header-number header) header) map)
500                   (nnweb-set-hashtb (cadar map) (car map))))))
501           (forward-line 1)))
502       (nnheader-message 7 "Searching Gmane...done")
503       (setq nnweb-articles
504             (sort (nconc nnweb-articles map) 'car-less-than-car)))))
505
506 (defun nnweb-gmane-wash-article ()
507   (let ((case-fold-search t))
508     (goto-char (point-min))
509     (when (search-forward "<!--X-Head-of-Message-->" nil t)
510       (delete-region (point-min) (point))
511       (goto-char (point-min))
512       (while (looking-at "^<li><em>\\([^ ]+\\)</em>.*</li>")
513         (replace-match "\\1\\2" t)
514         (forward-line 1))
515       (mm-url-remove-markup))))
516
517 (defun nnweb-gmane-search (search)
518   (mm-url-insert
519    (concat
520     (nnweb-definition 'address)
521     "?"
522     (mm-url-encode-www-form-urlencoded
523      `(("query" . ,search)
524        ("HITSPERPAGE" . ,(number-to-string nnweb-max-hits))
525        ;;("TOPDOC" . "1000")
526        ))))
527   (setq buffer-file-name nil)
528   (unless (featurep 'xemacs) (set-buffer-multibyte t))
529   (mm-decode-coding-region (point-min) (point-max) 'utf-8)
530   t)
531
532 (defun nnweb-gmane-identity (url)
533   "Return a unique identifier based on URL."
534   (if (string-match "group=\\(.+\\)" url)
535       (match-string 1 url)
536     url))
537
538 ;;;
539 ;;; General web/w3 interface utility functions
540 ;;;
541
542 (defun nnweb-insert-html (parse)
543   "Insert HTML based on a w3 parse tree."
544   (if (stringp parse)
545       ;; We used to call nnheader-string-as-multibyte here, but it cannot
546       ;; be right, so I removed it.  If a bug shows up because of this change,
547       ;; please do not blindly revert the change, but help me find the real
548       ;; cause of the bug instead.  --Stef
549       (insert parse)
550     (insert "<" (symbol-name (car parse)) " ")
551     (insert (mapconcat
552              (lambda (param)
553                (concat (symbol-name (car param)) "="
554                        (prin1-to-string
555                         (if (consp (cdr param))
556                             (cadr param)
557                           (cdr param)))))
558              (nth 1 parse)
559              " "))
560     (insert ">\n")
561     (mapc 'nnweb-insert-html (nth 2 parse))
562     (insert "</" (symbol-name (car parse)) ">\n")))
563
564 (defun nnweb-parse-find (type parse &optional maxdepth)
565   "Find the element of TYPE in PARSE."
566   (catch 'found
567     (nnweb-parse-find-1 type parse maxdepth)))
568
569 (defun nnweb-parse-find-1 (type contents maxdepth)
570   (when (or (null maxdepth)
571             (not (zerop maxdepth)))
572     (when (consp contents)
573       (when (eq (car contents) type)
574         (throw 'found contents))
575       (when (listp (cdr contents))
576         (dolist (element contents)
577           (when (consp element)
578             (nnweb-parse-find-1 type element
579                                 (and maxdepth (1- maxdepth)))))))))
580
581 (defun nnweb-parse-find-all (type parse)
582   "Find all elements of TYPE in PARSE."
583   (catch 'found
584     (nnweb-parse-find-all-1 type parse)))
585
586 (defun nnweb-parse-find-all-1 (type contents)
587   (let (result)
588     (when (consp contents)
589       (if (eq (car contents) type)
590           (push contents result)
591         (when (listp (cdr contents))
592           (dolist (element contents)
593             (when (consp element)
594               (setq result
595                     (nconc result (nnweb-parse-find-all-1 type element))))))))
596     result))
597
598 (defvar nnweb-text)
599 (defun nnweb-text (parse)
600   "Return a list of text contents in PARSE."
601   (let ((nnweb-text nil))
602     (nnweb-text-1 parse)
603     (nreverse nnweb-text)))
604
605 (defun nnweb-text-1 (contents)
606   (dolist (element contents)
607     (if (stringp element)
608         (push element nnweb-text)
609       (when (and (consp element)
610                  (listp (cdr element)))
611         (nnweb-text-1 element)))))
612
613 (provide 'nnweb)
614
615 ;;; nnweb.el ends here