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