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=0&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)))
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
95 (caddr (assoc group nnslashdot-groups))
96 nnslashdot-threshold start))
97 (setq buffer-file-name nil)
99 (while (re-search-forward
100 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score\\([^)]+\\))"
102 (setq article (match-string 1)
103 subject (match-string 2)
104 score (match-string 3))
106 (if (looking-at "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
107 (setq from (concat (match-string 1) " <" (match-string 2) ">"))
108 (looking-at "by \\([^ ]+\\) ")
109 (setq from (match-string 1)))
110 (goto-char (match-end 0))
111 (search-forward "on ")
113 (nnslashdot-date-to-date
114 (buffer-substring (point) (progn (end-of-line) (point)))))
115 (setq lines (count-lines (search-forward "<td")
116 (search-forward "</td>")))
119 (if (looking-at ".*cid=\\([0-9]+\\)")
125 (string-to-number article)
126 (make-full-mail-header
127 (string-to-number article) (concat subject " (" score ")")
128 from date (concat "<" group "%" article "@slashdot>")
129 (if parent (concat "<" group "%" parent "@slashdot>") "")
132 (setq nnslashdot-headers
133 (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
135 (set-buffer nntp-server-buffer)
137 (dolist (header nnslashdot-headers)
138 (nnheader-insert-nov (cdr header))))
141 (deffoo nnslashdot-request-group (group &optional server dont-check)
142 (nnslashdot-possibly-change-server nil server)
143 (let ((elem (assoc group nnslashdot-groups)))
146 (nnheader-report 'nnslashdot "Group does not exist"))
148 (nnheader-report 'nnslashdot "Opened group %s" group)
150 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
151 (prin1-to-string group))))))
153 (deffoo nnslashdot-close-group (group &optional server)
154 (nnslashdot-possibly-change-server group server)
155 (when (gnus-buffer-live-p nnslashdot-buffer)
157 (set-buffer nnslashdot-buffer)
158 (kill-buffer nnslashdot-buffer)))
161 (deffoo nnslashdot-request-article (article &optional group server buffer)
162 (nnslashdot-possibly-change-server group server)
165 (set-buffer nnslashdot-buffer)
166 (goto-char (point-min))
167 (when (and (numberp article)
168 (search-forward (format "<a name=\"%d\">" article)))
171 (re-search-forward "<td[^>]+>")
172 (search-forward "</td>")))))
175 (set-buffer (or buffer nntp-server-buffer))
178 ;;(nnweb-remove-markup)
179 ;;(nnweb-decode-entities)
180 (goto-char (point-min))
181 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
182 (let ((header (cdr (assq article nnslashdot-headers))))
183 (nnheader-insert-header header))
184 (nnheader-report 'nnslashdot "Fetched article %s" article)
187 (deffoo nnslashdot-close-server (&optional server)
188 (when (and (nnslashdot-server-opened server)
189 (gnus-buffer-live-p nnslashdot-buffer))
191 (set-buffer nnslashdot-buffer)
192 (kill-buffer nnslashdot-buffer)))
193 (nnoo-close-server 'nnslashdot server))
195 (deffoo nnslashdot-request-list (&optional server)
196 (nnslashdot-possibly-change-server nil server)
197 (let ((case-fold-search t)
199 sid elem description articles gname)
200 (while (> (- nnslashdot-group-number number) 0)
202 (url-insert-file-contents (format nnslashdot-active-url number))
203 (setq buffer-file-name nil)
204 (goto-char (point-min))
205 (while (re-search-forward
206 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
207 (setq sid (match-string 1)
208 description (match-string 2))
210 (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
211 (setq articles (string-to-number (match-string 1))))
212 (setq gname (concat description " (" sid ")"))
213 (if (setq elem (assoc gname nnslashdot-groups))
214 (setcar (cdr elem) articles)
215 (push (list gname articles sid) nnslashdot-groups))))
217 (nnslashdot-write-groups)
219 (set-buffer nntp-server-buffer)
221 (dolist (elem nnslashdot-groups)
222 (insert (prin1-to-string (car elem))
223 " " (number-to-string (cadr elem)) " 1 m\n")))
226 (deffoo nnslashdot-asynchronous-p ()
229 (nnoo-define-skeleton nnslashdot)
231 ;;; Internal functions
233 (defun nnslashdot-possibly-change-server (&optional group server)
234 (nnslashdot-init server)
236 (not (nnslashdot-server-opened server)))
237 (nnslashdot-open-server server))
238 (unless nnslashdot-groups
239 (nnslashdot-read-groups)))
241 (defun nnslashdot-read-groups ()
242 (let ((file (expand-file-name "groups" nnslashdot-directory)))
243 (when (file-exists-p file)
245 (insert-file-contents file)
246 (goto-char (point-min))
247 (setq nnslashdot-groups (read (current-buffer)))))))
249 (defun nnslashdot-write-groups ()
250 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
251 (prin1 nnslashdot-groups (current-buffer))))
253 (defun nnslashdot-init (server)
254 "Initialize buffers and such."
255 (unless (file-exists-p nnslashdot-directory)
256 (gnus-make-directory nnslashdot-directory))
257 (unless (gnus-buffer-live-p nnslashdot-buffer)
258 (setq nnslashdot-buffer
260 (nnheader-set-temp-buffer
261 (format " *nnslashdot %s*" server))))))
263 (defun nnslashdot-encode-www-form-urlencoded (pairs)
264 "Return PAIRS encoded for forms."
268 (concat (w3-form-encode-xwfu (car data)) "="
269 (w3-form-encode-xwfu (cdr data)))))
272 (defun nnslashdot-fetch-form (url pairs)
273 (let ((url-request-data (nnslashdot-encode-www-form-urlencoded pairs))
274 (url-request-method "POST")
275 (url-request-extra-headers
276 '(("Content-type" . "application/x-www-form-urlencoded"))))
277 (url-insert-file-contents url)
278 (setq buffer-file-name nil))
281 (defun nnslashdot-decode-entities ()
282 (goto-char (point-min))
283 (while (re-search-forward "&\\([a-z]+\\);" nil t)
284 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
289 (defun nnslashdot-remove-markup ()
290 (goto-char (point-min))
291 (while (search-forward "<!--" nil t)
292 (delete-region (match-beginning 0)
293 (or (search-forward "-->" nil t)
295 (goto-char (point-min))
296 (while (re-search-forward "<[^>]+>" nil t)
297 (replace-match "" t t)))
299 (defun nnslashdot-date-to-date (sdate)
300 (let ((elem (split-string sdate)))
301 (concat (substring (nth 0 elem) 0 3) " "
302 (substring (nth 1 elem) 0 3) " "
303 (substring (nth 2 elem) 0 2) " "
304 (substring (nth 3 elem) 1 6) " "
307 (provide 'nnslashdot)
309 ;;; nnslashdot.el ends here