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