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