2001-02-24 00: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) nnslashdot-groups))
411               (goto-char (point-max))
412               (widen)))
413           ;; Then do the older groups.
414           (while (> (- nnslashdot-group-number number) 0)
415             (mm-with-unibyte-buffer
416               (let ((case-fold-search t))
417                 (nnweb-insert (format nnslashdot-active-url number) t)
418                 (goto-char (point-min))
419                 (while (re-search-forward
420                         "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>"
421                         nil t)
422                   (setq sid (match-string 1)
423                         description
424                         (nnweb-decode-entities-string (match-string 2)))
425                   (forward-line 1)
426                   (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
427                     (setq articles (string-to-number (match-string 1))))
428                   (setq gname (concat description " (" sid ")"))
429                   (if (setq elem (assoc gname nnslashdot-groups))
430                       (setcar (cdr elem) articles)
431                     (push (list gname articles sid) nnslashdot-groups)))))
432             (incf number 30)))
433       (search-failed (nnslashdot-lose why)))
434     (nnslashdot-write-groups)
435     (nnslashdot-generate-active)
436     t))
437
438 (deffoo nnslashdot-request-newgroups (date &optional server)
439   (nnslashdot-possibly-change-server nil server)
440   (nnslashdot-generate-active)
441   t)
442
443 (deffoo nnslashdot-request-post (&optional server)
444   (nnslashdot-possibly-change-server nil server)
445   (let ((sid (nnslashdot-sid-strip (message-fetch-field "newsgroups")))
446         (subject (message-fetch-field "subject"))
447         (references (car (last (split-string
448                                 (message-fetch-field "references")))))
449         body quoted pid)
450     (string-match "%\\([0-9]+\\)@slashdot" references)
451     (setq pid (match-string 1 references))
452     (message-goto-body)
453     (narrow-to-region (point) (progn (message-goto-signature) (point)))
454     (goto-char (point-min))
455     (while (not (eobp))
456       (if (looking-at "> ")
457           (progn
458             (delete-region (point) (+ (point) 2))
459             (unless quoted
460               (insert "<blockquote>\n"))
461             (setq quoted t))
462         (when quoted
463           (insert "</blockquote>\n")
464           (setq quoted nil)))
465       (forward-line 1))
466     (goto-char (point-min))
467     (while (re-search-forward "^ *\n" nil t)
468       (replace-match "<p>\n"))
469     (widen)
470     (when (message-goto-signature)
471       (forward-line -1)
472       (insert "<p>\n")
473       (while (not (eobp))
474         (end-of-line)
475         (insert "<br>")
476         (forward-line 1)))
477     (message-goto-body)
478     (setq body (buffer-substring (point) (point-max)))
479     (erase-buffer)
480     (nnweb-fetch-form
481      "http://slashdot.org/comments.pl"
482      `(("sid" . ,sid)
483        ("pid" . ,pid)
484        ("rlogin" . "userlogin")
485        ("unickname" . ,nnslashdot-login-name)
486        ("upasswd" . ,nnslashdot-password)
487        ("postersubj" . ,subject)
488        ("op" . "Submit")
489        ("postercomment" . ,body)
490        ("posttype" . "html")))))
491
492 (deffoo nnslashdot-request-delete-group (group &optional force server)
493   (nnslashdot-possibly-change-server group server)
494   (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
495                                 nnslashdot-groups))
496   (nnslashdot-write-groups))
497
498 (deffoo nnslashdot-request-close ()
499   (setq nnslashdot-headers nil
500         nnslashdot-groups nil))
501
502 (nnoo-define-skeleton nnslashdot)
503
504 ;;; Internal functions
505
506 (defun nnslashdot-possibly-change-server (&optional group server)
507   (nnslashdot-init server)
508   (when (and server
509              (not (nnslashdot-server-opened server)))
510     (nnslashdot-open-server server))
511   (unless nnslashdot-groups
512     (nnslashdot-read-groups)))
513
514 (defun nnslashdot-read-groups ()
515   (let ((file (expand-file-name "groups" nnslashdot-directory)))
516     (when (file-exists-p file)
517       (mm-with-unibyte-buffer
518         (insert-file-contents file)
519         (goto-char (point-min))
520         (setq nnslashdot-groups (read (current-buffer)))))))
521
522 (defun nnslashdot-write-groups ()
523   (with-temp-file (expand-file-name "groups" nnslashdot-directory)
524     (prin1 nnslashdot-groups (current-buffer))))
525
526 (defun nnslashdot-init (server)
527   "Initialize buffers and such."
528   (unless (file-exists-p nnslashdot-directory)
529     (gnus-make-directory nnslashdot-directory))
530   (unless (gnus-buffer-live-p nnslashdot-buffer)
531     (setq nnslashdot-buffer
532           (save-excursion
533             (nnheader-set-temp-buffer
534              (format " *nnslashdot %s*" server))))))
535
536 (defun nnslashdot-date-to-date (sdate)
537   (condition-case err
538       (let ((elem (delete "" (split-string sdate))))
539         (concat (substring (nth 0 elem) 0 3) " "
540                 (substring (nth 1 elem) 0 3) " "
541                 (substring (nth 2 elem) 0 2) " "
542                 (substring (nth 3 elem) 1 6) " "
543                 (format-time-string "%Y") " "
544                 (nth 4 elem)))
545     (error "")))
546
547 (defun nnslashdot-generate-active ()
548   (save-excursion
549     (set-buffer nntp-server-buffer)
550     (erase-buffer)
551     (dolist (elem nnslashdot-groups)
552       (insert (prin1-to-string (car elem))
553               " " (number-to-string (cadr elem)) " 1 y\n"))))
554
555 (defun nnslashdot-lose (why)
556   (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
557
558 ;(defun nnslashdot-sid-strip (sid)
559 ;  (if (string-match "^00/" sid)
560 ;      (substring sid (match-end 0))
561 ;    sid))
562
563 (defalias 'nnslashdot-sid-strip 'identity)
564
565 (provide 'nnslashdot)
566
567 ;;; nnslashdot.el ends here