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