1 ;;; nnweb.el --- retrieving articles via web search engines
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
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)
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.
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.
28 ;; Note: You need to have `w3' installed for some functions to work.
30 ;; FIXME: Due to changes in the HTML output of Gmane, stuff related to Gmane
31 ;; web groups (`gnus-group-make-web-group') doesn't work anymore.
35 (eval-when-compile (require 'cl))
47 (autoload 'w3-parse-buffer "w3-parse")
51 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
52 "Where nnweb will save its files.")
54 (defvoo nnweb-type 'google
55 "What search engine type is being used.
56 Valid types include `google', `dejanews', and `gmane'.")
58 (defvar nnweb-type-definition
60 (id . "http://www.google.com/groups?as_umsgid=%s&hl=en&dmode=source")
61 (result . "http://groups.google.com/group/%s/msg/%s?dmode=source")
62 (article . nnweb-google-wash-article)
63 (reference . identity)
64 (map . nnweb-google-create-mapping)
65 (search . nnweb-google-search)
66 (address . "http://groups.google.com/groups")
67 (base . "http://groups.google.com")
68 (identifier . nnweb-google-identity))
69 (dejanews ;; alias of google
70 (id . "http://www.google.com/groups?as_umsgid=%s&hl=en&dmode=source")
71 (result . "http://groups.google.com/group/%s/msg/%s?dmode=source")
72 (article . nnweb-google-wash-article)
73 (reference . identity)
74 (map . nnweb-google-create-mapping)
75 (search . nnweb-google-search)
76 (address . "http://groups.google.com/groups")
77 (base . "http://groups.google.com")
78 (identifier . nnweb-google-identity))
80 (article . nnweb-gmane-wash-article)
81 (id . "http://gmane.org/view.php?group=%s")
82 (reference . identity)
83 (map . nnweb-gmane-create-mapping)
84 (search . nnweb-gmane-search)
85 (address . "http://gmane.org/")
86 (identifier . nnweb-gmane-identity)))
87 "Type-definition alist.")
89 (defvoo nnweb-search nil
90 "Search string to feed to Google.")
92 (defvoo nnweb-max-hits 999
93 "Maximum number of hits to display.")
95 (defvoo nnweb-ephemeral-p nil
96 "Whether this nnweb server is ephemeral.")
98 ;;; Internal variables
100 (defvoo nnweb-articles nil)
101 (defvoo nnweb-buffer nil)
102 (defvar nnweb-group-alist nil)
103 (defvoo nnweb-group nil)
104 (defvoo nnweb-hashtb nil)
106 ;;; Interface functions
108 (nnoo-define-basics nnweb)
110 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
111 (nnweb-possibly-change-server group server)
113 (set-buffer nntp-server-buffer)
115 (let (article header)
116 (mm-with-unibyte-current-buffer
117 (while (setq article (pop articles))
118 (when (setq header (cadr (assq article nnweb-articles)))
119 (nnheader-insert-nov header))))
122 (deffoo nnweb-request-scan (&optional group server)
123 (nnweb-possibly-change-server group server)
124 (if nnweb-ephemeral-p
125 (setq nnweb-hashtb (gnus-make-hashtable 4095))
126 (unless nnweb-articles
127 (nnweb-read-overview group)))
128 (funcall (nnweb-definition 'map))
129 (unless nnweb-ephemeral-p
131 (nnweb-write-overview group)))
133 (deffoo nnweb-request-group (group &optional server dont-check)
134 (nnweb-possibly-change-server group server)
135 (unless (or nnweb-ephemeral-p
138 (nnweb-read-overview group))
140 ((not nnweb-articles)
141 (nnheader-report 'nnweb "No matching articles"))
143 (let ((active (if nnweb-ephemeral-p
144 (cons (caar nnweb-articles)
145 (caar (last nnweb-articles)))
146 (cadr (assoc group nnweb-group-alist)))))
147 (nnheader-report 'nnweb "Opened group %s" group)
149 "211 %d %d %d %s\n" (length nnweb-articles)
150 (car active) (cdr active) group)))))
152 (deffoo nnweb-close-group (group &optional server)
153 (nnweb-possibly-change-server group server)
154 (when (gnus-buffer-live-p nnweb-buffer)
156 (set-buffer nnweb-buffer)
157 (set-buffer-modified-p nil)
158 (kill-buffer nnweb-buffer)))
161 (deffoo nnweb-request-article (article &optional group server buffer)
162 (nnweb-possibly-change-server group server)
164 (set-buffer (or buffer nntp-server-buffer))
165 (let* ((header (cadr (assq article nnweb-articles)))
166 (url (and header (mail-header-xref header))))
168 (mm-with-unibyte-current-buffer
169 (mm-url-insert url)))
170 (and (stringp article)
171 (nnweb-definition 'id t)
172 (let ((fetch (nnweb-definition 'id))
174 (when (string-match "^<\\(.*\\)>$" article)
175 (setq art (match-string 1 article)))
176 (when (and fetch art)
177 (setq url (format fetch art))
178 (mm-with-unibyte-current-buffer
180 (if (nnweb-definition 'reference t)
182 (funcall (nnweb-definition
183 'reference) article)))))))
184 (unless nnheader-callback-function
185 (funcall (nnweb-definition 'article)))
186 (nnheader-report 'nnweb "Fetched article %s" article)
187 (cons group (and (numberp article) article))))))
189 (deffoo nnweb-close-server (&optional server)
190 (when (and (nnweb-server-opened server)
191 (gnus-buffer-live-p nnweb-buffer))
193 (set-buffer nnweb-buffer)
194 (set-buffer-modified-p nil)
195 (kill-buffer nnweb-buffer)))
196 (nnoo-close-server 'nnweb server))
198 (deffoo nnweb-request-list (&optional server)
199 (nnweb-possibly-change-server nil server)
201 (set-buffer nntp-server-buffer)
202 (nnmail-generate-active (list (assoc server nnweb-group-alist)))
205 (deffoo nnweb-request-update-info (group info &optional server)
206 (nnweb-possibly-change-server group server))
208 (deffoo nnweb-asynchronous-p ()
211 (deffoo nnweb-request-create-group (group &optional server args)
212 (nnweb-possibly-change-server nil server)
213 (nnweb-request-delete-group group)
214 (push `(,group ,(cons 1 0)) nnweb-group-alist)
218 (deffoo nnweb-request-delete-group (group &optional force server)
219 (nnweb-possibly-change-server group server)
220 (gnus-pull group nnweb-group-alist t)
222 (gnus-delete-file (nnweb-overview-file group))
225 (nnoo-define-skeleton nnweb)
227 ;;; Internal functions
229 (defun nnweb-read-overview (group)
230 "Read the overview of GROUP and build the map."
231 (when (file-exists-p (nnweb-overview-file group))
232 (mm-with-unibyte-buffer
233 (nnheader-insert-file-contents (nnweb-overview-file group))
234 (goto-char (point-min))
237 (setq header (nnheader-parse-nov))
239 (push (list (mail-header-number header)
240 header (mail-header-xref header))
242 (nnweb-set-hashtb header (car nnweb-articles)))))))
244 (defun nnweb-write-overview (group)
245 "Write the overview file for GROUP."
246 (with-temp-file (nnweb-overview-file group)
247 (let ((articles nnweb-articles))
249 (nnheader-insert-nov (cadr (pop articles)))))))
251 (defun nnweb-set-hashtb (header data)
252 (gnus-sethash (nnweb-identifier (mail-header-xref header))
255 (defun nnweb-get-hashtb (url)
256 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
258 (defun nnweb-identifier (ident)
259 (funcall (nnweb-definition 'identifier) ident))
261 (defun nnweb-overview-file (group)
262 "Return the name of the overview file of GROUP."
263 (nnheader-concat nnweb-directory group ".overview"))
265 (defun nnweb-write-active ()
266 "Save the active file."
267 (gnus-make-directory nnweb-directory)
268 (with-temp-file (nnheader-concat nnweb-directory "active")
269 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
271 (defun nnweb-read-active ()
272 "Read the active file."
273 (load (nnheader-concat nnweb-directory "active") t t t))
275 (defun nnweb-definition (type &optional noerror)
276 "Return the definition of TYPE."
277 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
280 (error "Undefined definition %s" type))
283 (defun nnweb-possibly-change-server (&optional group server)
285 (unless (nnweb-server-opened server)
286 (nnweb-open-server server))
288 (unless nnweb-group-alist
291 (setq nnweb-hashtb (gnus-make-hashtable 4095)))
293 (setq nnweb-group group)))
295 (defun nnweb-init (server)
296 "Initialize buffers and such."
297 (unless (gnus-buffer-live-p nnweb-buffer)
301 (nnheader-set-temp-buffer
302 (format " *nnweb %s %s %s*"
303 nnweb-type nnweb-search server))
304 (current-buffer))))))
307 ;;; groups.google.com
310 (defun nnweb-google-wash-article ()
311 ;; We have Google's masked e-mail addresses here. :-/
312 (let ((case-fold-search t))
313 (goto-char (point-min))
315 (or (re-search-forward "The requested message.*could not be found."
317 (not (and (re-search-forward "^<pre>" nil t)
318 (re-search-forward "^</pre>" nil t)))))
319 ;; FIXME: Don't know how to indicate "not found".
320 ;; Should this function throw an error? --rsteib
322 (gnus-message 3 "Requested article not found")
324 (delete-region (point-min)
325 (1+ (re-search-forward "^<pre>" nil t)))
326 (goto-char (point-min))
327 (delete-region (- (re-search-forward "^</pre>" nil t) (length "</pre>"))
329 (mm-url-decode-entities))))
331 (defun nnweb-google-parse-1 (&optional Message-ID)
332 "Parse search result in current buffer."
335 (active (cadr (assoc nnweb-group nnweb-group-alist)))
336 Subject Score Date Newsgroups From
339 (push (list nnweb-group (setq active (cons 1 0)))
341 ;; Go through all the article hits on this page.
342 (goto-char (point-min))
345 "a +href=\"/group/\\([^>\"]+\\)/browse_thread/[^>]+#\\([0-9a-f]+\\)"
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)
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))
360 (narrow-to-region (point)
361 (search-forward "</td" nil t))
363 (mm-url-remove-markup)
364 (mm-url-decode-entities)
365 (search-backward " - ")
367 " - \\([a-zA-Z]+\\) \\([0-9]+\\)\\(?: \\([0-9]\\{4\\}\\)\\)?, [^\n]+by \\([^<\n]+\\)\n")
368 (setq From (match-string 4)
369 Date (format "%s %s 00:00:00 %s"
373 (substring (current-time-string) -4)))))
378 (unless (nnweb-get-hashtb url)
382 (make-full-mail-header
383 (cdr active) (if Newsgroups
384 (concat "(" Newsgroups ") " Subject)
386 From Date (or Message-ID mid)
389 (nnweb-set-hashtb (cadar map) (car map))))
392 (defun nnweb-google-reference (id)
393 (let ((map (nnweb-google-parse-1 id)) header)
395 (nconc nnweb-articles map))
396 (when (setq header (cadar map))
397 (mm-with-unibyte-current-buffer
398 (mm-url-insert (mail-header-xref header)))
401 (defun nnweb-google-create-mapping ()
402 "Perform the search and create a number-to-url alist."
404 (set-buffer nnweb-buffer)
406 (when (funcall (nnweb-definition 'search) nnweb-search)
411 (nconc nnweb-articles (nnweb-google-parse-1)))
412 ;; Check if there are more articles to fetch
413 (goto-char (point-min))
415 (if (or (not (re-search-forward
416 "<td><a href=\"\n\\([^>\"]+\\)\"><img src=\"/img/nav_next" nil t))
417 (>= i nnweb-max-hits))
419 ;; Yup, there are more articles
420 (setq more (concat (nnweb-definition 'base) (match-string 1)))
423 (mm-url-insert more))))
424 ;; Return the articles in the right order.
426 (sort nnweb-articles 'car-less-than-car))))))
428 (defun nnweb-google-search (search)
431 (nnweb-definition 'address)
433 (mm-url-encode-www-form-urlencoded
444 (defun nnweb-google-identity (url)
445 "Return an unique identifier based on URL."
446 (if (string-match "selm=\\([^ &>]+\\)" url)
453 (defun nnweb-gmane-create-mapping ()
454 "Perform the search and create a number-to-url alist."
456 (set-buffer nnweb-buffer)
458 (when (funcall (nnweb-definition 'search) nnweb-search)
461 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
465 ;; Remove stuff from the beginning of results
466 (goto-char (point-min))
467 (search-forward "Search Results</h1><ul>" nil t)
468 (delete-region (point-min) (point))
469 (goto-char (point-min))
470 ;; Iterate over the actual hits
471 (while (re-search-forward ".*href=\"\\([^\"]+\\)\">\\(.*\\)" nil t)
472 (setq url (concat "http://gmane.org/" (match-string 1)))
473 (setq subject (match-string 2))
474 (unless (nnweb-get-hashtb url)
478 (make-full-mail-header
479 (cdr active) (concat "(" group ") " subject) nil nil
482 (nnweb-set-hashtb (cadar map) (car map))))
483 ;; Return the articles in the right order.
485 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
487 (defun nnweb-gmane-wash-article ()
488 (let ((case-fold-search t))
489 (goto-char (point-min))
490 (search-forward "<!--X-Head-of-Message-->" nil t)
491 (delete-region (point-min) (point))
492 (goto-char (point-min))
493 (while (looking-at "^<li><em>\\([^ ]+\\)</em>.*</li>")
494 (replace-match "\\1\\2" t)
496 (mm-url-remove-markup)))
498 (defun nnweb-gmane-search (search)
501 (nnweb-definition 'address)
503 (mm-url-encode-www-form-urlencoded
504 `(("query" . ,search)))))
505 (setq buffer-file-name nil)
509 (defun nnweb-gmane-identity (url)
510 "Return a unique identifier based on URL."
511 (if (string-match "group=\\(.+\\)" url)
516 ;;; General web/w3 interface utility functions
519 (defun nnweb-insert-html (parse)
520 "Insert HTML based on a w3 parse tree."
522 (insert (nnheader-string-as-multibyte parse))
523 (insert "<" (symbol-name (car parse)) " ")
526 (concat (symbol-name (car param)) "="
528 (if (consp (cdr param))
534 (mapc 'nnweb-insert-html (nth 2 parse))
535 (insert "</" (symbol-name (car parse)) ">\n")))
537 (defun nnweb-parse-find (type parse &optional maxdepth)
538 "Find the element of TYPE in PARSE."
540 (nnweb-parse-find-1 type parse maxdepth)))
542 (defun nnweb-parse-find-1 (type contents maxdepth)
543 (when (or (null maxdepth)
544 (not (zerop maxdepth)))
545 (when (consp contents)
546 (when (eq (car contents) type)
547 (throw 'found contents))
548 (when (listp (cdr contents))
549 (dolist (element contents)
550 (when (consp element)
551 (nnweb-parse-find-1 type element
552 (and maxdepth (1- maxdepth)))))))))
554 (defun nnweb-parse-find-all (type parse)
555 "Find all elements of TYPE in PARSE."
557 (nnweb-parse-find-all-1 type parse)))
559 (defun nnweb-parse-find-all-1 (type contents)
561 (when (consp contents)
562 (if (eq (car contents) type)
563 (push contents result)
564 (when (listp (cdr contents))
565 (dolist (element contents)
566 (when (consp element)
568 (nconc result (nnweb-parse-find-all-1 type element))))))))
572 (defun nnweb-text (parse)
573 "Return a list of text contents in PARSE."
574 (let ((nnweb-text nil))
576 (nreverse nnweb-text)))
578 (defun nnweb-text-1 (contents)
579 (dolist (element contents)
580 (if (stringp element)
581 (push element nnweb-text)
582 (when (and (consp element)
583 (listp (cdr element)))
584 (nnweb-text-1 element)))))
588 ;;; nnweb.el ends here