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 from (concat (nnweb-decode-entities-string (match-string 1))
160 " <" (match-string 2) ">"))
161 (looking-at "by \\(.+\\) on ")
162 (setq from (nnweb-decode-entities-string (match-string 1))))
163 (goto-char (- (match-end 0) 5))
164 (search-forward " on ")
166 (nnslashdot-date-to-date
167 (buffer-substring (point) (progn (end-of-line) (point)))))
168 (setq lines (/ (abs (- (search-forward "<td ")
169 (search-forward "</td>")))
173 (if (looking-at ".*cid=\\([0-9]+\\)")
180 (make-full-mail-header
182 (concat subject " (" score ")")
184 (concat "<" (nnslashdot-sid-strip sid) "%"
185 (number-to-string (1+ article))
188 (concat "<" (nnslashdot-sid-strip sid) "%"
189 (number-to-string (1+ (string-to-number parent)))
194 (setq nnslashdot-headers (sort headers 'car-less-than-car))
196 (set-buffer nntp-server-buffer)
198 (dolist (header nnslashdot-headers)
199 (nnheader-insert-nov (cdr header))))
202 (deffoo nnslashdot-sane-retrieve-headers (articles group)
203 (let ((last (car (last articles)))
205 (start (max (1- (car articles)) 1))
206 (sid (caddr (assoc group nnslashdot-groups)))
207 headers article subject score from date lines parent point)
209 (set-buffer nnslashdot-buffer)
212 (nnweb-insert (format nnslashdot-article-url
213 (nnslashdot-sid-strip sid)) t)
214 (goto-char (point-min))
215 (search-forward "Posted by ")
216 (when (looking-at "<a[^>]+>\\([^<]+\\)")
217 (setq from (nnweb-decode-entities-string (match-string 1))))
218 (search-forward " on ")
219 (setq date (nnslashdot-date-to-date
220 (buffer-substring (point) (1- (search-forward "<")))))
222 (setq lines (count-lines (point)
224 "A href=\"\\(http://slashdot.org\\)?/article")))
228 (make-full-mail-header
229 1 group from date (concat "<" (nnslashdot-sid-strip sid)
233 (while (or (not article)
237 (setq start (1+ article)))
238 (setq point (goto-char (point-max)))
240 (format nnslashdot-comments-url (nnslashdot-sid-strip sid)
241 nnslashdot-threshold 4 start)
244 (while (re-search-forward
245 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
247 (setq article (string-to-number (match-string 1))
248 subject (match-string 3)
249 score (match-string 5))
250 (when (string-match "^Re: *" subject)
251 (setq subject (concat "Re: " (substring subject (match-end 0)))))
252 (setq subject (nnweb-decode-entities-string subject))
255 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
256 (setq from (concat (nnweb-decode-entities-string (match-string 1))
257 " <" (match-string 2) ">"))
258 (looking-at "by \\(.+\\) on ")
259 (setq from (nnweb-decode-entities-string (match-string 1))))
260 (goto-char (- (match-end 0) 5))
261 (search-forward " on ")
263 (nnslashdot-date-to-date
264 (buffer-substring (point) (progn (end-of-line) (point)))))
265 (setq lines (/ (abs (- (search-forward "<td ")
266 (search-forward "</td>")))
270 (if (looking-at ".*cid=\\([0-9]+\\)")
277 (make-full-mail-header
278 (1+ article) (concat subject " (" score ")")
280 (concat "<" (nnslashdot-sid-strip sid) "%"
281 (number-to-string (1+ article))
284 (concat "<" (nnslashdot-sid-strip sid) "%"
285 (number-to-string (1+ (string-to-number parent)))
290 (setq nnslashdot-headers
291 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
293 (set-buffer nntp-server-buffer)
295 (dolist (header nnslashdot-headers)
296 (nnheader-insert-nov (cdr header))))
299 (deffoo nnslashdot-request-group (group &optional server dont-check)
300 (nnslashdot-possibly-change-server nil server)
301 (let ((elem (assoc group nnslashdot-groups)))
304 (nnheader-report 'nnslashdot "Group does not exist"))
306 (nnheader-report 'nnslashdot "Opened group %s" group)
308 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
309 (prin1-to-string group))))))
311 (deffoo nnslashdot-close-group (group &optional server)
312 (nnslashdot-possibly-change-server group server)
313 (when (gnus-buffer-live-p nnslashdot-buffer)
315 (set-buffer nnslashdot-buffer)
316 (kill-buffer nnslashdot-buffer)))
319 (deffoo nnslashdot-request-article (article &optional group server buffer)
320 (nnslashdot-possibly-change-server group server)
324 (set-buffer nnslashdot-buffer)
325 (let ((case-fold-search t))
326 (goto-char (point-min))
327 (when (and (stringp article)
328 (string-match "%\\([0-9]+\\)@" article))
329 (setq article (string-to-number (match-string 1 article))))
330 (when (numberp article)
333 (re-search-forward "Posted by *<[^>]+>[^>]*<[^>]+> *on ")
334 (search-forward "<BR>")
340 "<p>.*A href=\"\\(http://slashdot.org\\)?/article")
341 (match-beginning 0)))))
342 (search-forward (format "<a name=\"%d\">" (1- article)))
345 (re-search-forward "<td[^>]+>")
346 (search-forward "</td>")))))))
347 (search-failed (nnslashdot-lose why)))
351 (set-buffer (or buffer nntp-server-buffer))
354 (goto-char (point-min))
355 (while (re-search-forward "\\(<br>\r?\\)+" nil t)
356 (replace-match "<p>" t t))
357 (goto-char (point-min))
358 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
359 (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
361 (let ((header (cdr (assq article nnslashdot-headers))))
362 (nnheader-insert-header header))
363 (nnheader-report 'nnslashdot "Fetched article %s" article)
364 (cons group article)))))
366 (deffoo nnslashdot-close-server (&optional server)
367 (when (and (nnslashdot-server-opened server)
368 (gnus-buffer-live-p nnslashdot-buffer))
370 (set-buffer nnslashdot-buffer)
371 (kill-buffer nnslashdot-buffer)))
372 (nnoo-close-server 'nnslashdot server))
374 (deffoo nnslashdot-request-list (&optional server)
375 (nnslashdot-possibly-change-server nil server)
377 sid elem description articles gname)
379 ;; First we do the Ultramode to get info on all the latest groups.
381 (mm-with-unibyte-buffer
382 (nnweb-insert "http://slashdot.org/slashdot.xml" t)
383 (goto-char (point-min))
384 (while (search-forward "<story>" nil t)
385 (narrow-to-region (point) (search-forward "</story>"))
386 (goto-char (point-min))
387 (re-search-forward "<title>\\([^<]+\\)</title>")
389 (nnweb-decode-entities-string (match-string 1)))
390 (re-search-forward "<url>\\([^<]+\\)</url>")
391 (setq sid (match-string 1))
392 (string-match "/\\([0-9/]+\\)\\(.shtml\\|$\\)" sid)
393 (setq sid (concat "00/" (match-string 1 sid)))
394 (re-search-forward "<comments>\\([^<]+\\)</comments>")
395 (setq articles (string-to-number (match-string 1)))
396 (setq gname (concat description " (" sid ")"))
397 (if (setq elem (assoc gname nnslashdot-groups))
398 (setcar (cdr elem) articles)
399 (push (list gname articles sid) nnslashdot-groups))
400 (goto-char (point-max))
402 ;; Then do the older groups.
403 (while (> (- nnslashdot-group-number number) 0)
404 (mm-with-unibyte-buffer
405 (let ((case-fold-search t))
406 (nnweb-insert (format nnslashdot-active-url number) t)
407 (goto-char (point-min))
408 (while (re-search-forward
409 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>"
411 (setq sid (match-string 1)
413 (nnweb-decode-entities-string (match-string 2)))
415 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
416 (setq articles (string-to-number (match-string 1))))
417 (setq gname (concat description " (" sid ")"))
418 (if (setq elem (assoc gname nnslashdot-groups))
419 (setcar (cdr elem) articles)
420 (push (list gname articles sid) nnslashdot-groups)))))
422 (search-failed (nnslashdot-lose why)))
423 (nnslashdot-write-groups)
424 (nnslashdot-generate-active)
427 (deffoo nnslashdot-request-newgroups (date &optional server)
428 (nnslashdot-possibly-change-server nil server)
429 (nnslashdot-generate-active)
432 (deffoo nnslashdot-request-post (&optional server)
433 (nnslashdot-possibly-change-server nil server)
434 (let ((sid (nnslashdot-sid-strip (message-fetch-field "newsgroups")))
435 (subject (message-fetch-field "subject"))
436 (references (car (last (split-string
437 (message-fetch-field "references")))))
439 (string-match "%\\([0-9]+\\)@slashdot" references)
440 (setq pid (match-string 1 references))
442 (narrow-to-region (point) (progn (message-goto-signature) (point)))
443 (goto-char (point-min))
445 (if (looking-at "> ")
447 (delete-region (point) (+ (point) 2))
449 (insert "<blockquote>\n"))
452 (insert "</blockquote>\n")
455 (goto-char (point-min))
456 (while (re-search-forward "^ *\n" nil t)
457 (replace-match "<p>\n"))
459 (when (message-goto-signature)
467 (setq body (buffer-substring (point) (point-max)))
470 "http://slashdot.org/comments.pl"
473 ("rlogin" . "userlogin")
474 ("unickname" . ,nnslashdot-login-name)
475 ("upasswd" . ,nnslashdot-password)
476 ("postersubj" . ,subject)
478 ("postercomment" . ,body)
479 ("posttype" . "html")))))
481 (deffoo nnslashdot-request-delete-group (group &optional force server)
482 (nnslashdot-possibly-change-server group server)
483 (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
485 (nnslashdot-write-groups))
487 (nnoo-define-skeleton nnslashdot)
489 ;;; Internal functions
491 (defun nnslashdot-possibly-change-server (&optional group server)
492 (nnslashdot-init server)
494 (not (nnslashdot-server-opened server)))
495 (nnslashdot-open-server server))
496 (unless nnslashdot-groups
497 (nnslashdot-read-groups)))
499 (defun nnslashdot-read-groups ()
500 (let ((file (expand-file-name "groups" nnslashdot-directory)))
501 (when (file-exists-p file)
502 (mm-with-unibyte-buffer
503 (insert-file-contents file)
504 (goto-char (point-min))
505 (setq nnslashdot-groups (read (current-buffer)))))))
507 (defun nnslashdot-write-groups ()
508 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
509 (prin1 nnslashdot-groups (current-buffer))))
511 (defun nnslashdot-init (server)
512 "Initialize buffers and such."
513 (unless (file-exists-p nnslashdot-directory)
514 (gnus-make-directory nnslashdot-directory))
515 (unless (gnus-buffer-live-p nnslashdot-buffer)
516 (setq nnslashdot-buffer
518 (nnheader-set-temp-buffer
519 (format " *nnslashdot %s*" server))))))
521 (defun nnslashdot-date-to-date (sdate)
523 (let ((elem (delete "" (split-string sdate))))
524 (concat (substring (nth 0 elem) 0 3) " "
525 (substring (nth 1 elem) 0 3) " "
526 (substring (nth 2 elem) 0 2) " "
527 (substring (nth 3 elem) 1 6) " "
528 (format-time-string "%Y") " "
532 (defun nnslashdot-generate-active ()
534 (set-buffer nntp-server-buffer)
536 (dolist (elem nnslashdot-groups)
537 (insert (prin1-to-string (car elem))
538 " " (number-to-string (cadr elem)) " 1 y\n"))))
540 (defun nnslashdot-lose (why)
541 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
543 ;(defun nnslashdot-sid-strip (sid)
544 ; (if (string-match "^00/" sid)
545 ; (substring sid (match-end 0))
548 (defalias 'nnslashdot-sid-strip 'identity)
550 (provide 'nnslashdot)
552 ;;; nnslashdot.el ends here