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