Add Eric Marsden's patch.
[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))
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           (when 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           (goto-char point)
141           (while (re-search-forward
142                   "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
143                   nil t)
144             (setq article (string-to-number (match-string 1))
145                   subject (match-string 2)
146                   score (match-string 3))
147             (when (string-match "^Re: *" subject)
148               (setq subject (concat "Re: " (substring subject (match-end 0)))))
149             (setq subject (nnweb-decode-entities-string subject))
150             (forward-line 1)
151             (if (looking-at
152                  "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
153                 (setq from (concat (nnweb-decode-entities-string (match-string 1))
154                                    " <" (match-string 2) ">"))
155               (looking-at "by \\(.+\\) on ")
156               (setq from (nnweb-decode-entities-string (match-string 1))))
157             (goto-char (- (match-end 0) 5))
158             (search-forward " on ")
159             (setq date
160                   (nnslashdot-date-to-date
161                    (buffer-substring (point) (progn (end-of-line) (point)))))
162             (setq lines (/ (abs (- (search-forward "<td ")
163                                    (search-forward "</td>")))
164                            70))
165             (forward-line 2)
166             (setq parent
167                   (if (looking-at ".*cid=\\([0-9]+\\)")
168                       (match-string 1)
169                     nil))
170             (setq did t)
171             (push
172              (cons
173               (1+ article)
174               (make-full-mail-header
175                (1+ article)
176                (concat subject " (" score ")")
177                from date
178                (concat "<" sid "%"
179                        (number-to-string (1+ article)) 
180                        "@slashdot>")
181                (if parent
182                    (concat "<" sid "%"
183                            (number-to-string (1+ (string-to-number parent)))
184                            "@slashdot>")
185                  "")
186                0 lines nil nil))
187              headers)))))
188     (setq nnslashdot-headers (sort headers 'car-less-than-car))
189     (save-excursion
190       (set-buffer nntp-server-buffer)
191       (erase-buffer)
192       (dolist (header nnslashdot-headers)
193         (nnheader-insert-nov (cdr header))))
194     'nov))
195
196 (deffoo nnslashdot-sane-retrieve-headers (articles group)
197   (let ((last (car (last articles)))
198         (did nil)
199         (start (max (1- (car articles)) 1))
200         (sid (caddr (assoc group nnslashdot-groups)))
201         headers article subject score from date lines parent point)
202     (save-excursion
203       (set-buffer nnslashdot-buffer)
204       (erase-buffer)
205       (when (= start 1)
206         (nnweb-insert (format nnslashdot-article-url sid))
207         (goto-char (point-min))
208         (search-forward "Posted by ")
209         (when (looking-at "<a[^>]+>\\([^<]+\\)")
210           (setq from (nnweb-decode-entities-string (match-string 1))))
211         (search-forward " on ")
212         (setq date (nnslashdot-date-to-date
213                     (buffer-substring (point) (1- (search-forward "<")))))
214         (forward-line 2)
215         (setq lines (count-lines (point)
216                                  (search-forward
217                                   "A href=http://slashdot.org/article.pl")))
218         (push
219          (cons
220           1
221           (make-full-mail-header
222            1 group from date (concat "<" sid "%1@slashdot>")
223            "" 0 lines nil nil))
224          headers))
225       (while (or (not article)
226                  (and did
227                       (< article last)))
228         (when article
229           (setq start (1+ article)))
230         (setq point (goto-char (point-max)))
231         (nnweb-insert
232          (format nnslashdot-comments-url sid nnslashdot-threshold 4 start))
233         (goto-char point)
234         (while (re-search-forward
235                 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
236                 nil t)
237           (setq article (string-to-number (match-string 1))
238                 subject (match-string 2)
239                 score (match-string 3))
240           (when (string-match "^Re: *" subject)
241             (setq subject (concat "Re: " (substring subject (match-end 0)))))
242           (setq subject (nnweb-decode-entities-string subject))
243           (forward-line 1)
244           (if (looking-at
245                "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
246               (setq from (concat (nnweb-decode-entities-string (match-string 1))
247                                  " <" (match-string 2) ">"))
248             (looking-at "by \\(.+\\) on ")
249             (setq from (nnweb-decode-entities-string (match-string 1))))
250           (goto-char (- (match-end 0) 5))
251           (search-forward " on ")
252           (setq date
253                 (nnslashdot-date-to-date
254                  (buffer-substring (point) (progn (end-of-line) (point)))))
255           (setq lines (/ (abs (- (search-forward "<td ")
256                                  (search-forward "</td>")))
257                          70))
258           (forward-line 2)
259           (setq parent
260                 (if (looking-at ".*cid=\\([0-9]+\\)")
261                     (match-string 1)
262                   nil))
263           (setq did t)
264           (push
265            (cons
266             (1+ article)
267             (make-full-mail-header
268              (1+ article) (concat subject " (" score ")")
269              from date
270              (concat "<" sid "%"
271                      (number-to-string (1+ article)) 
272                      "@slashdot>")
273              (if parent
274                  (concat "<" sid "%"
275                          (number-to-string (1+ (string-to-number parent)))
276                          "@slashdot>")
277                "")
278              0 lines nil nil))
279            headers))))
280     (setq nnslashdot-headers
281           (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
282     (save-excursion
283       (set-buffer nntp-server-buffer)
284       (erase-buffer)
285       (dolist (header nnslashdot-headers)
286         (nnheader-insert-nov (cdr header))))
287     'nov))
288
289 (deffoo nnslashdot-request-group (group &optional server dont-check)
290   (nnslashdot-possibly-change-server nil server)
291   (let ((elem (assoc group nnslashdot-groups)))
292     (cond
293      ((not elem)
294       (nnheader-report 'nnslashdot "Group does not exist"))
295      (t
296       (nnheader-report 'nnslashdot "Opened group %s" group)
297       (nnheader-insert
298        "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
299        (prin1-to-string group))))))
300
301 (deffoo nnslashdot-close-group (group &optional server)
302   (nnslashdot-possibly-change-server group server)
303   (when (gnus-buffer-live-p nnslashdot-buffer)
304     (save-excursion
305       (set-buffer nnslashdot-buffer)
306       (kill-buffer nnslashdot-buffer)))
307   t)
308
309 (deffoo nnslashdot-request-article (article &optional group server buffer)
310   (nnslashdot-possibly-change-server group server)
311   (let (contents)
312     (condition-case why
313         (save-excursion
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)
321               (if (= article 1)
322                   (progn
323                     (re-search-forward "Posted by .* on ")
324                     (forward-line 1)
325                     (setq contents
326                           (buffer-substring
327                            (point)
328                            (progn
329                              (re-search-forward
330                               "<p>.*A href=http://slashdot.org/article.pl")
331                              (match-beginning 0)))))
332                 (search-forward (format "<a name=\"%d\">" (1- article)))
333                 (setq contents
334                       (buffer-substring
335                        (re-search-forward "<td[^>]+>")
336                        (search-forward "</td>")))))))
337       (search-failed (nnslashdot-lose why)))
338
339     (when contents
340       (save-excursion
341         (set-buffer (or buffer nntp-server-buffer))
342         (erase-buffer)
343         (insert contents)
344         (goto-char (point-min))
345         (while (search-forward "<br><br>" nil t)
346           (replace-match "<p>" t t))
347         (goto-char (point-min))
348         (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
349         (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
350                 "\n")
351         (let ((header (cdr (assq article nnslashdot-headers))))
352           (nnheader-insert-header header))
353         (nnheader-report 'nnslashdot "Fetched article %s" article)
354         (cons group article)))))
355
356 (deffoo nnslashdot-close-server (&optional server)
357   (when (and (nnslashdot-server-opened server)
358              (gnus-buffer-live-p nnslashdot-buffer))
359     (save-excursion
360       (set-buffer nnslashdot-buffer)
361       (kill-buffer nnslashdot-buffer)))
362   (nnoo-close-server 'nnslashdot server))
363
364 (deffoo nnslashdot-request-list (&optional server)
365   (nnslashdot-possibly-change-server nil server)
366   (let ((number 0)
367         sid elem description articles gname)
368     (condition-case why
369         ;; First we do the Ultramode to get info on all the latest groups.
370         (progn 
371           (mm-with-unibyte-buffer
372             (nnweb-insert "http://slashdot.org/slashdot.xml")
373             (goto-char (point-min))
374             (while (search-forward "<story>" nil t)
375               (narrow-to-region (point) (search-forward "</story>"))
376               (goto-char (point-min))
377               (re-search-forward "<title>\\([^<]+\\)</title>")
378               (setq description (nnweb-decode-entities-string (match-string 1)))
379               (re-search-forward "<url>\\([^<]+\\)</url>")
380               (setq sid (match-string 1))
381               (string-match "/\\([0-9/]+\\).shtml" sid)
382               (setq sid (match-string 1 sid))
383               (re-search-forward "<comments>\\([^<]+\\)</comments>")
384               (setq articles (string-to-number (match-string 1)))
385               (setq gname (concat description " (" sid ")"))
386               (if (setq elem (assoc gname nnslashdot-groups))
387                   (setcar (cdr elem) articles)
388                 (push (list gname articles sid) nnslashdot-groups))
389               (goto-char (point-max))
390               (widen)))
391           ;; Then do the older groups.
392           (while (> (- nnslashdot-group-number number) 0)
393             (mm-with-unibyte-buffer
394               (let ((case-fold-search t))
395                 (nnweb-insert (format nnslashdot-active-url number))
396                 (goto-char (point-min))
397                 (while (re-search-forward
398                         "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
399                   (setq sid (match-string 1)
400                         description (nnweb-decode-entities-string (match-string 2)))
401                   (forward-line 1)
402                   (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
403                     (setq articles (string-to-number (match-string 1))))
404                   (setq gname (concat description " (" sid ")"))
405                   (if (setq elem (assoc gname nnslashdot-groups))
406                       (setcar (cdr elem) articles)
407                     (push (list gname articles sid) nnslashdot-groups)))))
408             (incf number 30)))
409       (search-failed (nnslashdot-lose why)))
410     (nnslashdot-write-groups)
411     (nnslashdot-generate-active)
412     t))
413   
414 (deffoo nnslashdot-request-newgroups (date &optional server)
415   (nnslashdot-possibly-change-server nil server)
416   (nnslashdot-generate-active)
417   t)
418
419 (deffoo nnslashdot-request-post (&optional server)
420   (nnslashdot-possibly-change-server nil server)
421   (let ((sid (message-fetch-field "newsgroups"))
422         (subject (message-fetch-field "subject"))
423         (references (car (last (split-string
424                                 (message-fetch-field "references")))))
425         body quoted pid)
426     (string-match "%\\([0-9]+\\)@slashdot" references)
427     (setq pid (match-string 1 references))
428     (message-goto-body)
429     (narrow-to-region (point) (progn (message-goto-signature) (point)))
430     (goto-char (point-min))
431     (while (not (eobp))
432       (if (looking-at "> ")
433           (progn
434             (delete-region (point) (+ (point) 2))
435             (unless quoted
436               (insert "<blockquote>\n"))
437             (setq quoted t))
438         (when quoted
439           (insert "</blockquote>\n")
440           (setq quoted nil)))
441       (forward-line 1))
442     (goto-char (point-min))
443     (while (re-search-forward "^ *\n" nil t)
444       (replace-match "<p>\n"))
445     (widen)
446     (when (message-goto-signature)
447       (forward-line -1)
448       (insert "<p>\n")
449       (while (not (eobp))
450         (end-of-line)
451         (insert "<br>")
452         (forward-line 1)))
453     (message-goto-body)
454     (setq body (buffer-substring (point) (point-max)))
455     (erase-buffer)
456     (nnweb-fetch-form
457      "http://slashdot.org/comments.pl"
458      `(("sid" . ,sid)
459        ("pid" . ,pid)
460        ("rlogin" . "userlogin")
461        ("unickname" . ,nnslashdot-login-name)
462        ("upasswd" . ,nnslashdot-password)
463        ("postersubj" . ,subject)
464        ("op" . "Submit")
465        ("postercomment" . ,body)
466        ("posttype" . "html")))))
467
468 (deffoo nnslashdot-request-delete-group (group &optional force server)
469   (nnslashdot-possibly-change-server group server)
470   (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
471                                 nnslashdot-groups))
472   (nnslashdot-write-groups))
473
474 (nnoo-define-skeleton nnslashdot)
475
476 ;;; Internal functions
477
478 (defun nnslashdot-possibly-change-server (&optional group server)
479   (nnslashdot-init server)
480   (when (and server
481              (not (nnslashdot-server-opened server)))
482     (nnslashdot-open-server server))
483   (unless nnslashdot-groups
484     (nnslashdot-read-groups)))
485
486 (defun nnslashdot-read-groups ()
487   (let ((file (expand-file-name "groups" nnslashdot-directory)))
488     (when (file-exists-p file)
489       (mm-with-unibyte-buffer
490         (insert-file-contents file)
491         (goto-char (point-min))
492         (setq nnslashdot-groups (read (current-buffer)))))))
493
494 (defun nnslashdot-write-groups ()
495   (with-temp-file (expand-file-name "groups" nnslashdot-directory)
496     (prin1 nnslashdot-groups (current-buffer))))
497     
498 (defun nnslashdot-init (server)
499   "Initialize buffers and such."
500   (unless (file-exists-p nnslashdot-directory)
501     (gnus-make-directory nnslashdot-directory))
502   (unless (gnus-buffer-live-p nnslashdot-buffer)
503     (setq nnslashdot-buffer
504           (save-excursion
505             (nnheader-set-temp-buffer
506              (format " *nnslashdot %s*" server))))))
507
508 (defun nnslashdot-date-to-date (sdate)
509   (let ((elem (delete "" (split-string sdate))))
510     (concat (substring (nth 0 elem) 0 3) " "
511             (substring (nth 1 elem) 0 3) " "
512             (substring (nth 2 elem) 0 2) " "
513             (substring (nth 3 elem) 1 6) " "
514             (format-time-string "%Y") " "
515             (nth 4 elem))))
516
517 (defun nnslashdot-generate-active ()
518   (save-excursion
519     (set-buffer nntp-server-buffer)
520     (erase-buffer)
521     (dolist (elem nnslashdot-groups)
522       (insert (prin1-to-string (car elem))
523               " " (number-to-string (cadr elem)) " 1 y\n"))))
524
525 (defun nnslashdot-lose (why)
526   (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
527
528 (provide 'nnslashdot)
529
530 ;;; nnslashdot.el ends here