1 ;;; nnslashdot.el --- interfacing with Slashdot
2 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 (eval-when-compile (require 'cl))
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-use-front-page nil
58 "Use the front page in addition to the backslash page.")
60 (defvoo nnslashdot-threshold -1
61 "The article threshold.")
63 (defvoo nnslashdot-threaded t
64 "Whether the nnslashdot groups should be threaded or not.")
66 (defvoo nnslashdot-group-number 0
67 "The number of non-fresh groups to keep updated.")
69 (defvoo nnslashdot-login-name ""
70 "The login name to use when posting.")
72 (defvoo nnslashdot-password ""
73 "The password to use when posting.")
75 ;;; Internal variables
77 (defvar nnslashdot-groups nil)
78 (defvar nnslashdot-buffer nil)
79 (defvar nnslashdot-headers nil)
81 ;;; Interface functions
83 (nnoo-define-basics nnslashdot)
85 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
86 (nnslashdot-possibly-change-server group server)
88 (unless gnus-nov-is-evil
89 (nnslashdot-retrieve-headers-1 articles group))
90 (search-failed (nnslashdot-lose why))))
92 (deffoo nnslashdot-retrieve-headers-1 (articles group)
93 (let* ((last (car (last articles)))
94 (start (if nnslashdot-threaded 1 (pop articles)))
95 (entry (assoc group nnslashdot-groups))
98 headers article subject score from date lines parent point cid
101 (set-buffer nnslashdot-buffer)
102 (let ((case-fold-search t))
105 (mm-url-insert (format nnslashdot-article-url sid) t)
106 (goto-char (point-min))
108 (error "Couldn't open connection to slashdot"))
109 (re-search-forward "Posted by[ \t\r\n]+")
110 (when (looking-at "\\(<a[^>]+>\\)?[ \t\r\n]*\\([^<\r\n]+\\)")
111 (setq from (mm-url-decode-entities-string (match-string 2))))
112 (search-forward "on ")
113 (setq date (nnslashdot-date-to-date
114 (buffer-substring (point) (1- (search-forward "<")))))
115 (setq lines (/ (- (point)
116 (progn (forward-line 1) (point)))
121 (make-full-mail-header
123 (concat "<" sid "%1@slashdot>")
126 (setq start (if nnslashdot-threaded 2 (pop articles))))
127 (while (and start (<= start last))
128 (setq point (goto-char (point-max)))
130 (format nnslashdot-comments-url sid
131 nnslashdot-threshold 0 (- start 2))
133 (when (and nnslashdot-threaded first-comments)
134 (setq first-comments nil)
135 (goto-char (point-max))
136 (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
137 (setq s (string-to-number (match-string 1)))
138 (unless (memq s startats)
140 (setq startats (sort startats '<)))
141 (setq article (if (and article (< start article)) article start))
143 (while (re-search-forward
144 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
146 (setq cid (match-string 1)
147 subject (match-string 3)
148 score (match-string 5))
149 (unless (assq article (nth 4 entry))
150 (setcar (nthcdr 4 entry) (cons (cons article cid) (nth 4 entry)))
152 (when (string-match "^Re: *" subject)
153 (setq subject (concat "Re: " (substring subject (match-end 0)))))
154 (setq subject (mm-url-decode-entities-string subject))
155 (search-forward "<BR>")
158 "by[ \t\n]+<a[^>]+>\\([^<]+\\)</a>[ \t\n]*(\\(<[^>]+>\\)*\\([^<>)]+\\))")
159 (goto-char (- (match-end 0) 5))
161 (mm-url-decode-entities-string (match-string 1))
162 " <" (match-string 3) ">")))
163 ((looking-at "by[ \t\n]+<a[^>]+>\\([^<(]+\\) (\\([0-9]+\\))</a>")
164 (goto-char (- (match-end 0) 5))
166 (mm-url-decode-entities-string (match-string 1))
167 " <" (match-string 2) ">")))
168 ((looking-at "by \\([^<>]*\\)[\t\n\r ]+on ")
169 (goto-char (- (match-end 0) 5))
170 (setq from (mm-url-decode-entities-string (match-string 1))))
173 (search-forward "on ")
175 (nnslashdot-date-to-date
176 (buffer-substring (point) (progn (skip-chars-forward "^()<>\n\r") (point)))))
177 (setq lines (/ (abs (- (search-forward "<td")
178 (search-forward "</td>")))
181 (re-search-forward ".*cid=\\([0-9]+\\)\">Parent</A>" nil t))
183 (setq parent (match-string 1))
184 (when (string= parent "0")
189 (make-full-mail-header
191 (concat subject " (" score ")")
193 (concat "<" sid "%" cid "@slashdot>")
195 (concat "<" sid "%" parent "@slashdot>")
199 (while (and articles (<= (car articles) article))
201 (setq article (1+ article)))
202 (if nnslashdot-threaded
204 (setq start (pop startats))
205 (if start (setq start (+ start 2))))
206 (setq start (pop articles))))))
207 (if changed (nnslashdot-write-groups))
208 (setq nnslashdot-headers (sort headers 'car-less-than-car))
210 (set-buffer nntp-server-buffer)
212 (mm-with-unibyte-current-buffer
213 (dolist (header nnslashdot-headers)
214 (nnheader-insert-nov (cdr header)))))
217 (deffoo nnslashdot-request-group (group &optional server dont-check)
218 (nnslashdot-possibly-change-server nil server)
219 (let ((elem (assoc group nnslashdot-groups)))
222 (nnheader-report 'nnslashdot "Group does not exist"))
224 (nnheader-report 'nnslashdot "Opened group %s" group)
226 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
227 (prin1-to-string group))))))
229 (deffoo nnslashdot-close-group (group &optional server)
230 (nnslashdot-possibly-change-server group server)
231 (when (gnus-buffer-live-p nnslashdot-buffer)
233 (set-buffer nnslashdot-buffer)
234 (kill-buffer nnslashdot-buffer)))
237 (deffoo nnslashdot-request-article (article &optional group server buffer)
238 (nnslashdot-possibly-change-server group server)
242 (set-buffer nnslashdot-buffer)
243 (let ((case-fold-search t))
244 (goto-char (point-min))
245 (when (and (stringp article)
246 (string-match "%\\([0-9]+\\)@" article))
247 (setq cid (match-string 1 article))
248 (let ((map (nth 4 (assoc group nnslashdot-groups))))
250 (if (equal (cdar map) cid)
251 (setq article (caar map)
253 (setq map (cdr map))))))
254 (when (numberp article)
257 (search-forward "Posted by")
258 (search-forward "<BR>")
264 "<IFRAME\\|<SCRIPT LANGUAGE=\"JAVASCRIPT\">\\|<!-- no ad 6 -->\\|< [ \t\r\n]*<A HREF=\"\\(\\(http:\\)?//slashdot\\.org\\)?/article")
265 (match-beginning 0)))))
266 (setq cid (cdr (assq article
267 (nth 4 (assoc group nnslashdot-groups)))))
268 (search-forward (format "<a name=\"%s\">" cid))
271 (re-search-forward "<td[^>]*>")
272 (search-forward "</td>")))))))
273 (search-failed (nnslashdot-lose why)))
277 (set-buffer (or buffer nntp-server-buffer))
279 (mm-with-unibyte-current-buffer
281 (goto-char (point-min))
282 (while (re-search-forward "\\(<br>\r?\\)+" nil t)
283 (replace-match "<p>" t t))
284 (goto-char (point-min))
285 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
286 (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
288 (let ((header (cdr (assq article nnslashdot-headers))))
289 (nnheader-insert-header header))
290 (nnheader-report 'nnslashdot "Fetched article %s" article))
291 (cons group article)))))
293 (deffoo nnslashdot-close-server (&optional server)
294 (when (and (nnslashdot-server-opened server)
295 (gnus-buffer-live-p nnslashdot-buffer))
297 (set-buffer nnslashdot-buffer)
298 (kill-buffer nnslashdot-buffer)))
299 (nnoo-close-server 'nnslashdot server))
301 (deffoo nnslashdot-request-list (&optional server)
302 (nnslashdot-possibly-change-server nil server)
304 (first nnslashdot-use-front-page)
305 sid elem description articles gname)
307 ;; First we do the Ultramode to get info on all the latest groups.
309 (mm-with-unibyte-buffer
310 (mm-url-insert nnslashdot-backslash-url t)
311 (goto-char (point-min))
313 (error "Couldn't open connection to slashdot"))
314 (while (search-forward "<story>" nil t)
315 (narrow-to-region (point) (search-forward "</story>"))
316 (goto-char (point-min))
317 (re-search-forward "<title>\\([^<]+\\)</title>")
319 (mm-url-decode-entities-string (match-string 1)))
320 (re-search-forward "<url>\\([^<]+\\)</url>")
321 (setq sid (match-string 1))
322 (string-match "sid=\\([0-9/]+\\)\\(.shtml\\|$\\)" sid)
323 (setq sid (match-string 1 sid))
324 (re-search-forward "<comments>\\([^<]+\\)</comments>")
325 (setq articles (string-to-number (match-string 1)))
326 (setq gname (concat description " (" sid ")"))
327 (if (setq elem (assoc gname nnslashdot-groups))
328 (setcar (cdr elem) articles)
329 (push (list gname articles sid (current-time) nil)
331 (goto-char (point-max))
333 ;; Then do the older groups.
335 (> (- nnslashdot-group-number number) 0))
337 (mm-with-unibyte-buffer
338 (let ((case-fold-search t))
339 (mm-url-insert (format nnslashdot-active-url number) t)
340 (goto-char (point-min))
341 (while (re-search-forward
342 "article.pl\\?sid=\\([^&]+\\).*>\\([^<]+\\)</a>"
344 (setq sid (match-string 1)
346 (mm-url-decode-entities-string (match-string 2)))
348 (when (re-search-forward "with \\([0-9]+\\) comment" nil t)
349 (setq articles (1+ (string-to-number (match-string 1)))))
350 (setq gname (concat description " (" sid ")"))
351 (if (setq elem (assoc gname nnslashdot-groups))
352 (setcar (cdr elem) articles)
353 (push (list gname articles sid (current-time) nil)
354 nnslashdot-groups)))))
356 (search-failed (nnslashdot-lose why)))
357 (nnslashdot-write-groups)
358 (nnslashdot-generate-active)
361 (deffoo nnslashdot-request-newgroups (date &optional server)
362 (nnslashdot-possibly-change-server nil server)
363 (nnslashdot-generate-active)
366 (deffoo nnslashdot-request-post (&optional server)
367 (nnslashdot-possibly-change-server nil server)
368 (let ((sid (message-fetch-field "newsgroups"))
369 (subject (message-fetch-field "subject"))
370 (references (car (last (split-string
371 (message-fetch-field "references")))))
373 (string-match "%\\([0-9]+\\)@slashdot" references)
374 (setq pid (match-string 1 references))
376 (narrow-to-region (point) (progn (message-goto-signature) (point)))
377 (goto-char (point-min))
379 (if (looking-at "> ")
381 (delete-region (point) (+ (point) 2))
383 (insert "<blockquote>\n"))
386 (insert "</blockquote>\n")
389 (goto-char (point-min))
390 (while (re-search-forward "^ *\n" nil t)
391 (replace-match "<p>\n"))
393 (when (message-goto-signature)
401 (setq body (buffer-substring (point) (point-max)))
404 "http://slashdot.org/comments.pl"
407 ("rlogin" . "userlogin")
408 ("unickname" . ,nnslashdot-login-name)
409 ("upasswd" . ,nnslashdot-password)
410 ("postersubj" . ,subject)
412 ("postercomment" . ,body)
413 ("posttype" . "html")))))
415 (deffoo nnslashdot-request-delete-group (group &optional force server)
416 (nnslashdot-possibly-change-server group server)
417 (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
419 (nnslashdot-write-groups))
421 (deffoo nnslashdot-request-close ()
422 (setq nnslashdot-headers nil
423 nnslashdot-groups nil))
425 (deffoo nnslashdot-request-expire-articles
426 (articles group &optional server force)
427 (nnslashdot-possibly-change-server group server)
428 (let ((item (assoc group nnslashdot-groups)))
431 (when (and (>= (length articles) (cadr item)) ;; All are expirable.
432 (nnmail-expired-article-p
436 (setq nnslashdot-groups (delq item nnslashdot-groups))
437 (nnslashdot-write-groups)
438 (setq articles nil)) ;; all expired.
439 (setcdr (cddr item) (list (current-time)))
440 (nnslashdot-write-groups))))
443 (nnoo-define-skeleton nnslashdot)
445 ;;; Internal functions
447 (defun nnslashdot-possibly-change-server (&optional group server)
448 (nnslashdot-init server)
450 (not (nnslashdot-server-opened server)))
451 (nnslashdot-open-server server))
452 (unless nnslashdot-groups
453 (nnslashdot-read-groups)))
455 (defun nnslashdot-make-tuple (tuple n)
460 (setcdr tuple (list nil)))
461 (setq tuple (cdr tuple)
464 (defun nnslashdot-read-groups ()
465 (let ((file (expand-file-name "groups" nnslashdot-directory)))
466 (when (file-exists-p file)
467 (mm-with-unibyte-buffer
468 (insert-file-contents file)
469 (goto-char (point-min))
470 (setq nnslashdot-groups (read (current-buffer))))
471 (if (and nnslashdot-groups (< (length (car nnslashdot-groups)) 5))
472 (let ((groups nnslashdot-groups))
474 (nnslashdot-make-tuple (car groups) 5)
475 (setq groups (cdr groups))))))))
477 (defun nnslashdot-write-groups ()
478 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
479 (gnus-prin1 nnslashdot-groups)))
481 (defun nnslashdot-init (server)
482 "Initialize buffers and such."
483 (unless (file-exists-p nnslashdot-directory)
484 (gnus-make-directory nnslashdot-directory))
485 (unless (gnus-buffer-live-p nnslashdot-buffer)
486 (setq nnslashdot-buffer
488 (nnheader-set-temp-buffer
489 (format " *nnslashdot %s*" server))))
490 (push nnslashdot-buffer gnus-buffers)))
492 (defun nnslashdot-date-to-date (sdate)
494 (let ((elem (delete "" (split-string sdate))))
495 (concat (substring (nth 0 elem) 0 3) " "
496 (substring (nth 1 elem) 0 3) " "
497 (substring (nth 2 elem) 0 2) " "
498 (substring (nth 3 elem) 1 6) " "
499 (format-time-string "%Y") " "
503 (defun nnslashdot-generate-active ()
505 (set-buffer nntp-server-buffer)
507 (dolist (elem nnslashdot-groups)
508 (when (numberp (cadr elem))
509 (insert (prin1-to-string (car elem))
510 " " (number-to-string (cadr elem)) " 1 y\n")))))
512 (defun nnslashdot-lose (why)
513 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
515 (provide 'nnslashdot)
517 ;;; nnslashdot.el ends here