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))
29 (eval-when-compile (require 'gnus-clfns))
39 (nnoo-declare nnslashdot)
41 (defvoo nnslashdot-directory (nnheader-concat gnus-directory "slashdot/")
42 "Where nnslashdot will save its files.")
44 (defvoo nnslashdot-active-url "http://slashdot.org/search.pl?section=&min=%d"
45 "Where nnslashdot will fetch the active file from.")
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.")
50 (defvoo nnslashdot-article-url
51 "http://slashdot.org/article.pl?sid=%s&mode=nocomment"
52 "Where nnslashdot will fetch the article from.")
54 (defvoo nnslashdot-backslash-url "http://slashdot.org/slashdot.xml"
55 "Where nnslashdot will fetch the stories from.")
57 (defvoo nnslashdot-threshold -1
58 "The article threshold.")
60 (defvoo nnslashdot-threaded t
61 "Whether the nnslashdot groups should be threaded or not.")
63 (defvoo nnslashdot-group-number 0
64 "The number of non-fresh groups to keep updated.")
66 (defvoo nnslashdot-login-name ""
67 "The login name to use when posting.")
69 (defvoo nnslashdot-password ""
70 "The password to use when posting.")
72 ;;; Internal variables
74 (defvar nnslashdot-groups nil)
75 (defvar nnslashdot-buffer nil)
76 (defvar nnslashdot-headers nil)
78 ;;; Interface functions
80 (nnoo-define-basics nnslashdot)
82 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
83 (nnslashdot-possibly-change-server group server)
85 (unless gnus-nov-is-evil
86 (nnslashdot-retrieve-headers-1 articles group))
87 (search-failed (nnslashdot-lose why))))
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))
95 headers article subject score from date lines parent point cid
98 (set-buffer nnslashdot-buffer)
99 (let ((case-fold-search t))
102 (mm-url-insert (format nnslashdot-article-url sid) t)
103 (goto-char (point-min))
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)))
118 (make-full-mail-header
120 (concat "<" sid "%1@slashdot>")
123 (setq start (if nnslashdot-threaded 2 (pop articles))))
124 (while (and start (<= start last))
125 (setq point (goto-char (point-max)))
127 (format nnslashdot-comments-url sid
128 nnslashdot-threshold 0 (- start 2))
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)
137 (setq startats (sort startats '<)))
138 (setq article (if (and article (< start article)) article start))
140 (while (re-search-forward
141 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
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)))
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>")
154 "by[ \t\n]+<a[^>]+>\\([^<]+\\)</a>[ \t\n]*(\\(<[^>]+>\\)*\\([^<>)]+\\))")
156 (goto-char (- (match-end 0) 5))
158 (mm-url-decode-entities-string (match-string 1))
159 " <" (match-string 3) ">")))
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 ")
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>")))
172 (re-search-forward ".*cid=\\([0-9]+\\)\">Parent</A>" nil t))
174 (setq parent (match-string 1))
175 (when (string= parent "0")
180 (make-full-mail-header
182 (concat subject " (" score ")")
184 (concat "<" sid "%" cid "@slashdot>")
186 (concat "<" sid "%" parent "@slashdot>")
190 (while (and articles (<= (car articles) article))
192 (setq article (1+ article)))
193 (if nnslashdot-threaded
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))
201 (set-buffer nntp-server-buffer)
203 (mm-with-unibyte-current-buffer
204 (dolist (header nnslashdot-headers)
205 (nnheader-insert-nov (cdr header)))))
208 (deffoo nnslashdot-request-group (group &optional server dont-check)
209 (nnslashdot-possibly-change-server nil server)
210 (let ((elem (assoc group nnslashdot-groups)))
213 (nnheader-report 'nnslashdot "Group does not exist"))
215 (nnheader-report 'nnslashdot "Opened group %s" group)
217 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
218 (prin1-to-string group))))))
220 (deffoo nnslashdot-close-group (group &optional server)
221 (nnslashdot-possibly-change-server group server)
222 (when (gnus-buffer-live-p nnslashdot-buffer)
224 (set-buffer nnslashdot-buffer)
225 (kill-buffer nnslashdot-buffer)))
228 (deffoo nnslashdot-request-article (article &optional group server buffer)
229 (nnslashdot-possibly-change-server group server)
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))))
241 (if (equal (cdar map) cid)
242 (setq article (caar map)
244 (setq map (cdr map))))))
245 (when (numberp article)
250 (search-forward "<BR>")
256 "<IFRAME\\|<SCRIPT LANGUAGE=\"JAVASCRIPT\">\\|<!-- no ad 6 -->\\|< [ \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))
263 (re-search-forward "<td[^>]*>")
264 (search-forward "</td>")))))))
265 (search-failed (nnslashdot-lose why)))
269 (set-buffer (or buffer nntp-server-buffer))
271 (mm-with-unibyte-current-buffer
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))
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)))))
285 (deffoo nnslashdot-close-server (&optional server)
286 (when (and (nnslashdot-server-opened server)
287 (gnus-buffer-live-p nnslashdot-buffer))
289 (set-buffer nnslashdot-buffer)
290 (kill-buffer nnslashdot-buffer)))
291 (nnoo-close-server 'nnslashdot server))
293 (deffoo nnslashdot-request-list (&optional server)
294 (nnslashdot-possibly-change-server nil server)
296 sid elem description articles gname)
298 ;; First we do the Ultramode to get info on all the latest groups.
300 (mm-with-unibyte-buffer
301 (mm-url-insert nnslashdot-backslash-url t)
302 (goto-char (point-min))
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>")
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)
322 (goto-char (point-max))
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>"
333 (setq sid (match-string 1)
335 (mm-url-decode-entities-string (match-string 2)))
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)))))
345 (search-failed (nnslashdot-lose why)))
346 (nnslashdot-write-groups)
347 (nnslashdot-generate-active)
350 (deffoo nnslashdot-request-newgroups (date &optional server)
351 (nnslashdot-possibly-change-server nil server)
352 (nnslashdot-generate-active)
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")))))
362 (string-match "%\\([0-9]+\\)@slashdot" references)
363 (setq pid (match-string 1 references))
365 (narrow-to-region (point) (progn (message-goto-signature) (point)))
366 (goto-char (point-min))
368 (if (looking-at "> ")
370 (delete-region (point) (+ (point) 2))
372 (insert "<blockquote>\n"))
375 (insert "</blockquote>\n")
378 (goto-char (point-min))
379 (while (re-search-forward "^ *\n" nil t)
380 (replace-match "<p>\n"))
382 (when (message-goto-signature)
390 (setq body (buffer-substring (point) (point-max)))
393 "http://slashdot.org/comments.pl"
396 ("rlogin" . "userlogin")
397 ("unickname" . ,nnslashdot-login-name)
398 ("upasswd" . ,nnslashdot-password)
399 ("postersubj" . ,subject)
401 ("postercomment" . ,body)
402 ("posttype" . "html")))))
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)
408 (nnslashdot-write-groups))
410 (deffoo nnslashdot-request-close ()
411 (setq nnslashdot-headers nil
412 nnslashdot-groups nil))
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)))
420 (when (and (>= (length articles) (cadr item)) ;; All are expirable.
421 (nnmail-expired-article-p
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))))
432 (nnoo-define-skeleton nnslashdot)
434 ;;; Internal functions
436 (defun nnslashdot-possibly-change-server (&optional group server)
437 (nnslashdot-init server)
439 (not (nnslashdot-server-opened server)))
440 (nnslashdot-open-server server))
441 (unless nnslashdot-groups
442 (nnslashdot-read-groups)))
444 (defun nnslashdot-make-tuple (tuple n)
449 (setcdr tuple (list nil)))
450 (setq tuple (cdr tuple)
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))
463 (nnslashdot-make-tuple (car groups) 5)
464 (setq groups (cdr groups))))))))
466 (defun nnslashdot-write-groups ()
467 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
468 (gnus-prin1 nnslashdot-groups)))
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
477 (nnheader-set-temp-buffer
478 (format " *nnslashdot %s*" server))))
479 (push nnslashdot-buffer gnus-buffers)))
481 (defun nnslashdot-date-to-date (sdate)
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") " "
492 (defun nnslashdot-generate-active ()
494 (set-buffer nntp-server-buffer)
496 (dolist (elem nnslashdot-groups)
497 (insert (prin1-to-string (car elem))
498 " " (number-to-string (cadr elem)) " 1 y\n"))))
500 (defun nnslashdot-lose (why)
501 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
503 ;(defun nnslashdot-sid-strip (sid)
504 ; (if (string-match "^00/" sid)
505 ; (substring sid (match-end 0))
508 (provide 'nnslashdot)
510 ;;; nnslashdot.el ends here