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))
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-threshold -1
61 "The article threshold.")
63 (defvoo nnslashdot-threaded t
64 "Whether the nnslashdot groups should be threaded or not.")
66 (defvoo nnslashdot-group-number 0
67 "The number of non-fresh groups to keep updated.")
69 (defvoo nnslashdot-login-name ""
70 "The login name to use when posting.")
72 (defvoo nnslashdot-password ""
73 "The password to use when posting.")
75 ;;; Internal variables
77 (defvar nnslashdot-groups nil)
78 (defvar nnslashdot-buffer nil)
79 (defvar nnslashdot-headers nil)
81 ;;; Interface functions
83 (nnoo-define-basics nnslashdot)
85 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
86 (nnslashdot-possibly-change-server group server)
88 (unless gnus-nov-is-evil
89 (if nnslashdot-threaded
90 (nnslashdot-threaded-retrieve-headers articles group)
91 (nnslashdot-sane-retrieve-headers articles group)))
92 (search-failed (nnslashdot-lose why))))
94 (deffoo nnslashdot-threaded-retrieve-headers (articles group)
95 (let ((last (car (last articles)))
98 (sid (caddr (assoc group nnslashdot-groups)))
101 headers article subject score from date lines parent point s)
103 (set-buffer nnslashdot-buffer)
104 (let ((case-fold-search t))
107 (nnweb-insert (format nnslashdot-article-url sid))
108 (goto-char (point-min))
109 (search-forward "Posted by ")
110 (when (looking-at "<a[^>]+>\\([^<]+\\)")
111 (setq from (match-string 1)))
112 (search-forward " on ")
113 (setq date (nnslashdot-date-to-date
114 (buffer-substring (point) (1- (search-forward "<")))))
116 (setq lines (count-lines
119 "A href=http://slashdot.org/article.pl")))
123 (make-full-mail-header
124 1 group from date (concat "<" sid "%1@slashdot>")
127 (while (and (setq start (pop startats))
129 (setq point (goto-char (point-max)))
131 (format nnslashdot-comments-url sid nnslashdot-threshold 0 start))
133 (setq first-comments nil)
134 (goto-char (point-max))
135 (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
136 (setq s (string-to-number (match-string 1)))
137 (unless (memq s startats)
139 (setq startats (sort startats '<)))
141 (while (re-search-forward
142 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
144 (setq article (string-to-number (match-string 1))
145 subject (match-string 2)
146 score (match-string 3))
147 (when (string-match "^Re: *" subject)
148 (setq subject (concat "Re: " (substring subject (match-end 0)))))
151 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
152 (setq from (concat (match-string 1)
153 " <" (match-string 2) ">"))
154 (looking-at "by \\(.+\\) on ")
155 (setq from (match-string 1)))
156 (goto-char (- (match-end 0) 5))
157 (search-forward " on ")
159 (nnslashdot-date-to-date
160 (buffer-substring (point) (progn (end-of-line) (point)))))
161 (setq lines (/ (abs (- (search-forward "<td ")
162 (search-forward "</td>")))
166 (if (looking-at ".*cid=\\([0-9]+\\)")
173 (make-full-mail-header
175 (concat subject " (" score ")")
178 (number-to-string (1+ article))
182 (number-to-string (1+ (string-to-number parent)))
187 (setq nnslashdot-headers (sort headers 'car-less-than-car))
189 (set-buffer nntp-server-buffer)
191 (dolist (header nnslashdot-headers)
192 (nnheader-insert-nov (cdr header))))
195 (deffoo nnslashdot-sane-retrieve-headers (articles group)
196 (let ((last (car (last articles)))
198 (start (max (1- (car articles)) 1))
199 (sid (caddr (assoc group nnslashdot-groups)))
200 headers article subject score from date lines parent point)
202 (set-buffer nnslashdot-buffer)
205 (nnweb-insert (format nnslashdot-article-url sid))
206 (goto-char (point-min))
207 (search-forward "Posted by ")
208 (when (looking-at "<a[^>]+>\\([^<]+\\)")
209 (setq from (match-string 1)))
210 (search-forward " on ")
211 (setq date (nnslashdot-date-to-date
212 (buffer-substring (point) (1- (search-forward "<")))))
214 (setq lines (count-lines (point)
216 "A href=http://slashdot.org/article.pl")))
220 (make-full-mail-header
221 1 group from date (concat "<" sid "%1@slashdot>")
224 (while (or (not article)
228 (setq start (1+ article)))
229 (setq point (goto-char (point-max)))
231 (format nnslashdot-comments-url sid nnslashdot-threshold 4 start))
233 (while (re-search-forward
234 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
236 (setq article (string-to-number (match-string 1))
237 subject (match-string 2)
238 score (match-string 3))
239 (when (string-match "^Re: *" subject)
240 (setq subject (concat "Re: " (substring subject (match-end 0)))))
243 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
244 (setq from (concat (match-string 1) " <" (match-string 2) ">"))
245 (looking-at "by \\(.+\\) on ")
246 (setq from (match-string 1)))
247 (goto-char (- (match-end 0) 5))
248 (search-forward " on ")
250 (nnslashdot-date-to-date
251 (buffer-substring (point) (progn (end-of-line) (point)))))
252 (setq lines (/ (abs (- (search-forward "<td ")
253 (search-forward "</td>")))
257 (if (looking-at ".*cid=\\([0-9]+\\)")
264 (make-full-mail-header
265 (1+ article) (concat subject " (" score ")")
268 (number-to-string (1+ article))
272 (number-to-string (1+ (string-to-number parent)))
277 (setq nnslashdot-headers
278 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
280 (set-buffer nntp-server-buffer)
282 (dolist (header nnslashdot-headers)
283 (nnheader-insert-nov (cdr header))))
286 (deffoo nnslashdot-request-group (group &optional server dont-check)
287 (nnslashdot-possibly-change-server nil server)
288 (let ((elem (assoc group nnslashdot-groups)))
291 (nnheader-report 'nnslashdot "Group does not exist"))
293 (nnheader-report 'nnslashdot "Opened group %s" group)
295 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
296 (prin1-to-string group))))))
298 (deffoo nnslashdot-close-group (group &optional server)
299 (nnslashdot-possibly-change-server group server)
300 (when (gnus-buffer-live-p nnslashdot-buffer)
302 (set-buffer nnslashdot-buffer)
303 (kill-buffer nnslashdot-buffer)))
306 (deffoo nnslashdot-request-article (article &optional group server buffer)
307 (nnslashdot-possibly-change-server group server)
311 (set-buffer nnslashdot-buffer)
312 (let ((case-fold-search t))
313 (goto-char (point-min))
314 (when (and (stringp article)
315 (string-match "%\\([0-9]+\\)@" article))
316 (setq article (string-to-number (match-string 1 article))))
317 (when (numberp article)
320 (re-search-forward "Posted by .* on ")
327 "<p>.*A href=http://slashdot.org/article.pl")
328 (match-beginning 0)))))
329 (search-forward (format "<a name=\"%d\">" (1- article)))
332 (re-search-forward "<td[^>]+>")
333 (search-forward "</td>")))))))
334 (search-failed (nnslashdot-lose why)))
338 (set-buffer (or buffer nntp-server-buffer))
341 (goto-char (point-min))
342 (while (search-forward "<br><br>" nil t)
343 (replace-match "<p>" t t))
344 (goto-char (point-min))
345 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
346 (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
348 (let ((header (cdr (assq article nnslashdot-headers))))
349 (nnheader-insert-header header))
350 (nnheader-report 'nnslashdot "Fetched article %s" article)
351 (cons group article)))))
353 (deffoo nnslashdot-close-server (&optional server)
354 (when (and (nnslashdot-server-opened server)
355 (gnus-buffer-live-p nnslashdot-buffer))
357 (set-buffer nnslashdot-buffer)
358 (kill-buffer nnslashdot-buffer)))
359 (nnoo-close-server 'nnslashdot server))
361 (deffoo nnslashdot-request-list (&optional server)
362 (nnslashdot-possibly-change-server nil server)
364 sid elem description articles gname)
366 ;; First we do the Ultramode to get info on all the latest groups.
367 (mm-with-unibyte-buffer
368 (nnweb-insert "http://slashdot.org/slashdot.xml")
369 (goto-char (point-min))
370 (while (search-forward "<story>" nil t)
371 (narrow-to-region (point) (search-forward "</story>"))
372 (goto-char (point-min))
373 (re-search-forward "<title>\\([^<]+\\)</title>")
374 (setq description (match-string 1))
375 (re-search-forward "<url>\\([^<]+\\)</url>")
376 (setq sid (match-string 1))
377 (string-match "/\\([0-9/]+\\).shtml" sid)
378 (setq sid (match-string 1 sid))
379 (re-search-forward "<comments>\\([^<]+\\)</comments>")
380 (setq articles (string-to-number (match-string 1)))
381 (setq gname (concat description " (" sid ")"))
382 (if (setq elem (assoc gname nnslashdot-groups))
383 (setcar (cdr elem) articles)
384 (push (list gname articles sid) nnslashdot-groups))
385 (goto-char (point-max))
387 ;; Then do the older groups.
388 (while (> (- nnslashdot-group-number number) 0)
389 (mm-with-unibyte-buffer
390 (let ((case-fold-search t))
391 (nnweb-insert (format nnslashdot-active-url number))
392 (goto-char (point-min))
393 (while (re-search-forward
394 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
395 (setq sid (match-string 1)
396 description (match-string 2))
398 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
399 (setq articles (string-to-number (match-string 1))))
400 (setq gname (concat description " (" sid ")"))
401 (if (setq elem (assoc gname nnslashdot-groups))
402 (setcar (cdr elem) articles)
403 (push (list gname articles sid) nnslashdot-groups)))))
405 (search-failed (nnslashdot-lose why)))
406 (nnslashdot-write-groups)
407 (nnslashdot-generate-active)
410 (deffoo nnslashdot-request-newgroups (date &optional server)
411 (nnslashdot-possibly-change-server nil server)
412 (nnslashdot-generate-active)
415 (deffoo nnslashdot-request-post (&optional server)
416 (nnslashdot-possibly-change-server nil server)
417 (let ((sid (message-fetch-field "newsgroups"))
418 (subject (message-fetch-field "subject"))
419 (references (car (last (split-string
420 (message-fetch-field "references")))))
422 (string-match "%\\([0-9]+\\)@slashdot" references)
423 (setq pid (match-string 1 references))
425 (narrow-to-region (point) (progn (message-goto-signature) (point)))
426 (goto-char (point-min))
428 (if (looking-at "> ")
430 (delete-region (point) (+ (point) 2))
432 (insert "<blockquote>\n"))
435 (insert "</blockquote>\n")
438 (goto-char (point-min))
439 (while (re-search-forward "^ *\n" nil t)
440 (replace-match "<p>\n"))
442 (when (message-goto-signature)
450 (setq body (buffer-substring (point) (point-max)))
453 "http://slashdot.org/comments.pl"
456 ("rlogin" . "userlogin")
457 ("unickname" . ,nnslashdot-login-name)
458 ("upasswd" . ,nnslashdot-password)
459 ("postersubj" . ,subject)
461 ("postercomment" . ,body)
462 ("posttype" . "html")))))
464 (nnoo-define-skeleton nnslashdot)
466 ;;; Internal functions
468 (defun nnslashdot-possibly-change-server (&optional group server)
469 (nnslashdot-init server)
471 (not (nnslashdot-server-opened server)))
472 (nnslashdot-open-server server))
473 (unless nnslashdot-groups
474 (nnslashdot-read-groups)))
476 (defun nnslashdot-read-groups ()
477 (let ((file (expand-file-name "groups" nnslashdot-directory)))
478 (when (file-exists-p file)
479 (mm-with-unibyte-buffer
480 (insert-file-contents file)
481 (goto-char (point-min))
482 (setq nnslashdot-groups (read (current-buffer)))))))
484 (defun nnslashdot-write-groups ()
485 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
486 (prin1 nnslashdot-groups (current-buffer))))
488 (defun nnslashdot-init (server)
489 "Initialize buffers and such."
490 (unless (file-exists-p nnslashdot-directory)
491 (gnus-make-directory nnslashdot-directory))
492 (unless (gnus-buffer-live-p nnslashdot-buffer)
493 (setq nnslashdot-buffer
495 (nnheader-set-temp-buffer
496 (format " *nnslashdot %s*" server))))))
498 (defun nnslashdot-date-to-date (sdate)
499 (let ((elem (delete "" (split-string sdate))))
500 (concat (substring (nth 0 elem) 0 3) " "
501 (substring (nth 1 elem) 0 3) " "
502 (substring (nth 2 elem) 0 2) " "
503 (substring (nth 3 elem) 1 6) " "
504 (format-time-string "%Y") " "
507 (defun nnslashdot-generate-active ()
509 (set-buffer nntp-server-buffer)
511 (dolist (elem nnslashdot-groups)
512 (insert (prin1-to-string (car elem))
513 " " (number-to-string (cadr elem)) " 1 y\n"))))
515 (defun nnslashdot-lose (why)
516 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
518 (provide 'nnslashdot)
520 ;;; nnslashdot.el ends here