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)
93 (unless gnus-nov-is-evil
94 (if nnslashdot-threaded
95 (nnslashdot-threaded-retrieve-headers articles group)
96 (nnslashdot-sane-retrieve-headers articles group))))
98 (deffoo nnslashdot-threaded-retrieve-headers (articles group)
99 (let ((last (car (last articles)))
102 (sid (caddr (assoc group nnslashdot-groups)))
105 headers article subject score from date lines parent point s)
107 (set-buffer nnslashdot-buffer)
108 (let ((case-fold-search t))
111 (nnweb-insert (format nnslashdot-article-url sid))
112 (goto-char (point-min))
113 (search-forward "Posted by ")
114 (when (looking-at "<a[^>]+>\\([^<]+\\)")
115 (setq from (match-string 1)))
116 (search-forward " on ")
117 (setq date (nnslashdot-date-to-date
118 (buffer-substring (point) (1- (search-forward "<")))))
120 (setq lines (count-lines
123 "A href=http://slashdot.org/article.pl")))
127 (make-full-mail-header
128 1 group from date (concat "<" sid "%1@slashdot>")
131 (while (and (setq start (pop startats))
133 (setq point (goto-char (point-max)))
135 (format nnslashdot-comments-url sid nnslashdot-threshold 0 start))
137 (setq first-comments nil)
138 (goto-char (point-max))
139 (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
140 (setq s (string-to-number (match-string 1)))
141 (unless (memq s startats)
143 (setq startats (sort startats '<)))
145 (while (re-search-forward
146 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
148 (setq article (string-to-number (match-string 1))
149 subject (match-string 2)
150 score (match-string 3))
151 (when (string-match "^Re: *" subject)
152 (setq subject (concat "Re: " (substring subject (match-end 0)))))
155 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
156 (setq from (concat (match-string 1)
157 " <" (match-string 2) ">"))
158 (looking-at "by \\(.+\\) on ")
159 (setq from (match-string 1)))
160 (goto-char (- (match-end 0) 5))
161 (search-forward " on ")
163 (nnslashdot-date-to-date
164 (buffer-substring (point) (progn (end-of-line) (point)))))
165 (setq lines (/ (abs (- (search-forward "<td ")
166 (search-forward "</td>")))
170 (if (looking-at ".*cid=\\([0-9]+\\)")
177 (make-full-mail-header
179 (concat subject " (" score ")")
182 (number-to-string (1+ article))
186 (number-to-string (1+ (string-to-number parent)))
191 (setq nnslashdot-headers (sort headers 'car-less-than-car))
193 (set-buffer nntp-server-buffer)
195 (dolist (header nnslashdot-headers)
196 (nnheader-insert-nov (cdr header))))
199 (deffoo nnslashdot-sane-retrieve-headers (articles group)
200 (let ((last (car (last articles)))
202 (start (max (1- (car articles)) 1))
203 (sid (caddr (assoc group nnslashdot-groups)))
204 headers article subject score from date lines parent point)
206 (set-buffer nnslashdot-buffer)
209 (nnweb-insert (format nnslashdot-article-url sid))
210 (goto-char (point-min))
211 (search-forward "Posted by ")
212 (when (looking-at "<a[^>]+>\\([^<]+\\)")
213 (setq from (match-string 1)))
214 (search-forward " on ")
215 (setq date (nnslashdot-date-to-date
216 (buffer-substring (point) (1- (search-forward "<")))))
218 (setq lines (count-lines (point)
220 "A href=http://slashdot.org/article.pl")))
224 (make-full-mail-header
225 1 group from date (concat "<" sid "%1@slashdot>")
228 (while (or (not article)
232 (setq start (1+ article)))
233 (setq point (goto-char (point-max)))
235 (format nnslashdot-comments-url sid nnslashdot-threshold 4 start))
237 (while (re-search-forward
238 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score\\([^)]+\\))"
240 (setq article (string-to-number (match-string 1))
241 subject (match-string 2)
242 score (match-string 3))
245 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
246 (setq from (concat (match-string 1) " <" (match-string 2) ">"))
247 (looking-at "by \\(.+\\) on ")
248 (setq from (match-string 1)))
249 (goto-char (- (match-end 0) 5))
250 (search-forward " on ")
252 (nnslashdot-date-to-date
253 (buffer-substring (point) (progn (end-of-line) (point)))))
254 (setq lines (/ (abs (- (search-forward "<td ")
255 (search-forward "</td>")))
259 (if (looking-at ".*cid=\\([0-9]+\\)")
266 (make-full-mail-header
267 (1+ article) (concat subject " (" score ")")
270 (number-to-string (1+ article))
274 (number-to-string (1+ (string-to-number parent)))
279 (setq nnslashdot-headers
280 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
282 (set-buffer nntp-server-buffer)
284 (dolist (header nnslashdot-headers)
285 (nnheader-insert-nov (cdr header))))
288 (deffoo nnslashdot-request-group (group &optional server dont-check)
289 (nnslashdot-possibly-change-server nil server)
290 (let ((elem (assoc group nnslashdot-groups)))
293 (nnheader-report 'nnslashdot "Group does not exist"))
295 (nnheader-report 'nnslashdot "Opened group %s" group)
297 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
298 (prin1-to-string group))))))
300 (deffoo nnslashdot-close-group (group &optional server)
301 (nnslashdot-possibly-change-server group server)
302 (when (gnus-buffer-live-p nnslashdot-buffer)
304 (set-buffer nnslashdot-buffer)
305 (kill-buffer nnslashdot-buffer)))
308 (deffoo nnslashdot-request-article (article &optional group server buffer)
309 (nnslashdot-possibly-change-server group server)
312 (set-buffer nnslashdot-buffer)
313 (let ((case-fold-search t))
314 (goto-char (point-min))
315 (when (and (stringp article)
316 (string-match "%\\([0-9]+\\)@" article))
317 (setq article (string-to-number (match-string 1 article))))
318 (when (numberp article)
321 (re-search-forward "Posted by .* on ")
328 "<p>.*A href=http://slashdot.org/article.pl")
329 (match-beginning 0)))))
330 (search-forward (format "<a name=\"%d\">" (1- article)))
333 (re-search-forward "<td[^>]+>")
334 (search-forward "</td>")))))))
337 (set-buffer (or buffer nntp-server-buffer))
340 (goto-char (point-min))
341 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
342 (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
344 (let ((header (cdr (assq article nnslashdot-headers))))
345 (nnheader-insert-header header))
346 (nnheader-report 'nnslashdot "Fetched article %s" article)
347 (cons group article)))))
349 (deffoo nnslashdot-close-server (&optional server)
350 (when (and (nnslashdot-server-opened server)
351 (gnus-buffer-live-p nnslashdot-buffer))
353 (set-buffer nnslashdot-buffer)
354 (kill-buffer nnslashdot-buffer)))
355 (nnoo-close-server 'nnslashdot server))
357 (deffoo nnslashdot-request-list (&optional server)
358 (nnslashdot-possibly-change-server nil server)
360 sid elem description articles gname)
361 ;; First we do the Ultramode to get info on all the latest groups.
363 (nnweb-insert "http://slashdot.org/slashdot.xml")
364 (goto-char (point-min))
365 (while (search-forward "<story>" nil t)
366 (narrow-to-region (point) (search-forward "</story>"))
367 (goto-char (point-min))
368 (re-search-forward "<title>\\([^<]+\\)</title>")
369 (setq description (match-string 1))
370 (re-search-forward "<url>\\([^<]+\\)</url>")
371 (setq sid (match-string 1))
372 (string-match "/\\([0-9/]+\\).shtml" sid)
373 (setq sid (match-string 1 sid))
374 (re-search-forward "<comments>\\([^<]+\\)</comments>")
375 (setq articles (string-to-number (match-string 1)))
376 (setq gname (concat description " (" sid ")"))
377 (if (setq elem (assoc gname nnslashdot-groups))
378 (setcar (cdr elem) articles)
379 (push (list gname articles sid) nnslashdot-groups))
380 (goto-char (point-max))
382 ;; Then do the older groups.
383 (while (> (- nnslashdot-group-number number) 0)
385 (let ((case-fold-search t))
386 (nnweb-insert (format nnslashdot-active-url number))
387 (goto-char (point-min))
388 (while (re-search-forward
389 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
390 (setq sid (match-string 1)
391 description (match-string 2))
393 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
394 (setq articles (string-to-number (match-string 1))))
395 (setq gname (concat description " (" sid ")"))
396 (if (setq elem (assoc gname nnslashdot-groups))
397 (setcar (cdr elem) articles)
398 (push (list gname articles sid) nnslashdot-groups)))))
400 (nnslashdot-write-groups)
401 (nnslashdot-generate-active)
404 (deffoo nnslashdot-request-newgroups (date &optional server)
405 (nnslashdot-possibly-change-server nil server)
406 (nnslashdot-generate-active)
409 (deffoo nnslashdot-request-post (&optional server)
410 (nnslashdot-possibly-change-server nil server)
411 (let ((sid (message-fetch-field "newsgroups"))
412 (subject (message-fetch-field "subject"))
413 (references (car (last (split-string
414 (message-fetch-field "references")))))
416 (string-match "%\\([0-9]+\\)@slashdot" references)
417 (setq pid (match-string 1 references))
419 (narrow-to-region (point) (progn (message-goto-signature) (point)))
420 (goto-char (point-min))
422 (if (looking-at "> ")
424 (delete-region (point) (+ (point) 2))
426 (insert "<blockquote>\n"))
429 (insert "</blockquote>\n")
433 (when (message-goto-signature)
441 (setq body (buffer-substring (point) (point-max)))
444 "http://slashdot.org/comments.pl"
447 ("rlogin" . "userlogin")
448 ("unickname" . ,nnslashdot-login-name)
449 ("upasswd" . ,nnslashdot-password)
450 ("postersubj" . ,subject)
452 ("postercomment" . ,body)
453 ("posttype" . "html")))))
455 (nnoo-define-skeleton nnslashdot)
457 ;;; Internal functions
459 (defun nnslashdot-possibly-change-server (&optional group server)
460 (nnslashdot-init server)
462 (not (nnslashdot-server-opened server)))
463 (nnslashdot-open-server server))
464 (unless nnslashdot-groups
465 (nnslashdot-read-groups)))
467 (defun nnslashdot-read-groups ()
468 (let ((file (expand-file-name "groups" nnslashdot-directory)))
469 (when (file-exists-p file)
471 (insert-file-contents file)
472 (goto-char (point-min))
473 (setq nnslashdot-groups (read (current-buffer)))))))
475 (defun nnslashdot-write-groups ()
476 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
477 (prin1 nnslashdot-groups (current-buffer))))
479 (defun nnslashdot-init (server)
480 "Initialize buffers and such."
481 (unless (file-exists-p nnslashdot-directory)
482 (gnus-make-directory nnslashdot-directory))
483 (unless (gnus-buffer-live-p nnslashdot-buffer)
484 (setq nnslashdot-buffer
486 (nnheader-set-temp-buffer
487 (format " *nnslashdot %s*" server))))))
489 (defun nnslashdot-date-to-date (sdate)
490 (let ((elem (delete "" (split-string sdate))))
491 (concat (substring (nth 0 elem) 0 3) " "
492 (substring (nth 1 elem) 0 3) " "
493 (substring (nth 2 elem) 0 2) " "
494 (substring (nth 3 elem) 1 6) " "
495 (format-time-string "%Y") " "
498 (defun nnslashdot-generate-active ()
500 (set-buffer nntp-server-buffer)
502 (dolist (elem nnslashdot-groups)
503 (insert (prin1-to-string (car elem))
504 " " (number-to-string (cadr elem)) " 1 y\n"))))
506 (provide 'nnslashdot)
508 ;;; nnslashdot.el ends here