1 ;;; nnslashdot.el --- interfacing with Slashdot
2 ;; Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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)
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.
28 (eval-when-compile (require 'cl))
38 (nnoo-declare nnslashdot)
40 (defvoo nnslashdot-directory (nnheader-concat gnus-directory "slashdot/")
41 "Where nnslashdot will save its files.")
43 (defvoo nnslashdot-active-url "http://slashdot.org/search.pl?section=&min=%d"
44 "Where nnslashdot will fetch the active file from.")
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.")
49 (defvoo nnslashdot-article-url
50 "http://slashdot.org/article.pl?sid=%s&mode=nocomment"
51 "Where nnslashdot will fetch the article from.")
53 (defvoo nnslashdot-backslash-url "http://slashdot.org/slashdot.xml"
54 "Where nnslashdot will fetch the stories from.")
56 (defvoo nnslashdot-threshold -1
57 "The article threshold.")
59 (defvoo nnslashdot-threaded t
60 "Whether the nnslashdot groups should be threaded or not.")
62 (defvoo nnslashdot-group-number 0
63 "The number of non-fresh groups to keep updated.")
65 (defvoo nnslashdot-login-name ""
66 "The login name to use when posting.")
68 (defvoo nnslashdot-password ""
69 "The password to use when posting.")
71 ;;; Internal variables
73 (defvar nnslashdot-groups nil)
74 (defvar nnslashdot-buffer nil)
75 (defvar nnslashdot-headers nil)
77 ;;; Interface functions
79 (nnoo-define-basics nnslashdot)
81 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
82 (nnslashdot-possibly-change-server group server)
84 (unless gnus-nov-is-evil
85 (nnslashdot-retrieve-headers-1 articles group))
86 (search-failed (nnslashdot-lose why))))
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))
94 headers article subject score from date lines parent point cid
97 (set-buffer nnslashdot-buffer)
98 (let ((case-fold-search t))
101 (mm-url-insert (format nnslashdot-article-url sid) t)
102 (goto-char (point-min))
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)))
117 (make-full-mail-header
119 (concat "<" sid "%1@slashdot>")
122 (setq start (if nnslashdot-threaded 2 (pop articles))))
123 (while (and start (<= start last))
124 (setq point (goto-char (point-max)))
126 (format nnslashdot-comments-url sid
127 nnslashdot-threshold 0 (- start 2))
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)
136 (setq startats (sort startats '<)))
137 (setq article (if (and article (< start article)) article start))
139 (while (re-search-forward
140 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
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)))
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>")
153 "by[ \t\n]+<a[^>]+>\\([^<]+\\)</a>[ \t\n]*(\\(<[^>]+>\\)*\\([^<>)]+\\))")
155 (goto-char (- (match-end 0) 5))
157 (mm-url-decode-entities-string (match-string 1))
158 " <" (match-string 3) ">")))
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 ")
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>")))
171 (re-search-forward ".*cid=\\([0-9]+\\)\">Parent</A>" nil t))
173 (setq parent (match-string 1))
174 (when (string= parent "0")
179 (make-full-mail-header
181 (concat subject " (" score ")")
183 (concat "<" sid "%" cid "@slashdot>")
185 (concat "<" sid "%" parent "@slashdot>")
189 (while (and articles (<= (car articles) article))
191 (setq article (1+ article)))
192 (if nnslashdot-threaded
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))
200 (set-buffer nntp-server-buffer)
202 (mm-with-unibyte-current-buffer
203 (dolist (header nnslashdot-headers)
204 (nnheader-insert-nov (cdr header)))))
207 (deffoo nnslashdot-request-group (group &optional server dont-check)
208 (nnslashdot-possibly-change-server nil server)
209 (let ((elem (assoc group nnslashdot-groups)))
212 (nnheader-report 'nnslashdot "Group does not exist"))
214 (nnheader-report 'nnslashdot "Opened group %s" group)
216 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
217 (prin1-to-string group))))))
219 (deffoo nnslashdot-close-group (group &optional server)
220 (nnslashdot-possibly-change-server group server)
221 (when (gnus-buffer-live-p nnslashdot-buffer)
223 (set-buffer nnslashdot-buffer)
224 (kill-buffer nnslashdot-buffer)))
227 (deffoo nnslashdot-request-article (article &optional group server buffer)
228 (nnslashdot-possibly-change-server group server)
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))))
240 (if (equal (cdar map) cid)
241 (setq article (caar map)
243 (setq map (cdr map))))))
244 (when (numberp article)
249 (search-forward "<BR>")
255 "< [ \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))
262 (re-search-forward "<td[^>]*>")
263 (search-forward "</td>")))))))
264 (search-failed (nnslashdot-lose why)))
268 (set-buffer (or buffer nntp-server-buffer))
270 (mm-with-unibyte-current-buffer
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))
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)))))
284 (deffoo nnslashdot-close-server (&optional server)
285 (when (and (nnslashdot-server-opened server)
286 (gnus-buffer-live-p nnslashdot-buffer))
288 (set-buffer nnslashdot-buffer)
289 (kill-buffer nnslashdot-buffer)))
290 (nnoo-close-server 'nnslashdot server))
292 (deffoo nnslashdot-request-list (&optional server)
293 (nnslashdot-possibly-change-server nil server)
295 sid elem description articles gname)
297 ;; First we do the Ultramode to get info on all the latest groups.
299 (mm-with-unibyte-buffer
300 (mm-url-insert nnslashdot-backslash-url t)
301 (goto-char (point-min))
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>")
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)
321 (goto-char (point-max))
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>"
332 (setq sid (match-string 1)
334 (mm-url-decode-entities-string (match-string 2)))
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)))))
344 (search-failed (nnslashdot-lose why)))
345 (nnslashdot-write-groups)
346 (nnslashdot-generate-active)
349 (deffoo nnslashdot-request-newgroups (date &optional server)
350 (nnslashdot-possibly-change-server nil server)
351 (nnslashdot-generate-active)
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")))))
361 (string-match "%\\([0-9]+\\)@slashdot" references)
362 (setq pid (match-string 1 references))
364 (narrow-to-region (point) (progn (message-goto-signature) (point)))
365 (goto-char (point-min))
367 (if (looking-at "> ")
369 (delete-region (point) (+ (point) 2))
371 (insert "<blockquote>\n"))
374 (insert "</blockquote>\n")
377 (goto-char (point-min))
378 (while (re-search-forward "^ *\n" nil t)
379 (replace-match "<p>\n"))
381 (when (message-goto-signature)
389 (setq body (buffer-substring (point) (point-max)))
392 "http://slashdot.org/comments.pl"
395 ("rlogin" . "userlogin")
396 ("unickname" . ,nnslashdot-login-name)
397 ("upasswd" . ,nnslashdot-password)
398 ("postersubj" . ,subject)
400 ("postercomment" . ,body)
401 ("posttype" . "html")))))
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)
407 (nnslashdot-write-groups))
409 (deffoo nnslashdot-request-close ()
410 (setq nnslashdot-headers nil
411 nnslashdot-groups nil))
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)))
419 (when (and (>= (length articles) (cadr item)) ;; All are expirable.
420 (nnmail-expired-article-p
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))))
431 (nnoo-define-skeleton nnslashdot)
433 ;;; Internal functions
435 (defun nnslashdot-possibly-change-server (&optional group server)
436 (nnslashdot-init server)
438 (not (nnslashdot-server-opened server)))
439 (nnslashdot-open-server server))
440 (unless nnslashdot-groups
441 (nnslashdot-read-groups)))
443 (defun nnslashdot-make-tuple (tuple n)
448 (setcdr tuple (list nil)))
449 (setq tuple (cdr tuple)
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))
462 (nnslashdot-make-tuple (car groups) 5)
463 (setq groups (cdr groups))))))))
465 (defun nnslashdot-write-groups ()
466 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
467 (gnus-prin1 nnslashdot-groups)))
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
476 (nnheader-set-temp-buffer
477 (format " *nnslashdot %s*" server))))
478 (push nnslashdot-buffer gnus-buffers)))
480 (defun nnslashdot-date-to-date (sdate)
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") " "
491 (defun nnslashdot-generate-active ()
493 (set-buffer nntp-server-buffer)
495 (dolist (elem nnslashdot-groups)
496 (insert (prin1-to-string (car elem))
497 " " (number-to-string (cadr elem)) " 1 y\n"))))
499 (defun nnslashdot-lose (why)
500 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
502 (provide 'nnslashdot)
504 ;;; nnslashdot.el ends here