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 (setq startats (sort startats '<))
122 (while (and (setq start (pop startats))
124 (setq point (goto-char (point-max)))
125 (url-insert-file-contents
126 (format nnslashdot-comments-url sid nnslashdot-threshold start))
127 (setq buffer-file-name nil)
129 (while (re-search-forward
130 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
132 (setq article (string-to-number (match-string 1))
133 subject (match-string 2)
134 score (match-string 3))
135 (when (string-match "^Re: *" subject)
136 (setq subject (concat "Re: " (substring subject (match-end 0)))))
138 (if (looking-at "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
139 (setq from (concat (match-string 1) " <" (match-string 2) ">"))
140 (looking-at "by \\(.+\\) on ")
141 (setq from (match-string 1)))
142 (goto-char (- (match-end 0) 5))
143 (search-forward " on ")
145 (nnslashdot-date-to-date
146 (buffer-substring (point) (progn (end-of-line) (point)))))
147 (setq lines (/ (abs (- (search-forward "<td ")
148 (search-forward "</td>")))
152 (if (looking-at ".*cid=\\([0-9]+\\)")
159 (make-full-mail-header
163 (number-to-string (1+ article))
167 (number-to-string (1+ (string-to-number parent)))
170 0 (string-to-number score) nil nil))
172 (setq nnslashdot-headers
173 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
175 (set-buffer nntp-server-buffer)
177 (dolist (header nnslashdot-headers)
178 (nnheader-insert-nov (cdr header))))
181 (deffoo nnslashdot-sane-retrieve-headers (articles &optional group
183 (nnslashdot-possibly-change-server group server)
184 (let ((last (car (last articles)))
186 (start (car articles))
187 (sid (caddr (assoc group nnslashdot-groups)))
188 headers article subject score from date lines parent point)
190 (set-buffer nnslashdot-buffer)
193 (url-insert-file-contents (format nnslashdot-article-url sid))
194 (setq buffer-file-name nil)
195 (goto-char (point-min))
196 (search-forward "Posted by ")
197 (when (looking-at "<a[^>]+>\\([^<]+\\)")
198 (setq from (match-string 1)))
199 (search-forward " on ")
200 (setq date (nnslashdot-date-to-date
201 (buffer-substring (point) (1- (search-forward "<")))))
203 (setq lines (count-lines (point)
205 "A href=http://slashdot.org/article.pl")))
209 (make-full-mail-header
210 1 group from date (concat "<" sid "%1@slashdot>")
213 (while (or (not article)
217 (setq start (1+ article)))
218 (setq point (goto-char (point-max)))
219 (url-insert-file-contents
220 (format nnslashdot-comments-url sid nnslashdot-threshold start))
221 (setq buffer-file-name nil)
223 (while (re-search-forward
224 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score\\([^)]+\\))"
226 (setq article (string-to-number (match-string 1))
227 subject (match-string 2)
228 score (match-string 3))
230 (if (looking-at "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
231 (setq from (concat (match-string 1) " <" (match-string 2) ">"))
232 (looking-at "by \\(.+\\) on ")
233 (setq from (match-string 1)))
234 (goto-char (- (match-end 0) 5))
235 (search-forward " on ")
237 (nnslashdot-date-to-date
238 (buffer-substring (point) (progn (end-of-line) (point)))))
239 (setq lines (/ (abs (- (search-forward "<td ")
240 (search-forward "</td>")))
244 (if (looking-at ".*cid=\\([0-9]+\\)")
251 (make-full-mail-header
252 (1+ article) (concat subject " (" score ")")
255 (number-to-string (1+ article))
259 (number-to-string (1+ (string-to-number parent)))
264 (setq nnslashdot-headers
265 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
267 (set-buffer nntp-server-buffer)
269 (dolist (header nnslashdot-headers)
270 (nnheader-insert-nov (cdr header))))
273 (deffoo nnslashdot-request-group (group &optional server dont-check)
274 (nnslashdot-possibly-change-server nil server)
275 (let ((elem (assoc group nnslashdot-groups)))
278 (nnheader-report 'nnslashdot "Group does not exist"))
280 (nnheader-report 'nnslashdot "Opened group %s" group)
282 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
283 (prin1-to-string group))))))
285 (deffoo nnslashdot-close-group (group &optional server)
286 (nnslashdot-possibly-change-server group server)
287 (when (gnus-buffer-live-p nnslashdot-buffer)
289 (set-buffer nnslashdot-buffer)
290 (kill-buffer nnslashdot-buffer)))
293 (deffoo nnslashdot-request-article (article &optional group server buffer)
294 (nnslashdot-possibly-change-server group server)
297 (set-buffer nnslashdot-buffer)
298 (goto-char (point-min))
299 (when (numberp article)
302 (search-forward " on ")
308 "A href=http://slashdot.org/article.pl"))))
309 (search-forward (format "<a name=\"%d\">" (1- article)))
312 (re-search-forward "<td[^>]+>")
313 (search-forward "</td>"))))))
316 (set-buffer (or buffer nntp-server-buffer))
319 ;;(nnweb-remove-markup)
320 ;;(nnweb-decode-entities)
321 (goto-char (point-min))
322 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
323 (let ((header (cdr (assq article nnslashdot-headers))))
324 (nnheader-insert-header header))
325 (nnheader-report 'nnslashdot "Fetched article %s" article)
328 (deffoo nnslashdot-close-server (&optional server)
329 (when (and (nnslashdot-server-opened server)
330 (gnus-buffer-live-p nnslashdot-buffer))
332 (set-buffer nnslashdot-buffer)
333 (kill-buffer nnslashdot-buffer)))
334 (nnoo-close-server 'nnslashdot server))
336 (deffoo nnslashdot-request-list (&optional server)
337 (nnslashdot-possibly-change-server nil server)
338 (let ((case-fold-search t)
340 sid elem description articles gname)
341 (while (> (- nnslashdot-group-number number) 0)
343 (url-insert-file-contents (format nnslashdot-active-url number))
344 (setq buffer-file-name nil)
345 (goto-char (point-min))
346 (while (re-search-forward
347 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
348 (setq sid (match-string 1)
349 description (match-string 2))
351 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
352 (setq articles (string-to-number (match-string 1))))
353 (setq gname (concat description " (" sid ")"))
354 (if (setq elem (assoc gname nnslashdot-groups))
355 (setcar (cdr elem) articles)
356 (push (list gname articles sid) nnslashdot-groups))))
358 (nnslashdot-write-groups)
360 (set-buffer nntp-server-buffer)
362 (dolist (elem nnslashdot-groups)
363 (insert (prin1-to-string (car elem))
364 " " (number-to-string (cadr elem)) " 1 y\n")))
367 (deffoo nnslashdot-request-newgroups (date &optional server)
368 (nnslashdot-request-list server))
370 (deffoo nnslashdot-asynchronous-p ()
373 (nnoo-define-skeleton nnslashdot)
375 ;;; Internal functions
377 (defun nnslashdot-possibly-change-server (&optional group server)
378 (nnslashdot-init server)
380 (not (nnslashdot-server-opened server)))
381 (nnslashdot-open-server server))
382 (unless nnslashdot-groups
383 (nnslashdot-read-groups)))
385 (defun nnslashdot-read-groups ()
386 (let ((file (expand-file-name "groups" nnslashdot-directory)))
387 (when (file-exists-p file)
389 (insert-file-contents file)
390 (goto-char (point-min))
391 (setq nnslashdot-groups (read (current-buffer)))))))
393 (defun nnslashdot-write-groups ()
394 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
395 (prin1 nnslashdot-groups (current-buffer))))
397 (defun nnslashdot-init (server)
398 "Initialize buffers and such."
399 (unless (file-exists-p nnslashdot-directory)
400 (gnus-make-directory nnslashdot-directory))
401 (unless (gnus-buffer-live-p nnslashdot-buffer)
402 (setq nnslashdot-buffer
404 (nnheader-set-temp-buffer
405 (format " *nnslashdot %s*" server))))))
407 (defun nnslashdot-encode-www-form-urlencoded (pairs)
408 "Return PAIRS encoded for forms."
412 (concat (w3-form-encode-xwfu (car data)) "="
413 (w3-form-encode-xwfu (cdr data)))))
416 (defun nnslashdot-fetch-form (url pairs)
417 (let ((url-request-data (nnslashdot-encode-www-form-urlencoded pairs))
418 (url-request-method "POST")
419 (url-request-extra-headers
420 '(("Content-type" . "application/x-www-form-urlencoded"))))
421 (url-insert-file-contents url)
422 (setq buffer-file-name nil))
425 (defun nnslashdot-decode-entities ()
426 (goto-char (point-min))
427 (while (re-search-forward "&\\([a-z]+\\);" nil t)
428 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
433 (defun nnslashdot-remove-markup ()
434 (goto-char (point-min))
435 (while (search-forward "<!--" nil t)
436 (delete-region (match-beginning 0)
437 (or (search-forward "-->" nil t)
439 (goto-char (point-min))
440 (while (re-search-forward "<[^>]+>" nil t)
441 (replace-match "" t t)))
443 (defun nnslashdot-date-to-date (sdate)
444 (let ((elem (split-string sdate)))
445 (concat (substring (nth 0 elem) 0 3) " "
446 (substring (nth 1 elem) 0 3) " "
447 (substring (nth 2 elem) 0 2) " "
448 (substring (nth 3 elem) 1 6) " "
449 (format-time-string "%Y") " "
452 (provide 'nnslashdot)
454 ;;; nnslashdot.el ends here