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