Importing Gnus v5.8.5.
[elisp/gnus.git-] / lisp / nnslashdot.el
1 ;;; nnslashdot.el --- interfacing with Slashdot
2 ;; Copyright (C) 1999, 2000 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" nil t)))
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\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
144                   nil t)
145             (setq article (string-to-number (match-string 1))
146                   subject (match-string 3)
147                   score (match-string 5))
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-date
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")))
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\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
238                 nil t)
239           (setq article (string-to-number (match-string 1))
240                 subject (match-string 3)
241                 score (match-string 5))
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                     (search-forward "<BR>")
327                     (setq contents
328                           (buffer-substring
329                            (point)
330                            (progn
331                              (re-search-forward
332                               "<p>.*A href=http://slashdot\\.org/article")
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 (re-search-forward "\\(<br>\r?\\)+" 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
381                     (nnweb-decode-entities-string (match-string 1)))
382               (re-search-forward "<url>\\([^<]+\\)</url>")
383               (setq sid (match-string 1))
384               (string-match "/\\([0-9/]+\\)\\(.shtml\\|$\\)" sid)
385               (setq sid (concat "00/" (match-string 1 sid)))
386               (re-search-forward "<comments>\\([^<]+\\)</comments>")
387               (setq articles (string-to-number (match-string 1)))
388               (setq gname (concat description " (" sid ")"))
389               (if (setq elem (assoc gname nnslashdot-groups))
390                   (setcar (cdr elem) articles)
391                 (push (list gname articles sid) nnslashdot-groups))
392               (goto-char (point-max))
393               (widen)))
394           ;; Then do the older groups.
395           (while (> (- nnslashdot-group-number number) 0)
396             (mm-with-unibyte-buffer
397               (let ((case-fold-search t))
398                 (nnweb-insert (format nnslashdot-active-url number) t)
399                 (goto-char (point-min))
400                 (while (re-search-forward
401                         "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>"
402                         nil t)
403                   (setq sid (match-string 1)
404                         description
405                         (nnweb-decode-entities-string (match-string 2)))
406                   (forward-line 1)
407                   (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
408                     (setq articles (string-to-number (match-string 1))))
409                   (setq gname (concat description " (" sid ")"))
410                   (if (setq elem (assoc gname nnslashdot-groups))
411                       (setcar (cdr elem) articles)
412                     (push (list gname articles sid) nnslashdot-groups)))))
413             (incf number 30)))
414       (search-failed (nnslashdot-lose why)))
415     (nnslashdot-write-groups)
416     (nnslashdot-generate-active)
417     t))
418   
419 (deffoo nnslashdot-request-newgroups (date &optional server)
420   (nnslashdot-possibly-change-server nil server)
421   (nnslashdot-generate-active)
422   t)
423
424 (deffoo nnslashdot-request-post (&optional server)
425   (nnslashdot-possibly-change-server nil server)
426   (let ((sid (message-fetch-field "newsgroups"))
427         (subject (message-fetch-field "subject"))
428         (references (car (last (split-string
429                                 (message-fetch-field "references")))))
430         body quoted pid)
431     (string-match "%\\([0-9]+\\)@slashdot" references)
432     (setq pid (match-string 1 references))
433     (message-goto-body)
434     (narrow-to-region (point) (progn (message-goto-signature) (point)))
435     (goto-char (point-min))
436     (while (not (eobp))
437       (if (looking-at "> ")
438           (progn
439             (delete-region (point) (+ (point) 2))
440             (unless quoted
441               (insert "<blockquote>\n"))
442             (setq quoted t))
443         (when quoted
444           (insert "</blockquote>\n")
445           (setq quoted nil)))
446       (forward-line 1))
447     (goto-char (point-min))
448     (while (re-search-forward "^ *\n" nil t)
449       (replace-match "<p>\n"))
450     (widen)
451     (when (message-goto-signature)
452       (forward-line -1)
453       (insert "<p>\n")
454       (while (not (eobp))
455         (end-of-line)
456         (insert "<br>")
457         (forward-line 1)))
458     (message-goto-body)
459     (setq body (buffer-substring (point) (point-max)))
460     (erase-buffer)
461     (nnweb-fetch-form
462      "http://slashdot.org/comments.pl"
463      `(("sid" . ,sid)
464        ("pid" . ,pid)
465        ("rlogin" . "userlogin")
466        ("unickname" . ,nnslashdot-login-name)
467        ("upasswd" . ,nnslashdot-password)
468        ("postersubj" . ,subject)
469        ("op" . "Submit")
470        ("postercomment" . ,body)
471        ("posttype" . "html")))))
472
473 (deffoo nnslashdot-request-delete-group (group &optional force server)
474   (nnslashdot-possibly-change-server group server)
475   (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
476                                 nnslashdot-groups))
477   (nnslashdot-write-groups))
478
479 (nnoo-define-skeleton nnslashdot)
480
481 ;;; Internal functions
482
483 (defun nnslashdot-possibly-change-server (&optional group server)
484   (nnslashdot-init server)
485   (when (and server
486              (not (nnslashdot-server-opened server)))
487     (nnslashdot-open-server server))
488   (unless nnslashdot-groups
489     (nnslashdot-read-groups)))
490
491 (defun nnslashdot-read-groups ()
492   (let ((file (expand-file-name "groups" nnslashdot-directory)))
493     (when (file-exists-p file)
494       (mm-with-unibyte-buffer
495         (insert-file-contents file)
496         (goto-char (point-min))
497         (setq nnslashdot-groups (read (current-buffer)))))))
498
499 (defun nnslashdot-write-groups ()
500   (with-temp-file (expand-file-name "groups" nnslashdot-directory)
501     (prin1 nnslashdot-groups (current-buffer))))
502     
503 (defun nnslashdot-init (server)
504   "Initialize buffers and such."
505   (unless (file-exists-p nnslashdot-directory)
506     (gnus-make-directory nnslashdot-directory))
507   (unless (gnus-buffer-live-p nnslashdot-buffer)
508     (setq nnslashdot-buffer
509           (save-excursion
510             (nnheader-set-temp-buffer
511              (format " *nnslashdot %s*" server))))))
512
513 (defun nnslashdot-date-to-date (sdate)
514   (condition-case err
515       (let ((elem (delete "" (split-string sdate))))
516         (concat (substring (nth 0 elem) 0 3) " "
517                 (substring (nth 1 elem) 0 3) " "
518                 (substring (nth 2 elem) 0 2) " "
519                 (substring (nth 3 elem) 1 6) " "
520                 (format-time-string "%Y") " "
521                 (nth 4 elem)))
522     (error "")))
523
524 (defun nnslashdot-generate-active ()
525   (save-excursion
526     (set-buffer nntp-server-buffer)
527     (erase-buffer)
528     (dolist (elem nnslashdot-groups)
529       (insert (prin1-to-string (car elem))
530               " " (number-to-string (cadr elem)) " 1 y\n"))))
531
532 (defun nnslashdot-lose (why)
533   (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
534
535 (provide 'nnslashdot)
536
537 ;;; nnslashdot.el ends here