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