1 ;;; nnslashdot.el --- interfacing with Slashdot
2 ;; Copyright (C) 1999, 2000 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-threshold -1
62 "The article threshold.")
64 (defvoo nnslashdot-threaded t
65 "Whether the nnslashdot groups should be threaded or not.")
67 (defvoo nnslashdot-group-number 0
68 "The number of non-fresh groups to keep updated.")
70 (defvoo nnslashdot-login-name ""
71 "The login name to use when posting.")
73 (defvoo nnslashdot-password ""
74 "The password to use when posting.")
76 ;;; Internal variables
78 (defvar nnslashdot-groups nil)
79 (defvar nnslashdot-buffer nil)
80 (defvar nnslashdot-headers nil)
82 ;;; Interface functions
84 (nnoo-define-basics nnslashdot)
86 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
87 (nnslashdot-possibly-change-server group server)
89 (unless gnus-nov-is-evil
90 (if nnslashdot-threaded
91 (nnslashdot-threaded-retrieve-headers articles group)
92 (nnslashdot-sane-retrieve-headers articles group)))
93 (search-failed (nnslashdot-lose why))))
95 (deffoo nnslashdot-threaded-retrieve-headers (articles group)
96 (let ((last (car (last articles)))
99 (sid (caddr (assoc group nnslashdot-groups)))
102 headers article subject score from date lines parent point s)
104 (set-buffer nnslashdot-buffer)
105 (let ((case-fold-search t))
108 (nnweb-insert (format nnslashdot-article-url
109 (nnslashdot-sid-strip sid)) t)
110 (goto-char (point-min))
111 (search-forward "Posted by ")
112 (when (looking-at "<a[^>]+>\\([^<]+\\)")
113 (setq from (nnweb-decode-entities-string (match-string 1))))
114 (search-forward " on ")
115 (setq date (nnslashdot-date-to-date
116 (buffer-substring (point) (1- (search-forward "<")))))
118 (setq lines (count-lines
121 "A href=\"\\(http://slashdot.org\\)?/article" nil t)))
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]*.*(\\([^)]+\\))")
159 (setq point (match-end 0)
161 (nnweb-decode-entities-string (match-string 1))
162 " <" (match-string 2) ">"))
163 (looking-at "by \\(.+\\) on ")
164 (setq point (match-end 0)
165 from (nnweb-decode-entities-string (match-string 1))))
166 (goto-char (- point 5))
167 (search-forward " on ")
169 (nnslashdot-date-to-date
170 (buffer-substring (point) (progn (end-of-line) (point)))))
171 (setq lines (/ (abs (- (search-forward "<td ")
172 (search-forward "</td>")))
176 (if (looking-at ".*cid=\\([0-9]+\\)")
183 (make-full-mail-header
185 (concat subject " (" score ")")
187 (concat "<" (nnslashdot-sid-strip sid) "%"
188 (number-to-string (1+ article))
191 (concat "<" (nnslashdot-sid-strip sid) "%"
192 (number-to-string (1+ (string-to-number parent)))
197 (setq nnslashdot-headers (sort headers 'car-less-than-car))
199 (set-buffer nntp-server-buffer)
201 (mm-with-unibyte-current-buffer
202 (dolist (header nnslashdot-headers)
203 (nnheader-insert-nov (cdr header)))))
206 (deffoo nnslashdot-sane-retrieve-headers (articles group)
207 (let ((last (car (last articles)))
209 (start (max (1- (car articles)) 1))
210 (sid (caddr (assoc group nnslashdot-groups)))
211 headers article subject score from date lines parent point)
213 (set-buffer nnslashdot-buffer)
216 (nnweb-insert (format nnslashdot-article-url
217 (nnslashdot-sid-strip sid)) t)
218 (goto-char (point-min))
219 (search-forward "Posted by ")
220 (when (looking-at "<a[^>]+>\\([^<]+\\)")
221 (setq from (nnweb-decode-entities-string (match-string 1))))
222 (search-forward " on ")
223 (setq date (nnslashdot-date-to-date
224 (buffer-substring (point) (1- (search-forward "<")))))
226 (setq lines (count-lines (point)
228 "A href=\"\\(http://slashdot.org\\)?/article")))
232 (make-full-mail-header
233 1 group from date (concat "<" (nnslashdot-sid-strip sid)
237 (while (or (not article)
241 (setq start (1+ article)))
242 (setq point (goto-char (point-max)))
244 (format nnslashdot-comments-url (nnslashdot-sid-strip sid)
245 nnslashdot-threshold 4 start)
248 (while (re-search-forward
249 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
251 (setq article (string-to-number (match-string 1))
252 subject (match-string 3)
253 score (match-string 5))
254 (when (string-match "^Re: *" subject)
255 (setq subject (concat "Re: " (substring subject (match-end 0)))))
256 (setq subject (nnweb-decode-entities-string subject))
259 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
260 (setq from (concat (nnweb-decode-entities-string (match-string 1))
261 " <" (match-string 2) ">"))
262 (looking-at "by \\(.+\\) on ")
263 (setq from (nnweb-decode-entities-string (match-string 1))))
264 (goto-char (- (match-end 0) 5))
265 (search-forward " on ")
267 (nnslashdot-date-to-date
268 (buffer-substring (point) (progn (end-of-line) (point)))))
269 (setq lines (/ (abs (- (search-forward "<td ")
270 (search-forward "</td>")))
274 (if (looking-at ".*cid=\\([0-9]+\\)")
281 (make-full-mail-header
282 (1+ article) (concat subject " (" score ")")
284 (concat "<" (nnslashdot-sid-strip sid) "%"
285 (number-to-string (1+ article))
288 (concat "<" (nnslashdot-sid-strip sid) "%"
289 (number-to-string (1+ (string-to-number parent)))
294 (setq nnslashdot-headers
295 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
297 (set-buffer nntp-server-buffer)
299 (mm-with-unibyte-current-buffer
300 (dolist (header nnslashdot-headers)
301 (nnheader-insert-nov (cdr header)))))
304 (deffoo nnslashdot-request-group (group &optional server dont-check)
305 (nnslashdot-possibly-change-server nil server)
306 (let ((elem (assoc group nnslashdot-groups)))
309 (nnheader-report 'nnslashdot "Group does not exist"))
311 (nnheader-report 'nnslashdot "Opened group %s" group)
313 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
314 (prin1-to-string group))))))
316 (deffoo nnslashdot-close-group (group &optional server)
317 (nnslashdot-possibly-change-server group server)
318 (when (gnus-buffer-live-p nnslashdot-buffer)
320 (set-buffer nnslashdot-buffer)
321 (kill-buffer nnslashdot-buffer)))
324 (deffoo nnslashdot-request-article (article &optional group server buffer)
325 (nnslashdot-possibly-change-server group server)
329 (set-buffer nnslashdot-buffer)
330 (let ((case-fold-search t))
331 (goto-char (point-min))
332 (when (and (stringp article)
333 (string-match "%\\([0-9]+\\)@" article))
334 (setq article (string-to-number (match-string 1 article))))
335 (when (numberp article)
338 (re-search-forward "Posted by *<[^>]+>[^>]*<[^>]+> *on ")
339 (search-forward "<BR>")
345 "<p>.*A href=\"\\(http://slashdot.org\\)?/article")
346 (match-beginning 0)))))
347 (search-forward (format "<a name=\"%d\">" (1- article)))
350 (re-search-forward "<td[^>]+>")
351 (search-forward "</td>")))))))
352 (search-failed (nnslashdot-lose why)))
356 (set-buffer (or buffer nntp-server-buffer))
358 (mm-with-unibyte-current-buffer
360 (goto-char (point-min))
361 (while (re-search-forward "\\(<br>\r?\\)+" nil t)
362 (replace-match "<p>" t t))
363 (goto-char (point-min))
364 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
365 (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
367 (let ((header (cdr (assq article nnslashdot-headers))))
368 (nnheader-insert-header header))
369 (nnheader-report 'nnslashdot "Fetched article %s" article))
370 (cons group article)))))
372 (deffoo nnslashdot-close-server (&optional server)
373 (when (and (nnslashdot-server-opened server)
374 (gnus-buffer-live-p nnslashdot-buffer))
376 (set-buffer nnslashdot-buffer)
377 (kill-buffer nnslashdot-buffer)))
378 (nnoo-close-server 'nnslashdot server))
380 (deffoo nnslashdot-request-list (&optional server)
381 (nnslashdot-possibly-change-server nil server)
383 sid elem description articles gname)
385 ;; First we do the Ultramode to get info on all the latest groups.
387 (mm-with-unibyte-buffer
388 (nnweb-insert "http://slashdot.org/slashdot.xml" t)
389 (goto-char (point-min))
390 (while (search-forward "<story>" nil t)
391 (narrow-to-region (point) (search-forward "</story>"))
392 (goto-char (point-min))
393 (re-search-forward "<title>\\([^<]+\\)</title>")
395 (nnweb-decode-entities-string (match-string 1)))
396 (re-search-forward "<url>\\([^<]+\\)</url>")
397 (setq sid (match-string 1))
398 (string-match "/\\([0-9/]+\\)\\(.shtml\\|$\\)" sid)
399 (setq sid (concat "00/" (match-string 1 sid)))
400 (re-search-forward "<comments>\\([^<]+\\)</comments>")
401 (setq articles (string-to-number (match-string 1)))
402 (setq gname (concat description " (" sid ")"))
403 (if (setq elem (assoc gname nnslashdot-groups))
404 (setcar (cdr elem) articles)
405 (push (list gname articles sid) nnslashdot-groups))
406 (goto-char (point-max))
408 ;; Then do the older groups.
409 (while (> (- nnslashdot-group-number number) 0)
410 (mm-with-unibyte-buffer
411 (let ((case-fold-search t))
412 (nnweb-insert (format nnslashdot-active-url number) t)
413 (goto-char (point-min))
414 (while (re-search-forward
415 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>"
417 (setq sid (match-string 1)
419 (nnweb-decode-entities-string (match-string 2)))
421 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
422 (setq articles (string-to-number (match-string 1))))
423 (setq gname (concat description " (" sid ")"))
424 (if (setq elem (assoc gname nnslashdot-groups))
425 (setcar (cdr elem) articles)
426 (push (list gname articles sid) nnslashdot-groups)))))
428 (search-failed (nnslashdot-lose why)))
429 (nnslashdot-write-groups)
430 (nnslashdot-generate-active)
433 (deffoo nnslashdot-request-newgroups (date &optional server)
434 (nnslashdot-possibly-change-server nil server)
435 (nnslashdot-generate-active)
438 (deffoo nnslashdot-request-post (&optional server)
439 (nnslashdot-possibly-change-server nil server)
440 (let ((sid (nnslashdot-sid-strip (message-fetch-field "newsgroups")))
441 (subject (message-fetch-field "subject"))
442 (references (car (last (split-string
443 (message-fetch-field "references")))))
445 (string-match "%\\([0-9]+\\)@slashdot" references)
446 (setq pid (match-string 1 references))
448 (narrow-to-region (point) (progn (message-goto-signature) (point)))
449 (goto-char (point-min))
451 (if (looking-at "> ")
453 (delete-region (point) (+ (point) 2))
455 (insert "<blockquote>\n"))
458 (insert "</blockquote>\n")
461 (goto-char (point-min))
462 (while (re-search-forward "^ *\n" nil t)
463 (replace-match "<p>\n"))
465 (when (message-goto-signature)
473 (setq body (buffer-substring (point) (point-max)))
476 "http://slashdot.org/comments.pl"
479 ("rlogin" . "userlogin")
480 ("unickname" . ,nnslashdot-login-name)
481 ("upasswd" . ,nnslashdot-password)
482 ("postersubj" . ,subject)
484 ("postercomment" . ,body)
485 ("posttype" . "html")))))
487 (deffoo nnslashdot-request-delete-group (group &optional force server)
488 (nnslashdot-possibly-change-server group server)
489 (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
491 (nnslashdot-write-groups))
493 (nnoo-define-skeleton nnslashdot)
495 ;;; Internal functions
497 (defun nnslashdot-possibly-change-server (&optional group server)
498 (nnslashdot-init server)
500 (not (nnslashdot-server-opened server)))
501 (nnslashdot-open-server server))
502 (unless nnslashdot-groups
503 (nnslashdot-read-groups)))
505 (defun nnslashdot-read-groups ()
506 (let ((file (expand-file-name "groups" nnslashdot-directory)))
507 (when (file-exists-p file)
508 (mm-with-unibyte-buffer
509 (insert-file-contents file)
510 (goto-char (point-min))
511 (setq nnslashdot-groups (read (current-buffer)))))))
513 (defun nnslashdot-write-groups ()
514 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
515 (prin1 nnslashdot-groups (current-buffer))))
517 (defun nnslashdot-init (server)
518 "Initialize buffers and such."
519 (unless (file-exists-p nnslashdot-directory)
520 (gnus-make-directory nnslashdot-directory))
521 (unless (gnus-buffer-live-p nnslashdot-buffer)
522 (setq nnslashdot-buffer
524 (nnheader-set-temp-buffer
525 (format " *nnslashdot %s*" server))))))
527 (defun nnslashdot-date-to-date (sdate)
529 (let ((elem (delete "" (split-string sdate))))
530 (concat (substring (nth 0 elem) 0 3) " "
531 (substring (nth 1 elem) 0 3) " "
532 (substring (nth 2 elem) 0 2) " "
533 (substring (nth 3 elem) 1 6) " "
534 (format-time-string "%Y") " "
538 (defun nnslashdot-generate-active ()
540 (set-buffer nntp-server-buffer)
542 (dolist (elem nnslashdot-groups)
543 (insert (prin1-to-string (car elem))
544 " " (number-to-string (cadr elem)) " 1 y\n"))))
546 (defun nnslashdot-lose (why)
547 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
549 ;(defun nnslashdot-sid-strip (sid)
550 ; (if (string-match "^00/" sid)
551 ; (substring sid (match-end 0))
554 (defalias 'nnslashdot-sid-strip 'identity)
556 (provide 'nnslashdot)
558 ;;; nnslashdot.el ends here