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