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))
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 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 "<")))))
117 (setq lines (count-lines
120 "A href=http://slashdot.org/article" nil t)))
124 (make-full-mail-header
125 1 group from date (concat "<" sid "%1@slashdot>")
128 (while (and (setq start (pop startats))
130 (setq point (goto-char (point-max)))
132 (format nnslashdot-comments-url sid nnslashdot-threshold 0 start)
135 (setq first-comments nil)
136 (goto-char (point-max))
137 (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
138 (setq s (string-to-number (match-string 1)))
139 (unless (memq s startats)
141 (setq startats (sort startats '<)))
143 (while (re-search-forward
144 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
146 (setq article (string-to-number (match-string 1))
147 subject (match-string 3)
148 score (match-string 5))
149 (when (string-match "^Re: *" subject)
150 (setq subject (concat "Re: " (substring subject (match-end 0)))))
151 (setq subject (nnweb-decode-entities-string subject))
154 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
155 (setq from (concat (nnweb-decode-entities-string (match-string 1))
156 " <" (match-string 2) ">"))
157 (looking-at "by \\(.+\\) on ")
158 (setq from (nnweb-decode-entities-string (match-string 1))))
159 (goto-char (- (match-end 0) 5))
160 (search-forward " on ")
162 (nnslashdot-date-to-date
163 (buffer-substring (point) (progn (end-of-line) (point)))))
164 (setq lines (/ (abs (- (search-forward "<td ")
165 (search-forward "</td>")))
169 (if (looking-at ".*cid=\\([0-9]+\\)")
176 (make-full-mail-header
178 (concat subject " (" score ")")
181 (number-to-string (1+ article))
185 (number-to-string (1+ (string-to-number parent)))
190 (setq nnslashdot-headers (sort headers 'car-less-than-car))
192 (set-buffer nntp-server-buffer)
194 (dolist (header nnslashdot-headers)
195 (nnheader-insert-nov (cdr header))))
198 (deffoo nnslashdot-sane-retrieve-headers (articles group)
199 (let ((last (car (last articles)))
201 (start (max (1- (car articles)) 1))
202 (sid (caddr (assoc group nnslashdot-groups)))
203 headers article subject score from date lines parent point)
205 (set-buffer nnslashdot-buffer)
208 (nnweb-insert (format nnslashdot-article-url sid) t)
209 (goto-char (point-min))
210 (search-forward "Posted by ")
211 (when (looking-at "<a[^>]+>\\([^<]+\\)")
212 (setq from (nnweb-decode-entities-string (match-string 1))))
213 (search-forward " on ")
214 (setq date (nnslashdot-date-to-date
215 (buffer-substring (point) (1- (search-forward "<")))))
217 (setq lines (count-lines (point)
219 "A href=http://slashdot.org/article")))
223 (make-full-mail-header
224 1 group from date (concat "<" sid "%1@slashdot>")
227 (while (or (not article)
231 (setq start (1+ article)))
232 (setq point (goto-char (point-max)))
234 (format nnslashdot-comments-url sid nnslashdot-threshold 4 start)
237 (while (re-search-forward
238 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
240 (setq article (string-to-number (match-string 1))
241 subject (match-string 3)
242 score (match-string 5))
243 (when (string-match "^Re: *" subject)
244 (setq subject (concat "Re: " (substring subject (match-end 0)))))
245 (setq subject (nnweb-decode-entities-string subject))
248 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
249 (setq from (concat (nnweb-decode-entities-string (match-string 1))
250 " <" (match-string 2) ">"))
251 (looking-at "by \\(.+\\) on ")
252 (setq from (nnweb-decode-entities-string (match-string 1))))
253 (goto-char (- (match-end 0) 5))
254 (search-forward " on ")
256 (nnslashdot-date-to-date
257 (buffer-substring (point) (progn (end-of-line) (point)))))
258 (setq lines (/ (abs (- (search-forward "<td ")
259 (search-forward "</td>")))
263 (if (looking-at ".*cid=\\([0-9]+\\)")
270 (make-full-mail-header
271 (1+ article) (concat subject " (" score ")")
274 (number-to-string (1+ article))
278 (number-to-string (1+ (string-to-number parent)))
283 (setq nnslashdot-headers
284 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
286 (set-buffer nntp-server-buffer)
288 (dolist (header nnslashdot-headers)
289 (nnheader-insert-nov (cdr header))))
292 (deffoo nnslashdot-request-group (group &optional server dont-check)
293 (nnslashdot-possibly-change-server nil server)
294 (let ((elem (assoc group nnslashdot-groups)))
297 (nnheader-report 'nnslashdot "Group does not exist"))
299 (nnheader-report 'nnslashdot "Opened group %s" group)
301 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
302 (prin1-to-string group))))))
304 (deffoo nnslashdot-close-group (group &optional server)
305 (nnslashdot-possibly-change-server group server)
306 (when (gnus-buffer-live-p nnslashdot-buffer)
308 (set-buffer nnslashdot-buffer)
309 (kill-buffer nnslashdot-buffer)))
312 (deffoo nnslashdot-request-article (article &optional group server buffer)
313 (nnslashdot-possibly-change-server group server)
317 (set-buffer nnslashdot-buffer)
318 (let ((case-fold-search t))
319 (goto-char (point-min))
320 (when (and (stringp article)
321 (string-match "%\\([0-9]+\\)@" article))
322 (setq article (string-to-number (match-string 1 article))))
323 (when (numberp article)
326 (re-search-forward "Posted by *<[^>]+>[^>]*<[^>]+> *on ")
327 (search-forward "<BR>")
333 "<p>.*A href=http://slashdot\\.org/article")
334 (match-beginning 0)))))
335 (search-forward (format "<a name=\"%d\">" (1- article)))
338 (re-search-forward "<td[^>]+>")
339 (search-forward "</td>")))))))
340 (search-failed (nnslashdot-lose why)))
344 (set-buffer (or buffer nntp-server-buffer))
347 (goto-char (point-min))
348 (while (re-search-forward "\\(<br>\r?\\)+" nil t)
349 (replace-match "<p>" t t))
350 (goto-char (point-min))
351 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
352 (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
354 (let ((header (cdr (assq article nnslashdot-headers))))
355 (nnheader-insert-header header))
356 (nnheader-report 'nnslashdot "Fetched article %s" article)
357 (cons group article)))))
359 (deffoo nnslashdot-close-server (&optional server)
360 (when (and (nnslashdot-server-opened server)
361 (gnus-buffer-live-p nnslashdot-buffer))
363 (set-buffer nnslashdot-buffer)
364 (kill-buffer nnslashdot-buffer)))
365 (nnoo-close-server 'nnslashdot server))
367 (deffoo nnslashdot-request-list (&optional server)
368 (nnslashdot-possibly-change-server nil server)
370 sid elem description articles gname)
372 ;; First we do the Ultramode to get info on all the latest groups.
374 (mm-with-unibyte-buffer
375 (nnweb-insert "http://slashdot.org/slashdot.xml" t)
376 (goto-char (point-min))
377 (while (search-forward "<story>" nil t)
378 (narrow-to-region (point) (search-forward "</story>"))
379 (goto-char (point-min))
380 (re-search-forward "<title>\\([^<]+\\)</title>")
382 (nnweb-decode-entities-string (match-string 1)))
383 (re-search-forward "<url>\\([^<]+\\)</url>")
384 (setq sid (match-string 1))
385 (string-match "/\\([0-9/]+\\)\\(.shtml\\|$\\)" sid)
386 (setq sid (concat "00/" (match-string 1 sid)))
387 (re-search-forward "<comments>\\([^<]+\\)</comments>")
388 (setq articles (string-to-number (match-string 1)))
389 (setq gname (concat description " (" sid ")"))
390 (if (setq elem (assoc gname nnslashdot-groups))
391 (setcar (cdr elem) articles)
392 (push (list gname articles sid) nnslashdot-groups))
393 (goto-char (point-max))
395 ;; Then do the older groups.
396 (while (> (- nnslashdot-group-number number) 0)
397 (mm-with-unibyte-buffer
398 (let ((case-fold-search t))
399 (nnweb-insert (format nnslashdot-active-url number) t)
400 (goto-char (point-min))
401 (while (re-search-forward
402 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>"
404 (setq sid (match-string 1)
406 (nnweb-decode-entities-string (match-string 2)))
408 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
409 (setq articles (string-to-number (match-string 1))))
410 (setq gname (concat description " (" sid ")"))
411 (if (setq elem (assoc gname nnslashdot-groups))
412 (setcar (cdr elem) articles)
413 (push (list gname articles sid) nnslashdot-groups)))))
415 (search-failed (nnslashdot-lose why)))
416 (nnslashdot-write-groups)
417 (nnslashdot-generate-active)
420 (deffoo nnslashdot-request-newgroups (date &optional server)
421 (nnslashdot-possibly-change-server nil server)
422 (nnslashdot-generate-active)
425 (deffoo nnslashdot-request-post (&optional server)
426 (nnslashdot-possibly-change-server nil server)
427 (let ((sid (message-fetch-field "newsgroups"))
428 (subject (message-fetch-field "subject"))
429 (references (car (last (split-string
430 (message-fetch-field "references")))))
432 (string-match "%\\([0-9]+\\)@slashdot" references)
433 (setq pid (match-string 1 references))
435 (narrow-to-region (point) (progn (message-goto-signature) (point)))
436 (goto-char (point-min))
438 (if (looking-at "> ")
440 (delete-region (point) (+ (point) 2))
442 (insert "<blockquote>\n"))
445 (insert "</blockquote>\n")
448 (goto-char (point-min))
449 (while (re-search-forward "^ *\n" nil t)
450 (replace-match "<p>\n"))
452 (when (message-goto-signature)
460 (setq body (buffer-substring (point) (point-max)))
463 "http://slashdot.org/comments.pl"
466 ("rlogin" . "userlogin")
467 ("unickname" . ,nnslashdot-login-name)
468 ("upasswd" . ,nnslashdot-password)
469 ("postersubj" . ,subject)
471 ("postercomment" . ,body)
472 ("posttype" . "html")))))
474 (deffoo nnslashdot-request-delete-group (group &optional force server)
475 (nnslashdot-possibly-change-server group server)
476 (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
478 (nnslashdot-write-groups))
480 (nnoo-define-skeleton nnslashdot)
482 ;;; Internal functions
484 (defun nnslashdot-possibly-change-server (&optional group server)
485 (nnslashdot-init server)
487 (not (nnslashdot-server-opened server)))
488 (nnslashdot-open-server server))
489 (unless nnslashdot-groups
490 (nnslashdot-read-groups)))
492 (defun nnslashdot-read-groups ()
493 (let ((file (expand-file-name "groups" nnslashdot-directory)))
494 (when (file-exists-p file)
495 (mm-with-unibyte-buffer
496 (insert-file-contents file)
497 (goto-char (point-min))
498 (setq nnslashdot-groups (read (current-buffer)))))))
500 (defun nnslashdot-write-groups ()
501 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
502 (prin1 nnslashdot-groups (current-buffer))))
504 (defun nnslashdot-init (server)
505 "Initialize buffers and such."
506 (unless (file-exists-p nnslashdot-directory)
507 (gnus-make-directory nnslashdot-directory))
508 (unless (gnus-buffer-live-p nnslashdot-buffer)
509 (setq nnslashdot-buffer
511 (nnheader-set-temp-buffer
512 (format " *nnslashdot %s*" server))))))
514 (defun nnslashdot-date-to-date (sdate)
516 (let ((elem (delete "" (split-string sdate))))
517 (concat (substring (nth 0 elem) 0 3) " "
518 (substring (nth 1 elem) 0 3) " "
519 (substring (nth 2 elem) 0 2) " "
520 (substring (nth 3 elem) 1 6) " "
521 (format-time-string "%Y") " "
525 (defun nnslashdot-generate-active ()
527 (set-buffer nntp-server-buffer)
529 (dolist (elem nnslashdot-groups)
530 (insert (prin1-to-string (car elem))
531 " " (number-to-string (cadr elem)) " 1 y\n"))))
533 (defun nnslashdot-lose (why)
534 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
536 (provide 'nnslashdot)
538 ;;; nnslashdot.el ends here