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=0&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-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
91 (set-buffer nnslashdot-buffer)
94 (url-insert-file-contents (format nnslashdot-article-url sid))
95 (setq buffer-file-name nil)
96 (goto-char (point-min))
97 (search-forward "Posted by ")
98 (when (looking-at "<a[^>]+>\\([^<]+\\)")
99 (setq from (match-string 1)))
100 (search-forward " on ")
101 (setq date (nnslashdot-date-to-date
102 (buffer-substring (point) (1- (search-forward "<")))))
104 (setq lines (count-lines (point)
106 "A href=http://slashdot.org/article.pl")))
110 (make-full-mail-header
111 1 group from date (concat "<" sid "%1@slashdot>")
114 (goto-char (point-max))
115 (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
116 (setq s (match-string 1))
117 (unless (memq s startats)
121 (dolist (start (sort startats '<))
122 (setq point (goto-char (point-max)))
123 (url-insert-file-contents
124 (format nnslashdot-comments-url sid nnslashdot-threshold start))
125 (setq buffer-file-name nil)
127 (while (re-search-forward
128 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
130 (setq article (string-to-number (match-string 1))
131 subject (match-string 2)
132 score (match-string 3))
133 (when (string-match "^Re: *" subject)
134 (setq subject (concat "Re: " (substring subject (match-end 0)))))
136 (if (looking-at "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
137 (setq from (concat (match-string 1) " <" (match-string 2) ">"))
138 (looking-at "by \\(.+\\) on ")
139 (setq from (match-string 1)))
140 (goto-char (- (match-end 0) 5))
141 (search-forward " on ")
143 (nnslashdot-date-to-date
144 (buffer-substring (point) (progn (end-of-line) (point)))))
145 (setq lines (/ (abs (- (search-forward "<td ")
146 (search-forward "</td>")))
150 (if (looking-at ".*cid=\\([0-9]+\\)")
157 (make-full-mail-header
161 (number-to-string (1+ article))
165 (number-to-string (1+ (string-to-number parent)))
168 0 (string-to-number score) nil nil))
170 (setq nnslashdot-headers
171 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
173 (set-buffer nntp-server-buffer)
175 (dolist (header nnslashdot-headers)
176 (nnheader-insert-nov (cdr header))))
179 (deffoo nnslashdot-sane-retrieve-headers (articles &optional group
181 (nnslashdot-possibly-change-server group server)
182 (let ((last (car (last articles)))
184 (start (car articles))
185 (sid (caddr (assoc group nnslashdot-groups)))
186 headers article subject score from date lines parent point)
188 (set-buffer nnslashdot-buffer)
191 (url-insert-file-contents (format nnslashdot-article-url sid))
192 (setq buffer-file-name nil)
193 (goto-char (point-min))
194 (search-forward "Posted by ")
195 (when (looking-at "<a[^>]+>\\([^<]+\\)")
196 (setq from (match-string 1)))
197 (search-forward " on ")
198 (setq date (nnslashdot-date-to-date
199 (buffer-substring (point) (1- (search-forward "<")))))
201 (setq lines (count-lines (point)
203 "A href=http://slashdot.org/article.pl")))
207 (make-full-mail-header
208 1 group from date (concat "<" sid "%1@slashdot>")
211 (while (or (not article)
215 (setq start (1+ article)))
216 (setq point (goto-char (point-max)))
217 (url-insert-file-contents
218 (format nnslashdot-comments-url sid nnslashdot-threshold start))
219 (setq buffer-file-name nil)
221 (while (re-search-forward
222 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score\\([^)]+\\))"
224 (setq article (string-to-number (match-string 1))
225 subject (match-string 2)
226 score (match-string 3))
228 (if (looking-at "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
229 (setq from (concat (match-string 1) " <" (match-string 2) ">"))
230 (looking-at "by \\(.+\\) on ")
231 (setq from (match-string 1)))
232 (goto-char (- (match-end 0) 5))
233 (search-forward " on ")
235 (nnslashdot-date-to-date
236 (buffer-substring (point) (progn (end-of-line) (point)))))
237 (setq lines (/ (abs (- (search-forward "<td ")
238 (search-forward "</td>")))
242 (if (looking-at ".*cid=\\([0-9]+\\)")
249 (make-full-mail-header
250 (1+ article) (concat subject " (" score ")")
253 (number-to-string (1+ article))
257 (number-to-string (1+ (string-to-number parent)))
262 (setq nnslashdot-headers
263 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
265 (set-buffer nntp-server-buffer)
267 (dolist (header nnslashdot-headers)
268 (nnheader-insert-nov (cdr header))))
271 (deffoo nnslashdot-request-group (group &optional server dont-check)
272 (nnslashdot-possibly-change-server nil server)
273 (let ((elem (assoc group nnslashdot-groups)))
276 (nnheader-report 'nnslashdot "Group does not exist"))
278 (nnheader-report 'nnslashdot "Opened group %s" group)
280 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
281 (prin1-to-string group))))))
283 (deffoo nnslashdot-close-group (group &optional server)
284 (nnslashdot-possibly-change-server group server)
285 (when (gnus-buffer-live-p nnslashdot-buffer)
287 (set-buffer nnslashdot-buffer)
288 (kill-buffer nnslashdot-buffer)))
291 (deffoo nnslashdot-request-article (article &optional group server buffer)
292 (nnslashdot-possibly-change-server group server)
295 (set-buffer nnslashdot-buffer)
296 (goto-char (point-min))
297 (when (numberp article)
300 (search-forward " on ")
306 "A href=http://slashdot.org/article.pl"))))
307 (search-forward (format "<a name=\"%d\">" (1- article)))
310 (re-search-forward "<td[^>]+>")
311 (search-forward "</td>"))))))
314 (set-buffer (or buffer nntp-server-buffer))
317 ;;(nnweb-remove-markup)
318 ;;(nnweb-decode-entities)
319 (goto-char (point-min))
320 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
321 (let ((header (cdr (assq article nnslashdot-headers))))
322 (nnheader-insert-header header))
323 (nnheader-report 'nnslashdot "Fetched article %s" article)
326 (deffoo nnslashdot-close-server (&optional server)
327 (when (and (nnslashdot-server-opened server)
328 (gnus-buffer-live-p nnslashdot-buffer))
330 (set-buffer nnslashdot-buffer)
331 (kill-buffer nnslashdot-buffer)))
332 (nnoo-close-server 'nnslashdot server))
334 (deffoo nnslashdot-request-list (&optional server)
335 (nnslashdot-possibly-change-server nil server)
336 (let ((case-fold-search t)
338 sid elem description articles gname)
339 (while (> (- nnslashdot-group-number number) 0)
341 (url-insert-file-contents (format nnslashdot-active-url number))
342 (setq buffer-file-name nil)
343 (goto-char (point-min))
344 (while (re-search-forward
345 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
346 (setq sid (match-string 1)
347 description (match-string 2))
349 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
350 (setq articles (string-to-number (match-string 1))))
351 (setq gname (concat description " (" sid ")"))
352 (if (setq elem (assoc gname nnslashdot-groups))
353 (setcar (cdr elem) articles)
354 (push (list gname articles sid) nnslashdot-groups))))
356 (nnslashdot-write-groups)
358 (set-buffer nntp-server-buffer)
360 (dolist (elem nnslashdot-groups)
361 (insert (prin1-to-string (car elem))
362 " " (number-to-string (cadr elem)) " 1 m\n")))
365 (deffoo nnslashdot-asynchronous-p ()
368 (nnoo-define-skeleton nnslashdot)
370 ;;; Internal functions
372 (defun nnslashdot-possibly-change-server (&optional group server)
373 (nnslashdot-init server)
375 (not (nnslashdot-server-opened server)))
376 (nnslashdot-open-server server))
377 (unless nnslashdot-groups
378 (nnslashdot-read-groups)))
380 (defun nnslashdot-read-groups ()
381 (let ((file (expand-file-name "groups" nnslashdot-directory)))
382 (when (file-exists-p file)
384 (insert-file-contents file)
385 (goto-char (point-min))
386 (setq nnslashdot-groups (read (current-buffer)))))))
388 (defun nnslashdot-write-groups ()
389 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
390 (prin1 nnslashdot-groups (current-buffer))))
392 (defun nnslashdot-init (server)
393 "Initialize buffers and such."
394 (unless (file-exists-p nnslashdot-directory)
395 (gnus-make-directory nnslashdot-directory))
396 (unless (gnus-buffer-live-p nnslashdot-buffer)
397 (setq nnslashdot-buffer
399 (nnheader-set-temp-buffer
400 (format " *nnslashdot %s*" server))))))
402 (defun nnslashdot-encode-www-form-urlencoded (pairs)
403 "Return PAIRS encoded for forms."
407 (concat (w3-form-encode-xwfu (car data)) "="
408 (w3-form-encode-xwfu (cdr data)))))
411 (defun nnslashdot-fetch-form (url pairs)
412 (let ((url-request-data (nnslashdot-encode-www-form-urlencoded pairs))
413 (url-request-method "POST")
414 (url-request-extra-headers
415 '(("Content-type" . "application/x-www-form-urlencoded"))))
416 (url-insert-file-contents url)
417 (setq buffer-file-name nil))
420 (defun nnslashdot-decode-entities ()
421 (goto-char (point-min))
422 (while (re-search-forward "&\\([a-z]+\\);" nil t)
423 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
428 (defun nnslashdot-remove-markup ()
429 (goto-char (point-min))
430 (while (search-forward "<!--" nil t)
431 (delete-region (match-beginning 0)
432 (or (search-forward "-->" nil t)
434 (goto-char (point-min))
435 (while (re-search-forward "<[^>]+>" nil t)
436 (replace-match "" t t)))
438 (defun nnslashdot-date-to-date (sdate)
439 (let ((elem (split-string sdate)))
440 (concat (substring (nth 0 elem) 0 3) " "
441 (substring (nth 1 elem) 0 3) " "
442 (substring (nth 2 elem) 0 2) " "
443 (substring (nth 3 elem) 1 6) " "
444 (format-time-string "%Y") " "
447 (provide 'nnslashdot)
449 ;;; nnslashdot.el ends here