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)
92 (let ((case-fold-search t))
94 (when (= (car articles) 1)
95 (url-insert-file-contents (format nnslashdot-article-url sid))
96 (setq buffer-file-name nil)
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 (point)
107 "A href=http://slashdot.org/article.pl")))
111 (make-full-mail-header
112 1 group from date (concat "<" sid "%1@slashdot>")
115 (goto-char (point-max))
116 (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
117 (setq s (match-string 1))
118 (unless (memq s startats)
122 (setq startats (sort startats '<))
123 (while (and (setq start (pop startats))
125 (setq point (goto-char (point-max)))
126 (url-insert-file-contents
127 (format nnslashdot-comments-url sid nnslashdot-threshold start))
128 (setq buffer-file-name nil)
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)))))
139 (if (looking-at "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
140 (setq from (concat (match-string 1) " <" (match-string 2) ">"))
141 (looking-at "by \\(.+\\) on ")
142 (setq from (match-string 1)))
143 (goto-char (- (match-end 0) 5))
144 (search-forward " on ")
146 (nnslashdot-date-to-date
147 (buffer-substring (point) (progn (end-of-line) (point)))))
148 (setq lines (/ (abs (- (search-forward "<td ")
149 (search-forward "</td>")))
153 (if (looking-at ".*cid=\\([0-9]+\\)")
160 (make-full-mail-header
164 (number-to-string (1+ article))
168 (number-to-string (1+ (string-to-number parent)))
171 0 (string-to-number score) nil nil))
173 (setq nnslashdot-headers
174 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
176 (set-buffer nntp-server-buffer)
178 (dolist (header nnslashdot-headers)
179 (nnheader-insert-nov (cdr header))))
182 (deffoo nnslashdot-sane-retrieve-headers (articles &optional group
184 (nnslashdot-possibly-change-server group server)
185 (let ((last (car (last articles)))
187 (start (car articles))
188 (sid (caddr (assoc group nnslashdot-groups)))
189 headers article subject score from date lines parent point)
191 (set-buffer nnslashdot-buffer)
194 (url-insert-file-contents (format nnslashdot-article-url sid))
195 (setq buffer-file-name nil)
196 (goto-char (point-min))
197 (search-forward "Posted by ")
198 (when (looking-at "<a[^>]+>\\([^<]+\\)")
199 (setq from (match-string 1)))
200 (search-forward " on ")
201 (setq date (nnslashdot-date-to-date
202 (buffer-substring (point) (1- (search-forward "<")))))
204 (setq lines (count-lines (point)
206 "A href=http://slashdot.org/article.pl")))
210 (make-full-mail-header
211 1 group from date (concat "<" sid "%1@slashdot>")
214 (while (or (not article)
218 (setq start (1+ article)))
219 (setq point (goto-char (point-max)))
220 (url-insert-file-contents
221 (format nnslashdot-comments-url sid nnslashdot-threshold start))
222 (setq buffer-file-name nil)
224 (while (re-search-forward
225 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score\\([^)]+\\))"
227 (setq article (string-to-number (match-string 1))
228 subject (match-string 2)
229 score (match-string 3))
231 (if (looking-at "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
232 (setq from (concat (match-string 1) " <" (match-string 2) ">"))
233 (looking-at "by \\(.+\\) on ")
234 (setq from (match-string 1)))
235 (goto-char (- (match-end 0) 5))
236 (search-forward " on ")
238 (nnslashdot-date-to-date
239 (buffer-substring (point) (progn (end-of-line) (point)))))
240 (setq lines (/ (abs (- (search-forward "<td ")
241 (search-forward "</td>")))
245 (if (looking-at ".*cid=\\([0-9]+\\)")
252 (make-full-mail-header
253 (1+ article) (concat subject " (" score ")")
256 (number-to-string (1+ article))
260 (number-to-string (1+ (string-to-number parent)))
265 (setq nnslashdot-headers
266 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
268 (set-buffer nntp-server-buffer)
270 (dolist (header nnslashdot-headers)
271 (nnheader-insert-nov (cdr header))))
274 (deffoo nnslashdot-request-group (group &optional server dont-check)
275 (nnslashdot-possibly-change-server nil server)
276 (let ((elem (assoc group nnslashdot-groups)))
279 (nnheader-report 'nnslashdot "Group does not exist"))
281 (nnheader-report 'nnslashdot "Opened group %s" group)
283 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
284 (prin1-to-string group))))))
286 (deffoo nnslashdot-close-group (group &optional server)
287 (nnslashdot-possibly-change-server group server)
288 (when (gnus-buffer-live-p nnslashdot-buffer)
290 (set-buffer nnslashdot-buffer)
291 (kill-buffer nnslashdot-buffer)))
294 (deffoo nnslashdot-request-article (article &optional group server buffer)
295 (nnslashdot-possibly-change-server group server)
298 (set-buffer nnslashdot-buffer)
299 (goto-char (point-min))
300 (when (and (stringp article)
301 (string-match "%\\([0-9]+\\)@" article))
302 (setq article (string-to-number (match-string 1 article))))
303 (when (numberp article)
306 (re-search-forward "Posted by .* on ")
313 "^<p>.*A href=http://slashdot.org/article.pl")
314 (match-beginning 0)))))
315 (search-forward (format "<a name=\"%d\">" (1- article)))
318 (re-search-forward "<td[^>]+>")
319 (search-forward "</td>"))))))
322 (set-buffer (or buffer nntp-server-buffer))
325 ;;(nnweb-remove-markup)
326 ;;(nnweb-decode-entities)
327 (goto-char (point-min))
328 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
329 (let ((header (cdr (assq article nnslashdot-headers))))
330 (nnheader-insert-header header))
331 (nnheader-report 'nnslashdot "Fetched article %s" article)
332 (cons group article)))))
334 (deffoo nnslashdot-close-server (&optional server)
335 (when (and (nnslashdot-server-opened server)
336 (gnus-buffer-live-p nnslashdot-buffer))
338 (set-buffer nnslashdot-buffer)
339 (kill-buffer nnslashdot-buffer)))
340 (nnoo-close-server 'nnslashdot server))
342 (deffoo nnslashdot-request-list (&optional server)
343 (nnslashdot-possibly-change-server nil server)
345 sid elem description articles gname)
346 (while (> (- nnslashdot-group-number number) 0)
348 (let ((case-fold-search t))
349 (url-insert-file-contents (format nnslashdot-active-url number))
350 (setq buffer-file-name nil)
351 (goto-char (point-min))
352 (while (re-search-forward
353 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
354 (setq sid (match-string 1)
355 description (match-string 2))
357 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
358 (setq articles (string-to-number (match-string 1))))
359 (setq gname (concat description " (" sid ")"))
360 (if (setq elem (assoc gname nnslashdot-groups))
361 (setcar (cdr elem) articles)
362 (push (list gname articles sid) nnslashdot-groups)))))
364 (nnslashdot-write-groups)
365 (nnslashdot-generate-active)
368 (deffoo nnslashdot-request-newgroups (date &optional server)
369 (nnslashdot-possibly-change-server nil server)
370 (nnslashdot-generate-active)
373 (deffoo nnslashdot-asynchronous-p ()
376 (nnoo-define-skeleton nnslashdot)
378 ;;; Internal functions
380 (defun nnslashdot-possibly-change-server (&optional group server)
381 (nnslashdot-init server)
383 (not (nnslashdot-server-opened server)))
384 (nnslashdot-open-server server))
385 (unless nnslashdot-groups
386 (nnslashdot-read-groups)))
388 (defun nnslashdot-read-groups ()
389 (let ((file (expand-file-name "groups" nnslashdot-directory)))
390 (when (file-exists-p file)
392 (insert-file-contents file)
393 (goto-char (point-min))
394 (setq nnslashdot-groups (read (current-buffer)))))))
396 (defun nnslashdot-write-groups ()
397 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
398 (prin1 nnslashdot-groups (current-buffer))))
400 (defun nnslashdot-init (server)
401 "Initialize buffers and such."
402 (unless (file-exists-p nnslashdot-directory)
403 (gnus-make-directory nnslashdot-directory))
404 (unless (gnus-buffer-live-p nnslashdot-buffer)
405 (setq nnslashdot-buffer
407 (nnheader-set-temp-buffer
408 (format " *nnslashdot %s*" server))))))
410 (defun nnslashdot-encode-www-form-urlencoded (pairs)
411 "Return PAIRS encoded for forms."
415 (concat (w3-form-encode-xwfu (car data)) "="
416 (w3-form-encode-xwfu (cdr data)))))
419 (defun nnslashdot-fetch-form (url pairs)
420 (let ((url-request-data (nnslashdot-encode-www-form-urlencoded pairs))
421 (url-request-method "POST")
422 (url-request-extra-headers
423 '(("Content-type" . "application/x-www-form-urlencoded"))))
424 (url-insert-file-contents url)
425 (setq buffer-file-name nil))
428 (defun nnslashdot-decode-entities ()
429 (goto-char (point-min))
430 (while (re-search-forward "&\\([a-z]+\\);" nil t)
431 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
436 (defun nnslashdot-remove-markup ()
437 (goto-char (point-min))
438 (while (search-forward "<!--" nil t)
439 (delete-region (match-beginning 0)
440 (or (search-forward "-->" nil t)
442 (goto-char (point-min))
443 (while (re-search-forward "<[^>]+>" nil t)
444 (replace-match "" t t)))
446 (defun nnslashdot-date-to-date (sdate)
447 (let ((elem (split-string sdate)))
448 (concat (substring (nth 0 elem) 0 3) " "
449 (substring (nth 1 elem) 0 3) " "
450 (substring (nth 2 elem) 0 2) " "
451 (substring (nth 3 elem) 1 6) " "
452 (format-time-string "%Y") " "
455 (defun nnslashdot-generate-active ()
457 (set-buffer nntp-server-buffer)
459 (dolist (elem nnslashdot-groups)
460 (insert (prin1-to-string (car elem))
461 " " (number-to-string (cadr elem)) " 1 y\n"))))
463 (provide 'nnslashdot)
465 ;;; nnslashdot.el ends here