further compilation fixes for spam.el
[gnus] / 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-use-front-page nil
57   "Use the front page in addition to the backslash page.")
58
59 (defvoo nnslashdot-threshold -1
60   "The article threshold.")
61
62 (defvoo nnslashdot-threaded t
63   "Whether the nnslashdot groups should be threaded or not.")
64
65 (defvoo nnslashdot-group-number 0
66   "The number of non-fresh groups to keep updated.")
67
68 (defvoo nnslashdot-login-name ""
69   "The login name to use when posting.")
70
71 (defvoo nnslashdot-password ""
72   "The password to use when posting.")
73
74 ;;; Internal variables
75
76 (defvar nnslashdot-groups nil)
77 (defvar nnslashdot-buffer nil)
78 (defvar nnslashdot-headers nil)
79
80 ;;; Interface functions
81
82 (nnoo-define-basics nnslashdot)
83
84 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
85   (nnslashdot-possibly-change-server group server)
86   (condition-case why
87       (unless gnus-nov-is-evil
88         (nnslashdot-retrieve-headers-1 articles group))
89     (search-failed (nnslashdot-lose why))))
90
91 (deffoo nnslashdot-retrieve-headers-1 (articles group)
92   (let* ((last (car (last articles)))
93          (start (if nnslashdot-threaded 1 (pop articles)))
94          (entry (assoc group nnslashdot-groups))
95          (sid (nth 2 entry))
96          (first-comments t)
97          headers article subject score from date lines parent point cid
98          s startats changed)
99     (save-excursion
100       (set-buffer nnslashdot-buffer)
101       (let ((case-fold-search t))
102         (erase-buffer)
103         (when (= start 1)
104           (mm-url-insert (format nnslashdot-article-url sid) t)
105           (goto-char (point-min))
106           (if (eobp)
107               (error "Couldn't open connection to slashdot"))
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 "<" 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 sid
130                    nnslashdot-threshold 0 (- start 2))
131            t)
132           (when (and nnslashdot-threaded first-comments)
133             (setq first-comments nil)
134             (goto-char (point-max))
135             (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
136               (setq s (string-to-number (match-string 1)))
137               (unless (memq s startats)
138                 (push s startats)))
139             (setq startats (sort startats '<)))
140           (setq article (if (and article (< start article)) article start))
141           (goto-char point)
142           (while (re-search-forward
143                   "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
144                   nil t)
145             (setq cid (match-string 1)
146                   subject (match-string 3)
147                   score (match-string 5))
148             (unless (assq article (nth 4 entry))
149               (setcar (nthcdr 4 entry) (cons (cons article cid) (nth 4 entry)))
150               (setq changed t))
151             (when (string-match "^Re: *" subject)
152               (setq subject (concat "Re: " (substring subject (match-end 0)))))
153             (setq subject (mm-url-decode-entities-string subject))
154             (search-forward "<BR>")
155             (if (looking-at
156                  "by[ \t\n]+<a[^>]+>\\([^<]+\\)</a>[ \t\n]*(\\(<[^>]+>\\)*\\([^<>)]+\\))")
157                 (progn
158                   (goto-char (- (match-end 0) 5))
159                   (setq from (concat
160                               (mm-url-decode-entities-string (match-string 1))
161                               " <" (match-string 3) ">")))
162               (setq from "")
163               (when (looking-at "by \\([^<>]*\\) on ")
164                 (goto-char (- (match-end 0) 5))
165                 (setq from (mm-url-decode-entities-string (match-string 1)))))
166             (search-forward " on ")
167             (setq date
168                   (nnslashdot-date-to-date
169                    (buffer-substring (point) (progn (skip-chars-forward "^()<>\n\r") (point)))))
170             (setq lines (/ (abs (- (search-forward "<td")
171                                    (search-forward "</td>")))
172                            70))
173             (if (not
174                  (re-search-forward ".*cid=\\([0-9]+\\)\">Parent</A>" nil t))
175                 (setq parent nil)
176               (setq parent (match-string 1))
177               (when (string= parent "0")
178                 (setq parent nil)))
179             (push
180              (cons
181               article
182               (make-full-mail-header
183                article
184                (concat subject " (" score ")")
185                from date
186                (concat "<" sid "%" cid "@slashdot>")
187                (if parent
188                    (concat "<" sid "%" parent "@slashdot>")
189                  "")
190                0 lines nil nil))
191              headers)
192             (while (and articles (<= (car articles) article))
193               (pop articles))
194             (setq article (1+ article)))
195           (if nnslashdot-threaded
196               (progn
197                 (setq start (pop startats))
198                 (if start (setq start (+ start 2))))
199             (setq start (pop articles))))))
200     (if changed (nnslashdot-write-groups))
201     (setq nnslashdot-headers (sort headers 'car-less-than-car))
202     (save-excursion
203       (set-buffer nntp-server-buffer)
204       (erase-buffer)
205       (mm-with-unibyte-current-buffer
206        (dolist (header nnslashdot-headers)
207          (nnheader-insert-nov (cdr header)))))
208     'nov))
209
210 (deffoo nnslashdot-request-group (group &optional server dont-check)
211   (nnslashdot-possibly-change-server nil server)
212   (let ((elem (assoc group nnslashdot-groups)))
213     (cond
214      ((not elem)
215       (nnheader-report 'nnslashdot "Group does not exist"))
216      (t
217       (nnheader-report 'nnslashdot "Opened group %s" group)
218       (nnheader-insert
219        "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
220        (prin1-to-string group))))))
221
222 (deffoo nnslashdot-close-group (group &optional server)
223   (nnslashdot-possibly-change-server group server)
224   (when (gnus-buffer-live-p nnslashdot-buffer)