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