1 ;;; nnslashdot.el --- interfacing with Slashdot
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (eval-when-compile (require 'cl
))
40 (nnoo-declare nnslashdot
)
42 (defvoo nnslashdot-directory
(nnheader-concat gnus-directory
"slashdot/")
43 "Where nnslashdot will save its files.")
45 (defvoo nnslashdot-active-url
"http://slashdot.org/search.pl?section=&min=%d"
46 "Where nnslashdot will fetch the active file from.")
48 (defvoo nnslashdot-comments-url
"http://slashdot.org/comments.pl?sid=%s&threshold=%d&commentsort=%d&mode=flat&startat=%d"
49 "Where nnslashdot will fetch comments from.")
51 (defvoo nnslashdot-article-url
52 "http://slashdot.org/article.pl?sid=%s&mode=nocomment"
53 "Where nnslashdot will fetch the article from.")
55 (defvoo nnslashdot-backslash-url
"http://slashdot.org/slashdot.xml"
56 "Where nnslashdot will fetch the stories from.")
58 (defvoo nnslashdot-use-front-page nil
59 "Use the front page in addition to the backslash page.")
61 (defvoo nnslashdot-threshold -
1
62 "The article threshold.")
64 (defvoo nnslashdot-threaded t
65 "Whether the nnslashdot groups should be threaded or not.")
67 (defvoo nnslashdot-group-number
0
68 "The number of non-fresh groups to keep updated.")
70 (defvoo nnslashdot-login-name
""
71 "The login name to use when posting.")
73 (defvoo nnslashdot-password
""
74 "The password to use when posting.")
76 ;;; Internal variables
78 (defvar nnslashdot-groups nil
)
79 (defvar nnslashdot-buffer nil
)
80 (defvar nnslashdot-headers nil
)
82 ;;; Interface functions
84 (nnoo-define-basics nnslashdot
)
86 (deffoo nnslashdot-retrieve-headers
(articles &optional group server fetch-old
)
87 (nnslashdot-possibly-change-server group server
)
89 (unless gnus-nov-is-evil
90 (nnslashdot-retrieve-headers-1 articles group
))
91 (search-failed (nnslashdot-lose why
))))
93 (deffoo nnslashdot-retrieve-headers-1
(articles group
)
94 (let* ((last (car (last articles
)))
95 (start (if nnslashdot-threaded
1 (pop articles
)))
96 (entry (assoc group nnslashdot-groups
))
99 headers article subject score from date lines parent point cid
102 (set-buffer nnslashdot-buffer
)
103 (let ((case-fold-search t
))
106 (mm-url-insert (format nnslashdot-article-url sid
) t
)
107 (goto-char (point-min))
109 (error "Couldn't open connection to slashdot"))
110 (re-search-forward "Posted by[ \t\r\n]+")
111 (when (looking-at "\\(<a[^>]+>\\)?[ \t\r\n]*\\([^<\r\n]+\\)")
112 (setq from
(mm-url-decode-entities-string (match-string 2))))
113 (search-forward "on ")
114 (setq date
(nnslashdot-date-to-date
115 (buffer-substring (point) (1- (search-forward "<")))))
116 (setq lines
(/ (- (point)
117 (progn (forward-line 1) (point)))
122 (make-full-mail-header
124 (concat "<" sid
"%1@slashdot>")
127 (setq start
(if nnslashdot-threaded
2 (pop articles
))))
128 (while (and start
(<= start last
))
129 (setq point
(goto-char (point-max)))
131 (format nnslashdot-comments-url sid
132 nnslashdot-threshold
0 (- start
2))
134 (when (and nnslashdot-threaded first-comments
)
135 (setq first-comments nil
)
136 (goto-char (point-max))
137 (while (re-search-backward "startat=\\([0-9]+\\)" nil t
)
138 (setq s
(string-to-number (match-string 1)))
139 (unless (memq s startats
)
141 (setq startats
(sort startats
'<)))
142 (setq article
(if (and article
(< start article
)) article start
))
144 (while (re-search-forward
145 "<a name=\"\\([0-9]+\\)\">\\([^<]+\\)\\(?:.*\n\\)\\{2,10\\}.*score:\\([^)]+\\))"
147 (setq cid
(match-string 1)
148 subject
(match-string 2)
149 score
(match-string 3))
150 (unless (assq article
(nth 4 entry
))
151 (setcar (nthcdr 4 entry
) (cons (cons article cid
) (nth 4 entry
)))
153 (when (string-match "^Re: *" subject
)
154 (setq subject
(concat "Re: " (substring subject
(match-end 0)))))
155 (setq subject
(mm-url-decode-entities-string subject
)
157 (when (re-search-forward "by[ \t\n]+<[^>]+>\\([^<(]+\\)" nil t
)
160 (mm-url-decode-entities-string (match-string 1))
161 " <nobody@slashdot.org>")))
162 (search-forward "on ")
164 (nnslashdot-date-to-date
166 (point) (progn (skip-chars-forward "^()<>\n\r") (point)))))
167 (setq lines
(/ (abs (- (search-forward "<div")
168 (search-forward "</div>")))
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
)
247 (search-forward "Posted by")
248 (search-forward "<div class=\"intro\">")
253 (search-forward "commentwrap")
254 (match-beginning 0)))))
255 (setq cid
(cdr (assq article
256 (nth 4 (assoc group nnslashdot-groups
)))))
257 (search-forward (format "<a name=\"%s\">" cid
))
260 (search-forward "<div class=\"commentBody\">")
262 (search-forward "<div class=\"commentSub\"")
263 (match-beginning 0))))))))
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 (first nnslashdot-use-front-page
)
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.
326 (> (- nnslashdot-group-number number
) 0))
328 (mm-with-unibyte-buffer
329 (let ((case-fold-search t
))
330 (mm-url-insert (format nnslashdot-active-url number
) t
)
331 (goto-char (point-min))
332 (while (re-search-forward
333 "article.pl\\?sid=\\([^&]+\\).*>\\([^<]+\\)</a>"
335 (setq sid
(match-string 1)
337 (mm-url-decode-entities-string (match-string 2)))
339 (when (re-search-forward "with \\([0-9]+\\) comment" nil t
)
340 (setq articles
(1+ (string-to-number (match-string 1)))))
341 (setq gname
(concat description
" (" sid
")"))
342 (if (setq elem
(assoc gname nnslashdot-groups
))
343 (setcar (cdr elem
) articles
)
344 (push (list gname articles sid
(current-time) nil
)
345 nnslashdot-groups
)))))
347 (search-failed (nnslashdot-lose why
)))
348 (nnslashdot-write-groups)
349 (nnslashdot-generate-active)
352 (deffoo nnslashdot-request-newgroups
(date &optional server
)
353 (nnslashdot-possibly-change-server nil server
)
354 (nnslashdot-generate-active)
357 (deffoo nnslashdot-request-post
(&optional server
)
358 (nnslashdot-possibly-change-server nil server
)
359 (let ((sid (message-fetch-field "newsgroups"))
360 (subject (message-fetch-field "subject"))
361 (references (car (last (split-string
362 (message-fetch-field "references")))))
364 (string-match "%\\([0-9]+\\)@slashdot" references
)
365 (setq pid
(match-string 1 references
))
367 (narrow-to-region (point) (progn (message-goto-signature) (point)))
368 (goto-char (point-min))
370 (if (looking-at "> ")
372 (delete-region (point) (+ (point) 2))
374 (insert "<blockquote>\n"))
377 (insert "</blockquote>\n")
380 (goto-char (point-min))
381 (while (re-search-forward "^ *\n" nil t
)
382 (replace-match "<p>\n"))
384 (when (message-goto-signature)
392 (setq body
(buffer-substring (point) (point-max)))
395 "http://slashdot.org/comments.pl"
398 ("rlogin" .
"userlogin")
399 ("unickname" .
,nnslashdot-login-name
)
400 ("upasswd" .
,nnslashdot-password
)
401 ("postersubj" .
,subject
)
403 ("postercomment" .
,body
)
404 ("posttype" .
"html")))))
406 (deffoo nnslashdot-request-delete-group
(group &optional force server
)
407 (nnslashdot-possibly-change-server group server
)
408 (setq nnslashdot-groups
(delq (assoc group nnslashdot-groups
)
410 (nnslashdot-write-groups))
412 (deffoo nnslashdot-request-close
()
413 (setq nnslashdot-headers nil
414 nnslashdot-groups nil
))
416 (deffoo nnslashdot-request-expire-articles
417 (articles group
&optional server force
)
418 (nnslashdot-possibly-change-server group server
)
419 (let ((item (assoc group nnslashdot-groups
)))
422 (when (and (>= (length articles
) (cadr item
)) ;; All are expirable.
423 (nnmail-expired-article-p
427 (setq nnslashdot-groups
(delq item nnslashdot-groups
))
428 (nnslashdot-write-groups)
429 (setq articles nil
)) ;; all expired.
430 (setcdr (cddr item
) (list (current-time)))
431 (nnslashdot-write-groups))))
434 (nnoo-define-skeleton nnslashdot
)
436 ;;; Internal functions
438 (defun nnslashdot-possibly-change-server (&optional group server
)
439 (nnslashdot-init server
)
441 (not (nnslashdot-server-opened server
)))
442 (nnslashdot-open-server server
))
443 (unless nnslashdot-groups
444 (nnslashdot-read-groups)))
446 (defun nnslashdot-make-tuple (tuple n
)
451 (setcdr tuple
(list nil
)))
452 (setq tuple
(cdr tuple
)
455 (defun nnslashdot-read-groups ()
456 (let ((file (expand-file-name "groups" nnslashdot-directory
)))
457 (when (file-exists-p file
)
458 (mm-with-unibyte-buffer
459 (insert-file-contents file
)
460 (goto-char (point-min))
461 (setq nnslashdot-groups
(read (current-buffer))))
462 (when (and nnslashdot-groups
(< (length (car nnslashdot-groups
)) 5))
463 (dolist (group nnslashdot-groups
)
464 (nnslashdot-make-tuple group
5))))))
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 (when (numberp (cadr elem
))
498 (insert (prin1-to-string (car elem
))
499 " " (number-to-string (cadr elem
)) " 1 y\n")))))
501 (defun nnslashdot-lose (why)
502 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
504 (provide 'nnslashdot
)
506 ;; arch-tag: aa73df7a-f7e6-4eef-bdea-5ce2f8c691b3
507 ;;; nnslashdot.el ends here