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))
32 (eval-when-compile (require 'gnus-clfns))
43 ;; Report failure to find w3 at load time if appropriate.
44 (eval '(require 'nnweb))
46 (nnoo-declare nnslashdot)
48 (defvoo nnslashdot-directory (nnheader-concat gnus-directory "slashdot/")
49 "Where nnslashdot will save its files.")
51 (defvoo nnslashdot-active-url "http://slashdot.org/search.pl?section=&min=%d"
52 "Where nnslashdot will fetch the active file from.")
54 (defvoo nnslashdot-comments-url "http://slashdot.org/comments.pl?sid=%s&threshold=%d&commentsort=%d&mode=flat&startat=%d"
55 "Where nnslashdot will fetch comments from.")
57 (defvoo nnslashdot-article-url
58 "http://slashdot.org/article.pl?sid=%s&mode=nocomment"
59 "Where nnslashdot will fetch the article from.")
61 (defvoo nnslashdot-backslash-url "http://slashdot.org/slashdot.xml"
62 "Where nnslashdot will fetch the stories from.")
64 (defvoo nnslashdot-threshold -1
65 "The article threshold.")
67 (defvoo nnslashdot-threaded t
68 "Whether the nnslashdot groups should be threaded or not.")
70 (defvoo nnslashdot-group-number 0
71 "The number of non-fresh groups to keep updated.")
73 (defvoo nnslashdot-login-name ""
74 "The login name to use when posting.")
76 (defvoo nnslashdot-password ""
77 "The password to use when posting.")
79 ;;; Internal variables
81 (defvar nnslashdot-groups nil)
82 (defvar nnslashdot-buffer nil)
83 (defvar nnslashdot-headers nil)
85 ;;; Interface functions
87 (nnoo-define-basics nnslashdot)
89 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
90 (nnslashdot-possibly-change-server group server)
92 (unless gnus-nov-is-evil
93 (if nnslashdot-threaded
94 (nnslashdot-threaded-retrieve-headers articles group)
95 (nnslashdot-sane-retrieve-headers articles group)))
96 (search-failed (nnslashdot-lose why))))
98 (deffoo nnslashdot-threaded-retrieve-headers (articles group)
99 (let ((last (car (last articles)))
102 (sid (caddr (assoc group nnslashdot-groups)))
105 headers article subject score from date lines parent point s)
107 (set-buffer nnslashdot-buffer)
108 (let ((case-fold-search t))
111 (nnweb-insert (format nnslashdot-article-url
112 (nnslashdot-sid-strip sid)) t)
113 (goto-char (point-min))
114 (search-forward "Posted by ")
115 (when (looking-at "<a[^>]+>\\([^<]+\\)")
116 (setq from (nnweb-decode-entities-string (match-string 1))))
117 (search-forward " on ")
118 (setq date (nnslashdot-date-to-date
119 (buffer-substring (point) (1- (search-forward "<")))))
120 (setq lines (/ (- (point)
121 (progn (forward-line 1) (point)))
126 (make-full-mail-header
128 (concat "<" (nnslashdot-sid-strip sid) "%1@slashdot>")
131 (while (and (setq start (pop startats))
133 (setq point (goto-char (point-max)))
135 (format nnslashdot-comments-url
136 (nnslashdot-sid-strip sid)
137 nnslashdot-threshold 0 start)
140 (setq first-comments nil)
141 (goto-char (point-max))
142 (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
143 (setq s (string-to-number (match-string 1)))
144 (unless (memq s startats)
146 (setq startats (sort startats '<)))
148 (while (re-search-forward
149 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
151 (setq article (string-to-number (match-string 1))
152 subject (match-string 3)
153 score (match-string 5))
154 (when (string-match "^Re: *" subject)
155 (setq subject (concat "Re: " (substring subject (match-end 0)))))
156 (setq subject (nnweb-decode-entities-string subject))
159 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
161 (goto-char (- (match-end 0) 5))
163 (nnweb-decode-entities-string (match-string 1))
164 " <" (match-string 2) ">")))
166 (when (looking-at "by \\(.+\\) on ")
167 (goto-char (- (match-end 0) 5))
168 (setq from (nnweb-decode-entities-string (match-string 1)))))
169 (search-forward " on ")
171 (nnslashdot-date-to-date
172 (buffer-substring (point) (progn (end-of-line) (point)))))
173 (setq lines (/ (abs (- (search-forward "<td ")
174 (search-forward "</td>")))
178 (if (looking-at ".*cid=\\([0-9]+\\)")
185 (make-full-mail-header
187 (concat subject " (" score ")")
189 (concat "<" (nnslashdot-sid-strip sid) "%"
190 (number-to-string (1+ article))
193 (concat "<" (nnslashdot-sid-strip sid) "%"
194 (number-to-string (1+ (string-to-number parent)))
199 (setq nnslashdot-headers (sort headers 'car-less-than-car))
201 (set-buffer nntp-server-buffer)
203 (mm-with-unibyte-current-buffer
204 (dolist (header nnslashdot-headers)
205 (nnheader-insert-nov (cdr header)))))
208 (deffoo nnslashdot-sane-retrieve-headers (articles group)
209 (let ((last (car (last articles)))
211 (start (max (1- (car articles)) 1))
212 (sid (caddr (assoc group nnslashdot-groups)))
213 headers article subject score from date lines parent point)
215 (set-buffer nnslashdot-buffer)
218 (nnweb-insert (format nnslashdot-article-url
219 (nnslashdot-sid-strip sid)) t)
220 (goto-char (point-min))
221 (search-forward "Posted by ")
222 (when (looking-at "<a[^>]+>\\([^<]+\\)")
223 (setq from (nnweb-decode-entities-string (match-string 1))))
224 (search-forward " on ")
225 (setq date (nnslashdot-date-to-date
226 (buffer-substring (point) (1- (search-forward "<")))))
228 (setq lines (count-lines (point)
230 "A href=\"\\(http://slashdot.org\\)?/article")))
234 (make-full-mail-header
235 1 group from date (concat "<" (nnslashdot-sid-strip sid)
239 (while (or (not article)
243 (setq start (1+ article)))
244 (setq point (goto-char (point-max)))
246 (format nnslashdot-comments-url (nnslashdot-sid-strip sid)
247 nnslashdot-threshold 4 start)
250 (while (re-search-forward
251 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
253 (setq article (string-to-number (match-string 1))
254 subject (match-string 3)
255 score (match-string 5))
256 (when (string-match "^Re: *" subject)
257 (setq subject (concat "Re: " (substring subject (match-end 0)))))
258 (setq subject (nnweb-decode-entities-string subject))
261 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
263 (goto-char (- (match-end 0) 5))
265 (nnweb-decode-entities-string (match-string 1))
266 " <" (match-string 2) ">")))
268 (when (looking-at "by \\(.+\\) on ")
269 (goto-char (- (match-end 0) 5))
270 (setq from (nnweb-decode-entities-string (match-string 1)))))
271 (search-forward " on ")
273 (nnslashdot-date-to-date
274 (buffer-substring (point) (progn (end-of-line) (point)))))
275 (setq lines (/ (abs (- (search-forward "<td ")
276 (search-forward "</td>")))
280 (if (looking-at ".*cid=\\([0-9]+\\)")
287 (make-full-mail-header
288 (1+ article) (concat subject " (" score ")")
290 (concat "<" (nnslashdot-sid-strip sid) "%"
291 (number-to-string (1+ article))
294 (concat "<" (nnslashdot-sid-strip sid) "%"
295 (number-to-string (1+ (string-to-number parent)))
300 (setq nnslashdot-headers
301 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
303 (set-buffer nntp-server-buffer)
305 (mm-with-unibyte-current-buffer
306 (dolist (header nnslashdot-headers)
307 (nnheader-insert-nov (cdr header)))))
310 (deffoo nnslashdot-request-group (group &optional server dont-check)
311 (nnslashdot-possibly-change-server nil server)
312 (let ((elem (assoc group nnslashdot-groups)))
315 (nnheader-report 'nnslashdot "Group does not exist"))
317 (nnheader-report 'nnslashdot "Opened group %s" group)
319 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
320 (prin1-to-string group))))))
322 (deffoo nnslashdot-close-group (group &optional server)
323 (nnslashdot-possibly-change-server group server)
324 (when (gnus-buffer-live-p nnslashdot-buffer)
326 (set-buffer nnslashdot-buffer)
327 (kill-buffer nnslashdot-buffer)))
330 (deffoo nnslashdot-request-article (article &optional group server buffer)
331 (nnslashdot-possibly-change-server group server)
335 (set-buffer nnslashdot-buffer)
336 (let ((case-fold-search t))
337 (goto-char (point-min))
338 (when (and (stringp article)
339 (string-match "%\\([0-9]+\\)@" article))
340 (setq article (string-to-number (match-string 1 article))))
341 (when (numberp article)
344 (re-search-forward "Posted by *<[^>]+>[^>]*<[^>]+> *on ")
345 (search-forward "<BR>")
351 "<p>.*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) nnslashdot-groups))
412 (goto-char (point-max))
414 ;; Then do the older groups.
415 (while (> (- nnslashdot-group-number number) 0)
416 (mm-with-unibyte-buffer
417 (let ((case-fold-search t))
418 (nnweb-insert (format nnslashdot-active-url number) t)
419 (goto-char (point-min))
420 (while (re-search-forward
421 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>"
423 (setq sid (match-string 1)
425 (nnweb-decode-entities-string (match-string 2)))
427 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
428 (setq articles (string-to-number (match-string 1))))
429 (setq gname (concat description " (" sid ")"))
430 (if (setq elem (assoc gname nnslashdot-groups))
431 (setcar (cdr elem) articles)
432 (push (list gname articles sid) nnslashdot-groups)))))
434 (search-failed (nnslashdot-lose why)))
435 (nnslashdot-write-groups)
436 (nnslashdot-generate-active)
439 (deffoo nnslashdot-request-newgroups (date &optional server)
440 (nnslashdot-possibly-change-server nil server)
441 (nnslashdot-generate-active)
444 (deffoo nnslashdot-request-post (&optional server)
445 (nnslashdot-possibly-change-server nil server)
446 (let ((sid (nnslashdot-sid-strip (message-fetch-field "newsgroups")))
447 (subject (message-fetch-field "subject"))
448 (references (car (last (split-string
449 (message-fetch-field "references")))))
451 (string-match "%\\([0-9]+\\)@slashdot" references)
452 (setq pid (match-string 1 references))
454 (narrow-to-region (point) (progn (message-goto-signature) (point)))
455 (goto-char (point-min))
457 (if (looking-at "> ")
459 (delete-region (point) (+ (point) 2))
461 (insert "<blockquote>\n"))
464 (insert "</blockquote>\n")
467 (goto-char (point-min))
468 (while (re-search-forward "^ *\n" nil t)
469 (replace-match "<p>\n"))
471 (when (message-goto-signature)
479 (setq body (buffer-substring (point) (point-max)))
482 "http://slashdot.org/comments.pl"
485 ("rlogin" . "userlogin")
486 ("unickname" . ,nnslashdot-login-name)
487 ("upasswd" . ,nnslashdot-password)
488 ("postersubj" . ,subject)
490 ("postercomment" . ,body)
491 ("posttype" . "html")))))
493 (deffoo nnslashdot-request-delete-group (group &optional force server)
494 (nnslashdot-possibly-change-server group server)
495 (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
497 (nnslashdot-write-groups))
499 (deffoo nnslashdot-request-close ()
500 (setq nnslashdot-headers nil
501 nnslashdot-groups nil))
503 (nnoo-define-skeleton nnslashdot)
505 ;;; Internal functions
507 (defun nnslashdot-possibly-change-server (&optional group server)
508 (nnslashdot-init server)
510 (not (nnslashdot-server-opened server)))
511 (nnslashdot-open-server server))
512 (unless nnslashdot-groups
513 (nnslashdot-read-groups)))
515 (defun nnslashdot-read-groups ()
516 (let ((file (expand-file-name "groups" nnslashdot-directory)))
517 (when (file-exists-p file)
518 (mm-with-unibyte-buffer
519 (insert-file-contents file)
520 (goto-char (point-min))
521 (setq nnslashdot-groups (read (current-buffer)))))))
523 (defun nnslashdot-write-groups ()
524 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
525 (prin1 nnslashdot-groups (current-buffer))))
527 (defun nnslashdot-init (server)
528 "Initialize buffers and such."
529 (unless (file-exists-p nnslashdot-directory)
530 (gnus-make-directory nnslashdot-directory))
531 (unless (gnus-buffer-live-p nnslashdot-buffer)
532 (setq nnslashdot-buffer
534 (nnheader-set-temp-buffer
535 (format " *nnslashdot %s*" server))))))
537 (defun nnslashdot-date-to-date (sdate)
539 (let ((elem (delete "" (split-string sdate))))
540 (concat (substring (nth 0 elem) 0 3) " "
541 (substring (nth 1 elem) 0 3) " "
542 (substring (nth 2 elem) 0 2) " "
543 (substring (nth 3 elem) 1 6) " "
544 (format-time-string "%Y") " "
548 (defun nnslashdot-generate-active ()
550 (set-buffer nntp-server-buffer)
552 (dolist (elem nnslashdot-groups)
553 (insert (prin1-to-string (car elem))
554 " " (number-to-string (cadr elem)) " 1 y\n"))))
556 (defun nnslashdot-lose (why)
557 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
559 ;(defun nnslashdot-sid-strip (sid)
560 ; (if (string-match "^00/" sid)
561 ; (substring sid (match-end 0))
564 (defalias 'nnslashdot-sid-strip 'identity)
566 (provide 'nnslashdot)
568 ;;; nnslashdot.el ends here