1 ;;; nnweb.el --- retrieving articles via web search engines
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 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 3 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
26 ;; Note: You need to have `w3' installed for some functions to work.
30 (eval-when-compile (require 'cl
))
42 (autoload 'w3-parse-buffer
"w3-parse")
46 (defvoo nnweb-directory
(nnheader-concat gnus-directory
"nnweb/")
47 "Where nnweb will save its files.")
49 (defvoo nnweb-type
'google
50 "What search engine type is being used.
51 Valid types include `google', `dejanews', and `gmane'.")
53 (defvar nnweb-type-definition
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
))
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.")
84 (defvoo nnweb-search nil
85 "Search string to feed to Google.")
87 (defvoo nnweb-max-hits
999
88 "Maximum number of hits to display.")
90 (defvoo nnweb-ephemeral-p nil
91 "Whether this nnweb server is ephemeral.")
93 ;;; Internal variables
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
)
101 ;;; Interface functions
103 (nnoo-define-basics nnweb
)
105 (deffoo nnweb-retrieve-headers
(articles &optional group server fetch-old
)
106 (nnweb-possibly-change-server group server
)
108 (set-buffer nntp-server-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
))))
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
126 (nnweb-write-overview group
)))
128 (deffoo nnweb-request-group
(group &optional server dont-check
)
129 (nnweb-possibly-change-server group server
)
130 (unless (or nnweb-ephemeral-p
133 (nnweb-read-overview group
))
135 ((not nnweb-articles
)
136 (nnheader-report 'nnweb
"No matching articles"))
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
)
144 "211 %d %d %d %s\n" (length nnweb-articles
)
145 (car active
) (cdr active
) group
)))))
147 (deffoo nnweb-close-group
(group &optional server
)
148 (nnweb-possibly-change-server group server
)
149 (when (gnus-buffer-live-p nnweb-buffer
)
151 (set-buffer nnweb-buffer
)
152 (set-buffer-modified-p nil
)
153 (kill-buffer nnweb-buffer
)))
156 (deffoo nnweb-request-article
(article &optional group server buffer
)
157 (nnweb-possibly-change-server group server
)
159 (set-buffer (or buffer nntp-server-buffer
))
160 (let* ((header (cadr (assq article nnweb-articles
)))
161 (url (and header
(mail-header-xref header
))))
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
))
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
176 (if (nnweb-definition 'reference t
)
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
))))))
185 (deffoo nnweb-close-server
(&optional server
)
186 (when (and (nnweb-server-opened server
)
187 (gnus-buffer-live-p nnweb-buffer
))
189 (set-buffer nnweb-buffer
)
190 (set-buffer-modified-p nil
)
191 (kill-buffer nnweb-buffer
)))
192 (nnoo-close-server 'nnweb server
))
194 (deffoo nnweb-request-list
(&optional server
)
195 (nnweb-possibly-change-server nil server
)
197 (set-buffer nntp-server-buffer
)
198 (nnmail-generate-active (list (assoc server nnweb-group-alist
)))
201 (deffoo nnweb-request-update-info
(group info
&optional server
)
202 (nnweb-possibly-change-server group server
))
204 (deffoo nnweb-asynchronous-p
()
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
)
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
)
218 (gnus-delete-file (nnweb-overview-file group
))
221 (nnoo-define-skeleton nnweb
)
223 ;;; Internal functions
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))
233 (setq header
(nnheader-parse-nov))
235 (push (list (mail-header-number header
)
236 header
(mail-header-xref header
))
238 (nnweb-set-hashtb header
(car nnweb-articles
)))))))
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
))
245 (nnheader-insert-nov (cadr (pop articles
)))))))
247 (defun nnweb-set-hashtb (header data
)
248 (gnus-sethash (nnweb-identifier (mail-header-xref header
))
251 (defun nnweb-get-hashtb (url)
252 (gnus-gethash (nnweb-identifier url
) nnweb-hashtb
))
254 (defun nnweb-identifier (ident)
255 (funcall (nnweb-definition 'identifier
) ident
))
257 (defun nnweb-overview-file (group)
258 "Return the name of the overview file of GROUP."
259 (nnheader-concat nnweb-directory group
".overview"))
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))))
267 (defun nnweb-read-active ()
268 "Read the active file."
269 (load (nnheader-concat nnweb-directory
"active") t t t
))
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
)))))
276 (error "Undefined definition %s" type
))
279 (defun nnweb-possibly-change-server (&optional group server
)
281 (unless (nnweb-server-opened server
)
282 (nnweb-open-server server
))
284 (unless nnweb-group-alist
287 (setq nnweb-hashtb
(gnus-make-hashtable 4095)))
289 (setq nnweb-group group
)))
291 (defun nnweb-init (server)
292 "Initialize buffers and such."
293 (unless (gnus-buffer-live-p nnweb-buffer
)
296 (nnheader-set-temp-buffer
297 (format " *nnweb %s %s %s*"
298 nnweb-type nnweb-search server
))
299 (mm-disable-multibyte)
303 ;;; groups.google.com
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))
313 (or (re-search-forward "The requested message.*could not be found."
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
320 (gnus-message 3 "Requested article not found")
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
)
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 "</table" nil t
))
363 (mm-url-remove-markup)
364 (mm-url-decode-entities)
365 (goto-char (point-max))
368 "^\\(?:\\(\\w+\\) \\([0-9]+\\)\\|\\S-+\\)\\(?: \\([0-9]\\{4\\}\\)\\)? by ?\\(.*\\)"
370 (setq Date
(if (match-string 1)
371 (format "%s %s 00:00:00 %s"
375 (substring (current-time-string) -
4)))
376 (current-time-string)))
377 (setq From
(match-string 4)))
380 (unless (nnweb-get-hashtb url
)
384 (make-full-mail-header
385 (cdr active
) (if Newsgroups
386 (concat "(" Newsgroups
") " Subject
)
388 From Date
(or Message-ID mid
)
391 (nnweb-set-hashtb (cadar map
) (car map
))))
394 (defun nnweb-google-reference (id)
395 (let ((map (nnweb-google-parse-1 id
)) header
)
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
)))
403 (defun nnweb-google-create-mapping ()
404 "Perform the search and create a number-to-url alist."
406 (set-buffer nnweb-buffer
)
408 (nnheader-message 7 "Searching google...")
409 (when (funcall (nnweb-definition 'search
) nnweb-search
)
414 (nconc nnweb-articles
(nnweb-google-parse-1)))
415 ;; Check if there are more articles to fetch
416 (goto-char (point-min))
418 (if (or (not (re-search-forward
419 "<a [^>]+href=\"\n?\\([^>\" \n\t]+\\)[^<]*<img[^>]+src=[^>]+next"
421 (>= i nnweb-max-hits
))
423 ;; Yup, there are more articles
424 (setq more
(concat (nnweb-definition 'base
) (match-string 1)))
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")
432 (sort nnweb-articles
'car-less-than-car
))))))
434 (defun nnweb-google-search (search)
437 (nnweb-definition 'address
)
439 (mm-url-encode-www-form-urlencoded
441 ("num" .
,(number-to-string
442 (min 100 nnweb-max-hits
)))
451 (defun nnweb-google-identity (url)
452 "Return an unique identifier based on URL."
453 (if (string-match "selm=\\([^ &>]+\\)" url
)
460 (defun nnweb-gmane-create-mapping ()
461 "Perform the search and create a number-to-url alist."
463 (set-buffer nnweb-buffer
)
464 (let ((case-fold-search t
)
465 (active (or (cadr (assoc nnweb-group nnweb-group-alist
))
469 (nnheader-message 7 "Searching Gmane..." )
470 (when (funcall (nnweb-definition 'search
) nnweb-search
)
471 (goto-char (point-min))
472 ;; Skip the status line
474 ;; Thanks to Olly Betts we now have NOV lines in our buffer!
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
485 (format "http://article.gmane.org/%s/%s/raw"
486 (match-string 1 xref
)
487 (match-string 2 xref
))))
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")))
494 (mail-header-set-subject header
495 (rfc2047-encode-string subject
))
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
))))))
502 (nnheader-message 7 "Searching Gmane...done")
504 (sort (nconc nnweb-articles map
) 'car-less-than-car
)))))
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
)
515 (mm-url-remove-markup))))
517 (defun nnweb-gmane-search (search)
520 (nnweb-definition 'address
)
522 (mm-url-encode-www-form-urlencoded
523 `(("query" .
,search
)
524 ("HITSPERPAGE" .
,(number-to-string nnweb-max-hits
))
525 ;;("TOPDOC" . "1000")
527 (setq buffer-file-name nil
)
528 (set-buffer-multibyte t
)
529 (mm-decode-coding-region (point-min) (point-max) 'utf-8
)
532 (defun nnweb-gmane-identity (url)
533 "Return a unique identifier based on URL."
534 (if (string-match "group=\\(.+\\)" url
)
539 ;;; General web/w3 interface utility functions
542 (defun nnweb-insert-html (parse)
543 "Insert HTML based on a w3 parse tree."
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
550 (insert "<" (symbol-name (car parse
)) " ")
553 (concat (symbol-name (car param
)) "="
555 (if (consp (cdr param
))
561 (mapc 'nnweb-insert-html
(nth 2 parse
))
562 (insert "</" (symbol-name (car parse
)) ">\n")))
564 (defun nnweb-parse-find (type parse
&optional maxdepth
)
565 "Find the element of TYPE in PARSE."
567 (nnweb-parse-find-1 type parse maxdepth
)))
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
)))))))))
581 (defun nnweb-parse-find-all (type parse
)
582 "Find all elements of TYPE in PARSE."
584 (nnweb-parse-find-all-1 type parse
)))
586 (defun nnweb-parse-find-all-1 (type contents
)
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
)
595 (nconc result
(nnweb-parse-find-all-1 type element
))))))))
599 (defun nnweb-text (parse)
600 "Return a list of text contents in PARSE."
601 (let ((nnweb-text nil
))
603 (nreverse nnweb-text
)))
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
)))))
615 ;; arch-tag: f59307eb-c90f-479f-b7d2-dbd8bf51b697
616 ;;; nnweb.el ends here