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))
243 (when (string-match "^Re: *" subject)
244 (setq subject (concat "Re: " (substring subject (match-end 0)))))
247 "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
248 (setq from (concat (match-string 1) " <" (match-string 2) ">"))
249 (looking-at "by \\(.+\\) on ")
250 (setq from (match-string 1)))
251 (goto-char (- (match-end 0) 5))
252 (search-forward " on ")
254 (nnslashdot-date-to-date
255 (buffer-substring (point) (progn (end-of-line) (point)))))
256 (setq lines (/ (abs (- (search-forward "<td ")
257 (search-forward "</td>")))
261 (if (looking-at ".*cid=\\([0-9]+\\)")
268 (make-full-mail-header
269 (1+ article) (concat subject " (" score ")")
272 (number-to-string (1+ article))
276 (number-to-string (1+ (string-to-number parent)))
281 (setq nnslashdot-headers
282 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
284 (set-buffer nntp-server-buffer)
286 (dolist (header nnslashdot-headers)
287 (nnheader-insert-nov (cdr header))))
290 (deffoo nnslashdot-request-group (group &optional server dont-check)
291 (nnslashdot-possibly-change-server nil server)
292 (let ((elem (assoc group nnslashdot-groups)))
295 (nnheader-report 'nnslashdot "Group does not exist"))
297 (nnheader-report 'nnslashdot "Opened group %s" group)
299 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
300 (prin1-to-string group))))))
302 (deffoo nnslashdot-close-group (group &optional server)
303 (nnslashdot-possibly-change-server group server)
304 (when (gnus-buffer-live-p nnslashdot-buffer)
306 (set-buffer nnslashdot-buffer)
307 (kill-buffer nnslashdot-buffer)))
310 (deffoo nnslashdot-request-article (article &optional group server buffer)
311 (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>")))))))
339 (set-buffer (or buffer nntp-server-buffer))
342 (goto-char (point-min))
343 (while (search-forward "<br><br>" nil t)
344 (replace-match "<p>" t t))
345 (goto-char (point-min))
346 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
347 (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
349 (let ((header (cdr (assq article nnslashdot-headers))))
350 (nnheader-insert-header header))
351 (nnheader-report 'nnslashdot "Fetched article %s" article)
352 (cons group article)))))
354 (deffoo nnslashdot-close-server (&optional server)
355 (when (and (nnslashdot-server-opened server)
356 (gnus-buffer-live-p nnslashdot-buffer))
358 (set-buffer nnslashdot-buffer)
359 (kill-buffer nnslashdot-buffer)))
360 (nnoo-close-server 'nnslashdot server))
362 (deffoo nnslashdot-request-list (&optional server)
363 (nnslashdot-possibly-change-server nil server)
365 sid elem description articles gname)
366 ;; First we do the Ultramode to get info on all the latest groups.
368 (nnweb-insert "http://slashdot.org/slashdot.xml")
369 (goto-char (point-min))
370 (while (search-forward "<story>" nil t)
371 (narrow-to-region (point) (search-forward "</story>"))
372 (goto-char (point-min))
373 (re-search-forward "<title>\\([^<]+\\)</title>")
374 (setq description (match-string 1))
375 (re-search-forward "<url>\\([^<]+\\)</url>")
376 (setq sid (match-string 1))
377 (string-match "/\\([0-9/]+\\).shtml" sid)
378 (setq sid (match-string 1 sid))
379 (re-search-forward "<comments>\\([^<]+\\)</comments>")
380 (setq articles (string-to-number (match-string 1)))
381 (setq gname (concat description " (" sid ")"))
382 (if (setq elem (assoc gname nnslashdot-groups))
383 (setcar (cdr elem) articles)
384 (push (list gname articles sid) nnslashdot-groups))
385 (goto-char (point-max))
387 ;; Then do the older groups.
388 (while (> (- nnslashdot-group-number number) 0)
390 (let ((case-fold-search t))
391 (nnweb-insert (format nnslashdot-active-url number))
392 (goto-char (point-min))
393 (while (re-search-forward
394 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
395 (setq sid (match-string 1)
396 description (match-string 2))
398 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
399 (setq articles (string-to-number (match-string 1))))
400 (setq gname (concat description " (" sid ")"))
401 (if (setq elem (assoc gname nnslashdot-groups))
402 (setcar (cdr elem) articles)
403 (push (list gname articles sid) nnslashdot-groups)))))
405 (nnslashdot-write-groups)
406 (nnslashdot-generate-active)
409 (deffoo nnslashdot-request-newgroups (date &optional server)
410 (nnslashdot-possibly-change-server nil server)
411 (nnslashdot-generate-active)
414 (deffoo nnslashdot-request-post (&optional server)
415 (nnslashdot-possibly-change-server nil server)
416 (let ((sid (message-fetch-field "newsgroups"))
417 (subject (message-fetch-field "subject"))
418 (references (car (last (split-string
419 (message-fetch-field "references")))))
421 (string-match "%\\([0-9]+\\)@slashdot" references)
422 (setq pid (match-string 1 references))
424 (narrow-to-region (point) (progn (message-goto-signature) (point)))
425 (goto-char (point-min))
427 (if (looking-at "> ")
429 (delete-region (point) (+ (point) 2))
431 (insert "<blockquote>\n"))
434 (insert "</blockquote>\n")
438 (when (message-goto-signature)
446 (setq body (buffer-substring (point) (point-max)))
449 "http://slashdot.org/comments.pl"
452 ("rlogin" . "userlogin")
453 ("unickname" . ,nnslashdot-login-name)
454 ("upasswd" . ,nnslashdot-password)
455 ("postersubj" . ,subject)
457 ("postercomment" . ,body)
458 ("posttype" . "html")))))
460 (nnoo-define-skeleton nnslashdot)
462 ;;; Internal functions
464 (defun nnslashdot-possibly-change-server (&optional group server)
465 (nnslashdot-init server)
467 (not (nnslashdot-server-opened server)))
468 (nnslashdot-open-server server))
469 (unless nnslashdot-groups
470 (nnslashdot-read-groups)))
472 (defun nnslashdot-read-groups ()
473 (let ((file (expand-file-name "groups" nnslashdot-directory)))
474 (when (file-exists-p file)
476 (insert-file-contents file)
477 (goto-char (point-min))
478 (setq nnslashdot-groups (read (current-buffer)))))))
480 (defun nnslashdot-write-groups ()
481 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
482 (prin1 nnslashdot-groups (current-buffer))))
484 (defun nnslashdot-init (server)
485 "Initialize buffers and such."
486 (unless (file-exists-p nnslashdot-directory)
487 (gnus-make-directory nnslashdot-directory))
488 (unless (gnus-buffer-live-p nnslashdot-buffer)
489 (setq nnslashdot-buffer
491 (nnheader-set-temp-buffer
492 (format " *nnslashdot %s*" server))))))
494 (defun nnslashdot-date-to-date (sdate)
495 (let ((elem (delete "" (split-string sdate))))
496 (concat (substring (nth 0 elem) 0 3) " "
497 (substring (nth 1 elem) 0 3) " "
498 (substring (nth 2 elem) 0 2) " "
499 (substring (nth 3 elem) 1 6) " "
500 (format-time-string "%Y") " "
503 (defun nnslashdot-generate-active ()
505 (set-buffer nntp-server-buffer)
507 (dolist (elem nnslashdot-groups)
508 (insert (prin1-to-string (car elem))
509 " " (number-to-string (cadr elem)) " 1 y\n"))))
511 (provide 'nnslashdot)
513 ;;; nnslashdot.el ends here