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) t)
108 (goto-char (point-min))
109 (search-forward "Posted by ")
110 (when (looking-at "<a[^>]+>\\([^<]+\\)")
111 (setq from (nnweb-decode-entities-string (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" nil t)))
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)
134 (setq first-comments nil)
135 (goto-char (point-max))
136 (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
137 (setq s (string-to-number (match-string 1)))
138 (unless (memq s startats)
140 (setq startats (sort startats '<)))
142 (while (re-search-forward
143 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
145 (setq article (string-to-number (match-string 1))
146 subject (match-string 3)
147 score (match-string 5))
148 (when (string-match "^Re: *" subject)
149 (setq subject (concat "Re: " (substring subject (match-end 0)))))
150 (setq subject (nnweb-decode-entities-string subject))
153 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
154 (setq from (concat (nnweb-decode-entities-string (match-string 1))
155 " <" (match-string 2) ">"))
156 (looking-at "by \\(.+\\) on ")
157 (setq from (nnweb-decode-entities-string (match-string 1))))
158 (goto-char (- (match-end 0) 5))
159 (search-forward " on ")
161 (nnslashdot-date-to-date
162 (buffer-substring (point) (progn (end-of-line) (point)))))
163 (setq lines (/ (abs (- (search-forward "<td ")
164 (search-forward "</td>")))
168 (if (looking-at ".*cid=\\([0-9]+\\)")
175 (make-full-mail-header
177 (concat subject " (" score ")")
180 (number-to-string (1+ article))
184 (number-to-string (1+ (string-to-number parent)))
189 (setq nnslashdot-headers (sort headers 'car-less-than-car))
191 (set-buffer nntp-server-buffer)
193 (dolist (header nnslashdot-headers)
194 (nnheader-insert-nov (cdr header))))
197 (deffoo nnslashdot-sane-retrieve-headers (articles group)
198 (let ((last (car (last articles)))
200 (start (max (1- (car articles)) 1))
201 (sid (caddr (assoc group nnslashdot-groups)))
202 headers article subject score from date lines parent point)
204 (set-buffer nnslashdot-buffer)
207 (nnweb-insert (format nnslashdot-article-url sid) t)
208 (goto-char (point-min))
209 (search-forward "Posted by ")
210 (when (looking-at "<a[^>]+>\\([^<]+\\)")
211 (setq from (nnweb-decode-entities-string (match-string 1))))
212 (search-forward " on ")
213 (setq date (nnslashdot-date-to-date
214 (buffer-substring (point) (1- (search-forward "<")))))
216 (setq lines (count-lines (point)
218 "A href=http://slashdot.org/article")))
222 (make-full-mail-header
223 1 group from date (concat "<" sid "%1@slashdot>")
226 (while (or (not article)
230 (setq start (1+ article)))
231 (setq point (goto-char (point-max)))
233 (format nnslashdot-comments-url sid nnslashdot-threshold 4 start)
236 (while (re-search-forward
237 "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
239 (setq article (string-to-number (match-string 1))
240 subject (match-string 3)
241 score (match-string 5))
242 (when (string-match "^Re: *" subject)
243 (setq subject (concat "Re: " (substring subject (match-end 0)))))
244 (setq subject (nnweb-decode-entities-string subject))
247 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
248 (setq from (concat (nnweb-decode-entities-string (match-string 1))
249 " <" (match-string 2) ">"))
250 (looking-at "by \\(.+\\) on ")
251 (setq from (nnweb-decode-entities-string (match-string 1))))
252 (goto-char (- (match-end 0) 5))
253 (search-forward " on ")
255 (nnslashdot-date-to-date
256 (buffer-substring (point) (progn (end-of-line) (point)))))
257 (setq lines (/ (abs (- (search-forward "<td ")
258 (search-forward "</td>")))
262 (if (looking-at ".*cid=\\([0-9]+\\)")
269 (make-full-mail-header
270 (1+ article) (concat subject " (" score ")")
273 (number-to-string (1+ article))
277 (number-to-string (1+ (string-to-number parent)))
282 (setq nnslashdot-headers
283 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
285 (set-buffer nntp-server-buffer)
287 (dolist (header nnslashdot-headers)
288 (nnheader-insert-nov (cdr header))))
291 (deffoo nnslashdot-request-group (group &optional server dont-check)
292 (nnslashdot-possibly-change-server nil server)
293 (let ((elem (assoc group nnslashdot-groups)))
296 (nnheader-report 'nnslashdot "Group does not exist"))
298 (nnheader-report 'nnslashdot "Opened group %s" group)
300 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
301 (prin1-to-string group))))))
303 (deffoo nnslashdot-close-group (group &optional server)
304 (nnslashdot-possibly-change-server group server)
305 (when (gnus-buffer-live-p nnslashdot-buffer)
307 (set-buffer nnslashdot-buffer)
308 (kill-buffer nnslashdot-buffer)))
311 (deffoo nnslashdot-request-article (article &optional group server buffer)
312 (nnslashdot-possibly-change-server group server)
316 (set-buffer nnslashdot-buffer)
317 (let ((case-fold-search t))
318 (goto-char (point-min))
319 (when (and (stringp article)
320 (string-match "%\\([0-9]+\\)@" article))
321 (setq article (string-to-number (match-string 1 article))))
322 (when (numberp article)
325 (re-search-forward "Posted by *<[^>]+>[^>]*<[^>]+> *on ")
326 (search-forward "<BR>")
332 "<p>.*A href=http://slashdot\\.org/article")
333 (match-beginning 0)))))
334 (search-forward (format "<a name=\"%d\">" (1- article)))
337 (re-search-forward "<td[^>]+>")
338 (search-forward "</td>")))))))
339 (search-failed (nnslashdot-lose why)))
343 (set-buffer (or buffer nntp-server-buffer))
346 (goto-char (point-min))
347 (while (search-forward "<br><br>" nil t)
348 (replace-match "<p>" t t))
349 (goto-char (point-min))
350 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
351 (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
353 (let ((header (cdr (assq article nnslashdot-headers))))
354 (nnheader-insert-header header))
355 (nnheader-report 'nnslashdot "Fetched article %s" article)
356 (cons group article)))))
358 (deffoo nnslashdot-close-server (&optional server)
359 (when (and (nnslashdot-server-opened server)
360 (gnus-buffer-live-p nnslashdot-buffer))
362 (set-buffer nnslashdot-buffer)
363 (kill-buffer nnslashdot-buffer)))
364 (nnoo-close-server 'nnslashdot server))
366 (deffoo nnslashdot-request-list (&optional server)
367 (nnslashdot-possibly-change-server nil server)
369 sid elem description articles gname)
371 ;; First we do the Ultramode to get info on all the latest groups.
373 (mm-with-unibyte-buffer
374 (nnweb-insert "http://slashdot.org/slashdot.xml" t)
375 (goto-char (point-min))
376 (while (search-forward "<story>" nil t)
377 (narrow-to-region (point) (search-forward "</story>"))
378 (goto-char (point-min))
379 (re-search-forward "<title>\\([^<]+\\)</title>")
380 (setq description (nnweb-decode-entities-string (match-string 1)))
381 (re-search-forward "<url>\\([^<]+\\)</url>")
382 (setq sid (match-string 1))
383 (string-match "/\\([0-9/]+\\).shtml" sid)
384 (setq sid (match-string 1 sid))
385 (re-search-forward "<comments>\\([^<]+\\)</comments>")
386 (setq articles (string-to-number (match-string 1)))
387 (setq gname (concat description " (" sid ")"))
388 (if (setq elem (assoc gname nnslashdot-groups))
389 (setcar (cdr elem) articles)
390 (push (list gname articles sid) nnslashdot-groups))
391 (goto-char (point-max))
393 ;; Then do the older groups.
394 (while (> (- nnslashdot-group-number number) 0)
395 (mm-with-unibyte-buffer
396 (let ((case-fold-search t))
397 (nnweb-insert (format nnslashdot-active-url number) t)
398 (goto-char (point-min))
399 (while (re-search-forward
400 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
401 (setq sid (match-string 1)
402 description (nnweb-decode-entities-string (match-string 2)))
404 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
405 (setq articles (string-to-number (match-string 1))))
406 (setq gname (concat description " (" sid ")"))
407 (if (setq elem (assoc gname nnslashdot-groups))
408 (setcar (cdr elem) articles)
409 (push (list gname articles sid) nnslashdot-groups)))))
411 (search-failed (nnslashdot-lose why)))
412 (nnslashdot-write-groups)
413 (nnslashdot-generate-active)
416 (deffoo nnslashdot-request-newgroups (date &optional server)
417 (nnslashdot-possibly-change-server nil server)
418 (nnslashdot-generate-active)
421 (deffoo nnslashdot-request-post (&optional server)
422 (nnslashdot-possibly-change-server nil server)
423 (let ((sid (message-fetch-field "newsgroups"))
424 (subject (message-fetch-field "subject"))
425 (references (car (last (split-string
426 (message-fetch-field "references")))))
428 (string-match "%\\([0-9]+\\)@slashdot" references)
429 (setq pid (match-string 1 references))
431 (narrow-to-region (point) (progn (message-goto-signature) (point)))
432 (goto-char (point-min))
434 (if (looking-at "> ")
436 (delete-region (point) (+ (point) 2))
438 (insert "<blockquote>\n"))
441 (insert "</blockquote>\n")
444 (goto-char (point-min))
445 (while (re-search-forward "^ *\n" nil t)
446 (replace-match "<p>\n"))
448 (when (message-goto-signature)
456 (setq body (buffer-substring (point) (point-max)))
459 "http://slashdot.org/comments.pl"
462 ("rlogin" . "userlogin")
463 ("unickname" . ,nnslashdot-login-name)
464 ("upasswd" . ,nnslashdot-password)
465 ("postersubj" . ,subject)
467 ("postercomment" . ,body)
468 ("posttype" . "html")))))
470 (deffoo nnslashdot-request-delete-group (group &optional force server)
471 (nnslashdot-possibly-change-server group server)
472 (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
474 (nnslashdot-write-groups))
476 (nnoo-define-skeleton nnslashdot)
478 ;;; Internal functions
480 (defun nnslashdot-possibly-change-server (&optional group server)
481 (nnslashdot-init server)
483 (not (nnslashdot-server-opened server)))
484 (nnslashdot-open-server server))
485 (unless nnslashdot-groups
486 (nnslashdot-read-groups)))
488 (defun nnslashdot-read-groups ()
489 (let ((file (expand-file-name "groups" nnslashdot-directory)))
490 (when (file-exists-p file)
491 (mm-with-unibyte-buffer
492 (insert-file-contents file)
493 (goto-char (point-min))
494 (setq nnslashdot-groups (read (current-buffer)))))))
496 (defun nnslashdot-write-groups ()
497 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
498 (prin1 nnslashdot-groups (current-buffer))))
500 (defun nnslashdot-init (server)
501 "Initialize buffers and such."
502 (unless (file-exists-p nnslashdot-directory)
503 (gnus-make-directory nnslashdot-directory))
504 (unless (gnus-buffer-live-p nnslashdot-buffer)
505 (setq nnslashdot-buffer
507 (nnheader-set-temp-buffer
508 (format " *nnslashdot %s*" server))))))
510 (defun nnslashdot-date-to-date (sdate)
512 (let ((elem (delete "" (split-string sdate))))
513 (concat (substring (nth 0 elem) 0 3) " "
514 (substring (nth 1 elem) 0 3) " "
515 (substring (nth 2 elem) 0 2) " "
516 (substring (nth 3 elem) 1 6) " "
517 (format-time-string "%Y") " "
521 (defun nnslashdot-generate-active ()
523 (set-buffer nntp-server-buffer)
525 (dolist (elem nnslashdot-groups)
526 (insert (prin1-to-string (car elem))
527 " " (number-to-string (cadr elem)) " 1 y\n"))))
529 (defun nnslashdot-lose (why)
530 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
532 (provide 'nnslashdot)
534 ;;; nnslashdot.el ends here