]> code.delx.au - gnu-emacs/blob - lisp/gnus/nnslashdot.el
Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[gnu-emacs] / lisp / gnus / nnslashdot.el
1 ;;; nnslashdot.el --- interfacing with Slashdot
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'nnoo)
31 (require 'message)
32 (require 'gnus-util)
33 (require 'gnus)
34 (require 'nnmail)
35 (require 'mm-util)
36 (require 'mm-url)
37
38 (nnoo-declare nnslashdot)
39
40 (defvoo nnslashdot-directory (nnheader-concat gnus-directory "slashdot/")
41 "Where nnslashdot will save its files.")
42
43 (defvoo nnslashdot-active-url "http://slashdot.org/search.pl?section=&min=%d"
44 "Where nnslashdot will fetch the active file from.")
45
46 (defvoo nnslashdot-comments-url "http://slashdot.org/comments.pl?sid=%s&threshold=%d&commentsort=%d&mode=flat&startat=%d"
47 "Where nnslashdot will fetch comments from.")
48
49 (defvoo nnslashdot-article-url
50 "http://slashdot.org/article.pl?sid=%s&mode=nocomment"
51 "Where nnslashdot will fetch the article from.")
52
53 (defvoo nnslashdot-backslash-url "http://slashdot.org/slashdot.xml"
54 "Where nnslashdot will fetch the stories from.")
55
56 (defvoo nnslashdot-use-front-page nil
57 "Use the front page in addition to the backslash page.")
58
59 (defvoo nnslashdot-threshold -1
60 "The article threshold.")
61
62 (defvoo nnslashdot-threaded t
63 "Whether the nnslashdot groups should be threaded or not.")
64
65 (defvoo nnslashdot-group-number 0
66 "The number of non-fresh groups to keep updated.")
67
68 (defvoo nnslashdot-login-name ""
69 "The login name to use when posting.")
70
71 (defvoo nnslashdot-password ""
72 "The password to use when posting.")
73
74 ;;; Internal variables
75
76 (defvar nnslashdot-groups nil)
77 (defvar nnslashdot-buffer nil)
78 (defvar nnslashdot-headers nil)
79
80 ;;; Interface functions
81
82 (nnoo-define-basics nnslashdot)
83
84 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
85 (nnslashdot-possibly-change-server group server)
86 (condition-case why
87 (unless gnus-nov-is-evil
88 (nnslashdot-retrieve-headers-1 articles group))
89 (search-failed (nnslashdot-lose why))))
90
91 (deffoo nnslashdot-retrieve-headers-1 (articles group)
92 (let* ((last (car (last articles)))
93 (start (if nnslashdot-threaded 1 (pop articles)))
94 (entry (assoc group nnslashdot-groups))
95 (sid (nth 2 entry))
96 (first-comments t)
97 headers article subject score from date lines parent point cid
98 s startats changed)
99 (save-excursion
100 (set-buffer nnslashdot-buffer)
101 (let ((case-fold-search t))
102 (erase-buffer)
103 (when (= start 1)
104 (mm-url-insert (format nnslashdot-article-url sid) t)
105 (goto-char (point-min))
106 (if (eobp)
107 (error "Couldn't open connection to slashdot"))
108 (re-search-forward "Posted by[ \t\r\n]+")
109 (when (looking-at "\\(<a[^>]+>\\)?[ \t\r\n]*\\([^<\r\n]+\\)")
110 (setq from (mm-url-decode-entities-string (match-string 2))))
111 (search-forward "on ")
112 (setq date (nnslashdot-date-to-date
113 (buffer-substring (point) (1- (search-forward "<")))))
114 (setq lines (/ (- (point)
115 (progn (forward-line 1) (point)))
116 60))
117 (push
118 (cons
119 1
120 (make-full-mail-header
121 1 group from date
122 (concat "<" sid "%1@slashdot>")
123 "" 0 lines nil nil))
124 headers)
125 (setq start (if nnslashdot-threaded 2 (pop articles))))
126 (while (and start (<= start last))
127 (setq point (goto-char (point-max)))
128 (mm-url-insert
129 (format nnslashdot-comments-url sid
130 nnslashdot-threshold 0 (- start 2))
131 t)
132 (when (and nnslashdot-threaded first-comments)
133 (setq first-comments nil)
134 (goto-char (point-max))
135 (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
136 (setq s (string-to-number (match-string 1)))
137 (unless (memq s startats)
138 (push s startats)))
139 (setq startats (sort startats '<)))
140 (setq article (if (and article (< start article)) article start))
141 (goto-char point)
142 (while (re-search-forward
143 "<a name=\"\\([0-9]+\\)\">\\([^<]+\\)\\(?:.*\n\\)\\{2,10\\}.*score:\\([^)]+\\))"
144 nil t)
145 (setq cid (match-string 1)
146 subject (match-string 2)
147 score (match-string 3))
148 (unless (assq article (nth 4 entry))
149 (setcar (nthcdr 4 entry) (cons (cons article cid) (nth 4 entry)))
150 (setq changed t))
151 (when (string-match "^Re: *" subject)
152 (setq subject (concat "Re: " (substring subject (match-end 0)))))
153 (setq subject (mm-url-decode-entities-string subject)
154 from "")
155 (when (re-search-forward "by[ \t\n]+<[^>]+>\\([^<(]+\\)" nil t)
156 (setq from
157 (concat
158 (mm-url-decode-entities-string (match-string 1))
159 " <nobody@slashdot.org>")))
160 (search-forward "on ")
161 (setq date
162 (nnslashdot-date-to-date
163 (buffer-substring
164 (point) (progn (skip-chars-forward "^()<>\n\r") (point)))))
165 (setq lines (/ (abs (- (search-forward "<div")
166 (search-forward "</div>")))
167 70))
168 (if (not
169 (re-search-forward ".*cid=\\([0-9]+\\)\">Parent</A>" nil t))
170 (setq parent nil)
171 (setq parent (match-string 1))
172 (when (string= parent "0")
173 (setq parent nil)))
174 (push
175 (cons
176 article
177 (make-full-mail-header
178 article
179 (concat subject " (" score ")")
180 from date
181 (concat "<" sid "%" cid "@slashdot>")
182 (if parent
183 (concat "<" sid "%" parent "@slashdot>")
184 "")
185 0 lines nil nil))
186 headers)
187 (while (and articles (<= (car articles) article))
188 (pop articles))
189 (setq article (1+ article)))
190 (if nnslashdot-threaded
191 (progn
192 (setq start (pop startats))
193 (if start (setq start (+ start 2))))
194 (setq start (pop articles))))))
195 (if changed (nnslashdot-write-groups))
196 (setq nnslashdot-headers (sort headers 'car-less-than-car))
197 (save-excursion
198 (set-buffer nntp-server-buffer)
199 (erase-buffer)
200 (mm-with-unibyte-current-buffer
201 (dolist (header nnslashdot-headers)
202 (nnheader-insert-nov (cdr header)))))
203 'nov))
204
205 (deffoo nnslashdot-request-group (group &optional server dont-check)
206 (nnslashdot-possibly-change-server nil server)
207 (let ((elem (assoc group nnslashdot-groups)))
208 (cond
209 ((not elem)
210 (nnheader-report 'nnslashdot "Group does not exist"))
211 (t
212 (nnheader-report 'nnslashdot "Opened group %s" group)
213 (nnheader-insert
214 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
215 (prin1-to-string group))))))
216
217 (deffoo nnslashdot-close-group (group &optional server)
218 (nnslashdot-possibly-change-server group server)
219 (when (gnus-buffer-live-p nnslashdot-buffer)
220 (save-excursion
221 (set-buffer nnslashdot-buffer)
222 (kill-buffer nnslashdot-buffer)))
223 t)
224
225 (deffoo nnslashdot-request-article (article &optional group server buffer)
226 (nnslashdot-possibly-change-server group server)
227 (let (contents cid)
228 (condition-case why
229 (save-excursion
230 (set-buffer nnslashdot-buffer)
231 (let ((case-fold-search t))
232 (goto-char (point-min))
233 (when (and (stringp article)
234 (string-match "%\\([0-9]+\\)@" article))
235 (setq cid (match-string 1 article))
236 (let ((map (nth 4 (assoc group nnslashdot-groups))))
237 (while map
238 (if (equal (cdar map) cid)
239 (setq article (caar map)
240 map nil)
241 (setq map (cdr map))))))
242 (when (numberp article)
243 (if (= article 1)
244 (progn
245 (search-forward "Posted by")
246 (search-forward "<div class=\"intro\">")
247 (setq contents
248 (buffer-substring
249 (point)
250 (progn
251 (search-forward "commentwrap")
252 (match-beginning 0)))))
253 (setq cid (cdr (assq article
254 (nth 4 (assoc group nnslashdot-groups)))))
255 (search-forward (format "<a name=\"%s\">" cid))
256 (setq contents
257 (buffer-substring
258 (search-forward "<div class=\"commentBody\">")
259 (progn
260 (search-forward "<div class=\"commentSub\"")
261 (match-beginning 0))))))))
262 (search-failed (nnslashdot-lose why)))
263
264 (when contents
265 (save-excursion
266 (set-buffer (or buffer nntp-server-buffer))
267 (erase-buffer)
268 (mm-with-unibyte-current-buffer
269 (insert contents)
270 (goto-char (point-min))
271 (while (re-search-forward "\\(<br>\r?\\)+" nil t)
272 (replace-match "<p>" t t))
273 (goto-char (point-min))
274 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
275 (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
276 "\n")
277 (let ((header (cdr (assq article nnslashdot-headers))))
278 (nnheader-insert-header header))
279 (nnheader-report 'nnslashdot "Fetched article %s" article))
280 (cons group article)))))
281
282 (deffoo nnslashdot-close-server (&optional server)
283 (when (and (nnslashdot-server-opened server)
284 (gnus-buffer-live-p nnslashdot-buffer))
285 (save-excursion
286 (set-buffer nnslashdot-buffer)
287 (kill-buffer nnslashdot-buffer)))
288 (nnoo-close-server 'nnslashdot server))
289
290 (deffoo nnslashdot-request-list (&optional server)
291 (nnslashdot-possibly-change-server nil server)
292 (let ((number 0)
293 (first nnslashdot-use-front-page)
294 sid elem description articles gname)
295 (condition-case why
296 ;; First we do the Ultramode to get info on all the latest groups.
297 (progn
298 (mm-with-unibyte-buffer
299 (mm-url-insert nnslashdot-backslash-url t)
300 (goto-char (point-min))
301 (if (eobp)
302 (error "Couldn't open connection to slashdot"))
303 (while (search-forward "<story>" nil t)
304 (narrow-to-region (point) (search-forward "</story>"))
305 (goto-char (point-min))
306 (re-search-forward "<title>\\([^<]+\\)</title>")
307 (setq description
308 (mm-url-decode-entities-string (match-string 1)))
309 (re-search-forward "<url>\\([^<]+\\)</url>")
310 (setq sid (match-string 1))
311 (string-match "sid=\\([0-9/]+\\)\\(.shtml\\|$\\)" sid)
312 (setq sid (match-string 1 sid))
313 (re-search-forward "<comments>\\([^<]+\\)</comments>")
314 (setq articles (string-to-number (match-string 1)))
315 (setq gname (concat description " (" sid ")"))
316 (if (setq elem (assoc gname nnslashdot-groups))
317 (setcar (cdr elem) articles)
318 (push (list gname articles sid (current-time) nil)
319 nnslashdot-groups))
320 (goto-char (point-max))
321 (widen)))
322 ;; Then do the older groups.
323 (while (or first
324 (> (- nnslashdot-group-number number) 0))
325 (setq first nil)
326 (mm-with-unibyte-buffer
327 (let ((case-fold-search t))
328 (mm-url-insert (format nnslashdot-active-url number) t)
329 (goto-char (point-min))
330 (while (re-search-forward
331 "article.pl\\?sid=\\([^&]+\\).*>\\([^<]+\\)</a>"
332 nil t)
333 (setq sid (match-string 1)
334 description
335 (mm-url-decode-entities-string (match-string 2)))
336 (forward-line 1)
337 (when (re-search-forward "with \\([0-9]+\\) comment" nil t)
338 (setq articles (1+ (string-to-number (match-string 1)))))
339 (setq gname (concat description " (" sid ")"))
340 (if (setq elem (assoc gname nnslashdot-groups))
341 (setcar (cdr elem) articles)
342 (push (list gname articles sid (current-time) nil)
343 nnslashdot-groups)))))
344 (incf number 30)))
345 (search-failed (nnslashdot-lose why)))
346 (nnslashdot-write-groups)
347 (nnslashdot-generate-active)
348 t))
349
350 (deffoo nnslashdot-request-newgroups (date &optional server)
351 (nnslashdot-possibly-change-server nil server)
352 (nnslashdot-generate-active)
353 t)
354
355 (deffoo nnslashdot-request-post (&optional server)
356 (nnslashdot-possibly-change-server nil server)
357 (let ((sid (message-fetch-field "newsgroups"))
358 (subject (message-fetch-field "subject"))
359 (references (car (last (split-string
360 (message-fetch-field "references")))))
361 body quoted pid)
362 (string-match "%\\([0-9]+\\)@slashdot" references)
363 (setq pid (match-string 1 references))
364 (message-goto-body)
365 (narrow-to-region (point) (progn (message-goto-signature) (point)))
366 (goto-char (point-min))
367 (while (not (eobp))
368 (if (looking-at "> ")
369 (progn
370 (delete-region (point) (+ (point) 2))
371 (unless quoted
372 (insert "<blockquote>\n"))
373 (setq quoted t))
374 (when quoted
375 (insert "</blockquote>\n")
376 (setq quoted nil)))
377 (forward-line 1))
378 (goto-char (point-min))
379 (while (re-search-forward "^ *\n" nil t)
380 (replace-match "<p>\n"))
381 (widen)
382 (when (message-goto-signature)
383 (forward-line -1)
384 (insert "<p>\n")
385 (while (not (eobp))
386 (end-of-line)
387 (insert "<br>")
388 (forward-line 1)))
389 (message-goto-body)
390 (setq body (buffer-substring (point) (point-max)))
391 (erase-buffer)
392 (mm-url-fetch-form
393 "http://slashdot.org/comments.pl"
394 `(("sid" . ,sid)
395 ("pid" . ,pid)
396 ("rlogin" . "userlogin")
397 ("unickname" . ,nnslashdot-login-name)
398 ("upasswd" . ,nnslashdot-password)
399 ("postersubj" . ,subject)
400 ("op" . "Submit")
401 ("postercomment" . ,body)
402 ("posttype" . "html")))))
403
404 (deffoo nnslashdot-request-delete-group (group &optional force server)
405 (nnslashdot-possibly-change-server group server)
406 (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
407 nnslashdot-groups))
408 (nnslashdot-write-groups))
409
410 (deffoo nnslashdot-request-close ()
411 (setq nnslashdot-headers nil
412 nnslashdot-groups nil))
413
414 (deffoo nnslashdot-request-expire-articles
415 (articles group &optional server force)
416 (nnslashdot-possibly-change-server group server)
417 (let ((item (assoc group nnslashdot-groups)))
418 (when item
419 (if (fourth item)
420 (when (and (>= (length articles) (cadr item)) ;; All are expirable.
421 (nnmail-expired-article-p
422 group
423 (fourth item)
424 force))
425 (setq nnslashdot-groups (delq item nnslashdot-groups))
426 (nnslashdot-write-groups)
427 (setq articles nil)) ;; all expired.
428 (setcdr (cddr item) (list (current-time)))
429 (nnslashdot-write-groups))))
430 articles)
431
432 (nnoo-define-skeleton nnslashdot)
433
434 ;;; Internal functions
435
436 (defun nnslashdot-possibly-change-server (&optional group server)
437 (nnslashdot-init server)
438 (when (and server
439 (not (nnslashdot-server-opened server)))
440 (nnslashdot-open-server server))
441 (unless nnslashdot-groups
442 (nnslashdot-read-groups)))
443
444 (defun nnslashdot-make-tuple (tuple n)
445 (prog1
446 tuple
447 (while (> n 1)
448 (unless (cdr tuple)
449 (setcdr tuple (list nil)))
450 (setq tuple (cdr tuple)
451 n (1- n)))))
452
453 (defun nnslashdot-read-groups ()
454 (let ((file (expand-file-name "groups" nnslashdot-directory)))
455 (when (file-exists-p file)
456 (mm-with-unibyte-buffer
457 (insert-file-contents file)
458 (goto-char (point-min))
459 (setq nnslashdot-groups (read (current-buffer))))
460 (when (and nnslashdot-groups (< (length (car nnslashdot-groups)) 5))
461 (dolist (group nnslashdot-groups)
462 (nnslashdot-make-tuple group 5))))))
463
464 (defun nnslashdot-write-groups ()
465 (with-temp-file (expand-file-name "groups" nnslashdot-directory)
466 (gnus-prin1 nnslashdot-groups)))
467
468 (defun nnslashdot-init (server)
469 "Initialize buffers and such."
470 (unless (file-exists-p nnslashdot-directory)
471 (gnus-make-directory nnslashdot-directory))
472 (unless (gnus-buffer-live-p nnslashdot-buffer)
473 (setq nnslashdot-buffer
474 (save-excursion
475 (nnheader-set-temp-buffer
476 (format " *nnslashdot %s*" server))))
477 (push nnslashdot-buffer gnus-buffers)))
478
479 (defun nnslashdot-date-to-date (sdate)
480 (condition-case err
481 (let ((elem (delete "" (split-string sdate))))
482 (concat (substring (nth 0 elem) 0 3) " "
483 (substring (nth 1 elem) 0 3) " "
484 (substring (nth 2 elem) 0 2) " "
485 (substring (nth 3 elem) 1 6) " "
486 (format-time-string "%Y") " "
487 (nth 4 elem)))
488 (error "")))
489
490 (defun nnslashdot-generate-active ()
491 (save-excursion
492 (set-buffer nntp-server-buffer)
493 (erase-buffer)
494 (dolist (elem nnslashdot-groups)
495 (when (numberp (cadr elem))
496 (insert (prin1-to-string (car elem))
497 " " (number-to-string (cadr elem)) " 1 y\n")))))
498
499 (defun nnslashdot-lose (why)
500 (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
501
502 (provide 'nnslashdot)
503
504 ;; arch-tag: aa73df7a-f7e6-4eef-bdea-5ce2f8c691b3
505 ;;; nnslashdot.el ends here