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-articles-url "http://slashdot.org/article.pl?sid=%s&threshold=%d&commentsort=4&mode=flat&startat=%d"
60 "Where nnslashdot will fetch articles from.")
62 (defvoo nnslashdot-threshold 0
63 "The article threshold.")
65 (defvoo nnslashdot-group-number 30
66 "The number of groups to keep updated.")
68 ;;; Internal variables
70 (defvar nnslashdot-groups nil)
71 (defvar nnslashdot-buffer nil)
72 (defvar nnslashdot-headers nil)
74 ;;; Interface functions
76 (nnoo-define-basics nnslashdot)
78 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
79 (nnslashdot-possibly-change-server group server)
80 (let ((last (car (last articles)))
82 (start (car articles))
83 headers article subject score from date lines parent point)
85 (set-buffer nnslashdot-buffer)
87 (while (or (not article)
89 (< (string-to-number article) last)))
91 (setq start (1+ (string-to-number article))))
92 (setq point (goto-char (point-max)))
93 (url-insert-file-contents
94 (format nnslashdot-articles-url group nnslashdot-threshold start))
95 (setq buffer-file-name nil)
97 (while (re-search-forward
98 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score\\([^)]+\\))"
100 (setq article (match-string 1)
101 subject (match-string 2)
102 score (match-string 3))
104 (if (looking-at "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
105 (setq from (concat (match-string 1) " <" (match-string 2) ">"))
106 (looking-at "by \\([^ ]+\\) ")
107 (setq from (match-string 1)))
108 (goto-char (match-end 0))
109 (search-forward "on ")
111 (nnslashdot-date-to-date
112 (buffer-substring (point) (progn (end-of-line) (point)))))
113 (setq lines (count-lines (search-forward "<td")
114 (search-forward "</td>")))
117 (if (looking-at ".*cid=\\([0-9]+\\)")
123 (string-to-number article)
124 (make-full-mail-header
125 (string-to-number article) (concat subject " (" score ")")
126 from date (concat "<" group "%" article "@slashdot>")
127 (if parent (concat "<" group "%" parent "@slashdot>") "")
130 (setq nnslashdot-headers (nreverse headers))
132 (set-buffer nntp-server-buffer)
134 (dolist (header nnslashdot-headers)
135 (nnheader-insert-nov (cdr header))))
138 (deffoo nnslashdot-request-group (group &optional server dont-check)
139 (nnslashdot-possibly-change-server nil server)
140 (let ((elem (assoc group nnslashdot-groups)))
143 (nnheader-report 'nnslashdot "Group does not exist"))
145 (nnheader-report 'nnslashdot "Opened group %s" group)
147 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem) group)))))
149 (deffoo nnslashdot-close-group (group &optional server)
150 (nnslashdot-possibly-change-server group server)
151 (when (gnus-buffer-live-p nnslashdot-buffer)
153 (set-buffer nnslashdot-buffer)
154 (kill-buffer nnslashdot-buffer)))
157 (deffoo nnslashdot-request-article (article &optional group server buffer)
158 (nnslashdot-possibly-change-server group server)
161 (set-buffer nnslashdot-buffer)
162 (goto-char (point-min))
163 (when (and (numberp article)
164 (search-forward (format "<a name=\"%d\">" article)))
167 (re-search-forward "<td[^>]+>")
168 (search-forward "</td>")))))
171 (set-buffer (or buffer nntp-server-buffer))
174 ;;(nnweb-remove-markup)
175 ;;(nnweb-decode-entities)
176 (goto-char (point-min))
177 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
178 (let ((header (cdr (assq article nnslashdot-headers))))
179 (nnheader-insert-header header))
180 (nnheader-report 'nnslashdot "Fetched article %s" article)
183 (deffoo nnslashdot-close-server (&optional server)
184 (when (and (nnslashdot-server-opened server)
185 (gnus-buffer-live-p nnslashdot-buffer))
187 (set-buffer nnslashdot-buffer)
188 (kill-buffer nnslashdot-buffer)))
189 (nnoo-close-server 'nnslashdot server))
191 (deffoo nnslashdot-request-list (&optional server)
192 (nnslashdot-possibly-change-server nil server)
193 (let ((case-fold-search t)
195 sid elem description articles)
196 (while (> (- nnslashdot-group-number number) 0)
198 (url-insert-file-contents (format nnslashdot-active-url number))
199 (setq buffer-file-name nil)
200 (goto-char (point-min))
201 (while (re-search-forward
202 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
203 (setq sid (match-string 1)
204 description (match-string 2))
206 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
207 (setq articles (string-to-number (match-string 1))))
208 (if (setq elem (assoc sid nnslashdot-groups))
209 (setcar (cdr elem) articles)
210 (push (list sid articles description) nnslashdot-groups))))
212 (nnslashdot-write-groups)
214 (set-buffer nntp-server-buffer)
216 (dolist (elem nnslashdot-groups)
217 (insert (car elem) " " (number-to-string (cadr elem)) " 1 m\n")))
220 (deffoo nnslashdot-asynchronous-p ()
223 (nnoo-define-skeleton nnslashdot)
225 ;;; Internal functions
227 (defun nnslashdot-possibly-change-server (&optional group server)
228 (nnslashdot-init server)
230 (not (nnslashdot-server-opened server)))
231 (nnslashdot-open-server server))
232 (unless nnslashdot-groups
233 (nnslashdot-read-groups)))
235 (defun nnslashdot-read-groups ()
236 (let ((file (expand-file-name "groups" nnslashdot-directory)))
237 (when (file-exists-p file)
239 (insert-file-contents file)
240 (goto-char (point-min))
241 (setq nnslashdot-groups (read (current-buffer)))))))
243 (defun nnslashdot-write-groups ()
244 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
245 (prin1 nnslashdot-groups (current-buffer))))
247 (defun nnslashdot-init (server)
248 "Initialize buffers and such."
249 (unless (file-exists-p nnslashdot-directory)
250 (gnus-make-directory nnslashdot-directory))
251 (unless (gnus-buffer-live-p nnslashdot-buffer)
252 (setq nnslashdot-buffer
254 (nnheader-set-temp-buffer
255 (format " *nnslashdot %s*" server))))))
257 (defun nnslashdot-encode-www-form-urlencoded (pairs)
258 "Return PAIRS encoded for forms."
262 (concat (w3-form-encode-xwfu (car data)) "="
263 (w3-form-encode-xwfu (cdr data)))))
266 (defun nnslashdot-fetch-form (url pairs)
267 (let ((url-request-data (nnslashdot-encode-www-form-urlencoded pairs))
268 (url-request-method "POST")
269 (url-request-extra-headers
270 '(("Content-type" . "application/x-www-form-urlencoded"))))
271 (url-insert-file-contents url)
272 (setq buffer-file-name nil))
275 (defun nnslashdot-decode-entities ()
276 (goto-char (point-min))
277 (while (re-search-forward "&\\([a-z]+\\);" nil t)
278 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
283 (defun nnslashdot-remove-markup ()
284 (goto-char (point-min))
285 (while (search-forward "<!--" nil t)
286 (delete-region (match-beginning 0)
287 (or (search-forward "-->" nil t)
289 (goto-char (point-min))
290 (while (re-search-forward "<[^>]+>" nil t)
291 (replace-match "" t t)))
293 (defun nnslashdot-date-to-date (sdate)
294 (let ((elem (split-string sdate)))
295 (concat (substring (nth 0 elem) 0 3) " "
296 (substring (nth 1 elem) 0 3) " "
297 (substring (nth 2 elem) 0 2) " "
298 (substring (nth 3 elem) 1 6) " "
301 (provide 'nnslashdot)
303 ;;; nnslashdot.el ends here