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))
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.pl")))
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))
133 (setq first-comments nil)
134 (goto-char (point-max))
135 (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
136 (setq s (string-to-number (match-string 1)))
137 (unless (memq s startats)
139 (setq startats (sort startats '<)))
141 (while (re-search-forward
142 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
144 (setq article (string-to-number (match-string 1))
145 subject (match-string 2)
146 score (match-string 3))
147 (when (string-match "^Re: *" subject)
148 (setq subject (concat "Re: " (substring subject (match-end 0)))))
149 (setq subject (nnweb-decode-entities-string subject))
152 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
153 (setq from (concat (nnweb-decode-entities-string (match-string 1))
154 " <" (match-string 2) ">"))
155 (looking-at "by \\(.+\\) on ")
156 (setq from (nnweb-decode-entities-string (match-string 1))))
157 (goto-char (- (match-end 0) 5))
158 (search-forward " on ")
160 (nnslashdot-date-to-date
161 (buffer-substring (point) (progn (end-of-line) (point)))))
162 (setq lines (/ (abs (- (search-forward "<td ")
163 (search-forward "</td>")))
167 (if (looking-at ".*cid=\\([0-9]+\\)")
174 (make-full-mail-header
176 (concat subject " (" score ")")
179 (number-to-string (1+ article))
183 (number-to-string (1+ (string-to-number parent)))
188 (setq nnslashdot-headers (sort headers 'car-less-than-car))
190 (set-buffer nntp-server-buffer)
192 (dolist (header nnslashdot-headers)
193 (nnheader-insert-nov (cdr header))))
196 (deffoo nnslashdot-sane-retrieve-headers (articles group)
197 (let ((last (car (last articles)))
199 (start (max (1- (car articles)) 1))
200 (sid (caddr (assoc group nnslashdot-groups)))
201 headers article subject score from date lines parent point)
203 (set-buffer nnslashdot-buffer)
206 (nnweb-insert (format nnslashdot-article-url sid))
207 (goto-char (point-min))
208 (search-forward "Posted by ")
209 (when (looking-at "<a[^>]+>\\([^<]+\\)")
210 (setq from (nnweb-decode-entities-string (match-string 1))))
211 (search-forward " on ")
212 (setq date (nnslashdot-date-to-date
213 (buffer-substring (point) (1- (search-forward "<")))))
215 (setq lines (count-lines (point)
217 "A href=http://slashdot.org/article.pl")))
221 (make-full-mail-header
222 1 group from date (concat "<" sid "%1@slashdot>")
225 (while (or (not article)
229 (setq start (1+ article)))
230 (setq point (goto-char (point-max)))
232 (format nnslashdot-comments-url sid nnslashdot-threshold 4 start))
234 (while (re-search-forward
235 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
237 (setq article (string-to-number (match-string 1))
238 subject (match-string 2)
239 score (match-string 3))
240 (when (string-match "^Re: *" subject)
241 (setq subject (concat "Re: " (substring subject (match-end 0)))))
242 (setq subject (nnweb-decode-entities-string subject))
245 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
246 (setq from (concat (nnweb-decode-entities-string (match-string 1))
247 " <" (match-string 2) ">"))
248 (looking-at "by \\(.+\\) on ")
249 (setq from (nnweb-decode-entities-string (match-string 1))))
250 (goto-char (- (match-end 0) 5))
251 (search-forward " on ")
253 (nnslashdot-date-to-date
254 (buffer-substring (point) (progn (end-of-line) (point)))))
255 (setq lines (/ (abs (- (search-forward "<td ")
256 (search-forward "</td>")))
260 (if (looking-at ".*cid=\\([0-9]+\\)")
267 (make-full-mail-header
268 (1+ article) (concat subject " (" score ")")
271 (number-to-string (1+ article))
275 (number-to-string (1+ (string-to-number parent)))
280 (setq nnslashdot-headers
281 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
283 (set-buffer nntp-server-buffer)
285 (dolist (header nnslashdot-headers)
286 (nnheader-insert-nov (cdr header))))
289 (deffoo nnslashdot-request-group (group &optional server dont-check)
290 (nnslashdot-possibly-change-server nil server)
291 (let ((elem (assoc group nnslashdot-groups)))
294 (nnheader-report 'nnslashdot "Group does not exist"))
296 (nnheader-report 'nnslashdot "Opened group %s" group)
298 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
299 (prin1-to-string group))))))
301 (deffoo nnslashdot-close-group (group &optional server)
302 (nnslashdot-possibly-change-server group server)
303 (when (gnus-buffer-live-p nnslashdot-buffer)
305 (set-buffer nnslashdot-buffer)
306 (kill-buffer nnslashdot-buffer)))
309 (deffoo nnslashdot-request-article (article &optional group server buffer)
310 (nnslashdot-possibly-change-server group server)
314 (set-buffer nnslashdot-buffer)
315 (let ((case-fold-search t))
316 (goto-char (point-min))
317 (when (and (stringp article)
318 (string-match "%\\([0-9]+\\)@" article))
319 (setq article (string-to-number (match-string 1 article))))
320 (when (numberp article)
323 (re-search-forward "Posted by .* on ")
330 "<p>.*A href=http://slashdot.org/article.pl")
331 (match-beginning 0)))))
332 (search-forward (format "<a name=\"%d\">" (1- article)))
335 (re-search-forward "<td[^>]+>")
336 (search-forward "</td>")))))))
337 (search-failed (nnslashdot-lose why)))
341 (set-buffer (or buffer nntp-server-buffer))
344 (goto-char (point-min))
345 (while (search-forward "<br><br>" nil t)
346 (replace-match "<p>" t t))
347 (goto-char (point-min))
348 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
349 (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
351 (let ((header (cdr (assq article nnslashdot-headers))))
352 (nnheader-insert-header header))
353 (nnheader-report 'nnslashdot "Fetched article %s" article)
354 (cons group article)))))
356 (deffoo nnslashdot-close-server (&optional server)
357 (when (and (nnslashdot-server-opened server)
358 (gnus-buffer-live-p nnslashdot-buffer))
360 (set-buffer nnslashdot-buffer)
361 (kill-buffer nnslashdot-buffer)))
362 (nnoo-close-server 'nnslashdot server))
364 (deffoo nnslashdot-request-list (&optional server)
365 (nnslashdot-possibly-change-server nil server)
367 sid elem description articles gname)
369 ;; First we do the Ultramode to get info on all the latest groups.
371 (mm-with-unibyte-buffer
372 (nnweb-insert "http://slashdot.org/slashdot.xml")
373 (goto-char (point-min))
374 (while (search-forward "<story>" nil t)
375 (narrow-to-region (point) (search-forward "</story>"))
376 (goto-char (point-min))
377 (re-search-forward "<title>\\([^<]+\\)</title>")
378 (setq description (nnweb-decode-entities-string (match-string 1)))
379 (re-search-forward "<url>\\([^<]+\\)</url>")
380 (setq sid (match-string 1))
381 (string-match "/\\([0-9/]+\\).shtml" sid)
382 (setq sid (match-string 1 sid))
383 (re-search-forward "<comments>\\([^<]+\\)</comments>")
384 (setq articles (string-to-number (match-string 1)))
385 (setq gname (concat description " (" sid ")"))
386 (if (setq elem (assoc gname nnslashdot-groups))
387 (setcar (cdr elem) articles)
388 (push (list gname articles sid) nnslashdot-groups))
389 (goto-char (point-max))
391 ;; Then do the older groups.
392 (while (> (- nnslashdot-group-number number) 0)
393 (mm-with-unibyte-buffer
394 (let ((case-fold-search t))
395 (nnweb-insert (format nnslashdot-active-url number))
396 (goto-char (point-min))
397 (while (re-search-forward
398 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
399 (setq sid (match-string 1)
400 description (nnweb-decode-entities-string (match-string 2)))
402 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
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)))))
409 (search-failed (nnslashdot-lose why)))
410 (nnslashdot-write-groups)
411 (nnslashdot-generate-active)
414 (deffoo nnslashdot-request-newgroups (date &optional server)
415 (nnslashdot-possibly-change-server nil server)
416 (nnslashdot-generate-active)
419 (deffoo nnslashdot-request-post (&optional server)
420 (nnslashdot-possibly-change-server nil server)
421 (let ((sid (message-fetch-field "newsgroups"))
422 (subject (message-fetch-field "subject"))
423 (references (car (last (split-string
424 (message-fetch-field "references")))))
426 (string-match "%\\([0-9]+\\)@slashdot" references)
427 (setq pid (match-string 1 references))
429 (narrow-to-region (point) (progn (message-goto-signature) (point)))
430 (goto-char (point-min))
432 (if (looking-at "> ")
434 (delete-region (point) (+ (point) 2))
436 (insert "<blockquote>\n"))
439 (insert "</blockquote>\n")
442 (goto-char (point-min))
443 (while (re-search-forward "^ *\n" nil t)
444 (replace-match "<p>\n"))
446 (when (message-goto-signature)
454 (setq body (buffer-substring (point) (point-max)))
457 "http://slashdot.org/comments.pl"
460 ("rlogin" . "userlogin")
461 ("unickname" . ,nnslashdot-login-name)
462 ("upasswd" . ,nnslashdot-password)
463 ("postersubj" . ,subject)
465 ("postercomment" . ,body)
466 ("posttype" . "html")))))
468 (deffoo nnslashdot-request-delete-group (group &optional force server)
469 (nnslashdot-possibly-change-server group server)
470 (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
472 (nnslashdot-write-groups))
474 (nnoo-define-skeleton nnslashdot)
476 ;;; Internal functions
478 (defun nnslashdot-possibly-change-server (&optional group server)
479 (nnslashdot-init server)
481 (not (nnslashdot-server-opened server)))
482 (nnslashdot-open-server server))
483 (unless nnslashdot-groups
484 (nnslashdot-read-groups)))
486 (defun nnslashdot-read-groups ()
487 (let ((file (expand-file-name "groups" nnslashdot-directory)))
488 (when (file-exists-p file)
489 (mm-with-unibyte-buffer
490 (insert-file-contents file)
491 (goto-char (point-min))
492 (setq nnslashdot-groups (read (current-buffer)))))))
494 (defun nnslashdot-write-groups ()
495 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
496 (prin1 nnslashdot-groups (current-buffer))))
498 (defun nnslashdot-init (server)
499 "Initialize buffers and such."
500 (unless (file-exists-p nnslashdot-directory)
501 (gnus-make-directory nnslashdot-directory))
502 (unless (gnus-buffer-live-p nnslashdot-buffer)
503 (setq nnslashdot-buffer
505 (nnheader-set-temp-buffer
506 (format " *nnslashdot %s*" server))))))
508 (defun nnslashdot-date-to-date (sdate)
509 (let ((elem (delete "" (split-string sdate))))
510 (concat (substring (nth 0 elem) 0 3) " "
511 (substring (nth 1 elem) 0 3) " "
512 (substring (nth 2 elem) 0 2) " "
513 (substring (nth 3 elem) 1 6) " "
514 (format-time-string "%Y") " "
517 (defun nnslashdot-generate-active ()
519 (set-buffer nntp-server-buffer)
521 (dolist (elem nnslashdot-groups)
522 (insert (prin1-to-string (car elem))
523 " " (number-to-string (cadr elem)) " 1 y\n"))))
525 (defun nnslashdot-lose (why)
526 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
528 (provide 'nnslashdot)
530 ;;; nnslashdot.el ends here