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
))
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
108 (nnslashdot-sid-strip sid
)) t
)
109 (goto-char (point-min))
110 (search-forward "Posted by ")
111 (when (looking-at "<a[^>]+>\\([^<]+\\)")
112 (setq from
(nnweb-decode-entities-string (match-string 1))))
113 (search-forward " on ")
114 (setq date
(nnslashdot-date-to-date
115 (buffer-substring (point) (1- (search-forward "<")))))
116 (setq lines
(/ (- (point)
117 (progn (forward-line 1) (point)))
122 (make-full-mail-header
124 (concat "<" (nnslashdot-sid-strip sid
) "%1@slashdot>")
127 (while (and (setq start
(pop startats
))
129 (setq point
(goto-char (point-max)))
131 (format nnslashdot-comments-url
132 (nnslashdot-sid-strip sid
)
133 nnslashdot-threshold
0 start
)
136 (setq first-comments nil
)
137 (goto-char (point-max))
138 (while (re-search-backward "startat=\\([0-9]+\\)" nil t
)
139 (setq s
(string-to-number (match-string 1)))
140 (unless (memq s startats
)
142 (setq startats
(sort startats
'<)))
144 (while (re-search-forward
145 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
147 (setq article
(string-to-number (match-string 1))
148 subject
(match-string 3)
149 score
(match-string 5))
150 (when (string-match "^Re: *" subject
)
151 (setq subject
(concat "Re: " (substring subject
(match-end 0)))))
152 (setq subject
(nnweb-decode-entities-string subject
))
155 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
157 (goto-char (- (match-end 0) 5))
159 (nnweb-decode-entities-string (match-string 1))
160 " <" (match-string 2) ">")))
162 (when (looking-at "by \\(.+\\) on ")
163 (goto-char (- (match-end 0) 5))
164 (setq from
(nnweb-decode-entities-string (match-string 1)))))
165 (search-forward " on ")
167 (nnslashdot-date-to-date
168 (buffer-substring (point) (progn (end-of-line) (point)))))
169 (setq lines
(/ (abs (- (search-forward "<td ")
170 (search-forward "</td>")))
174 (if (looking-at ".*cid=\\([0-9]+\\)")
181 (make-full-mail-header
183 (concat subject
" (" score
")")
185 (concat "<" (nnslashdot-sid-strip sid
) "%"
186 (number-to-string (1+ article
))
189 (concat "<" (nnslashdot-sid-strip sid
) "%"
190 (number-to-string (1+ (string-to-number parent
)))
195 (setq nnslashdot-headers
(sort headers
'car-less-than-car
))
197 (set-buffer nntp-server-buffer
)
199 (mm-with-unibyte-current-buffer
200 (dolist (header nnslashdot-headers
)
201 (nnheader-insert-nov (cdr header
)))))
204 (deffoo nnslashdot-sane-retrieve-headers
(articles group
)
205 (let ((last (car (last articles
)))
207 (start (max (1- (car articles
)) 1))
208 (sid (caddr (assoc group nnslashdot-groups
)))
209 headers article subject score from date lines parent point
)
211 (set-buffer nnslashdot-buffer
)
214 (nnweb-insert (format nnslashdot-article-url
215 (nnslashdot-sid-strip sid
)) t
)
216 (goto-char (point-min))
217 (search-forward "Posted by ")
218 (when (looking-at "<a[^>]+>\\([^<]+\\)")
219 (setq from
(nnweb-decode-entities-string (match-string 1))))
220 (search-forward " on ")
221 (setq date
(nnslashdot-date-to-date
222 (buffer-substring (point) (1- (search-forward "<")))))
224 (setq lines
(count-lines (point)
226 "A href=\"\\(http://slashdot.org\\)?/article")))
230 (make-full-mail-header
231 1 group from date
(concat "<" (nnslashdot-sid-strip sid
)
235 (while (or (not article
)
239 (setq start
(1+ article
)))
240 (setq point
(goto-char (point-max)))
242 (format nnslashdot-comments-url
(nnslashdot-sid-strip sid
)
243 nnslashdot-threshold
4 start
)
246 (while (re-search-forward
247 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
249 (setq article
(string-to-number (match-string 1))
250 subject
(match-string 3)
251 score
(match-string 5))
252 (when (string-match "^Re: *" subject
)
253 (setq subject
(concat "Re: " (substring subject
(match-end 0)))))
254 (setq subject
(nnweb-decode-entities-string subject
))
257 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
259 (goto-char (- (match-end 0) 5))
261 (nnweb-decode-entities-string (match-string 1))
262 " <" (match-string 2) ">")))
264 (when (looking-at "by \\(.+\\) on ")
265 (goto-char (- (match-end 0) 5))
266 (setq from
(nnweb-decode-entities-string (match-string 1)))))
267 (search-forward " on ")
269 (nnslashdot-date-to-date
270 (buffer-substring (point) (progn (end-of-line) (point)))))
271 (setq lines
(/ (abs (- (search-forward "<td ")
272 (search-forward "</td>")))
276 (if (looking-at ".*cid=\\([0-9]+\\)")
283 (make-full-mail-header
284 (1+ article
) (concat subject
" (" score
")")
286 (concat "<" (nnslashdot-sid-strip sid
) "%"
287 (number-to-string (1+ article
))
290 (concat "<" (nnslashdot-sid-strip sid
) "%"
291 (number-to-string (1+ (string-to-number parent
)))
296 (setq nnslashdot-headers
297 (sort headers
(lambda (s1 s2
) (< (car s1
) (car s2
)))))
299 (set-buffer nntp-server-buffer
)
301 (mm-with-unibyte-current-buffer
302 (dolist (header nnslashdot-headers
)
303 (nnheader-insert-nov (cdr header
)))))
306 (deffoo nnslashdot-request-group
(group &optional server dont-check
)
307 (nnslashdot-possibly-change-server nil server
)
308 (let ((elem (assoc group nnslashdot-groups
)))
311 (nnheader-report 'nnslashdot
"Group does not exist"))
313 (nnheader-report 'nnslashdot
"Opened group %s" group
)
315 "211 %d %d %d %s\n" (cadr elem
) 1 (cadr elem
)
316 (prin1-to-string group
))))))
318 (deffoo nnslashdot-close-group
(group &optional server
)
319 (nnslashdot-possibly-change-server group server
)
320 (when (gnus-buffer-live-p nnslashdot-buffer
)
322 (set-buffer nnslashdot-buffer
)
323 (kill-buffer nnslashdot-buffer
)))
326 (deffoo nnslashdot-request-article
(article &optional group server buffer
)
327 (nnslashdot-possibly-change-server group server
)
331 (set-buffer nnslashdot-buffer
)
332 (let ((case-fold-search t
))
333 (goto-char (point-min))
334 (when (and (stringp article
)
335 (string-match "%\\([0-9]+\\)@" article
))
336 (setq article
(string-to-number (match-string 1 article
))))
337 (when (numberp article
)
340 (re-search-forward "Posted by *<[^>]+>[^>]*<[^>]+> *on ")
341 (search-forward "<BR>")
347 "<p>.*A href=\"\\(http://slashdot.org\\)?/article")
348 (match-beginning 0)))))
349 (search-forward (format "<a name=\"%d\">" (1- article
)))
352 (re-search-forward "<td[^>]+>")
353 (search-forward "</td>")))))))
354 (search-failed (nnslashdot-lose why
)))
358 (set-buffer (or buffer nntp-server-buffer
))
360 (mm-with-unibyte-current-buffer
362 (goto-char (point-min))
363 (while (re-search-forward "\\(<br>\r?\\)+" nil t
)
364 (replace-match "<p>" t t
))
365 (goto-char (point-min))
366 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
367 (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups
))
369 (let ((header (cdr (assq article nnslashdot-headers
))))
370 (nnheader-insert-header header
))
371 (nnheader-report 'nnslashdot
"Fetched article %s" article
))
372 (cons group article
)))))
374 (deffoo nnslashdot-close-server
(&optional server
)
375 (when (and (nnslashdot-server-opened server
)
376 (gnus-buffer-live-p nnslashdot-buffer
))
378 (set-buffer nnslashdot-buffer
)
379 (kill-buffer nnslashdot-buffer
)))
380 (nnoo-close-server 'nnslashdot server
))
382 (deffoo nnslashdot-request-list
(&optional server
)
383 (nnslashdot-possibly-change-server nil server
)
385 sid elem description articles gname
)
387 ;; First we do the Ultramode to get info on all the latest groups.
389 (mm-with-unibyte-buffer
390 (nnweb-insert "http://slashdot.org/slashdot.xml" t
)
391 (goto-char (point-min))
392 (while (search-forward "<story>" nil t
)
393 (narrow-to-region (point) (search-forward "</story>"))
394 (goto-char (point-min))
395 (re-search-forward "<title>\\([^<]+\\)</title>")
397 (nnweb-decode-entities-string (match-string 1)))
398 (re-search-forward "<url>\\([^<]+\\)</url>")
399 (setq sid
(match-string 1))
400 (string-match "sid=\\([0-9/]+\\)\\(.shtml\\|$\\)" sid
)
401 (setq sid
(match-string 1 sid
))
402 (re-search-forward "<comments>\\([^<]+\\)</comments>")
403 (setq articles
(string-to-number (match-string 1)))
404 (setq gname
(concat description
" (" sid
")"))
405 (if (setq elem
(assoc gname nnslashdot-groups
))
406 (setcar (cdr elem
) articles
)
407 (push (list gname articles sid
) nnslashdot-groups
))
408 (goto-char (point-max))
410 ;; Then do the older groups.
411 (while (> (- nnslashdot-group-number number
) 0)
412 (mm-with-unibyte-buffer
413 (let ((case-fold-search t
))
414 (nnweb-insert (format nnslashdot-active-url number
) t
)
415 (goto-char (point-min))
416 (while (re-search-forward
417 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>"
419 (setq sid
(match-string 1)
421 (nnweb-decode-entities-string (match-string 2)))
423 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t
)
424 (setq articles
(string-to-number (match-string 1))))
425 (setq gname
(concat description
" (" sid
")"))
426 (if (setq elem
(assoc gname nnslashdot-groups
))
427 (setcar (cdr elem
) articles
)
428 (push (list gname articles sid
) nnslashdot-groups
)))))
430 (search-failed (nnslashdot-lose why
)))
431 (nnslashdot-write-groups)
432 (nnslashdot-generate-active)
435 (deffoo nnslashdot-request-newgroups
(date &optional server
)
436 (nnslashdot-possibly-change-server nil server
)
437 (nnslashdot-generate-active)
440 (deffoo nnslashdot-request-post
(&optional server
)
441 (nnslashdot-possibly-change-server nil server
)
442 (let ((sid (nnslashdot-sid-strip (message-fetch-field "newsgroups")))
443 (subject (message-fetch-field "subject"))
444 (references (car (last (split-string
445 (message-fetch-field "references")))))
447 (string-match "%\\([0-9]+\\)@slashdot" references
)
448 (setq pid
(match-string 1 references
))
450 (narrow-to-region (point) (progn (message-goto-signature) (point)))
451 (goto-char (point-min))
453 (if (looking-at "> ")
455 (delete-region (point) (+ (point) 2))
457 (insert "<blockquote>\n"))
460 (insert "</blockquote>\n")
463 (goto-char (point-min))
464 (while (re-search-forward "^ *\n" nil t
)
465 (replace-match "<p>\n"))
467 (when (message-goto-signature)
475 (setq body
(buffer-substring (point) (point-max)))
478 "http://slashdot.org/comments.pl"
481 ("rlogin" .
"userlogin")
482 ("unickname" .
,nnslashdot-login-name
)
483 ("upasswd" .
,nnslashdot-password
)
484 ("postersubj" .
,subject
)
486 ("postercomment" .
,body
)
487 ("posttype" .
"html")))))
489 (deffoo nnslashdot-request-delete-group
(group &optional force server
)
490 (nnslashdot-possibly-change-server group server
)
491 (setq nnslashdot-groups
(delq (assoc group nnslashdot-groups
)
493 (nnslashdot-write-groups))
495 (deffoo nnslashdot-request-close
()
496 (setq nnslashdot-headers nil
497 nnslashdot-groups nil
))
499 (nnoo-define-skeleton nnslashdot
)
501 ;;; Internal functions
503 (defun nnslashdot-possibly-change-server (&optional group server
)
504 (nnslashdot-init server
)
506 (not (nnslashdot-server-opened server
)))
507 (nnslashdot-open-server server
))
508 (unless nnslashdot-groups
509 (nnslashdot-read-groups)))
511 (defun nnslashdot-read-groups ()
512 (let ((file (expand-file-name "groups" nnslashdot-directory
)))
513 (when (file-exists-p file
)
514 (mm-with-unibyte-buffer
515 (insert-file-contents file
)
516 (goto-char (point-min))
517 (setq nnslashdot-groups
(read (current-buffer)))))))
519 (defun nnslashdot-write-groups ()
520 (with-temp-file (expand-file-name "groups" nnslashdot-directory
)
521 (prin1 nnslashdot-groups
(current-buffer))))
523 (defun nnslashdot-init (server)
524 "Initialize buffers and such."
525 (unless (file-exists-p nnslashdot-directory
)
526 (gnus-make-directory nnslashdot-directory
))
527 (unless (gnus-buffer-live-p nnslashdot-buffer
)
528 (setq nnslashdot-buffer
530 (nnheader-set-temp-buffer
531 (format " *nnslashdot %s*" server
))))))
533 (defun nnslashdot-date-to-date (sdate)
535 (let ((elem (delete "" (split-string sdate
))))
536 (concat (substring (nth 0 elem
) 0 3) " "
537 (substring (nth 1 elem
) 0 3) " "
538 (substring (nth 2 elem
) 0 2) " "
539 (substring (nth 3 elem
) 1 6) " "
540 (format-time-string "%Y") " "
544 (defun nnslashdot-generate-active ()
546 (set-buffer nntp-server-buffer
)
548 (dolist (elem nnslashdot-groups
)
549 (insert (prin1-to-string (car elem
))
550 " " (number-to-string (cadr elem
)) " 1 y\n"))))
552 (defun nnslashdot-lose (why)
553 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
555 ;(defun nnslashdot-sid-strip (sid)
556 ; (if (string-match "^00/" sid)
557 ; (substring sid (match-end 0))
560 (defalias 'nnslashdot-sid-strip
'identity
)
562 (provide 'nnslashdot
)
564 ;;; nnslashdot.el ends here