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