1 ;;; nnslashdot.el --- interfacing with Slashdot
2 ;; Copyright (C) 1999, 2000, 2001 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))
42 ;; Report failure to find w3 at load time if appropriate.
43 (eval '(require 'nnweb))
45 (nnoo-declare nnslashdot)
47 (defvoo nnslashdot-directory (nnheader-concat gnus-directory "slashdot/")
48 "Where nnslashdot will save its files.")
50 (defvoo nnslashdot-active-url "http://slashdot.org/search.pl?section=&min=%d"
51 "Where nnslashdot will fetch the active file from.")
53 (defvoo nnslashdot-comments-url "http://slashdot.org/comments.pl?sid=%s&threshold=%d&commentsort=%d&mode=flat&startat=%d"
54 "Where nnslashdot will fetch comments from.")
56 (defvoo nnslashdot-article-url
57 "http://slashdot.org/article.pl?sid=%s&mode=nocomment"
58 "Where nnslashdot will fetch the article from.")
60 (defvoo nnslashdot-backslash-url "http://slashdot.org/slashdot.xml"
61 "Where nnslashdot will fetch the stories from.")
63 (defvoo nnslashdot-threshold -1
64 "The article threshold.")
66 (defvoo nnslashdot-threaded t
67 "Whether the nnslashdot groups should be threaded or not.")
69 (defvoo nnslashdot-group-number 0
70 "The number of non-fresh groups to keep updated.")
72 (defvoo nnslashdot-login-name ""
73 "The login name to use when posting.")
75 (defvoo nnslashdot-password ""
76 "The password to use when posting.")
78 ;;; Internal variables
80 (defvar nnslashdot-groups nil)
81 (defvar nnslashdot-buffer nil)
82 (defvar nnslashdot-headers nil)
84 ;;; Interface functions
86 (nnoo-define-basics nnslashdot)
88 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
89 (nnslashdot-possibly-change-server group server)
91 (unless gnus-nov-is-evil
92 (if nnslashdot-threaded
93 (nnslashdot-threaded-retrieve-headers articles group)
94 (nnslashdot-sane-retrieve-headers articles group)))
95 (search-failed (nnslashdot-lose why))))
97 (deffoo nnslashdot-threaded-retrieve-headers (articles group)
98 (let ((last (car (last articles)))
101 (sid (caddr (assoc group nnslashdot-groups)))
104 headers article subject score from date lines parent point s)
106 (set-buffer nnslashdot-buffer)
107 (let ((case-fold-search t))
110 (nnweb-insert (format nnslashdot-article-url
111 (nnslashdot-sid-strip sid)) t)
112 (goto-char (point-min))
113 (re-search-forward "Posted by[ \t\r\n]+")
114 (when (looking-at "<a[^>]+>[ \t\r\n]*\\([^<\r\n]+\\)")
115 (setq from (nnweb-decode-entities-string (match-string 1))))
116 (search-forward " on ")
117 (setq date (nnslashdot-date-to-date
118 (buffer-substring (point) (1- (search-forward "<")))))
119 (setq lines (/ (- (point)
120 (progn (forward-line 1) (point)))
125 (make-full-mail-header
127 (concat "<" (nnslashdot-sid-strip sid) "%1@slashdot>")
130 (while (and (setq start (pop startats))
132 (setq point (goto-char (point-max)))
134 (format nnslashdot-comments-url
135 (nnslashdot-sid-strip sid)
136 nnslashdot-threshold 0 start)
139 (setq first-comments nil)
140 (goto-char (point-max))
141 (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
142 (setq s (string-to-number (match-string 1)))
143 (unless (memq s startats)
145 (setq startats (sort startats '<)))
147 (while (re-search-forward
148 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
150 (setq article (string-to-number (match-string 1))
151 subject (match-string 3)
152 score (match-string 5))
153 (when (string-match "^Re: *" subject)
154 (setq subject (concat "Re: " (substring subject (match-end 0)))))
155 (setq subject (nnweb-decode-entities-string subject))
158 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
160 (goto-char (- (match-end 0) 5))
162 (nnweb-decode-entities-string (match-string 1))
163 " <" (match-string 2) ">")))
165 (when (looking-at "by \\(.+\\) on ")
166 (goto-char (- (match-end 0) 5))
167 (setq from (nnweb-decode-entities-string (match-string 1)))))
168 (search-forward " on ")
170 (nnslashdot-date-to-date
171 (buffer-substring (point) (progn (end-of-line) (point)))))
172 (setq lines (/ (abs (- (search-forward "<td ")
173 (search-forward "</td>")))
177 (if (looking-at ".*cid=\\([0-9]+\\)")
184 (make-full-mail-header
186 (concat subject " (" score ")")
188 (concat "<" (nnslashdot-sid-strip sid) "%"
189 (number-to-string (1+ article))
192 (concat "<" (nnslashdot-sid-strip sid) "%"
193 (number-to-string (1+ (string-to-number parent)))
198 (setq nnslashdot-headers (sort headers 'car-less-than-car))
200 (set-buffer nntp-server-buffer)
202 (mm-with-unibyte-current-buffer
203 (dolist (header nnslashdot-headers)
204 (nnheader-insert-nov (cdr header)))))
207 (deffoo nnslashdot-sane-retrieve-headers (articles group)
208 (let ((last (car (last articles)))
210 (start (max (1- (car articles)) 1))
211 (sid (caddr (assoc group nnslashdot-groups)))
212 headers article subject score from date lines parent point)
214 (set-buffer nnslashdot-buffer)
217 (nnweb-insert (format nnslashdot-article-url
218 (nnslashdot-sid-strip sid)) t)
219 (goto-char (point-min))
220 (re-search-forward "Posted by[ \t\r\n]+")
221 (when (looking-at "<a[^>]+>[ \t\n\r]*\\([^<\r\n]+\\)")
222 (setq from (nnweb-decode-entities-string (match-string 1))))
223 (search-forward " on ")
224 (setq date (nnslashdot-date-to-date
225 (buffer-substring (point) (1- (search-forward "<")))))
227 (setq lines (count-lines (point)
229 "< <A href=\"\\(http://slashdot.org\\)?/article")))
233 (make-full-mail-header
234 1 group from date (concat "<" (nnslashdot-sid-strip sid)
238 (while (or (not article)
242 (setq start (1+ article)))
243 (setq point (goto-char (point-max)))
245 (format nnslashdot-comments-url (nnslashdot-sid-strip sid)
246 nnslashdot-threshold 4 start)
249 (while (re-search-forward
250 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
252 (setq article (string-to-number (match-string 1))
253 subject (match-string 3)
254 score (match-string 5))
255 (when (string-match "^Re: *" subject)
256 (setq subject (concat "Re: " (substring subject (match-end 0)))))
257 (setq subject (nnweb-decode-entities-string subject))
260 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
262 (goto-char (- (match-end 0) 5))
264 (nnweb-decode-entities-string (match-string 1))
265 " <" (match-string 2) ">")))
267 (when (looking-at "by \\(.+\\) on ")
268 (goto-char (- (match-end 0) 5))
269 (setq from (nnweb-decode-entities-string (match-string 1)))))
270 (search-forward " on ")
272 (nnslashdot-date-to-date
273 (buffer-substring (point) (progn (end-of-line) (point)))))
274 (setq lines (/ (abs (- (search-forward "<td ")
275 (search-forward "</td>")))
279 (if (looking-at ".*cid=\\([0-9]+\\)")
286 (make-full-mail-header
287 (1+ article) (concat subject " (" score ")")
289 (concat "<" (nnslashdot-sid-strip sid) "%"
290 (number-to-string (1+ article))
293 (concat "<" (nnslashdot-sid-strip sid) "%"
294 (number-to-string (1+ (string-to-number parent)))
299 (setq nnslashdot-headers
300 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
302 (set-buffer nntp-server-buffer)
304 (mm-with-unibyte-current-buffer
305 (dolist (header nnslashdot-headers)
306 (nnheader-insert-nov (cdr header)))))
309 (deffoo nnslashdot-request-group (group &optional server dont-check)
310 (nnslashdot-possibly-change-server nil server)
311 (let ((elem (assoc group nnslashdot-groups)))
314 (nnheader-report 'nnslashdot "Group does not exist"))
316 (nnheader-report 'nnslashdot "Opened group %s" group)
318 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
319 (prin1-to-string group))))))
321 (deffoo nnslashdot-close-group (group &optional server)
322 (nnslashdot-possibly-change-server group server)
323 (when (gnus-buffer-live-p nnslashdot-buffer)
325 (set-buffer nnslashdot-buffer)
326 (kill-buffer nnslashdot-buffer)))
329 (deffoo nnslashdot-request-article (article &optional group server buffer)
330 (nnslashdot-possibly-change-server group server)
334 (set-buffer nnslashdot-buffer)
335 (let ((case-fold-search t))
336 (goto-char (point-min))
337 (when (and (stringp article)
338 (string-match "%\\([0-9]+\\)@" article))
339 (setq article (string-to-number (match-string 1 article))))
340 (when (numberp article)
344 "Posted by[ \t\r\n]+<[^>]+>[ \t\r\n]*[^>]*[ \t\r\n]*<[^>]+> *[ \t\r\n]*on ")
345 (search-forward "<BR>")
351 "< [ \t\r\n]*<A HREF=\"\\(\\(http:\\)?//slashdot\\.org\\)?/article")
352 (match-beginning 0)))))
353 (search-forward (format "<a name=\"%d\">" (1- article)))
356 (re-search-forward "<td[^>]+>")
357 (search-forward "</td>")))))))
358 (search-failed (nnslashdot-lose why)))
362 (set-buffer (or buffer nntp-server-buffer))
364 (mm-with-unibyte-current-buffer
366 (goto-char (point-min))
367 (while (re-search-forward "\\(<br>\r?\\)+" nil t)
368 (replace-match "<p>" t t))
369 (goto-char (point-min))
370 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
371 (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
373 (let ((header (cdr (assq article nnslashdot-headers))))
374 (nnheader-insert-header header))
375 (nnheader-report 'nnslashdot "Fetched article %s" article))
376 (cons group article)))))
378 (deffoo nnslashdot-close-server (&optional server)
379 (when (and (nnslashdot-server-opened server)
380 (gnus-buffer-live-p nnslashdot-buffer))
382 (set-buffer nnslashdot-buffer)
383 (kill-buffer nnslashdot-buffer)))
384 (nnoo-close-server 'nnslashdot server))
386 (deffoo nnslashdot-request-list (&optional server)
387 (nnslashdot-possibly-change-server nil server)
389 sid elem description articles gname)
391 ;; First we do the Ultramode to get info on all the latest groups.
393 (mm-with-unibyte-buffer
394 (nnweb-insert nnslashdot-backslash-url t)
395 (goto-char (point-min))
396 (while (search-forward "<story>" nil t)
397 (narrow-to-region (point) (search-forward "</story>"))
398 (goto-char (point-min))
399 (re-search-forward "<title>\\([^<]+\\)</title>")
401 (nnweb-decode-entities-string (match-string 1)))
402 (re-search-forward "<url>\\([^<]+\\)</url>")
403 (setq sid (match-string 1))
404 (string-match "sid=\\([0-9/]+\\)\\(.shtml\\|$\\)" sid)
405 (setq sid (match-string 1 sid))
406 (re-search-forward "<comments>\\([^<]+\\)</comments>")
407 (setq articles (string-to-number (match-string 1)))
408 (setq gname (concat description " (" sid ")"))
409 (if (setq elem (assoc gname nnslashdot-groups))
410 (setcar (cdr elem) articles)
411 (push (list gname articles sid (current-time))
413 (goto-char (point-max))
415 ;; Then do the older groups.
416 (while (> (- nnslashdot-group-number number) 0)
417 (mm-with-unibyte-buffer
418 (let ((case-fold-search t))
419 (nnweb-insert (format nnslashdot-active-url number) t)
420 (goto-char (point-min))
421 (while (re-search-forward
422 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>"
424 (setq sid (match-string 1)
426 (nnweb-decode-entities-string (match-string 2)))
428 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
429 (setq articles (string-to-number (match-string 1))))
430 (setq gname (concat description " (" sid ")"))
431 (if (setq elem (assoc gname nnslashdot-groups))
432 (setcar (cdr elem) articles)
433 (push (list gname articles sid (current-time))
434 nnslashdot-groups)))))
436 (search-failed (nnslashdot-lose why)))
437 (nnslashdot-write-groups)
438 (nnslashdot-generate-active)
441 (deffoo nnslashdot-request-newgroups (date &optional server)
442 (nnslashdot-possibly-change-server nil server)
443 (nnslashdot-generate-active)
446 (deffoo nnslashdot-request-post (&optional server)
447 (nnslashdot-possibly-change-server nil server)
448 (let ((sid (nnslashdot-sid-strip (message-fetch-field "newsgroups")))
449 (subject (message-fetch-field "subject"))
450 (references (car (last (split-string
451 (message-fetch-field "references")))))
453 (string-match "%\\([0-9]+\\)@slashdot" references)
454 (setq pid (match-string 1 references))
456 (narrow-to-region (point) (progn (message-goto-signature) (point)))
457 (goto-char (point-min))
459 (if (looking-at "> ")
461 (delete-region (point) (+ (point) 2))
463 (insert "<blockquote>\n"))
466 (insert "</blockquote>\n")
469 (goto-char (point-min))
470 (while (re-search-forward "^ *\n" nil t)
471 (replace-match "<p>\n"))
473 (when (message-goto-signature)
481 (setq body (buffer-substring (point) (point-max)))
484 "http://slashdot.org/comments.pl"
487 ("rlogin" . "userlogin")
488 ("unickname" . ,nnslashdot-login-name)
489 ("upasswd" . ,nnslashdot-password)
490 ("postersubj" . ,subject)
492 ("postercomment" . ,body)
493 ("posttype" . "html")))))
495 (deffoo nnslashdot-request-delete-group (group &optional force server)
496 (nnslashdot-possibly-change-server group server)
497 (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
499 (nnslashdot-write-groups))
501 (deffoo nnslashdot-request-close ()
502 (setq nnslashdot-headers nil
503 nnslashdot-groups nil))
505 (deffoo nnslashdot-request-expire-articles
506 (articles group &optional server force)
507 (nnslashdot-possibly-change-server group server)
508 (let ((item (assoc group nnslashdot-groups)))
511 (when (and (>= (length articles) (cadr item)) ;; All are expirable.
512 (nnmail-expired-article-p
516 (setq nnslashdot-groups (delq item nnslashdot-groups))
517 (nnslashdot-write-groups)
518 (setq articles nil)) ;; all expired.
519 (setcdr (cddr item) (list (current-time)))
520 (nnslashdot-write-groups))))
523 (nnoo-define-skeleton nnslashdot)
525 ;;; Internal functions
527 (defun nnslashdot-possibly-change-server (&optional group server)
528 (nnslashdot-init server)
530 (not (nnslashdot-server-opened server)))
531 (nnslashdot-open-server server))
532 (unless nnslashdot-groups
533 (nnslashdot-read-groups)))
535 (defun nnslashdot-read-groups ()
536 (let ((file (expand-file-name "groups" nnslashdot-directory)))
537 (when (file-exists-p file)
538 (mm-with-unibyte-buffer
539 (insert-file-contents file)
540 (goto-char (point-min))
541 (setq nnslashdot-groups (read (current-buffer)))))))
543 (defun nnslashdot-write-groups ()
544 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
545 (gnus-prin1 nnslashdot-groups)))
547 (defun nnslashdot-init (server)
548 "Initialize buffers and such."
549 (unless (file-exists-p nnslashdot-directory)
550 (gnus-make-directory nnslashdot-directory))
551 (unless (gnus-buffer-live-p nnslashdot-buffer)
552 (setq nnslashdot-buffer
554 (nnheader-set-temp-buffer
555 (format " *nnslashdot %s*" server))))
556 (push nnslashdot-buffer gnus-buffers)))
558 (defun nnslashdot-date-to-date (sdate)
560 (let ((elem (delete "" (split-string sdate))))
561 (concat (substring (nth 0 elem) 0 3) " "
562 (substring (nth 1 elem) 0 3) " "
563 (substring (nth 2 elem) 0 2) " "
564 (substring (nth 3 elem) 1 6) " "
565 (format-time-string "%Y") " "
569 (defun nnslashdot-generate-active ()
571 (set-buffer nntp-server-buffer)
573 (dolist (elem nnslashdot-groups)
574 (insert (prin1-to-string (car elem))
575 " " (number-to-string (cadr elem)) " 1 y\n"))))
577 (defun nnslashdot-lose (why)
578 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
580 (defalias 'nnslashdot-sid-strip 'identity)
582 (provide 'nnslashdot)
584 ;;; nnslashdot.el ends here