*** empty log message ***
[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=4&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 0
67   "The article threshold.")
68
69 (defvoo nnslashdot-group-number 30
70   "The number of groups to keep updated.")
71
72 ;;; Internal variables
73
74 (defvar nnslashdot-groups nil)
75 (defvar nnslashdot-buffer nil)
76 (defvar nnslashdot-headers nil)
77
78 ;;; Interface functions
79
80 (nnoo-define-basics nnslashdot)
81
82 (deffoo nnslashdot-thread-retrieve-headers (articles &optional group server fetch-old)
83   (nnslashdot-possibly-change-server group server)
84   (let ((last (car (last articles)))
85         (did nil)
86         (start 1)
87         (sid (caddr (assoc group nnslashdot-groups)))
88         headers article subject score from date lines parent point
89         startats s)
90     (unless gnus-nov-is-evil
91       (save-excursion
92         (set-buffer nnslashdot-buffer)
93         (let ((case-fold-search t))
94           (erase-buffer)
95           (when (= start 1)
96             (nnweb-insert (format nnslashdot-article-url sid))
97             (goto-char (point-min))
98             (search-forward "Posted by ")
99             (when (looking-at "<a[^>]+>\\([^<]+\\)")
100               (setq from (match-string 1)))
101             (search-forward " on ")
102             (setq date (nnslashdot-date-to-date
103                         (buffer-substring (point) (1- (search-forward "<")))))
104             (forward-line 2)
105             (setq lines (count-lines
106                          (point)
107                          (search-forward
108                           "A href=http://slashdot.org/article.pl")))
109             (push
110              (cons
111               1
112               (make-full-mail-header
113                1 group from date (concat "<" sid "%1@slashdot>")
114                "" 0 lines nil nil))
115              headers)
116             (goto-char (point-max))
117             (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
118               (setq s (match-string 1))
119               (unless (memq s startats)
120                 (push s startats)))
121             (unless startats
122               (push 1 startats)))
123           (setq startats (sort startats '<))
124           (while (and (setq start (pop startats))
125                       (< start last))
126             (setq point (goto-char (point-max)))
127             (nnweb-insert
128              (format nnslashdot-comments-url sid nnslashdot-threshold start))
129             (goto-char point)
130             (while (re-search-forward
131                     "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
132                     nil t)
133               (setq article (string-to-number (match-string 1))
134                     subject (match-string 2)
135                     score (match-string 3))
136               (when (string-match "^Re: *" subject)
137                 (setq subject (concat "Re: " (substring subject (match-end 0)))))
138               (forward-line 1)
139               (if (looking-at
140                    "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
141                   (setq from (concat (match-string 1)
142                                      " <" (match-string 2) ">"))
143                 (looking-at "by \\(.+\\) on ")
144                 (setq from (match-string 1)))
145               (goto-char (- (match-end 0) 5))
146               (search-forward " on ")
147               (setq date
148                     (nnslashdot-date-to-date
149                      (buffer-substring (point) (progn (end-of-line) (point)))))
150               (setq lines (/ (abs (- (search-forward "<td ")
151                                      (search-forward "</td>")))
152                              70))
153               (forward-line 2)
154               (setq parent
155                     (if (looking-at ".*cid=\\([0-9]+\\)")
156                         (match-string 1)
157                       nil))
158               (setq did t)
159               (push
160                (cons
161                 (1+ article)
162                 (make-full-mail-header
163                  (1+ article) subject
164                  from date
165                  (concat "<" sid "%"
166                          (number-to-string (1+ article)) 
167                          "@slashdot>")
168                  (if parent
169                      (concat "<" sid "%"
170                              (number-to-string (1+ (string-to-number parent)))
171                              "@slashdot>")
172                    "")
173                  0 (string-to-number score) nil nil))
174                headers)))))
175       (setq nnslashdot-headers
176             (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
177       (save-excursion
178         (set-buffer nntp-server-buffer)
179         (erase-buffer)
180         (dolist (header nnslashdot-headers)
181           (nnheader-insert-nov (cdr header))))
182       'nov)))
183
184 (deffoo nnslashdot-retrieve-headers (articles &optional group
185                                               server fetch-old)
186   (unless gnus-nov-is-evil
187     (nnslashdot-possibly-change-server group server)
188     (let ((last (car (last articles)))
189           (did nil)
190           (start (max (1- (car articles)) 1))
191           (sid (caddr (assoc group nnslashdot-groups)))
192           headers article subject score from date lines parent point)
193       (save-excursion
194         (set-buffer nnslashdot-buffer)
195         (erase-buffer)
196         (when (= start 1)
197           (nnweb-insert (format nnslashdot-article-url sid))
198           (goto-char (point-min))
199           (search-forward "Posted by ")
200           (when (looking-at "<a[^>]+>\\([^<]+\\)")
201             (setq from (match-string 1)))
202           (search-forward " on ")
203           (setq date (nnslashdot-date-to-date
204                       (buffer-substring (point) (1- (search-forward "<")))))
205           (forward-line 2)
206           (setq lines (count-lines (point)
207                                    (search-forward
208                                     "A href=http://slashdot.org/article.pl")))
209           (push
210            (cons
211             1
212             (make-full-mail-header
213              1 group from date (concat "<" sid "%1@slashdot>")
214              "" 0 lines nil nil))
215            headers))
216         (while (or (not article)
217                    (and did
218                         (< article last)))
219           (when article
220             (setq start (1+ article)))
221           (setq point (goto-char (point-max)))
222           (nnweb-insert
223            (format nnslashdot-comments-url sid nnslashdot-threshold start))
224           (goto-char point)
225           (while (re-search-forward
226                   "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score\\([^)]+\\))"
227                   nil t)
228             (setq article (string-to-number (match-string 1))
229                   subject (match-string 2)
230                   score (match-string 3))
231             (forward-line 1)
232             (if (looking-at
233                  "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
234                 (setq from (concat (match-string 1) " <" (match-string 2) ">"))
235               (looking-at "by \\(.+\\) on ")
236               (setq from (match-string 1)))
237             (goto-char (- (match-end 0) 5))
238             (search-forward " on ")
239             (setq date
240                   (nnslashdot-date-to-date
241                    (buffer-substring (point) (progn (end-of-line) (point)))))
242             (setq lines (/ (abs (- (search-forward "<td ")
243                                    (search-forward "</td>")))
244                            70))
245             (forward-line 2)
246             (setq parent
247                   (if (looking-at ".*cid=\\([0-9]+\\)")
248                       (match-string 1)
249                     nil))
250             (setq did t)
251             (push
252              (cons
253               (1+ article)
254               (make-full-mail-header
255                (1+ article) (concat subject " (" score ")")
256                from date
257                (concat "<" sid "%"
258                        (number-to-string (1+ article)) 
259                        "@slashdot>")
260                (if parent
261                    (concat "<" sid "%"
262                            (number-to-string (1+ (string-to-number parent)))
263                            "@slashdot>")
264                  "")
265                0 lines nil nil))
266              headers))))
267       (setq nnslashdot-headers
268             (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
269       (save-excursion
270         (set-buffer nntp-server-buffer)
271         (erase-buffer)
272         (dolist (header nnslashdot-headers)
273           (nnheader-insert-nov (cdr header))))
274       'nov)))
275
276 (deffoo nnslashdot-request-group (group &optional server dont-check)
277   (nnslashdot-possibly-change-server nil server)
278   (let ((elem (assoc group nnslashdot-groups)))
279     (cond
280      ((not elem)
281       (nnheader-report 'nnslashdot "Group does not exist"))
282      (t
283       (nnheader-report 'nnslashdot "Opened group %s" group)
284       (nnheader-insert
285        "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
286        (prin1-to-string group))))))
287
288 (deffoo nnslashdot-close-group (group &optional server)
289   (nnslashdot-possibly-change-server group server)
290   (when (gnus-buffer-live-p nnslashdot-buffer)
291     (save-excursion
292       (set-buffer nnslashdot-buffer)
293       (kill-buffer nnslashdot-buffer)))
294   t)
295
296 (deffoo nnslashdot-request-article (article &optional group server buffer)
297   (nnslashdot-possibly-change-server group server)
298   (let (contents)
299     (save-excursion
300       (set-buffer nnslashdot-buffer)
301       (let ((case-fold-search t))
302         (goto-char (point-min))
303         (when (and (stringp article)
304                    (string-match "%\\([0-9]+\\)@" article))
305           (setq article (string-to-number (match-string 1 article))))
306         (when (numberp article)
307           (if (= article 1)
308               (progn
309                 (re-search-forward "Posted by .* on ")
310                 (forward-line 1)
311                 (setq contents
312                       (buffer-substring
313                        (point)
314                        (progn
315                          (re-search-forward
316                           "^<p>.*A href=http://slashdot.org/article.pl")
317                          (match-beginning 0)))))
318             (search-forward (format "<a name=\"%d\">" (1- article)))
319             (setq contents
320                   (buffer-substring
321                    (re-search-forward "<td[^>]+>")
322                    (search-forward "</td>")))))))
323     (when contents
324       (save-excursion
325         (set-buffer (or buffer nntp-server-buffer))
326         (erase-buffer)
327         (insert contents)
328         ;;(nnweb-remove-markup)
329         ;;(nnweb-decode-entities)
330         (goto-char (point-min))
331         (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
332         (let ((header (cdr (assq article nnslashdot-headers))))
333           (nnheader-insert-header header))
334         (nnheader-report 'nnslashdot "Fetched article %s" article)
335         (cons group article)))))
336
337 (deffoo nnslashdot-close-server (&optional server)
338   (when (and (nnslashdot-server-opened server)
339              (gnus-buffer-live-p nnslashdot-buffer))
340     (save-excursion
341       (set-buffer nnslashdot-buffer)
342       (kill-buffer nnslashdot-buffer)))
343   (nnoo-close-server 'nnslashdot server))
344
345 (deffoo nnslashdot-request-list (&optional server)
346   (nnslashdot-possibly-change-server nil server)
347   (let ((number 0)
348         sid elem description articles gname)
349     (while (> (- nnslashdot-group-number number) 0)
350       (with-temp-buffer
351         (let ((case-fold-search t))
352           (nnweb-insert (format nnslashdot-active-url number))
353           (goto-char (point-min))
354           (while (re-search-forward
355                   "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
356             (setq sid (match-string 1)
357                   description (match-string 2))
358             (forward-line 1)
359             (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
360               (setq articles (string-to-number (match-string 1))))
361             (setq gname (concat description " (" sid ")"))
362             (if (setq elem (assoc gname nnslashdot-groups))
363                 (setcar (cdr elem) articles)
364               (push (list gname articles sid) nnslashdot-groups)))))
365       (incf number 30))
366     (nnslashdot-write-groups)
367     (nnslashdot-generate-active)
368     t))
369
370 (deffoo nnslashdot-request-newgroups (date &optional server)
371   (nnslashdot-possibly-change-server nil server)
372   (nnslashdot-generate-active)
373   t)
374
375 (nnoo-define-skeleton nnslashdot)
376
377 ;;; Internal functions
378
379 (defun nnslashdot-possibly-change-server (&optional group server)
380   (nnslashdot-init server)
381   (when (and server
382              (not (nnslashdot-server-opened server)))
383     (nnslashdot-open-server server))
384   (unless nnslashdot-groups
385     (nnslashdot-read-groups)))
386
387 (defun nnslashdot-read-groups ()
388   (let ((file (expand-file-name "groups" nnslashdot-directory)))
389     (when (file-exists-p file)
390       (with-temp-buffer
391         (insert-file-contents file)
392         (goto-char (point-min))
393         (setq nnslashdot-groups (read (current-buffer)))))))
394
395 (defun nnslashdot-write-groups ()
396   (with-temp-file (expand-file-name "groups" nnslashdot-directory)
397     (prin1 nnslashdot-groups (current-buffer))))
398     
399 (defun nnslashdot-init (server)
400   "Initialize buffers and such."
401   (unless (file-exists-p nnslashdot-directory)
402     (gnus-make-directory nnslashdot-directory))
403   (unless (gnus-buffer-live-p nnslashdot-buffer)
404     (setq nnslashdot-buffer
405           (save-excursion
406             (nnheader-set-temp-buffer
407              (format " *nnslashdot %s*" server))))))
408
409 (defun nnslashdot-date-to-date (sdate)
410   (let ((elem (split-string sdate)))
411     (concat (substring (nth 0 elem) 0 3) " "
412             (substring (nth 1 elem) 0 3) " "
413             (substring (nth 2 elem) 0 2) " "
414             (substring (nth 3 elem) 1 6) " "
415             (format-time-string "%Y") " "
416             (nth 4 elem))))
417
418 (defun nnslashdot-generate-active ()
419   (save-excursion
420     (set-buffer nntp-server-buffer)
421     (erase-buffer)
422     (dolist (elem nnslashdot-groups)
423       (insert (prin1-to-string (car elem))
424               " " (number-to-string (cadr elem)) " 1 y\n"))))
425
426 (provide 'nnslashdot)
427
428 ;;; nnslashdot.el ends here