1 ;;; nnslashdot.el --- interfacing with Slashdot
2 ;; Copyright (C) 1999 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.
26 ;; Note: You need to have `url' and `w3' installed for this
31 (eval-when-compile (require 'cl))
45 ;; Report failure to find w3 at load time if appropriate.
51 (nnoo-declare nnslashdot)
53 (defvoo nnslashdot-directory (nnheader-concat gnus-directory "slashdot/")
54 "Where nnslashdot will save its files.")
56 (defvoo nnslashdot-active-url "http://slashdot.org/search.pl?section=&min=%d"
57 "Where nnslashdot will fetch the active file from.")
59 (defvoo nnslashdot-comments-url "http://slashdot.org/comments.pl?sid=%s&threshold=%d&commentsort=%d&mode=flat&startat=%d"
60 "Where nnslashdot will fetch comments from.")
62 (defvoo nnslashdot-article-url
63 "http://slashdot.org/article.pl?sid=%s&mode=nocomment"
64 "Where nnslashdot will fetch the article from.")
66 (defvoo nnslashdot-threshold -1
67 "The article threshold.")
69 (defvoo nnslashdot-threaded t
70 "Whether the nnslashdot groups should be threaded or not.")
72 (defvoo nnslashdot-group-number 30
73 "The number of non-fresh groups to keep updated.")
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)
87 (unless gnus-nov-is-evil
88 (if nnslashdot-threaded
89 (nnslashdot-threaded-retrieve-headers articles group)
90 (nnslashdot-sane-retrieve-headers articles group))))
92 (deffoo nnslashdot-threaded-retrieve-headers (articles group)
93 (let ((last (car (last articles)))
96 (sid (caddr (assoc group nnslashdot-groups)))
99 headers article subject score from date lines parent point s)
101 (set-buffer nnslashdot-buffer)
102 (let ((case-fold-search t))
105 (nnweb-insert (format nnslashdot-article-url sid))
106 (goto-char (point-min))
107 (search-forward "Posted by ")
108 (when (looking-at "<a[^>]+>\\([^<]+\\)")
109 (setq from (match-string 1)))
110 (search-forward " on ")
111 (setq date (nnslashdot-date-to-date
112 (buffer-substring (point) (1- (search-forward "<")))))
114 (setq lines (count-lines
117 "A href=http://slashdot.org/article.pl")))
121 (make-full-mail-header
122 1 group from date (concat "<" sid "%1@slashdot>")
125 (while (and (setq start (pop startats))
127 (setq point (goto-char (point-max)))
129 (format nnslashdot-comments-url sid nnslashdot-threshold 0 start))
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 '<)))
139 (while (re-search-forward
140 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
142 (setq article (string-to-number (match-string 1))
143 subject (match-string 2)
144 score (match-string 3))
145 (when (string-match "^Re: *" subject)
146 (setq subject (concat "Re: " (substring subject (match-end 0)))))
149 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
150 (setq from (concat (match-string 1)
151 " <" (match-string 2) ">"))
152 (looking-at "by \\(.+\\) on ")
153 (setq from (match-string 1)))
154 (goto-char (- (match-end 0) 5))
155 (search-forward " on ")
157 (nnslashdot-date-to-date
158 (buffer-substring (point) (progn (end-of-line) (point)))))
159 (setq lines (/ (abs (- (search-forward "<td ")
160 (search-forward "</td>")))
164 (if (looking-at ".*cid=\\([0-9]+\\)")
171 (make-full-mail-header
173 (concat subject " (" score ")")
176 (number-to-string (1+ article))
180 (number-to-string (1+ (string-to-number parent)))
185 (setq nnslashdot-headers (sort headers 'car-less-than-car))
187 (set-buffer nntp-server-buffer)
189 (dolist (header nnslashdot-headers)
190 (nnheader-insert-nov (cdr header))))
193 (deffoo nnslashdot-sane-retrieve-headers (articles group)
194 (let ((last (car (last articles)))
196 (start (max (1- (car articles)) 1))
197 (sid (caddr (assoc group nnslashdot-groups)))
198 headers article subject score from date lines parent point)
200 (set-buffer nnslashdot-buffer)
203 (nnweb-insert (format nnslashdot-article-url sid))
204 (goto-char (point-min))
205 (search-forward "Posted by ")
206 (when (looking-at "<a[^>]+>\\([^<]+\\)")
207 (setq from (match-string 1)))
208 (search-forward " on ")
209 (setq date (nnslashdot-date-to-date
210 (buffer-substring (point) (1- (search-forward "<")))))
212 (setq lines (count-lines (point)
214 "A href=http://slashdot.org/article.pl")))
218 (make-full-mail-header
219 1 group from date (concat "<" sid "%1@slashdot>")
222 (while (or (not article)
226 (setq start (1+ article)))
227 (setq point (goto-char (point-max)))
229 (format nnslashdot-comments-url sid nnslashdot-threshold 4 start))
231 (while (re-search-forward
232 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score\\([^)]+\\))"
234 (setq article (string-to-number (match-string 1))
235 subject (match-string 2)
236 score (match-string 3))
239 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
240 (setq from (concat (match-string 1) " <" (match-string 2) ">"))
241 (looking-at "by \\(.+\\) on ")
242 (setq from (match-string 1)))
243 (goto-char (- (match-end 0) 5))
244 (search-forward " on ")
246 (nnslashdot-date-to-date
247 (buffer-substring (point) (progn (end-of-line) (point)))))
248 (setq lines (/ (abs (- (search-forward "<td ")
249 (search-forward "</td>")))
253 (if (looking-at ".*cid=\\([0-9]+\\)")
260 (make-full-mail-header
261 (1+ article) (concat subject " (" score ")")
264 (number-to-string (1+ article))
268 (number-to-string (1+ (string-to-number parent)))
273 (setq nnslashdot-headers
274 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
276 (set-buffer nntp-server-buffer)
278 (dolist (header nnslashdot-headers)
279 (nnheader-insert-nov (cdr header))))
282 (deffoo nnslashdot-request-group (group &optional server dont-check)
283 (nnslashdot-possibly-change-server nil server)
284 (let ((elem (assoc group nnslashdot-groups)))
287 (nnheader-report 'nnslashdot "Group does not exist"))
289 (nnheader-report 'nnslashdot "Opened group %s" group)
291 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
292 (prin1-to-string group))))))
294 (deffoo nnslashdot-close-group (group &optional server)
295 (nnslashdot-possibly-change-server group server)
296 (when (gnus-buffer-live-p nnslashdot-buffer)
298 (set-buffer nnslashdot-buffer)
299 (kill-buffer nnslashdot-buffer)))
302 (deffoo nnslashdot-request-article (article &optional group server buffer)
303 (nnslashdot-possibly-change-server group server)
306 (set-buffer nnslashdot-buffer)
307 (let ((case-fold-search t))
308 (goto-char (point-min))
309 (when (and (stringp article)
310 (string-match "%\\([0-9]+\\)@" article))
311 (setq article (string-to-number (match-string 1 article))))
312 (when (numberp article)
315 (re-search-forward "Posted by .* on ")
322 "^<p>.*A href=http://slashdot.org/article.pl")
323 (match-beginning 0)))))
324 (search-forward (format "<a name=\"%d\">" (1- article)))
327 (re-search-forward "<td[^>]+>")
328 (search-forward "</td>")))))))
331 (set-buffer (or buffer nntp-server-buffer))
334 (goto-char (point-min))
335 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
336 (let ((header (cdr (assq article nnslashdot-headers))))
337 (nnheader-insert-header header))
338 (nnheader-report 'nnslashdot "Fetched article %s" article)
339 (cons group article)))))
341 (deffoo nnslashdot-close-server (&optional server)
342 (when (and (nnslashdot-server-opened server)
343 (gnus-buffer-live-p nnslashdot-buffer))
345 (set-buffer nnslashdot-buffer)
346 (kill-buffer nnslashdot-buffer)))
347 (nnoo-close-server 'nnslashdot server))
349 (deffoo nnslashdot-request-list (&optional server)
350 (nnslashdot-possibly-change-server nil server)
352 sid elem description articles gname)
353 ;; First we do the Ultramode to get info on all the latest groups.
355 (nnweb-insert "http://slashdot.org/slashdot.xml")
356 (goto-char (point-min))
357 (while (search-forward "<story>" nil t)
358 (narrow-to-region (point) (search-forward "</story>"))
359 (goto-char (point-min))
360 (re-search-forward "<title>\\([^<]+\\)</title>")
361 (setq description (match-string 1))
362 (re-search-forward "<url>\\([^<]+\\)</url>")
363 (setq sid (match-string 1))
364 (string-match "/\\([0-9/]+\\).shtml" sid)
365 (setq sid (match-string 1 sid))
366 (re-search-forward "<comments>\\([^<]+\\)</comments>")
367 (setq articles (string-to-number (match-string 1)))
368 (setq gname (concat description " (" sid ")"))
369 (if (setq elem (assoc gname nnslashdot-groups))
370 (setcar (cdr elem) articles)
371 (push (list gname articles sid) nnslashdot-groups))
372 (goto-char (point-max))
374 ;; Then do the older groups.
375 (while (> (- nnslashdot-group-number number) 0)
377 (let ((case-fold-search t))
378 (nnweb-insert (format nnslashdot-active-url number))
379 (goto-char (point-min))
380 (while (re-search-forward
381 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
382 (setq sid (match-string 1)
383 description (match-string 2))
385 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
386 (setq articles (string-to-number (match-string 1))))
387 (setq gname (concat description " (" sid ")"))
388 (if (setq elem (assoc gname nnslashdot-groups))
389 (setcar (cdr elem) articles)
390 (push (list gname articles sid) nnslashdot-groups)))))
392 (nnslashdot-write-groups)
393 (nnslashdot-generate-active)
396 (deffoo nnslashdot-request-newgroups (date &optional server)
397 (nnslashdot-possibly-change-server nil server)
398 (nnslashdot-generate-active)
401 (nnoo-define-skeleton nnslashdot)
403 ;;; Internal functions
405 (defun nnslashdot-possibly-change-server (&optional group server)
406 (nnslashdot-init server)
408 (not (nnslashdot-server-opened server)))
409 (nnslashdot-open-server server))
410 (unless nnslashdot-groups
411 (nnslashdot-read-groups)))
413 (defun nnslashdot-read-groups ()
414 (let ((file (expand-file-name "groups" nnslashdot-directory)))
415 (when (file-exists-p file)
417 (insert-file-contents file)
418 (goto-char (point-min))
419 (setq nnslashdot-groups (read (current-buffer)))))))
421 (defun nnslashdot-write-groups ()
422 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
423 (prin1 nnslashdot-groups (current-buffer))))
425 (defun nnslashdot-init (server)
426 "Initialize buffers and such."
427 (unless (file-exists-p nnslashdot-directory)
428 (gnus-make-directory nnslashdot-directory))
429 (unless (gnus-buffer-live-p nnslashdot-buffer)
430 (setq nnslashdot-buffer
432 (nnheader-set-temp-buffer
433 (format " *nnslashdot %s*" server))))))
435 (defun nnslashdot-date-to-date (sdate)
436 (let ((elem (delete "" (split-string sdate))))
437 (concat (substring (nth 0 elem) 0 3) " "
438 (substring (nth 1 elem) 0 3) " "
439 (substring (nth 2 elem) 0 2) " "
440 (substring (nth 3 elem) 1 6) " "
441 (format-time-string "%Y") " "
444 (defun nnslashdot-generate-active ()
446 (set-buffer nntp-server-buffer)
448 (dolist (elem nnslashdot-groups)
449 (insert (prin1-to-string (car elem))
450 " " (number-to-string (cadr elem)) " 1 y\n"))))
452 (provide 'nnslashdot)
454 ;;; nnslashdot.el ends here