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