1 ;;; nnweb.el --- retrieving articles via web search engines
3 ;; Copyright (C) 1996-2016 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl
))
42 (defvoo nnweb-directory
(nnheader-concat gnus-directory
"nnweb/")
43 "Where nnweb will save its files.")
45 (defvoo nnweb-type
'google
46 "What search engine type is being used.
47 Valid types include `google', `dejanews', and `gmane'.")
49 (defvar nnweb-type-definition
51 (id .
"http://www.google.com/groups?as_umsgid=%s&hl=en&dmode=source")
52 (result .
"http://groups.google.com/group/%s/msg/%s?dmode=source")
53 (article . nnweb-google-wash-article
)
54 (reference . identity
)
55 (map . nnweb-google-create-mapping
)
56 (search . nnweb-google-search
)
57 (address .
"http://groups.google.com/groups")
58 (base .
"http://groups.google.com")
59 (identifier . nnweb-google-identity
))
60 (dejanews ;; alias of google
61 (id .
"http://www.google.com/groups?as_umsgid=%s&hl=en&dmode=source")
62 (result .
"http://groups.google.com/group/%s/msg/%s?dmode=source")
63 (article . nnweb-google-wash-article
)
64 (reference . identity
)
65 (map . nnweb-google-create-mapping
)
66 (search . nnweb-google-search
)
67 (address .
"http://groups.google.com/groups")
68 (base .
"http://groups.google.com")
69 (identifier . nnweb-google-identity
))
71 (article . nnweb-gmane-wash-article
)
72 (id .
"http://gmane.org/view.php?group=%s")
73 (reference . identity
)
74 (map . nnweb-gmane-create-mapping
)
75 (search . nnweb-gmane-search
)
76 (address .
"http://search.gmane.org/nov.php")
77 (identifier . nnweb-gmane-identity
)))
78 "Type-definition alist.")
80 (defvoo nnweb-search nil
81 "Search string to feed to Google.")
83 (defvoo nnweb-max-hits
999
84 "Maximum number of hits to display.")
86 (defvoo nnweb-ephemeral-p nil
87 "Whether this nnweb server is ephemeral.")
89 ;;; Internal variables
91 (defvoo nnweb-articles nil
)
92 (defvoo nnweb-buffer nil
)
93 (defvoo nnweb-group-alist nil
)
94 (defvoo nnweb-group nil
)
95 (defvoo nnweb-hashtb nil
)
97 ;;; Interface functions
99 (nnoo-define-basics nnweb
)
101 (deffoo nnweb-retrieve-headers
(articles &optional group server fetch-old
)
102 (nnweb-possibly-change-server group server
)
103 (with-current-buffer nntp-server-buffer
105 (let (article header
)
106 (mm-with-unibyte-current-buffer
107 (while (setq article
(pop articles
))
108 (when (setq header
(cadr (assq article nnweb-articles
)))
109 (nnheader-insert-nov header
))))
112 (deffoo nnweb-request-scan
(&optional group server
)
113 (nnweb-possibly-change-server group server
)
114 (if nnweb-ephemeral-p
115 (setq nnweb-hashtb
(gnus-make-hashtable 4095))
116 (unless nnweb-articles
117 (nnweb-read-overview group
)))
118 (funcall (nnweb-definition 'map
))
119 (unless nnweb-ephemeral-p
121 (nnweb-write-overview group
)))
123 (deffoo nnweb-request-group
(group &optional server dont-check info
)
124 (nnweb-possibly-change-server group server
)
125 (unless (or nnweb-ephemeral-p
128 (nnweb-read-overview group
))
130 ((not nnweb-articles
)
131 (nnheader-report 'nnweb
"No matching articles"))
133 (let ((active (if nnweb-ephemeral-p
134 (cons (caar nnweb-articles
)
135 (caar (last nnweb-articles
)))
136 (cadr (assoc group nnweb-group-alist
)))))
137 (nnheader-report 'nnweb
"Opened group %s" group
)
139 "211 %d %d %d %s\n" (length nnweb-articles
)
140 (car active
) (cdr active
) group
)))))
142 (deffoo nnweb-close-group
(group &optional server
)
143 (nnweb-possibly-change-server group server
)
144 (when (gnus-buffer-live-p nnweb-buffer
)
145 (with-current-buffer nnweb-buffer
146 (set-buffer-modified-p nil
)
147 (kill-buffer nnweb-buffer
)))
150 (deffoo nnweb-request-article
(article &optional group server buffer
)
151 (nnweb-possibly-change-server group server
)
152 (with-current-buffer (or buffer nntp-server-buffer
)
153 (let* ((header (cadr (assq article nnweb-articles
)))
154 (url (and header
(mail-header-xref header
))))
156 (mm-with-unibyte-current-buffer
157 (mm-url-insert url
)))
158 (and (stringp article
)
159 (nnweb-definition 'id t
)
160 (let ((fetch (nnweb-definition 'id
))
162 (when (string-match "^<\\(.*\\)>$" article
)
163 (setq art
(match-string 1 article
)))
164 (when (and fetch art
)
165 (setq url
(format fetch
166 (mm-url-form-encode-xwfu art
)))
167 (mm-with-unibyte-current-buffer
169 (if (nnweb-definition 'reference t
)
171 (funcall (nnweb-definition
172 'reference
) article
)))))))
173 (unless nnheader-callback-function
174 (funcall (nnweb-definition 'article
)))
175 (nnheader-report 'nnweb
"Fetched article %s" article
)
176 (cons group
(and (numberp article
) article
))))))
178 (deffoo nnweb-close-server
(&optional server
)
179 (when (and (nnweb-server-opened server
)
180 (gnus-buffer-live-p nnweb-buffer
))
181 (with-current-buffer nnweb-buffer
182 (set-buffer-modified-p nil
)
183 (kill-buffer nnweb-buffer
)))
184 (nnoo-close-server 'nnweb server
))
186 (deffoo nnweb-request-list
(&optional server
)
187 (nnweb-possibly-change-server nil server
)
188 (with-current-buffer nntp-server-buffer
189 (nnmail-generate-active (list (assoc server nnweb-group-alist
)))
192 (deffoo nnweb-request-update-info
(group info
&optional server
))
194 (deffoo nnweb-asynchronous-p
()
197 (deffoo nnweb-request-create-group
(group &optional server args
)
198 (nnweb-possibly-change-server nil server
)
199 (nnweb-request-delete-group group
)
200 (push `(,group
,(cons 1 0)) nnweb-group-alist
)
204 (deffoo nnweb-request-delete-group
(group &optional force server
)
205 (nnweb-possibly-change-server group server
)
206 (gnus-alist-pull group nnweb-group-alist t
)
208 (gnus-delete-file (nnweb-overview-file group
))
211 (nnoo-define-skeleton nnweb
)
213 ;;; Internal functions
215 (defun nnweb-read-overview (group)
216 "Read the overview of GROUP and build the map."
217 (when (file-exists-p (nnweb-overview-file group
))
218 (mm-with-unibyte-buffer
219 (nnheader-insert-file-contents (nnweb-overview-file group
))
220 (goto-char (point-min))
223 (setq header
(nnheader-parse-nov))
225 (push (list (mail-header-number header
)
226 header
(mail-header-xref header
))
228 (nnweb-set-hashtb header
(car nnweb-articles
)))))))
230 (defun nnweb-write-overview (group)
231 "Write the overview file for GROUP."
232 (with-temp-file (nnweb-overview-file group
)
233 (let ((articles nnweb-articles
))
235 (nnheader-insert-nov (cadr (pop articles
)))))))
237 (defun nnweb-set-hashtb (header data
)
238 (gnus-sethash (nnweb-identifier (mail-header-xref header
))
241 (defun nnweb-get-hashtb (url)
242 (gnus-gethash (nnweb-identifier url
) nnweb-hashtb
))
244 (defun nnweb-identifier (ident)
245 (funcall (nnweb-definition 'identifier
) ident
))
247 (defun nnweb-overview-file (group)
248 "Return the name of the overview file of GROUP."
249 (nnheader-concat nnweb-directory group
".overview"))
251 (defun nnweb-write-active ()
252 "Save the active file."
253 (gnus-make-directory nnweb-directory
)
254 (with-temp-file (nnheader-concat nnweb-directory
"active")
255 (prin1 `(setq nnweb-group-alist
',nnweb-group-alist
) (current-buffer))))
257 (defun nnweb-read-active ()
258 "Read the active file."
259 (load (nnheader-concat nnweb-directory
"active") t t t
))
261 (defun nnweb-definition (type &optional noerror
)
262 "Return the definition of TYPE."
263 (let ((def (cdr (assq type
(assq nnweb-type nnweb-type-definition
)))))
266 (error "Undefined definition %s" type
))
269 (defun nnweb-possibly-change-server (&optional group server
)
271 (unless (nnweb-server-opened server
)
272 (nnweb-open-server server
))
274 (unless nnweb-group-alist
277 (setq nnweb-hashtb
(gnus-make-hashtable 4095)))
279 (setq nnweb-group group
)))
281 (defun nnweb-init (server)
282 "Initialize buffers and such."
283 (unless (gnus-buffer-live-p nnweb-buffer
)
286 (nnheader-set-temp-buffer
287 (format " *nnweb %s %s %s*"
288 nnweb-type nnweb-search server
))
289 (mm-disable-multibyte)
293 ;;; groups.google.com
296 (defun nnweb-google-wash-article ()
297 ;; We have Google's masked e-mail addresses here. :-/
298 (let ((case-fold-search t
)
299 (start-re "<pre>[\r\n ]*")
300 (end-re "[\r\n ]*</pre>"))
301 (goto-char (point-min))
303 (or (re-search-forward "The requested message.*could not be found."
305 (not (and (re-search-forward start-re nil t
)
306 (re-search-forward end-re nil t
)))))
307 ;; FIXME: Don't know how to indicate "not found".
308 ;; Should this function throw an error? --rsteib
310 (gnus-message 3 "Requested article not found")
312 (delete-region (point-min)
313 (re-search-forward start-re
))
314 (goto-char (point-min))
315 (delete-region (progn
316 (re-search-forward end-re
)
319 (mm-url-decode-entities))))
321 (defun nnweb-google-parse-1 (&optional Message-ID
)
322 "Parse search result in current buffer."
325 (active (cadr (assoc nnweb-group nnweb-group-alist
)))
326 Subject Score Date Newsgroups From
329 (push (list nnweb-group
(setq active
(cons 1 0)))
331 ;; Go through all the article hits on this page.
332 (goto-char (point-min))
335 "a +href=\"/group/\\([^>\"]+\\)/browse_thread/[^>]+#\\([0-9a-f]+\\)"
337 (setq Newsgroups
(match-string-no-properties 1)
338 ;; Note: Starting with Google Groups 2, `mid' is a Google-internal
339 ;; ID, not a proper Message-ID.
340 mid
(match-string-no-properties 2)
342 (nnweb-definition 'result
) Newsgroups mid
))
343 (narrow-to-region (search-forward ">" nil t
)
344 (search-forward "</a>" nil t
))
345 (mm-url-remove-markup)
346 (mm-url-decode-entities)
347 (setq Subject
(buffer-string))
348 (goto-char (point-max))
350 (narrow-to-region (point)
351 (search-forward "</table" nil t
))
353 (mm-url-remove-markup)
354 (mm-url-decode-entities)
355 (goto-char (point-max))
358 "^\\(?:\\(\\w+\\) \\([0-9]+\\)\\|\\S-+\\)\\(?: \\([0-9]\\{4\\}\\)\\)? by ?\\(.*\\)"
360 (setq Date
(if (match-string 1)
361 (format "%s %s 00:00:00 %s"
365 (format-time-string "%Y")))
366 (current-time-string)))
367 (setq From
(match-string 4)))
370 (unless (nnweb-get-hashtb url
)
374 (make-full-mail-header
375 (cdr active
) (if Newsgroups
376 (concat "(" Newsgroups
") " Subject
)
378 From Date
(or Message-ID mid
)
381 (nnweb-set-hashtb (cadar map
) (car map
))))
384 (defun nnweb-google-reference (id)
385 (let ((map (nnweb-google-parse-1 id
)) header
)
387 (nconc nnweb-articles map
))
388 (when (setq header
(cadar map
))
389 (mm-with-unibyte-current-buffer
390 (mm-url-insert (mail-header-xref header
)))
393 (defun nnweb-google-create-mapping ()
394 "Perform the search and create a number-to-url alist."
395 (with-current-buffer nnweb-buffer
397 (nnheader-message 7 "Searching google...")
398 (when (funcall (nnweb-definition 'search
) nnweb-search
)
403 (nconc nnweb-articles
(nnweb-google-parse-1)))
404 ;; Check if there are more articles to fetch
405 (goto-char (point-min))
407 (if (or (not (re-search-forward
408 "<a [^>]+href=\"\n?\\([^>\" \n\t]+\\)[^<]*<img[^>]+src=[^>]+next"
410 (>= i nnweb-max-hits
))
412 ;; Yup, there are more articles
413 (setq more
(concat (nnweb-definition 'base
) (match-string 1)))
416 (nnheader-message 7 "Searching google...(%d)" i
)
417 (mm-url-insert more
))))
418 ;; Return the articles in the right order.
419 (nnheader-message 7 "Searching google...done")
421 (sort nnweb-articles
'car-less-than-car
))))))
423 (defun nnweb-google-search (search)
426 (nnweb-definition 'address
)
428 (mm-url-encode-www-form-urlencoded
430 ("num" .
,(number-to-string
431 (min 100 nnweb-max-hits
)))
440 (defun nnweb-google-identity (url)
441 "Return a unique identifier based on URL."
442 (if (string-match "selm=\\([^ &>]+\\)" url
)
449 (defun nnweb-gmane-create-mapping ()
450 "Perform the search and create a number-to-url alist."
451 (with-current-buffer nnweb-buffer
452 (let ((case-fold-search t
)
453 (active (or (cadr (assoc nnweb-group nnweb-group-alist
))
457 (nnheader-message 7 "Searching Gmane..." )
458 (when (funcall (nnweb-definition 'search
) nnweb-search
)
459 (goto-char (point-min))
460 ;; Skip the status line
462 ;; Thanks to Olly Betts we now have NOV lines in our buffer!
464 (unless (or (eolp) (looking-at "\x0d"))
465 (let ((header (nnheader-parse-nov)))
466 (let ((xref (mail-header-xref header
))
467 (from (mail-header-from header
))
468 (subject (mail-header-subject header
))
469 (rfc2047-encoding-type 'mime
))
470 (when (string-match " \\([^:]+\\)[:/]\\([0-9]+\\)" xref
)
471 (mail-header-set-xref
473 (format "http://article.gmane.org/%s/%s/raw"
474 (match-string 1 xref
)
475 (match-string 2 xref
))))
477 ;; Add host part to gmane-encrypted addresses
478 (when (string-match "@$" from
)
479 (mail-header-set-from header
480 (concat from
"public.gmane.org")))
482 (mail-header-set-subject header
483 (rfc2047-encode-string subject
))
485 (unless (nnweb-get-hashtb (mail-header-xref header
))
486 (mail-header-set-number header
(incf (cdr active
)))
487 (push (list (mail-header-number header
) header
) map
)
488 (nnweb-set-hashtb (cadar map
) (car map
))))))
490 (nnheader-message 7 "Searching Gmane...done")
492 (sort (nconc nnweb-articles map
) 'car-less-than-car
)))))
494 (defun nnweb-gmane-wash-article ()
495 (let ((case-fold-search t
))
496 (goto-char (point-min))
497 (when (search-forward "<!--X-Head-of-Message-->" nil t
)
498 (delete-region (point-min) (point))
499 (goto-char (point-min))
500 (while (looking-at "^<li><em>\\([^ ]+\\)</em>.*</li>")
501 (replace-match "\\1\\2" t
)
503 (mm-url-remove-markup))))
505 (defun nnweb-gmane-search (search)
508 (nnweb-definition 'address
)
510 (mm-url-encode-www-form-urlencoded
511 `(("query" .
,search
)
512 ("HITSPERPAGE" .
,(number-to-string nnweb-max-hits
))
513 ;;("TOPDOC" . "1000")
515 (setq buffer-file-name nil
)
516 (unless (featurep 'xemacs
) (set-buffer-multibyte t
))
517 (mm-decode-coding-region (point-min) (point-max) 'utf-8
)
520 (defun nnweb-gmane-identity (url)
521 "Return a unique identifier based on URL."
522 (if (string-match "group=\\(.+\\)" url
)
527 ;;; General web interface utility functions
530 (defun nnweb-insert-html (parse)
531 "Insert HTML based on a w3 parse tree."
533 ;; We used to call nnheader-string-as-multibyte here, but it cannot
534 ;; be right, so I removed it. If a bug shows up because of this change,
535 ;; please do not blindly revert the change, but help me find the real
536 ;; cause of the bug instead. --Stef
538 (insert "<" (symbol-name (car parse
)) " ")
541 (concat (symbol-name (car param
)) "="
543 (if (consp (cdr param
))
549 (mapc 'nnweb-insert-html
(nth 2 parse
))
550 (insert "</" (symbol-name (car parse
)) ">\n")))
552 (defun nnweb-parse-find (type parse
&optional maxdepth
)
553 "Find the element of TYPE in PARSE."
555 (nnweb-parse-find-1 type parse maxdepth
)))
557 (defun nnweb-parse-find-1 (type contents maxdepth
)
558 (when (or (null maxdepth
)
559 (not (zerop maxdepth
)))
560 (when (consp contents
)
561 (when (eq (car contents
) type
)
562 (throw 'found contents
))
563 (when (listp (cdr contents
))
564 (dolist (element contents
)
565 (when (consp element
)
566 (nnweb-parse-find-1 type element
567 (and maxdepth
(1- maxdepth
)))))))))
569 (defun nnweb-parse-find-all (type parse
)
570 "Find all elements of TYPE in PARSE."
572 (nnweb-parse-find-all-1 type parse
)))
574 (defun nnweb-parse-find-all-1 (type contents
)
576 (when (consp contents
)
577 (if (eq (car contents
) type
)
578 (push contents result
)
579 (when (listp (cdr contents
))
580 (dolist (element contents
)
581 (when (consp element
)
583 (nconc result
(nnweb-parse-find-all-1 type element
))))))))
587 (defun nnweb-text (parse)
588 "Return a list of text contents in PARSE."
589 (let ((nnweb-text nil
))
591 (nreverse nnweb-text
)))
593 (defun nnweb-text-1 (contents)
594 (dolist (element contents
)
595 (if (stringp element
)
596 (push element nnweb-text
)
597 (when (and (consp element
)
598 (listp (cdr element
)))
599 (nnweb-text-1 element
)))))
603 ;;; nnweb.el ends here