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))
45 ;; Report failure to find w3 at load time if appropriate.
51 (nnoo-declare nnslashdot)
53 (defvoo nnslashdot-directory (nnheader-concat gnus-directory "slashdot/")
54 "Where nnslashdot will save its files.")
56 (defvoo nnslashdot-active-url "http://slashdot.org/search.pl?section=&min=%d"
57 "Where nnslashdot will fetch the active file from.")
59 (defvoo nnslashdot-comments-url "http://slashdot.org/comments.pl?sid=%s&threshold=%d&commentsort=%d&mode=flat&startat=%d"
60 "Where nnslashdot will fetch comments from.")
62 (defvoo nnslashdot-article-url
63 "http://slashdot.org/article.pl?sid=%s&mode=nocomment"
64 "Where nnslashdot will fetch the article from.")
66 (defvoo nnslashdot-threshold -1
67 "The article threshold.")
69 (defvoo nnslashdot-threaded t
70 "Whether the nnslashdot groups should be threaded or not.")
72 (defvoo nnslashdot-group-number 0
73 "The number of non-fresh groups to keep updated.")
75 (defvoo nnslashdot-login-name ""
76 "The login name to use when posting.")
78 (defvoo nnslashdot-password ""
79 "The password to use when posting.")
81 ;;; Internal variables
83 (defvar nnslashdot-groups nil)
84 (defvar nnslashdot-buffer nil)
85 (defvar nnslashdot-headers nil)
87 ;;; Interface functions
89 (nnoo-define-basics nnslashdot)
91 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
92 (nnslashdot-possibly-change-server group server)
94 (unless gnus-nov-is-evil
95 (if nnslashdot-threaded
96 (nnslashdot-threaded-retrieve-headers articles group)
97 (nnslashdot-sane-retrieve-headers articles group)))
98 (search-failed (nnslashdot-lose why))))
100 (deffoo nnslashdot-threaded-retrieve-headers (articles group)
101 (let ((last (car (last articles)))
104 (sid (caddr (assoc group nnslashdot-groups)))
107 headers article subject score from date lines parent point s)
109 (set-buffer nnslashdot-buffer)
110 (let ((case-fold-search t))
113 (nnweb-insert (format nnslashdot-article-url sid))
114 (goto-char (point-min))
115 (search-forward "Posted by ")
116 (when (looking-at "<a[^>]+>\\([^<]+\\)")
117 (setq from (match-string 1)))
118 (search-forward " on ")
119 (setq date (nnslashdot-date-to-date
120 (buffer-substring (point) (1- (search-forward "<")))))
122 (setq lines (count-lines
125 "A href=http://slashdot.org/article.pl")))
129 (make-full-mail-header
130 1 group from date (concat "<" sid "%1@slashdot>")
133 (while (and (setq start (pop startats))
135 (setq point (goto-char (point-max)))
137 (format nnslashdot-comments-url sid 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>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
150 (setq article (string-to-number (match-string 1))
151 subject (match-string 2)
152 score (match-string 3))
153 (when (string-match "^Re: *" subject)
154 (setq subject (concat "Re: " (substring subject (match-end 0)))))
157 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
158 (setq from (concat (match-string 1)
159 " <" (match-string 2) ">"))
160 (looking-at "by \\(.+\\) on ")
161 (setq from (match-string 1)))
162 (goto-char (- (match-end 0) 5))
163 (search-forward " on ")
165 (nnslashdot-date-to-date
166 (buffer-substring (point) (progn (end-of-line) (point)))))
167 (setq lines (/ (abs (- (search-forward "<td ")
168 (search-forward "</td>")))
172 (if (looking-at ".*cid=\\([0-9]+\\)")
179 (make-full-mail-header
181 (concat subject " (" score ")")
184 (number-to-string (1+ article))
188 (number-to-string (1+ (string-to-number parent)))
193 (setq nnslashdot-headers (sort headers 'car-less-than-car))
195 (set-buffer nntp-server-buffer)
197 (dolist (header nnslashdot-headers)
198 (nnheader-insert-nov (cdr header))))
201 (deffoo nnslashdot-sane-retrieve-headers (articles group)
202 (let ((last (car (last articles)))
204 (start (max (1- (car articles)) 1))
205 (sid (caddr (assoc group nnslashdot-groups)))
206 headers article subject score from date lines parent point)
208 (set-buffer nnslashdot-buffer)
211 (nnweb-insert (format nnslashdot-article-url sid))
212 (goto-char (point-min))
213 (search-forward "Posted by ")
214 (when (looking-at "<a[^>]+>\\([^<]+\\)")
215 (setq from (match-string 1)))
216 (search-forward " on ")
217 (setq date (nnslashdot-date-to-date
218 (buffer-substring (point) (1- (search-forward "<")))))
220 (setq lines (count-lines (point)
222 "A href=http://slashdot.org/article.pl")))
226 (make-full-mail-header
227 1 group from date (concat "<" sid "%1@slashdot>")
230 (while (or (not article)
234 (setq start (1+ article)))
235 (setq point (goto-char (point-max)))
237 (format nnslashdot-comments-url sid nnslashdot-threshold 4 start))
239 (while (re-search-forward
240 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
242 (setq article (string-to-number (match-string 1))
243 subject (match-string 2)
244 score (match-string 3))
245 (when (string-match "^Re: *" subject)
246 (setq subject (concat "Re: " (substring subject (match-end 0)))))
249 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
250 (setq from (concat (match-string 1) " <" (match-string 2) ">"))
251 (looking-at "by \\(.+\\) on ")
252 (setq from (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 ")
333 "<p>.*A href=http://slashdot.org/article.pl")
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 (search-forward "<br><br>" 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.
373 (mm-with-unibyte-buffer
374 (nnweb-insert "http://slashdot.org/slashdot.xml")
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 (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))
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 (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 (nnoo-define-skeleton nnslashdot)
472 ;;; Internal functions
474 (defun nnslashdot-possibly-change-server (&optional group server)
475 (nnslashdot-init server)
477 (not (nnslashdot-server-opened server)))
478 (nnslashdot-open-server server))
479 (unless nnslashdot-groups
480 (nnslashdot-read-groups)))
482 (defun nnslashdot-read-groups ()
483 (let ((file (expand-file-name "groups" nnslashdot-directory)))
484 (when (file-exists-p file)
485 (mm-with-unibyte-buffer
486 (insert-file-contents file)
487 (goto-char (point-min))
488 (setq nnslashdot-groups (read (current-buffer)))))))
490 (defun nnslashdot-write-groups ()
491 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
492 (prin1 nnslashdot-groups (current-buffer))))
494 (defun nnslashdot-init (server)
495 "Initialize buffers and such."
496 (unless (file-exists-p nnslashdot-directory)
497 (gnus-make-directory nnslashdot-directory))
498 (unless (gnus-buffer-live-p nnslashdot-buffer)
499 (setq nnslashdot-buffer
501 (nnheader-set-temp-buffer
502 (format " *nnslashdot %s*" server))))))
504 (defun nnslashdot-date-to-date (sdate)
505 (let ((elem (delete "" (split-string sdate))))
506 (concat (substring (nth 0 elem) 0 3) " "
507 (substring (nth 1 elem) 0 3) " "
508 (substring (nth 2 elem) 0 2) " "
509 (substring (nth 3 elem) 1 6) " "
510 (format-time-string "%Y") " "
513 (defun nnslashdot-generate-active ()
515 (set-buffer nntp-server-buffer)
517 (dolist (elem nnslashdot-groups)
518 (insert (prin1-to-string (car elem))
519 " " (number-to-string (cadr elem)) " 1 y\n"))))
521 (defun nnslashdot-lose (why)
522 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
524 (provide 'nnslashdot)
526 ;;; nnslashdot.el ends here