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