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 (search-forward "Posted by ")
114 (when (looking-at "<a[^>]+>\\([^<]+\\)")
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 (search-forward "Posted by ")
221 (when (looking-at "<a[^>]+>\\([^<]+\\)")
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)
343 (re-search-forward "Posted by *<[^>]+>[^>]*<[^>]+> *on ")
344 (search-forward "<BR>")
350 "<p>.*A href=\"\\(http://slashdot.org\\)?/article")
351 (match-beginning 0)))))
352 (search-forward (format "<a name=\"%d\">" (1- article)))
355 (re-search-forward "<td[^>]+>")
356 (search-forward "</td>")))))))
357 (search-failed (nnslashdot-lose why)))
361 (set-buffer (or buffer nntp-server-buffer))
363 (mm-with-unibyte-current-buffer
365 (goto-char (point-min))
366 (while (re-search-forward "\\(<br>\r?\\)+" nil t)
367 (replace-match "<p>" t t))
368 (goto-char (point-min))
369 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
370 (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
372 (let ((header (cdr (assq article nnslashdot-headers))))
373 (nnheader-insert-header header))
374 (nnheader-report 'nnslashdot "Fetched article %s" article))
375 (cons group article)))))
377 (deffoo nnslashdot-close-server (&optional server)
378 (when (and (nnslashdot-server-opened server)
379 (gnus-buffer-live-p nnslashdot-buffer))
381 (set-buffer nnslashdot-buffer)
382 (kill-buffer nnslashdot-buffer)))
383 (nnoo-close-server 'nnslashdot server))
385 (deffoo nnslashdot-request-list (&optional server)
386 (nnslashdot-possibly-change-server nil server)
388 sid elem description articles gname)
390 ;; First we do the Ultramode to get info on all the latest groups.
392 (mm-with-unibyte-buffer
393 (nnweb-insert nnslashdot-backslash-url t)
394 (goto-char (point-min))
395 (while (search-forward "<story>" nil t)
396 (narrow-to-region (point) (search-forward "</story>"))
397 (goto-char (point-min))
398 (re-search-forward "<title>\\([^<]+\\)</title>")
400 (nnweb-decode-entities-string (match-string 1)))
401 (re-search-forward "<url>\\([^<]+\\)</url>")
402 (setq sid (match-string 1))
403 (string-match "sid=\\([0-9/]+\\)\\(.shtml\\|$\\)" sid)
404 (setq sid (match-string 1 sid))
405 (re-search-forward "<comments>\\([^<]+\\)</comments>")
406 (setq articles (string-to-number (match-string 1)))
407 (setq gname (concat description " (" sid ")"))
408 (if (setq elem (assoc gname nnslashdot-groups))
409 (setcar (cdr elem) articles)
410 (push (list gname articles sid) nnslashdot-groups))
411 (goto-char (point-max))
413 ;; Then do the older groups.
414 (while (> (- nnslashdot-group-number number) 0)
415 (mm-with-unibyte-buffer
416 (let ((case-fold-search t))
417 (nnweb-insert (format nnslashdot-active-url number) t)
418 (goto-char (point-min))
419 (while (re-search-forward
420 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>"
422 (setq sid (match-string 1)
424 (nnweb-decode-entities-string (match-string 2)))
426 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
427 (setq articles (string-to-number (match-string 1))))
428 (setq gname (concat description " (" sid ")"))
429 (if (setq elem (assoc gname nnslashdot-groups))
430 (setcar (cdr elem) articles)
431 (push (list gname articles sid) nnslashdot-groups)))))
433 (search-failed (nnslashdot-lose why)))
434 (nnslashdot-write-groups)
435 (nnslashdot-generate-active)
438 (deffoo nnslashdot-request-newgroups (date &optional server)
439 (nnslashdot-possibly-change-server nil server)
440 (nnslashdot-generate-active)
443 (deffoo nnslashdot-request-post (&optional server)
444 (nnslashdot-possibly-change-server nil server)
445 (let ((sid (nnslashdot-sid-strip (message-fetch-field "newsgroups")))
446 (subject (message-fetch-field "subject"))
447 (references (car (last (split-string
448 (message-fetch-field "references")))))
450 (string-match "%\\([0-9]+\\)@slashdot" references)
451 (setq pid (match-string 1 references))
453 (narrow-to-region (point) (progn (message-goto-signature) (point)))
454 (goto-char (point-min))
456 (if (looking-at "> ")
458 (delete-region (point) (+ (point) 2))
460 (insert "<blockquote>\n"))
463 (insert "</blockquote>\n")
466 (goto-char (point-min))
467 (while (re-search-forward "^ *\n" nil t)
468 (replace-match "<p>\n"))
470 (when (message-goto-signature)
478 (setq body (buffer-substring (point) (point-max)))
481 "http://slashdot.org/comments.pl"
484 ("rlogin" . "userlogin")
485 ("unickname" . ,nnslashdot-login-name)
486 ("upasswd" . ,nnslashdot-password)
487 ("postersubj" . ,subject)
489 ("postercomment" . ,body)
490 ("posttype" . "html")))))
492 (deffoo nnslashdot-request-delete-group (group &optional force server)
493 (nnslashdot-possibly-change-server group server)
494 (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
496 (nnslashdot-write-groups))
498 (deffoo nnslashdot-request-close ()
499 (setq nnslashdot-headers nil
500 nnslashdot-groups nil))
502 (nnoo-define-skeleton nnslashdot)
504 ;;; Internal functions
506 (defun nnslashdot-possibly-change-server (&optional group server)
507 (nnslashdot-init server)
509 (not (nnslashdot-server-opened server)))
510 (nnslashdot-open-server server))
511 (unless nnslashdot-groups
512 (nnslashdot-read-groups)))
514 (defun nnslashdot-read-groups ()
515 (let ((file (expand-file-name "groups" nnslashdot-directory)))
516 (when (file-exists-p file)
517 (mm-with-unibyte-buffer
518 (insert-file-contents file)
519 (goto-char (point-min))
520 (setq nnslashdot-groups (read (current-buffer)))))))
522 (defun nnslashdot-write-groups ()
523 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
524 (prin1 nnslashdot-groups (current-buffer))))
526 (defun nnslashdot-init (server)
527 "Initialize buffers and such."
528 (unless (file-exists-p nnslashdot-directory)
529 (gnus-make-directory nnslashdot-directory))
530 (unless (gnus-buffer-live-p nnslashdot-buffer)
531 (setq nnslashdot-buffer
533 (nnheader-set-temp-buffer
534 (format " *nnslashdot %s*" server))))))
536 (defun nnslashdot-date-to-date (sdate)
538 (let ((elem (delete "" (split-string sdate))))
539 (concat (substring (nth 0 elem) 0 3) " "
540 (substring (nth 1 elem) 0 3) " "
541 (substring (nth 2 elem) 0 2) " "
542 (substring (nth 3 elem) 1 6) " "
543 (format-time-string "%Y") " "
547 (defun nnslashdot-generate-active ()
549 (set-buffer nntp-server-buffer)
551 (dolist (elem nnslashdot-groups)
552 (insert (prin1-to-string (car elem))
553 " " (number-to-string (cadr elem)) " 1 y\n"))))
555 (defun nnslashdot-lose (why)
556 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
558 ;(defun nnslashdot-sid-strip (sid)
559 ; (if (string-match "^00/" sid)
560 ; (substring sid (match-end 0))
563 (defalias 'nnslashdot-sid-strip 'identity)
565 (provide 'nnslashdot)
567 ;;; nnslashdot.el ends here