2001-05-05 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         (if nnslashdot-threaded
93             (nnslashdot-threaded-retrieve-headers articles group)
94           (nnslashdot-sane-retrieve-headers articles group)))
95     (search-failed (nnslashdot-lose why))))
96
97 (deffoo nnslashdot-threaded-retrieve-headers (articles group)
98   (let ((last (car (last articles)))
99         (did nil)
100         (start 1)
101         (sid (caddr (assoc group nnslashdot-groups)))
102         (first-comments t)
103         (startats '(1))
104         headers article subject score from date lines parent point s)
105     (save-excursion
106       (set-buffer nnslashdot-buffer)
107       (let ((case-fold-search t))
108         (erase-buffer)
109         (when (= start 1)
110           (nnweb-insert (format nnslashdot-article-url
111                                 (nnslashdot-sid-strip sid)) t)
112           (goto-char (point-min))
113           (search-forward "Posted by ")
114           (when (looking-at "<a[^>]+>\\([^<]+\\)")
115             (setq from (nnweb-decode-entities-string (match-string 1))))
116           (search-forward " on ")
117           (setq date (nnslashdot-date-to-date
118                       (buffer-substring (point) (1- (search-forward "<")))))
119           (setq lines (/ (- (point)
120                             (progn (forward-line 1) (point)))
121                          60))
122           (push
123            (cons
124             1
125             (make-full-mail-header
126              1 group from date
127              (concat "<" (nnslashdot-sid-strip sid) "%1@slashdot>")
128              "" 0 lines nil nil))
129            headers))
130         (while (and (setq start (pop startats))
131                     (< start last))
132           (setq point (goto-char (point-max)))
133           (nnweb-insert
134            (format nnslashdot-comments-url
135                    (nnslashdot-sid-strip sid)
136                    nnslashdot-threshold 0 start)
137            t)
138           (when first-comments
139             (setq first-comments nil)
140             (goto-char (point-max))
141             (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
142               (setq s (string-to-number (match-string 1)))
143               (unless (memq s startats)
144                 (push s startats)))
145             (setq startats (sort startats '<)))
146           (goto-char point)
147           (while (re-search-forward
148                   "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
149                   nil t)
150             (setq article (string-to-number (match-string 1))
151                   subject (match-string 3)
152                   score (match-string 5))
153             (when (string-match "^Re: *" subject)
154               (setq subject (concat "Re: " (substring subject (match-end 0)))))
155             (setq subject (nnweb-decode-entities-string subject))
156             (forward-line 1)
157             (if (looking-at
158                  "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
159                 (progn
160                   (goto-char (- (match-end 0) 5))
161                   (setq from (concat
162                               (nnweb-decode-entities-string (match-string 1))
163                               " <" (match-string 2) ">")))
164               (setq from "")
165               (when (looking-at "by \\(.+\\) on ")
166                 (goto-char (- (match-end 0) 5))
167                 (setq from (nnweb-decode-entities-string (match-string 1)))))
168             (search-forward " on ")
169             (setq date
170                   (nnslashdot-date-to-date
171                    (buffer-substring (point) (progn (end-of-line) (point)))))
172             (setq lines (/ (abs (- (search-forward "<td ")
173                                    (search-forward "</td>")))
174                            70))
175             (forward-line 4)
176             (setq parent
177                   (if (looking-at ".*cid=\\([0-9]+\\)")
178                       (match-string 1)
179                     nil))
180             (setq did t)
181             (push
182              (cons
183               (1+ article)
184               (make-full-mail-header
185                (1+ article)
186                (concat subject " (" score ")")
187                from date
188                (concat "<" (nnslashdot-sid-strip sid) "%"
189                        (number-to-string (1+ article))
190                        "@slashdot>")
191                (if parent
192                    (concat "<" (nnslashdot-sid-strip sid) "%"
193                            (number-to-string (1+ (string-to-number parent)))
194                            "@slashdot>")
195                  "")
196                0 lines nil nil))
197              headers)))))
198     (setq nnslashdot-headers (sort headers 'car-less-than-car))
199     (save-excursion
200       (set-buffer nntp-server-buffer)
201       (erase-buffer)
202       (mm-with-unibyte-current-buffer
203        (dolist (header nnslashdot-headers)
204          (nnheader-insert-nov (cdr header)))))
205     'nov))
206
207 (deffoo nnslashdot-sane-retrieve-headers (articles group)
208   (let ((last (car (last articles)))
209         (did nil)
210         (start (max (1- (car articles)) 1))
211         (sid (caddr (assoc group nnslashdot-groups)))
212         headers article subject score from date lines parent point)
213     (save-excursion
214       (set-buffer nnslashdot-buffer)
215       (erase-buffer)
216       (when (= start 1)
217         (nnweb-insert (format nnslashdot-article-url
218                               (nnslashdot-sid-strip sid)) t)
219         (goto-char (point-min))
220         (search-forward "Posted by ")
221         (when (looking-at "<a[^>]+>\\([^<]+\\)")
222           (setq from (nnweb-decode-entities-string (match-string 1))))
223         (search-forward " on ")
224         (setq date (nnslashdot-date-to-date
225                     (buffer-substring (point) (1- (search-forward "<")))))
226         (forward-line 2)
227         (setq lines (count-lines (point)
228                                  (re-search-forward
229                                   "A href=\"\\(http://slashdot.org\\)?/article")))
230         (push
231          (cons
232           1
233           (make-full-mail-header
234            1 group from date (concat "<" (nnslashdot-sid-strip sid)
235                                      "%1@slashdot>")
236            "" 0 lines nil nil))
237          headers))
238       (while (or (not article)
239                  (and did
240                       (< article last)))
241         (when article
242           (setq start (1+ article)))
243         (setq point (goto-char (point-max)))
244         (nnweb-insert
245          (format nnslashdot-comments-url (nnslashdot-sid-strip sid)
246                  nnslashdot-threshold 4 start)
247          t)
248         (goto-char point)
249         (while (re-search-forward
250                   "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
251                 nil t)
252           (setq article (string-to-number (match-string 1))
253                 subject (match-string 3)
254                 score (match-string 5))
255           (when (string-match "^Re: *" subject)
256             (setq subject (concat "Re: " (substring subject (match-end 0)))))
257           (setq subject (nnweb-decode-entities-string subject))
258           (forward-line 1)
259           (if (looking-at
260                "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
261               (progn
262                 (goto-char (- (match-end 0) 5))
263                 (setq from (concat
264                             (nnweb-decode-entities-string (match-string 1))
265                             " <" (match-string 2) ">")))
266             (setq from "")
267             (when (looking-at "by \\(.+\\) on ")
268               (goto-char (- (match-end 0) 5))
269               (setq from (nnweb-decode-entities-string (match-string 1)))))
270           (search-forward " on ")
271           (setq date
272                 (nnslashdot-date-to-date
273                  (buffer-substring (point) (progn (end-of-line) (point)))))
274           (setq lines (/ (abs (- (search-forward "<td ")
275                                  (search-forward "</td>")))
276                          70))
277           (forward-line 2)
278           (setq parent
279                 (if (looking-at ".*cid=\\([0-9]+\\)")
280                     (match-string 1)
281                   nil))
282           (setq did t)
283           (push
284            (cons
285             (1+ article)
286             (make-full-mail-header
287              (1+ article) (concat subject " (" score ")")
288              from date
289              (concat "<" (nnslashdot-sid-strip sid) "%"
290                      (number-to-string (1+ article))
291                      "@slashdot>")
292              (if parent
293                  (concat "<" (nnslashdot-sid-strip sid) "%"
294                          (number-to-string (1+ (string-to-number parent)))
295                          "@slashdot>")
296                "")
297              0 lines nil nil))
298            headers))))
299     (setq nnslashdot-headers
300           (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
301     (save-excursion
302       (set-buffer nntp-server-buffer)
303       (erase-buffer)
304       (mm-with-unibyte-current-buffer
305         (dolist (header nnslashdot-headers)
306           (nnheader-insert-nov (cdr header)))))
307     'nov))
308
309 (deffoo nnslashdot-request-group (group &optional server dont-check)
310   (nnslashdot-possibly-change-server nil server)
311   (let ((elem (assoc group nnslashdot-groups)))
312     (cond
313      ((not elem)
314       (nnheader-report 'nnslashdot "Group does not exist"))
315      (t
316       (nnheader-report 'nnslashdot "Opened group %s" group)
317       (nnheader-insert
318        "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
319        (prin1-to-string group))))))
320
321 (deffoo nnslashdot-close-group (group &optional server)
322   (nnslashdot-possibly-change-server group server)
323   (when (gnus-buffer-live-p nnslashdot-buffer)
324     (save-excursion
325       (set-buffer nnslashdot-buffer)
326       (kill-buffer nnslashdot-buffer)))
327   t)
328
329 (deffoo nnslashdot-request-article (article &optional group server buffer)
330   (nnslashdot-possibly-change-server group server)
331   (let (contents)
332     (condition-case why
333         (save-excursion
334           (set-buffer nnslashdot-buffer)
335           (let ((case-fold-search t))
336             (goto-char (point-min))
337             (when (and (stringp article)
338                        (string-match "%\\([0-9]+\\)@" article))
339               (setq article (string-to-number (match-string 1 article))))
340             (when (numberp article)
341               (if (= article 1)
342                   (progn
343                     (re-search-forward "Posted by *<[^>]+>[^>]*<[^>]+> *on ")
344                     (search-forward "<BR>")
345                     (setq contents
346                           (buffer-substring
347                            (point)
348                            (progn
349                              (re-search-forward
350                               "<p>.*A href=\"\\(http://slashdot.org\\)?/article")
351                              (match-beginning 0)))))
352                 (search-forward (format "<a name=\"%d\">" (1- article)))
353                 (setq contents
354                       (buffer-substring
355                        (re-search-forward "<td[^>]+>")
356                        (search-forward "</td>")))))))
357       (search-failed (nnslashdot-lose why)))
358
359     (when contents
360       (save-excursion
361         (set-buffer (or buffer nntp-server-buffer))
362         (erase-buffer)
363         (mm-with-unibyte-current-buffer
364           (insert contents)
365           (goto-char (point-min))
366           (while (re-search-forward "\\(<br>\r?\\)+" nil t)
367             (replace-match "<p>" t t))
368           (goto-char (point-min))
369           (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
370           (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
371                   "\n")
372           (let ((header (cdr (assq article nnslashdot-headers))))
373             (nnheader-insert-header header))
374           (nnheader-report 'nnslashdot "Fetched article %s" article))
375         (cons group article)))))
376
377 (deffoo nnslashdot-close-server (&optional server)
378   (when (and (nnslashdot-server-opened server)
379              (gnus-buffer-live-p nnslashdot-buffer))
380     (save-excursion
381       (set-buffer nnslashdot-buffer)
382       (kill-buffer nnslashdot-buffer)))
383   (nnoo-close-server 'nnslashdot server))
384
385 (deffoo nnslashdot-request-list (&optional server)
386   (nnslashdot-possibly-change-server nil server)
387   (let ((number 0)
388         sid elem description articles gname)
389     (condition-case why
390         ;; First we do the Ultramode to get info on all the latest groups.
391         (progn
392           (mm-with-unibyte-buffer
393             (nnweb-insert nnslashdot-backslash-url t)
394             (goto-char (point-min))
395             (while (search-forward "<story>" nil t)
396               (narrow-to-region (point) (search-forward "</story>"))
397               (goto-char (point-min))
398               (re-search-forward "<title>\\([^<]+\\)</title>")
399               (setq description
400                     (nnweb-decode-entities-string (match-string 1)))
401               (re-search-forward "<url>\\([^<]+\\)</url>")
402               (setq sid (match-string 1))
403               (string-match "sid=\\([0-9/]+\\)\\(.shtml\\|$\\)" sid)
404               (setq sid (match-string 1 sid))
405               (re-search-forward "<comments>\\([^<]+\\)</comments>")
406               (setq articles (string-to-number (match-string 1)))
407               (setq gname (concat description " (" sid ")"))
408               (if (setq elem (assoc gname nnslashdot-groups))
409                   (setcar (cdr elem) articles)
410                 (push (list gname articles sid (current-time)) 
411                       nnslashdot-groups))
412               (goto-char (point-max))
413               (widen)))
414           ;; Then do the older groups.
415           (while (> (- nnslashdot-group-number number) 0)
416             (mm-with-unibyte-buffer
417               (let ((case-fold-search t))
418                 (nnweb-insert (format nnslashdot-active-url number) t)
419                 (goto-char (point-min))
420                 (while (re-search-forward
421                         "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>"
422                         nil t)
423                   (setq sid (match-string 1)
424                         description
425                         (nnweb-decode-entities-string (match-string 2)))
426                   (forward-line 1)
427                   (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
428                     (setq articles (string-to-number (match-string 1))))
429                   (setq gname (concat description " (" sid ")"))
430                   (if (setq elem (assoc gname nnslashdot-groups))
431                       (setcar (cdr elem) articles)
432                     (push (list gname articles sid (current-time)) 
433                           nnslashdot-groups)))))
434             (incf number 30)))
435       (search-failed (nnslashdot-lose why)))
436     (nnslashdot-write-groups)
437     (nnslashdot-generate-active)
438     t))
439
440 (deffoo nnslashdot-request-newgroups (date &optional server)
441   (nnslashdot-possibly-change-server nil server)
442   (nnslashdot-generate-active)
443   t)
444
445 (deffoo nnslashdot-request-post (&optional server)
446   (nnslashdot-possibly-change-server nil server)
447   (let ((sid (nnslashdot-sid-strip (message-fetch-field "newsgroups")))
448         (subject (message-fetch-field "subject"))
449         (references (car (last (split-string
450                                 (message-fetch-field "references")))))
451         body quoted pid)
452     (string-match "%\\([0-9]+\\)@slashdot" references)
453     (setq pid (match-string 1 references))
454     (message-goto-body)
455     (narrow-to-region (point) (progn (message-goto-signature) (point)))
456     (goto-char (point-min))
457     (while (not (eobp))
458       (if (looking-at "> ")
459           (progn
460             (delete-region (point) (+ (point) 2))
461             (unless quoted
462               (insert "<blockquote>\n"))
463             (setq quoted t))
464         (when quoted
465           (insert "</blockquote>\n")
466           (setq quoted nil)))
467       (forward-line 1))
468     (goto-char (point-min))
469     (while (re-search-forward "^ *\n" nil t)
470       (replace-match "<p>\n"))
471     (widen)
472     (when (message-goto-signature)
473       (forward-line -1)
474       (insert "<p>\n")
475       (while (not (eobp))
476         (end-of-line)
477         (insert "<br>")
478         (forward-line 1)))
479     (message-goto-body)
480     (setq body (buffer-substring (point) (point-max)))
481     (erase-buffer)
482     (nnweb-fetch-form
483      "http://slashdot.org/comments.pl"
484      `(("sid" . ,sid)
485        ("pid" . ,pid)
486        ("rlogin" . "userlogin")
487        ("unickname" . ,nnslashdot-login-name)
488        ("upasswd" . ,nnslashdot-password)
489        ("postersubj" . ,subject)
490        ("op" . "Submit")
491        ("postercomment" . ,body)
492        ("posttype" . "html")))))
493
494 (deffoo nnslashdot-request-delete-group (group &optional force server)
495   (nnslashdot-possibly-change-server group server)
496   (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
497                                 nnslashdot-groups))
498   (nnslashdot-write-groups))
499
500 (deffoo nnslashdot-request-close ()
501   (setq nnslashdot-headers nil
502         nnslashdot-groups nil))
503
504 (deffoo nnslashdot-request-expire-articles
505     (articles group &optional server force)
506   (nnslashdot-possibly-change-server group server)
507   (let ((item (assoc group nnslashdot-groups)))
508     (when item
509       (if (fourth item)
510           (when (and (>= (length articles) (cadr item)) ;; All are expirable.
511                      (nnmail-expired-article-p 
512                       group
513                       (fourth item) 
514                       force))
515             (setq nnslashdot-groups (delq item nnslashdot-groups))
516             (nnslashdot-write-groups)
517             (setq articles nil)) ;; all expired.
518         (setcdr (cddr item) (list (current-time)))
519         (nnslashdot-write-groups))))
520   articles)
521
522 (nnoo-define-skeleton nnslashdot)
523
524 ;;; Internal functions
525
526 (defun nnslashdot-possibly-change-server (&optional group server)
527   (nnslashdot-init server)
528   (when (and server
529              (not (nnslashdot-server-opened server)))
530     (nnslashdot-open-server server))
531   (unless nnslashdot-groups
532     (nnslashdot-read-groups)))
533
534 (defun nnslashdot-read-groups ()
535   (let ((file (expand-file-name "groups" nnslashdot-directory)))
536     (when (file-exists-p file)
537       (mm-with-unibyte-buffer
538         (insert-file-contents file)
539         (goto-char (point-min))
540         (setq nnslashdot-groups (read (current-buffer)))))))
541
542 (defun nnslashdot-write-groups ()
543   (with-temp-file (expand-file-name "groups" nnslashdot-directory)
544     (prin1 nnslashdot-groups (current-buffer))))
545
546 (defun nnslashdot-init (server)
547   "Initialize buffers and such."
548   (unless (file-exists-p nnslashdot-directory)
549     (gnus-make-directory nnslashdot-directory))
550   (unless (gnus-buffer-live-p nnslashdot-buffer)
551     (setq nnslashdot-buffer
552           (save-excursion
553             (nnheader-set-temp-buffer
554              (format " *nnslashdot %s*" server))))))
555
556 (defun nnslashdot-date-to-date (sdate)
557   (condition-case err
558       (let ((elem (delete "" (split-string sdate))))
559         (concat (substring (nth 0 elem) 0 3) " "
560                 (substring (nth 1 elem) 0 3) " "
561                 (substring (nth 2 elem) 0 2) " "
562                 (substring (nth 3 elem) 1 6) " "
563                 (format-time-string "%Y") " "
564                 (nth 4 elem)))
565     (error "")))
566
567 (defun nnslashdot-generate-active ()
568   (save-excursion
569     (set-buffer nntp-server-buffer)
570     (erase-buffer)
571     (dolist (elem nnslashdot-groups)
572       (insert (prin1-to-string (car elem))
573               " " (number-to-string (cadr elem)) " 1 y\n"))))
574
575 (defun nnslashdot-lose (why)
576   (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
577
578 ;(defun nnslashdot-sid-strip (sid)
579 ;  (if (string-match "^00/" sid)
580 ;      (substring sid (match-end 0))
581 ;    sid))
582
583 (defalias 'nnslashdot-sid-strip 'identity)
584
585 (provide 'nnslashdot)
586
587 ;;; nnslashdot.el ends here