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 "<")))))
117 (setq lines (/ (- (point)
118 (progn (forward-line 1) (point)))
123 (make-full-mail-header
125 (concat "<" (nnslashdot-sid-strip sid) "%1@slashdot>")
128 (while (and (setq start (pop startats))
130 (setq point (goto-char (point-max)))
132 (format nnslashdot-comments-url
133 (nnslashdot-sid-strip sid)
134 nnslashdot-threshold 0 start)
137 (setq first-comments nil)
138 (goto-char (point-max))
139 (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
140 (setq s (string-to-number (match-string 1)))
141 (unless (memq s startats)
143 (setq startats (sort startats '<)))
145 (while (re-search-forward
146 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
148 (setq article (string-to-number (match-string 1))
149 subject (match-string 3)
150 score (match-string 5))
151 (when (string-match "^Re: *" subject)
152 (setq subject (concat "Re: " (substring subject (match-end 0)))))
153 (setq subject (nnweb-decode-entities-string subject))
156 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
157 (setq point (match-end 0)
159 (nnweb-decode-entities-string (match-string 1))
160 " <" (match-string 2) ">"))
161 (looking-at "by \\(.+\\) on ")
162 (setq point (match-end 0)
163 from (nnweb-decode-entities-string (match-string 1))))
164 (goto-char (- point 5))
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]*.*(\\([^)]+\\))")
258 (setq from (concat (nnweb-decode-entities-string (match-string 1))
259 " <" (match-string 2) ">"))
260 (looking-at "by \\(.+\\) on ")
261 (setq from (nnweb-decode-entities-string (match-string 1))))
262 (goto-char (- (match-end 0) 5))
263 (search-forward " on ")
265 (nnslashdot-date-to-date
266 (buffer-substring (point) (progn (end-of-line) (point)))))
267 (setq lines (/ (abs (- (search-forward "<td ")
268 (search-forward "</td>")))
272 (if (looking-at ".*cid=\\([0-9]+\\)")
279 (make-full-mail-header
280 (1+ article) (concat subject " (" score ")")
282 (concat "<" (nnslashdot-sid-strip sid) "%"
283 (number-to-string (1+ article))
286 (concat "<" (nnslashdot-sid-strip sid) "%"
287 (number-to-string (1+ (string-to-number parent)))
292 (setq nnslashdot-headers
293 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
295 (set-buffer nntp-server-buffer)
297 (mm-with-unibyte-current-buffer
298 (dolist (header nnslashdot-headers)
299 (nnheader-insert-nov (cdr header)))))
302 (deffoo nnslashdot-request-group (group &optional server dont-check)
303 (nnslashdot-possibly-change-server nil server)
304 (let ((elem (assoc group nnslashdot-groups)))
307 (nnheader-report 'nnslashdot "Group does not exist"))
309 (nnheader-report 'nnslashdot "Opened group %s" group)
311 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
312 (prin1-to-string group))))))
314 (deffoo nnslashdot-close-group (group &optional server)
315 (nnslashdot-possibly-change-server group server)
316 (when (gnus-buffer-live-p nnslashdot-buffer)
318 (set-buffer nnslashdot-buffer)
319 (kill-buffer nnslashdot-buffer)))
322 (deffoo nnslashdot-request-article (article &optional group server buffer)
323 (nnslashdot-possibly-change-server group server)
327 (set-buffer nnslashdot-buffer)
328 (let ((case-fold-search t))
329 (goto-char (point-min))
330 (when (and (stringp article)
331 (string-match "%\\([0-9]+\\)@" article))
332 (setq article (string-to-number (match-string 1 article))))
333 (when (numberp article)
336 (re-search-forward "Posted by *<[^>]+>[^>]*<[^>]+> *on ")
337 (search-forward "<BR>")
343 "<p>.*A href=\"\\(http://slashdot.org\\)?/article")
344 (match-beginning 0)))))
345 (search-forward (format "<a name=\"%d\">" (1- article)))
348 (re-search-forward "<td[^>]+>")
349 (search-forward "</td>")))))))
350 (search-failed (nnslashdot-lose why)))
354 (set-buffer (or buffer nntp-server-buffer))
356 (mm-with-unibyte-current-buffer
358 (goto-char (point-min))
359 (while (re-search-forward "\\(<br>\r?\\)+" nil t)
360 (replace-match "<p>" t t))
361 (goto-char (point-min))
362 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
363 (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
365 (let ((header (cdr (assq article nnslashdot-headers))))
366 (nnheader-insert-header header))
367 (nnheader-report 'nnslashdot "Fetched article %s" article))
368 (cons group article)))))
370 (deffoo nnslashdot-close-server (&optional server)
371 (when (and (nnslashdot-server-opened server)
372 (gnus-buffer-live-p nnslashdot-buffer))
374 (set-buffer nnslashdot-buffer)
375 (kill-buffer nnslashdot-buffer)))
376 (nnoo-close-server 'nnslashdot server))
378 (deffoo nnslashdot-request-list (&optional server)
379 (nnslashdot-possibly-change-server nil server)
381 sid elem description articles gname)
383 ;; First we do the Ultramode to get info on all the latest groups.
385 (mm-with-unibyte-buffer
386 (nnweb-insert "http://slashdot.org/slashdot.xml" t)
387 (goto-char (point-min))
388 (while (search-forward "<story>" nil t)
389 (narrow-to-region (point) (search-forward "</story>"))
390 (goto-char (point-min))
391 (re-search-forward "<title>\\([^<]+\\)</title>")
393 (nnweb-decode-entities-string (match-string 1)))
394 (re-search-forward "<url>\\([^<]+\\)</url>")
395 (setq sid (match-string 1))
396 (string-match "/\\([0-9/]+\\)\\(.shtml\\|$\\)" sid)
397 (setq sid (concat "00/" (match-string 1 sid)))
398 (re-search-forward "<comments>\\([^<]+\\)</comments>")
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))
404 (goto-char (point-max))
406 ;; Then do the older groups.
407 (while (> (- nnslashdot-group-number number) 0)
408 (mm-with-unibyte-buffer
409 (let ((case-fold-search t))
410 (nnweb-insert (format nnslashdot-active-url number) t)
411 (goto-char (point-min))
412 (while (re-search-forward
413 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>"
415 (setq sid (match-string 1)
417 (nnweb-decode-entities-string (match-string 2)))
419 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
420 (setq articles (string-to-number (match-string 1))))
421 (setq gname (concat description " (" sid ")"))
422 (if (setq elem (assoc gname nnslashdot-groups))
423 (setcar (cdr elem) articles)
424 (push (list gname articles sid) nnslashdot-groups)))))
426 (search-failed (nnslashdot-lose why)))
427 (nnslashdot-write-groups)
428 (nnslashdot-generate-active)
431 (deffoo nnslashdot-request-newgroups (date &optional server)
432 (nnslashdot-possibly-change-server nil server)
433 (nnslashdot-generate-active)
436 (deffoo nnslashdot-request-post (&optional server)
437 (nnslashdot-possibly-change-server nil server)
438 (let ((sid (nnslashdot-sid-strip (message-fetch-field "newsgroups")))
439 (subject (message-fetch-field "subject"))
440 (references (car (last (split-string
441 (message-fetch-field "references")))))
443 (string-match "%\\([0-9]+\\)@slashdot" references)
444 (setq pid (match-string 1 references))
446 (narrow-to-region (point) (progn (message-goto-signature) (point)))
447 (goto-char (point-min))
449 (if (looking-at "> ")
451 (delete-region (point) (+ (point) 2))
453 (insert "<blockquote>\n"))
456 (insert "</blockquote>\n")
459 (goto-char (point-min))
460 (while (re-search-forward "^ *\n" nil t)
461 (replace-match "<p>\n"))
463 (when (message-goto-signature)
471 (setq body (buffer-substring (point) (point-max)))
474 "http://slashdot.org/comments.pl"
477 ("rlogin" . "userlogin")
478 ("unickname" . ,nnslashdot-login-name)
479 ("upasswd" . ,nnslashdot-password)
480 ("postersubj" . ,subject)
482 ("postercomment" . ,body)
483 ("posttype" . "html")))))
485 (deffoo nnslashdot-request-delete-group (group &optional force server)
486 (nnslashdot-possibly-change-server group server)
487 (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
489 (nnslashdot-write-groups))
491 (deffoo nnslashdot-request-close ()
492 (setq nnslashdot-headers nil
493 nnslashdot-groups nil))
495 (nnoo-define-skeleton nnslashdot)
497 ;;; Internal functions
499 (defun nnslashdot-possibly-change-server (&optional group server)
500 (nnslashdot-init server)
502 (not (nnslashdot-server-opened server)))
503 (nnslashdot-open-server server))
504 (unless nnslashdot-groups
505 (nnslashdot-read-groups)))
507 (defun nnslashdot-read-groups ()
508 (let ((file (expand-file-name "groups" nnslashdot-directory)))
509 (when (file-exists-p file)
510 (mm-with-unibyte-buffer
511 (insert-file-contents file)
512 (goto-char (point-min))
513 (setq nnslashdot-groups (read (current-buffer)))))))
515 (defun nnslashdot-write-groups ()
516 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
517 (prin1 nnslashdot-groups (current-buffer))))
519 (defun nnslashdot-init (server)
520 "Initialize buffers and such."
521 (unless (file-exists-p nnslashdot-directory)
522 (gnus-make-directory nnslashdot-directory))
523 (unless (gnus-buffer-live-p nnslashdot-buffer)
524 (setq nnslashdot-buffer
526 (nnheader-set-temp-buffer
527 (format " *nnslashdot %s*" server))))))
529 (defun nnslashdot-date-to-date (sdate)
531 (let ((elem (delete "" (split-string sdate))))
532 (concat (substring (nth 0 elem) 0 3) " "
533 (substring (nth 1 elem) 0 3) " "
534 (substring (nth 2 elem) 0 2) " "
535 (substring (nth 3 elem) 1 6) " "
536 (format-time-string "%Y") " "
540 (defun nnslashdot-generate-active ()
542 (set-buffer nntp-server-buffer)
544 (dolist (elem nnslashdot-groups)
545 (insert (prin1-to-string (car elem))
546 " " (number-to-string (cadr elem)) " 1 y\n"))))
548 (defun nnslashdot-lose (why)
549 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
551 ;(defun nnslashdot-sid-strip (sid)
552 ; (if (string-match "^00/" sid)
553 ; (substring sid (match-end 0))
556 (defalias 'nnslashdot-sid-strip 'identity)
558 (provide 'nnslashdot)
560 ;;; nnslashdot.el ends here