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=4&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 0
67 "The article threshold.")
69 (defvoo nnslashdot-group-number 30
70 "The number of groups to keep updated.")
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-thread-retrieve-headers (articles &optional group server fetch-old)
83 (nnslashdot-possibly-change-server group server)
84 (let ((last (car (last articles)))
87 (sid (caddr (assoc group nnslashdot-groups)))
88 headers article subject score from date lines parent point
90 (unless gnus-nov-is-evil
92 (set-buffer nnslashdot-buffer)
93 (let ((case-fold-search t))
96 (nnweb-insert (format nnslashdot-article-url sid))
97 (goto-char (point-min))
98 (search-forward "Posted by ")
99 (when (looking-at "<a[^>]+>\\([^<]+\\)")
100 (setq from (match-string 1)))
101 (search-forward " on ")
102 (setq date (nnslashdot-date-to-date
103 (buffer-substring (point) (1- (search-forward "<")))))
105 (setq lines (count-lines
108 "A href=http://slashdot.org/article.pl")))
112 (make-full-mail-header
113 1 group from date (concat "<" sid "%1@slashdot>")
116 (goto-char (point-max))
117 (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
118 (setq s (match-string 1))
119 (unless (memq s startats)
123 (setq startats (sort startats '<))
124 (while (and (setq start (pop startats))
126 (setq point (goto-char (point-max)))
128 (format nnslashdot-comments-url sid nnslashdot-threshold start))
130 (while (re-search-forward
131 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
133 (setq article (string-to-number (match-string 1))
134 subject (match-string 2)
135 score (match-string 3))
136 (when (string-match "^Re: *" subject)
137 (setq subject (concat "Re: " (substring subject (match-end 0)))))
140 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
141 (setq from (concat (match-string 1)
142 " <" (match-string 2) ">"))
143 (looking-at "by \\(.+\\) on ")
144 (setq from (match-string 1)))
145 (goto-char (- (match-end 0) 5))
146 (search-forward " on ")
148 (nnslashdot-date-to-date
149 (buffer-substring (point) (progn (end-of-line) (point)))))
150 (setq lines (/ (abs (- (search-forward "<td ")
151 (search-forward "</td>")))
155 (if (looking-at ".*cid=\\([0-9]+\\)")
162 (make-full-mail-header
166 (number-to-string (1+ article))
170 (number-to-string (1+ (string-to-number parent)))
173 0 (string-to-number score) nil nil))
175 (setq nnslashdot-headers
176 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
178 (set-buffer nntp-server-buffer)
180 (dolist (header nnslashdot-headers)
181 (nnheader-insert-nov (cdr header))))
184 (deffoo nnslashdot-retrieve-headers (articles &optional group
186 (unless gnus-nov-is-evil
187 (nnslashdot-possibly-change-server group server)
188 (let ((last (car (last articles)))
190 (start (max (1- (car articles)) 1))
191 (sid (caddr (assoc group nnslashdot-groups)))
192 headers article subject score from date lines parent point)
194 (set-buffer nnslashdot-buffer)
197 (nnweb-insert (format nnslashdot-article-url sid))
198 (goto-char (point-min))
199 (search-forward "Posted by ")
200 (when (looking-at "<a[^>]+>\\([^<]+\\)")
201 (setq from (match-string 1)))
202 (search-forward " on ")
203 (setq date (nnslashdot-date-to-date
204 (buffer-substring (point) (1- (search-forward "<")))))
206 (setq lines (count-lines (point)
208 "A href=http://slashdot.org/article.pl")))
212 (make-full-mail-header
213 1 group from date (concat "<" sid "%1@slashdot>")
216 (while (or (not article)
220 (setq start (1+ article)))
221 (setq point (goto-char (point-max)))
223 (format nnslashdot-comments-url sid nnslashdot-threshold start))
225 (while (re-search-forward
226 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score\\([^)]+\\))"
228 (setq article (string-to-number (match-string 1))
229 subject (match-string 2)
230 score (match-string 3))
233 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
234 (setq from (concat (match-string 1) " <" (match-string 2) ">"))
235 (looking-at "by \\(.+\\) on ")
236 (setq from (match-string 1)))
237 (goto-char (- (match-end 0) 5))
238 (search-forward " on ")
240 (nnslashdot-date-to-date
241 (buffer-substring (point) (progn (end-of-line) (point)))))
242 (setq lines (/ (abs (- (search-forward "<td ")
243 (search-forward "</td>")))
247 (if (looking-at ".*cid=\\([0-9]+\\)")
254 (make-full-mail-header
255 (1+ article) (concat subject " (" score ")")
258 (number-to-string (1+ article))
262 (number-to-string (1+ (string-to-number parent)))
267 (setq nnslashdot-headers
268 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
270 (set-buffer nntp-server-buffer)
272 (dolist (header nnslashdot-headers)
273 (nnheader-insert-nov (cdr header))))
276 (deffoo nnslashdot-request-group (group &optional server dont-check)
277 (nnslashdot-possibly-change-server nil server)
278 (let ((elem (assoc group nnslashdot-groups)))
281 (nnheader-report 'nnslashdot "Group does not exist"))
283 (nnheader-report 'nnslashdot "Opened group %s" group)
285 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
286 (prin1-to-string group))))))
288 (deffoo nnslashdot-close-group (group &optional server)
289 (nnslashdot-possibly-change-server group server)
290 (when (gnus-buffer-live-p nnslashdot-buffer)
292 (set-buffer nnslashdot-buffer)
293 (kill-buffer nnslashdot-buffer)))
296 (deffoo nnslashdot-request-article (article &optional group server buffer)
297 (nnslashdot-possibly-change-server group server)
300 (set-buffer nnslashdot-buffer)
301 (let ((case-fold-search t))
302 (goto-char (point-min))
303 (when (and (stringp article)
304 (string-match "%\\([0-9]+\\)@" article))
305 (setq article (string-to-number (match-string 1 article))))
306 (when (numberp article)
309 (re-search-forward "Posted by .* on ")
316 "^<p>.*A href=http://slashdot.org/article.pl")
317 (match-beginning 0)))))
318 (search-forward (format "<a name=\"%d\">" (1- article)))
321 (re-search-forward "<td[^>]+>")
322 (search-forward "</td>")))))))
325 (set-buffer (or buffer nntp-server-buffer))
328 ;;(nnweb-remove-markup)
329 ;;(nnweb-decode-entities)
330 (goto-char (point-min))
331 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
332 (let ((header (cdr (assq article nnslashdot-headers))))
333 (nnheader-insert-header header))
334 (nnheader-report 'nnslashdot "Fetched article %s" article)
335 (cons group article)))))
337 (deffoo nnslashdot-close-server (&optional server)
338 (when (and (nnslashdot-server-opened server)
339 (gnus-buffer-live-p nnslashdot-buffer))
341 (set-buffer nnslashdot-buffer)
342 (kill-buffer nnslashdot-buffer)))
343 (nnoo-close-server 'nnslashdot server))
345 (deffoo nnslashdot-request-list (&optional server)
346 (nnslashdot-possibly-change-server nil server)
348 sid elem description articles gname)
349 (while (> (- nnslashdot-group-number number) 0)
351 (let ((case-fold-search t))
352 (nnweb-insert (format nnslashdot-active-url number))
353 (goto-char (point-min))
354 (while (re-search-forward
355 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
356 (setq sid (match-string 1)
357 description (match-string 2))
359 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
360 (setq articles (string-to-number (match-string 1))))
361 (setq gname (concat description " (" sid ")"))
362 (if (setq elem (assoc gname nnslashdot-groups))
363 (setcar (cdr elem) articles)
364 (push (list gname articles sid) nnslashdot-groups)))))
366 (nnslashdot-write-groups)
367 (nnslashdot-generate-active)
370 (deffoo nnslashdot-request-newgroups (date &optional server)
371 (nnslashdot-possibly-change-server nil server)
372 (nnslashdot-generate-active)
375 (nnoo-define-skeleton nnslashdot)
377 ;;; Internal functions
379 (defun nnslashdot-possibly-change-server (&optional group server)
380 (nnslashdot-init server)
382 (not (nnslashdot-server-opened server)))
383 (nnslashdot-open-server server))
384 (unless nnslashdot-groups
385 (nnslashdot-read-groups)))
387 (defun nnslashdot-read-groups ()
388 (let ((file (expand-file-name "groups" nnslashdot-directory)))
389 (when (file-exists-p file)
391 (insert-file-contents file)
392 (goto-char (point-min))
393 (setq nnslashdot-groups (read (current-buffer)))))))
395 (defun nnslashdot-write-groups ()
396 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
397 (prin1 nnslashdot-groups (current-buffer))))
399 (defun nnslashdot-init (server)
400 "Initialize buffers and such."
401 (unless (file-exists-p nnslashdot-directory)
402 (gnus-make-directory nnslashdot-directory))
403 (unless (gnus-buffer-live-p nnslashdot-buffer)
404 (setq nnslashdot-buffer
406 (nnheader-set-temp-buffer
407 (format " *nnslashdot %s*" server))))))
409 (defun nnslashdot-date-to-date (sdate)
410 (let ((elem (split-string sdate)))
411 (concat (substring (nth 0 elem) 0 3) " "
412 (substring (nth 1 elem) 0 3) " "
413 (substring (nth 2 elem) 0 2) " "
414 (substring (nth 3 elem) 1 6) " "
415 (format-time-string "%Y") " "
418 (defun nnslashdot-generate-active ()
420 (set-buffer nntp-server-buffer)
422 (dolist (elem nnslashdot-groups)
423 (insert (prin1-to-string (car elem))
424 " " (number-to-string (cadr elem)) " 1 y\n"))))
426 (provide 'nnslashdot)
428 ;;; nnslashdot.el ends here