c4360170a0f2531acb28cbcd717a131143f1403e
[gnus] / lisp / nnslashdot.el
1 ;;; nnslashdot.el --- interfacing with Slashdot
2 ;; Copyright (C) 1999 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; Note: You need to have `url' and `w3' installed for this
27 ;; backend to work.
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32
33 (require 'nnoo)
34 (require 'message)
35 (require 'gnus-util)
36 (require 'gnus)
37 (require 'nnmail)
38 (require 'mm-util)
39 (require 'nnweb)
40 (eval-when-compile
41   (ignore-errors
42     (require 'w3)
43     (require 'url)
44     (require 'w3-forms)))
45 ;; Report failure to find w3 at load time if appropriate.
46 (eval '(progn
47          (require 'w3)
48          (require 'url)
49          (require 'w3-forms)))
50
51 (nnoo-declare nnslashdot)
52
53 (defvoo nnslashdot-directory (nnheader-concat gnus-directory "slashdot/")
54   "Where nnslashdot will save its files.")
55
56 (defvoo nnslashdot-active-url "http://slashdot.org/search.pl?section=&min=%d"
57   "Where nnslashdot will fetch the active file from.")
58
59 (defvoo nnslashdot-comments-url "http://slashdot.org/comments.pl?sid=%s&threshold=%d&commentsort=%d&mode=flat&startat=%d"
60   "Where nnslashdot will fetch comments from.")
61
62 (defvoo nnslashdot-article-url
63     "http://slashdot.org/article.pl?sid=%s&mode=nocomment"
64   "Where nnslashdot will fetch the article from.")
65
66 (defvoo nnslashdot-threshold -1
67   "The article threshold.")
68
69 (defvoo nnslashdot-threaded t
70   "Whether the nnslashdot groups should be threaded or not.")
71
72 (defvoo nnslashdot-group-number 30
73   "The number of non-fresh groups to keep updated.")
74
75 ;;; Internal variables
76
77 (defvar nnslashdot-groups nil)
78 (defvar nnslashdot-buffer nil)
79 (defvar nnslashdot-headers nil)
80
81 ;;; Interface functions
82
83 (nnoo-define-basics nnslashdot)
84
85 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
86   (nnslashdot-possibly-change-server group server)
87   (unless gnus-nov-is-evil
88     (if nnslashdot-threaded
89         (nnslashdot-threaded-retrieve-headers articles group)
90       (nnslashdot-sane-retrieve-headers articles group))))
91   
92 (deffoo nnslashdot-threaded-retrieve-headers (articles group)
93   (let ((last (car (last articles)))
94         (did nil)
95         (start 1)
96         (sid (caddr (assoc group nnslashdot-groups)))
97         (first-comments t)
98         (startats '(1))
99         headers article subject score from date lines parent point s)
100     (save-excursion
101       (set-buffer nnslashdot-buffer)
102       (let ((case-fold-search t))
103         (erase-buffer)
104         (when (= start 1)
105           (nnweb-insert (format nnslashdot-article-url sid))
106           (goto-char (point-min))
107           (search-forward "Posted by ")
108           (when (looking-at "<a[^>]+>\\([^<]+\\)")
109             (setq from (match-string 1)))
110           (search-forward " on ")
111           (setq date (nnslashdot-date-to-date
112                       (buffer-substring (point) (1- (search-forward "<")))))
113           (forward-line 2)
114           (setq lines (count-lines
115                        (point)
116                        (search-forward
117                         "A href=http://slashdot.org/article.pl")))
118           (push
119            (cons
120             1
121             (make-full-mail-header
122              1 group from date (concat "<" sid "%1@slashdot>")
123              "" 0 lines nil nil))
124            headers))
125         (while (and (setq start (pop startats))
126                     (< start last))
127           (setq point (goto-char (point-max)))
128           (nnweb-insert
129            (format nnslashdot-comments-url sid nnslashdot-threshold 0 start))
130           (when first-comments
131             (setq first-comments nil)
132             (goto-char (point-max))
133             (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
134               (setq s (string-to-number (match-string 1)))
135               (unless (memq s startats)
136                 (push s startats)))
137             (setq startats (sort startats '<)))
138           (goto-char point)
139           (while (re-search-forward
140                   "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
141                   nil t)
142             (setq article (string-to-number (match-string 1))
143                   subject (match-string 2)
144                   score (match-string 3))
145             (when (string-match "^Re: *" subject)
146               (setq subject (concat "Re: " (substring subject (match-end 0)))))
147             (forward-line 1)
148             (if (looking-at
149                  "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
150                 (setq from (concat (match-string 1)
151                                    " <" (match-string 2) ">"))
152               (looking-at "by \\(.+\\) on ")
153               (setq from (match-string 1)))
154             (goto-char (- (match-end 0) 5))
155             (search-forward " on ")
156             (setq date
157                   (nnslashdot-date-to-date
158                    (buffer-substring (point) (progn (end-of-line) (point)))))
159             (setq lines (/ (abs (- (search-forward "<td ")
160                                    (search-forward "</td>")))
161                            70))
162             (forward-line 2)
163             (setq parent
164                   (if (looking-at ".*cid=\\([0-9]+\\)")
165                       (match-string 1)
166                     nil))
167             (setq did t)
168             (push
169              (cons
170               (1+ article)
171               (make-full-mail-header
172                (1+ article)
173                (concat subject " (" score ")")
174                from date
175                (concat "<" sid "%"
176                        (number-to-string (1+ article)) 
177                        "@slashdot>")
178                (if parent
179                    (concat "<" sid "%"
180                            (number-to-string (1+ (string-to-number parent)))
181                            "@slashdot>")
182                  "")
183                0 lines nil nil))
184              headers)))))
185     (setq nnslashdot-headers (sort headers 'car-less-than-car))
186     (save-excursion
187       (set-buffer nntp-server-buffer)
188       (erase-buffer)
189       (dolist (header nnslashdot-headers)
190         (nnheader-insert-nov (cdr header))))
191     'nov))
192
193 (deffoo nnslashdot-sane-retrieve-headers (articles group)
194   (let ((last (car (last articles)))
195         (did nil)
196         (start (max (1- (car articles)) 1))
197         (sid (caddr (assoc group nnslashdot-groups)))
198         headers article subject score from date lines parent point)
199     (save-excursion
200       (set-buffer nnslashdot-buffer)
201       (erase-buffer)
202       (when (= start 1)
203         (nnweb-insert (format nnslashdot-article-url sid))
204         (goto-char (point-min))
205         (search-forward "Posted by ")
206         (when (looking-at "<a[^>]+>\\([^<]+\\)")
207           (setq from (match-string 1)))
208         (search-forward " on ")
209         (setq date (nnslashdot-date-to-date
210                     (buffer-substring (point) (1- (search-forward "<")))))
211         (forward-line 2)
212         (setq lines (count-lines (point)
213                                  (search-forward
214                                   "A href=http://slashdot.org/article.pl")))
215         (push
216          (cons
217           1
218           (make-full-mail-header
219            1 group from date (concat "<" sid "%1@slashdot>")
220            "" 0 lines nil nil))
221          headers))
222       (while (or (not article)
223                  (and did
224                       (< article last)))
225         (when article
226           (setq start (1+ article)))
227         (setq point (goto-char (point-max)))
228         (nnweb-insert
229          (format nnslashdot-comments-url sid nnslashdot-threshold 4 start))
230         (goto-char point)
231         (while (re-search-forward
232                 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score\\([^)]+\\))"
233                 nil t)
234           (setq article (string-to-number (match-string 1))
235                 subject (match-string 2)
236                 score (match-string 3))
237           (forward-line 1)
238           (if (looking-at
239                "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
240               (setq from (concat (match-string 1) " <" (match-string 2) ">"))
241             (looking-at "by \\(.+\\) on ")
242             (setq from (match-string 1)))
243           (goto-char (- (match-end 0) 5))
244           (search-forward " on ")
245           (setq date
246                 (nnslashdot-date-to-date
247                  (buffer-substring (point) (progn (end-of-line) (point)))))
248           (setq lines (/ (abs (- (search-forward "<td ")
249                                  (search-forward "</td>")))
250                          70))
251           (forward-line 2)
252           (setq parent
253                 (if (looking-at ".*cid=\\([0-9]+\\)")
254                     (match-string 1)
255                   nil))
256           (setq did t)
257           (push
258            (cons
259             (1+ article)
260             (make-full-mail-header
261              (1+ article) (concat subject " (" score ")")
262              from date
263              (concat "<" sid "%"
264                      (number-to-string (1+ article)) 
265                      "@slashdot>")
266              (if parent
267                  (concat "<" sid "%"
268                          (number-to-string (1+ (string-to-number parent)))
269                          "@slashdot>")
270                "")
271              0 lines nil nil))
272            headers))))
273     (setq nnslashdot-headers
274           (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
275     (save-excursion
276       (set-buffer nntp-server-buffer)
277       (erase-buffer)
278       (dolist (header nnslashdot-headers)
279         (nnheader-insert-nov (cdr header))))
280     'nov))
281
282 (deffoo nnslashdot-request-group (group &optional server dont-check)
283   (nnslashdot-possibly-change-server nil server)
284   (let ((elem (assoc group nnslashdot-groups)))
285     (cond
286      ((not elem)
287       (nnheader-report 'nnslashdot "Group does not exist"))
288      (t
289       (nnheader-report 'nnslashdot "Opened group %s" group)
290       (nnheader-insert
291        "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
292        (prin1-to-string group))))))
293
294 (deffoo nnslashdot-close-group (group &optional server)
295   (nnslashdot-possibly-change-server group server)
296   (when (gnus-buffer-live-p nnslashdot-buffer)
297     (save-excursion
298       (set-buffer nnslashdot-buffer)
299       (kill-buffer nnslashdot-buffer)))
300   t)
301
302 (deffoo nnslashdot-request-article (article &optional group server buffer)
303   (nnslashdot-possibly-change-server group server)
304   (let (contents)
305     (save-excursion
306       (set-buffer nnslashdot-buffer)
307       (let ((case-fold-search t))
308         (goto-char (point-min))
309         (when (and (stringp article)
310                    (string-match "%\\([0-9]+\\)@" article))
311           (setq article (string-to-number (match-string 1 article))))
312         (when (numberp article)
313           (if (= article 1)
314               (progn
315                 (re-search-forward "Posted by .* on ")
316                 (forward-line 1)
317                 (setq contents
318                       (buffer-substring
319                        (point)
320                        (progn
321                          (re-search-forward
322                           "<p>.*A href=http://slashdot.org/article.pl")
323                          (match-beginning 0)))))
324             (search-forward (format "<a name=\"%d\">" (1- article)))
325             (setq contents
326                   (buffer-substring
327                    (re-search-forward "<td[^>]+>")
328                    (search-forward "</td>")))))))
329     (when contents
330       (save-excursion
331         (set-buffer (or buffer nntp-server-buffer))
332         (erase-buffer)
333         (insert contents)
334         (goto-char (point-min))
335         (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
336         (let ((header (cdr (assq article nnslashdot-headers))))
337           (nnheader-insert-header header))
338         (nnheader-report 'nnslashdot "Fetched article %s" article)
339         (cons group article)))))
340
341 (deffoo nnslashdot-close-server (&optional server)
342   (when (and (nnslashdot-server-opened server)
343              (gnus-buffer-live-p nnslashdot-buffer))
344     (save-excursion
345       (set-buffer nnslashdot-buffer)
346       (kill-buffer nnslashdot-buffer)))
347   (nnoo-close-server 'nnslashdot server))
348
349 (deffoo nnslashdot-request-list (&optional server)
350   (nnslashdot-possibly-change-server nil server)
351   (let ((number 0)
352         sid elem description articles gname)
353     ;; First we do the Ultramode to get info on all the latest groups.
354     (with-temp-buffer
355       (nnweb-insert "http://slashdot.org/slashdot.xml")
356       (goto-char (point-min))
357       (while (search-forward "<story>" nil t)
358         (narrow-to-region (point) (search-forward "</story>"))
359         (goto-char (point-min))
360         (re-search-forward "<title>\\([^<]+\\)</title>")
361         (setq description (match-string 1))
362         (re-search-forward "<url>\\([^<]+\\)</url>")
363         (setq sid (match-string 1))
364         (string-match "/\\([0-9/]+\\).shtml" sid)
365         (setq sid (match-string 1 sid))
366         (re-search-forward "<comments>\\([^<]+\\)</comments>")
367         (setq articles (string-to-number (match-string 1)))
368         (setq gname (concat description " (" sid ")"))
369         (if (setq elem (assoc gname nnslashdot-groups))
370             (setcar (cdr elem) articles)
371           (push (list gname articles sid) nnslashdot-groups))
372         (goto-char (point-max))
373         (widen)))
374     ;; Then do the older groups.
375     (while (> (- nnslashdot-group-number number) 0)
376       (with-temp-buffer
377         (let ((case-fold-search t))
378           (nnweb-insert (format nnslashdot-active-url number))
379           (goto-char (point-min))
380           (while (re-search-forward
381                   "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
382             (setq sid (match-string 1)
383                   description (match-string 2))
384             (forward-line 1)
385             (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
386               (setq articles (string-to-number (match-string 1))))
387             (setq gname (concat description " (" sid ")"))
388             (if (setq elem (assoc gname nnslashdot-groups))
389                 (setcar (cdr elem) articles)
390               (push (list gname articles sid) nnslashdot-groups)))))
391       (incf number 30))
392     (nnslashdot-write-groups)
393     (nnslashdot-generate-active)
394     t))
395
396 (deffoo nnslashdot-request-newgroups (date &optional server)
397   (nnslashdot-possibly-change-server nil server)
398   (nnslashdot-generate-active)
399   t)
400
401 (nnoo-define-skeleton nnslashdot)
402
403 ;;; Internal functions
404
405 (defun nnslashdot-possibly-change-server (&optional group server)
406   (nnslashdot-init server)
407   (when (and server
408              (not (nnslashdot-server-opened server)))
409     (nnslashdot-open-server server))
410   (unless nnslashdot-groups
411     (nnslashdot-read-groups)))
412
413 (defun nnslashdot-read-groups ()
414   (let ((file (expand-file-name "groups" nnslashdot-directory)))
415     (when (file-exists-p file)
416       (with-temp-buffer
417         (insert-file-contents file)
418         (goto-char (point-min))
419         (setq nnslashdot-groups (read (current-buffer)))))))
420
421 (defun nnslashdot-write-groups ()
422   (with-temp-file (expand-file-name "groups" nnslashdot-directory)
423     (prin1 nnslashdot-groups (current-buffer))))
424     
425 (defun nnslashdot-init (server)
426   "Initialize buffers and such."
427   (unless (file-exists-p nnslashdot-directory)
428     (gnus-make-directory nnslashdot-directory))
429   (unless (gnus-buffer-live-p nnslashdot-buffer)
430     (setq nnslashdot-buffer
431           (save-excursion
432             (nnheader-set-temp-buffer
433              (format " *nnslashdot %s*" server))))))
434
435 (defun nnslashdot-date-to-date (sdate)
436   (let ((elem (delete "" (split-string sdate))))
437     (concat (substring (nth 0 elem) 0 3) " "
438             (substring (nth 1 elem) 0 3) " "
439             (substring (nth 2 elem) 0 2) " "
440             (substring (nth 3 elem) 1 6) " "
441             (format-time-string "%Y") " "
442             (nth 4 elem))))
443
444 (defun nnslashdot-generate-active ()
445   (save-excursion
446     (set-buffer nntp-server-buffer)
447     (erase-buffer)
448     (dolist (elem nnslashdot-groups)
449       (insert (prin1-to-string (car elem))
450               " " (number-to-string (cadr elem)) " 1 y\n"))))
451
452 (provide 'nnslashdot)
453
454 ;;; nnslashdot.el ends here