2001-08-20 15:00:00 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / nnslashdot.el
1 ;;; nnslashdot.el --- interfacing with Slashdot
2 ;; Copyright (C) 1999, 2000, 2001 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 (eval-when-compile
40   (ignore-errors
41     (require 'nnweb)))
42 ;; Report failure to find w3 at load time if appropriate.
43 (eval '(require 'nnweb))
44
45 (nnoo-declare nnslashdot)
46
47 (defvoo nnslashdot-directory (nnheader-concat gnus-directory "slashdot/")
48   "Where nnslashdot will save its files.")
49
50 (defvoo nnslashdot-active-url "http://slashdot.org/search.pl?section=&min=%d"
51   "Where nnslashdot will fetch the active file from.")
52
53 (defvoo nnslashdot-comments-url "http://slashdot.org/comments.pl?sid=%s&threshold=%d&commentsort=%d&mode=flat&startat=%d"
54   "Where nnslashdot will fetch comments from.")
55
56 (defvoo nnslashdot-article-url
57     "http://slashdot.org/article.pl?sid=%s&mode=nocomment"
58   "Where nnslashdot will fetch the article from.")
59
60 (defvoo nnslashdot-backslash-url "http://slashdot.org/slashdot.xml"
61   "Where nnslashdot will fetch the stories from.")
62
63 (defvoo nnslashdot-threshold -1
64   "The article threshold.")
65
66 (defvoo nnslashdot-threaded t
67   "Whether the nnslashdot groups should be threaded or not.")
68
69 (defvoo nnslashdot-group-number 0
70   "The number of non-fresh groups to keep updated.")
71
72 (defvoo nnslashdot-login-name ""
73   "The login name to use when posting.")
74
75 (defvoo nnslashdot-password ""
76   "The password to use when posting.")
77
78 ;;; Internal variables
79
80 (defvar nnslashdot-groups nil)
81 (defvar nnslashdot-buffer nil)
82 (defvar nnslashdot-headers nil)
83
84 ;;; Interface functions
85
86 (nnoo-define-basics nnslashdot)
87
88 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
89   (nnslashdot-possibly-change-server group server)
90   (condition-case why
91       (unless gnus-nov-is-evil
92         (nnslashdot-retrieve-headers-1 articles group))
93     (search-failed (nnslashdot-lose why))))
94
95 (deffoo nnslashdot-retrieve-headers-1 (articles group)
96   (let* ((last (car (last articles)))
97          (start (if nnslashdot-threaded 1 (pop articles)))
98          (entry (assoc group nnslashdot-groups))
99          (sid (nth 2 entry))
100          (first-comments t)
101          headers article subject score from date lines parent point cid 
102          s startats changed)
103     (save-excursion
104       (set-buffer nnslashdot-buffer)
105       (let ((case-fold-search t))
106         (erase-buffer)
107         (when (= start 1)
108           (nnweb-insert (format nnslashdot-article-url
109                                 (nnslashdot-sid-strip sid)) t)
110           (goto-char (point-min))
111           (re-search-forward "Posted by[ \t\r\n]+")
112           (when (looking-at "<a[^>]+>[ \t\r\n]*\\([^<\r\n]+\\)")
113             (setq from (nnweb-decode-entities-string (match-string 1))))
114           (search-forward " on ")
115           (setq date (nnslashdot-date-to-date
116                       (buffer-substring (point) (1- (search-forward "<")))))
117           (setq lines (/ (- (point)
118                             (progn (forward-line 1) (point)))
119                          60))
120           (push
121            (cons
122             1
123             (make-full-mail-header
124              1 group from date
125              (concat "<" (nnslashdot-sid-strip sid) "%1@slashdot>")
126              "" 0 lines nil nil))
127            headers)
128           (setq start (if nnslashdot-threaded 2 (pop articles))))
129         (while (and start (<= start last))
130           (setq point (goto-char (point-max)))
131           (nnweb-insert
132            (format nnslashdot-comments-url
133                    (nnslashdot-sid-strip sid)
134                    nnslashdot-threshold 0 (- start 2))
135            t)
136           (when (and nnslashdot-threaded first-comments)
137             (setq first-comments nil)
138             (goto-char (point-max))
139             (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
140               (setq s (string-to-number (match-string 1)))
141               (unless (memq s startats)
142                 (push s startats)))
143             (setq startats (sort startats '<)))
144           (setq article (if (and article (< start article)) article start))
145           (goto-char point)
146           (while (re-search-forward
147                   "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
148                   nil t)
149             (setq cid (match-string 1)
150                   subject (match-string 3)
151                   score (match-string 5))
152             (unless (assq article (nth 4 entry))
153               (setcar (nthcdr 4 entry) (cons (cons article cid) (nth 4 entry)))
154               (setq changed t))
155             (when (string-match "^Re: *" subject)
156               (setq subject (concat "Re: " (substring subject (match-end 0)))))
157             (setq subject (nnweb-decode-entities-string subject))
158             (search-forward "<BR>")
159             (if (looking-at
160                  "by[ \t\n]+<a[^>]+>\\([^<]+\\)</a>[ \t\n]*(\\(<[^>]+>\\)*\\([^<>)]+\\))")
161                 (progn
162                   (goto-char (- (match-end 0) 5))
163                   (setq from (concat
164                               (nnweb-decode-entities-string (match-string 1))
165                               " <" (match-string 3) ">")))
166               (setq from "")
167               (when (looking-at "by \\([^<>]*\\) on ")
168                 (goto-char (- (match-end 0) 5))
169                 (setq from (nnweb-decode-entities-string (match-string 1)))))
170             (search-forward " on ")
171             (setq date
172                   (nnslashdot-date-to-date
173                    (buffer-substring (point) (progn (skip-chars-forward "^()<>\n\r") (point)))))
174             (setq lines (/ (abs (- (search-forward "<td ")
175                                    (search-forward "</td>")))
176                            70))
177             (forward-line 4)
178             (setq parent
179                   (if (looking-at ".*cid=\\([0-9]+\\)")
180                       (match-string 1)
181                     nil))
182             (push
183              (cons
184               article
185               (make-full-mail-header
186                article
187                (concat subject " (" score ")")
188                from date
189                (concat "<" (nnslashdot-sid-strip sid) "%" cid "@slashdot>")
190                (if parent
191                    (concat "<" (nnslashdot-sid-strip sid) "%" 
192                            parent "@slashdot>")
193                  "")
194                0 lines nil nil))
195              headers)
196             (while (and articles (<= (car articles) article))
197               (pop articles))
198             (setq article (1+ article)))
199           (if nnslashdot-threaded 
200               (progn
201                 (setq start (pop startats))
202                 (if start (setq start (+ start 2))))
203             (setq start (pop articles))))))
204     (if changed (nnslashdot-write-groups))
205     (setq nnslashdot-headers (sort headers 'car-less-than-car))
206     (save-excursion
207       (set-buffer nntp-server-buffer)
208       (erase-buffer)
209       (mm-with-unibyte-current-buffer
210        (dolist (header nnslashdot-headers)
211          (nnheader-insert-nov (cdr header)))))
212     'nov))
213
214 (deffoo nnslashdot-request-group (group &optional server dont-check)
215   (nnslashdot-possibly-change-server nil server)
216   (let ((elem (assoc group nnslashdot-groups)))
217     (cond
218      ((not elem)
219       (nnheader-report 'nnslashdot "Group does not exist"))
220      (t
221       (nnheader-report 'nnslashdot "Opened group %s" group)
222       (nnheader-insert
223        "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
224        (prin1-to-string group))))))
225
226 (deffoo nnslashdot-close-group (group &optional server)
227   (nnslashdot-possibly-change-server group server)
228   (when (gnus-buffer-live-p nnslashdot-buffer)
229     (save-excursion
230       (set-buffer nnslashdot-buffer)
231       (kill-buffer nnslashdot-buffer)))
232   t)
233
234 (deffoo nnslashdot-request-article (article &optional group server buffer)
235   (nnslashdot-possibly-change-server group server)
236   (let (contents cid)
237     (condition-case why
238         (save-excursion
239           (set-buffer nnslashdot-buffer)
240           (let ((case-fold-search t))
241             (goto-char (point-min))
242             (when (and (stringp article)
243                        (string-match "%\\([0-9]+\\)@" article))
244               (setq cid (match-string 1 article))
245               (let ((map (nth 4 (assoc group nnslashdot-groups))))
246                 (while map
247                   (if (equal (cdar map) cid)
248                       (setq article (caar map)
249                             map nil)
250                     (setq map (cdr map))))))
251             (when (numberp article)
252               (if (= article 1)
253                   (progn
254                     (re-search-forward 
255                      "Posted by[ \t\r\n]+\\(<[^>]+>[ \t\r\n]*[^>]*[ \t\r\n]*<[^>]+> *[ \t\r\n]*\\)?on ")
256                     (search-forward "<BR>")
257                     (setq contents
258                           (buffer-substring
259                            (point)
260                            (progn
261                              (re-search-forward
262                               "&lt;&nbsp;[ \t\r\n]*<A HREF=\"\\(\\(http:\\)?//slashdot\\.org\\)?/article")
263                              (match-beginning 0)))))
264                 (setq cid (cdr (assq article 
265                                      (nth 4 (assoc group nnslashdot-groups)))))
266                 (search-forward (format "<a name=\"%s\">" cid))
267                 (setq contents
268                       (buffer-substring
269                        (re-search-forward "<td[^>]*>")
270                        (search-forward "</td>")))))))
271       (search-failed (nnslashdot-lose why)))
272
273     (when contents
274       (save-excursion
275         (set-buffer (or buffer nntp-server-buffer))
276         (erase-buffer)
277         (mm-with-unibyte-current-buffer
278           (insert contents)
279           (goto-char (point-min))
280           (while (re-search-forward "\\(<br>\r?\\)+" nil t)
281             (replace-match "<p>" t t))
282           (goto-char (point-min))
283           (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
284           (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
285                   "\n")
286           (let ((header (cdr (assq article nnslashdot-headers))))
287             (nnheader-insert-header header))
288           (nnheader-report 'nnslashdot "Fetched article %s" article))
289         (cons group article)))))
290
291 (deffoo nnslashdot-close-server (&optional server)
292   (when (and (nnslashdot-server-opened server)
293              (gnus-buffer-live-p nnslashdot-buffer))
294     (save-excursion
295       (set-buffer nnslashdot-buffer)
296       (kill-buffer nnslashdot-buffer)))
297   (nnoo-close-server 'nnslashdot server))
298
299 (deffoo nnslashdot-request-list (&optional server)
300   (nnslashdot-possibly-change-server nil server)
301   (let ((number 0)
302         sid elem description articles gname)
303     (condition-case why
304         ;; First we do the Ultramode to get info on all the latest groups.
305         (progn
306           (mm-with-unibyte-buffer
307             (nnweb-insert nnslashdot-backslash-url t)
308             (goto-char (point-min))
309             (while (search-forward "<story>" nil t)
310               (narrow-to-region (point) (search-forward "</story>"))
311               (goto-char (point-min))
312               (re-search-forward "<title>\\([^<]+\\)</title>")
313               (setq description
314                     (nnweb-decode-entities-string (match-string 1)))
315               (re-search-forward "<url>\\([^<]+\\)</url>")
316               (setq sid (match-string 1))
317               (string-match "sid=\\([0-9/]+\\)\\(.shtml\\|$\\)" sid)
318               (setq sid (match-string 1 sid))
319               (re-search-forward "<comments>\\([^<]+\\)</comments>")
320               (setq articles (string-to-number (match-string 1)))
321               (setq gname (concat description " (" sid ")"))
322               (if (setq elem (assoc gname nnslashdot-groups))
323                   (setcar (cdr elem) articles)
324                 (push (list gname articles sid (current-time) nil)
325                       nnslashdot-groups))
326               (goto-char (point-max))
327               (widen)))
328           ;; Then do the older groups.
329           (while (> (- nnslashdot-group-number number) 0)
330             (mm-with-unibyte-buffer
331               (let ((case-fold-search t))
332                 (nnweb-insert (format nnslashdot-active-url number) t)
333                 (goto-char (point-min))
334                 (while (re-search-forward
335                         "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>"
336                         nil t)
337                   (setq sid (match-string 1)
338                         description
339                         (nnweb-decode-entities-string (match-string 2)))
340                   (forward-line 1)
341                   (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
342                     (setq articles (string-to-number (match-string 1))))
343                   (setq gname (concat description " (" sid ")"))
344                   (if (setq elem (assoc gname nnslashdot-groups))
345                       (setcar (cdr elem) articles)
346                     (push (list gname articles sid (current-time) nil)
347                           nnslashdot-groups)))))
348             (incf number 30)))
349       (search-failed (nnslashdot-lose why)))
350     (nnslashdot-write-groups)
351     (nnslashdot-generate-active)
352     t))
353
354 (deffoo nnslashdot-request-newgroups (date &optional server)
355   (nnslashdot-possibly-change-server nil server)
356   (nnslashdot-generate-active)
357   t)
358
359 (deffoo nnslashdot-request-post (&optional server)
360   (nnslashdot-possibly-change-server nil server)
361   (let ((sid (nnslashdot-sid-strip (message-fetch-field "newsgroups")))
362         (subject (message-fetch-field "subject"))
363         (references (car (last (split-string
364                                 (message-fetch-field "references")))))
365         body quoted pid)
366     (string-match "%\\([0-9]+\\)@slashdot" references)
367     (setq pid (match-string 1 references))
368     (message-goto-body)
369     (narrow-to-region (point) (progn (message-goto-signature) (point)))
370     (goto-char (point-min))
371     (while (not (eobp))
372       (if (looking-at "> ")
373           (progn
374             (delete-region (point) (+ (point) 2))
375             (unless quoted
376               (insert "<blockquote>\n"))
377             (setq quoted t))
378         (when quoted
379           (insert "</blockquote>\n")
380           (setq quoted nil)))
381       (forward-line 1))
382     (goto-char (point-min))
383     (while (re-search-forward "^ *\n" nil t)
384       (replace-match "<p>\n"))
385     (widen)
386     (when (message-goto-signature)
387       (forward-line -1)
388       (insert "<p>\n")
389       (while (not (eobp))
390         (end-of-line)
391         (insert "<br>")
392         (forward-line 1)))
393     (message-goto-body)
394     (setq body (buffer-substring (point) (point-max)))
395     (erase-buffer)
396     (nnweb-fetch-form
397      "http://slashdot.org/comments.pl"
398      `(("sid" . ,sid)
399        ("pid" . ,pid)
400        ("rlogin" . "userlogin")
401        ("unickname" . ,nnslashdot-login-name)
402        ("upasswd" . ,nnslashdot-password)
403        ("postersubj" . ,subject)
404        ("op" . "Submit")
405        ("postercomment" . ,body)
406        ("posttype" . "html")))))
407
408 (deffoo nnslashdot-request-delete-group (group &optional force server)
409   (nnslashdot-possibly-change-server group server)
410   (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
411                                 nnslashdot-groups))
412   (nnslashdot-write-groups))
413
414 (deffoo nnslashdot-request-close ()
415   (setq nnslashdot-headers nil
416         nnslashdot-groups nil))
417
418 (deffoo nnslashdot-request-expire-articles
419     (articles group &optional server force)
420   (nnslashdot-possibly-change-server group server)
421   (let ((item (assoc group nnslashdot-groups)))
422     (when item
423       (if (fourth item)
424           (when (and (>= (length articles) (cadr item)) ;; All are expirable.
425                      (nnmail-expired-article-p
426                       group
427                       (fourth item)
428                       force))
429             (setq nnslashdot-groups (delq item nnslashdot-groups))
430             (nnslashdot-write-groups)
431             (setq articles nil)) ;; all expired.
432         (setcdr (cddr item) (list (current-time)))
433         (nnslashdot-write-groups))))
434   articles)
435
436 (nnoo-define-skeleton nnslashdot)
437
438 ;;; Internal functions
439
440 (defun nnslashdot-possibly-change-server (&optional group server)
441   (nnslashdot-init server)
442   (when (and server
443              (not (nnslashdot-server-opened server)))
444     (nnslashdot-open-server server))
445   (unless nnslashdot-groups
446     (nnslashdot-read-groups)))
447
448 (defun nnslashdot-make-tuple (tuple n)
449   (prog1
450       tuple
451     (while (> n 1)
452       (unless (cdr tuple)
453         (setcdr tuple (list nil)))
454       (setq tuple (cdr tuple)
455             n (1- n)))))
456
457 (defun nnslashdot-read-groups ()
458   (let ((file (expand-file-name "groups" nnslashdot-directory)))
459     (when (file-exists-p file)
460       (mm-with-unibyte-buffer
461         (insert-file-contents file)
462         (goto-char (point-min))
463         (setq nnslashdot-groups (read (current-buffer))))
464       (if (and nnslashdot-groups (< (length (car nnslashdot-groups)) 5))
465           (let ((groups nnslashdot-groups))
466             (while groups
467               (nnslashdot-make-tuple (car groups) 5)
468               (setq groups (cdr groups))))))))
469
470 (defun nnslashdot-write-groups ()
471   (with-temp-file (expand-file-name "groups" nnslashdot-directory)
472     (gnus-prin1 nnslashdot-groups)))
473
474 (defun nnslashdot-init (server)
475   "Initialize buffers and such."
476   (unless (file-exists-p nnslashdot-directory)
477     (gnus-make-directory nnslashdot-directory))
478   (unless (gnus-buffer-live-p nnslashdot-buffer)
479     (setq nnslashdot-buffer
480           (save-excursion
481             (nnheader-set-temp-buffer
482              (format " *nnslashdot %s*" server))))
483     (push nnslashdot-buffer gnus-buffers)))
484
485 (defun nnslashdot-date-to-date (sdate)
486   (condition-case err
487       (let ((elem (delete "" (split-string sdate))))
488         (concat (substring (nth 0 elem) 0 3) " "
489                 (substring (nth 1 elem) 0 3) " "
490                 (substring (nth 2 elem) 0 2) " "
491                 (substring (nth 3 elem) 1 6) " "
492                 (format-time-string "%Y") " "
493                 (nth 4 elem)))
494     (error "")))
495
496 (defun nnslashdot-generate-active ()
497   (save-excursion
498     (set-buffer nntp-server-buffer)
499     (erase-buffer)
500     (dolist (elem nnslashdot-groups)
501       (insert (prin1-to-string (car elem))
502               " " (number-to-string (cadr elem)) " 1 y\n"))))
503
504 (defun nnslashdot-lose (why)
505   (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
506
507 (defalias 'nnslashdot-sid-strip 'identity)
508
509 (provide 'nnslashdot)
510
511 ;;; nnslashdot.el ends here