f3a6f6f9135057333a4343b024a6d80474717d98
[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
33 (require 'nnoo)
34 (require 'message)
35 (require 'gnus-util)
36 (require 'gnus)
37 (require 'nnmail)
38 (require 'mm-util)
39 (require 'mm-url)
40
41 (nnoo-declare nnslashdot)
42
43 (defvoo nnslashdot-directory (nnheader-concat gnus-directory "slashdot/")
44   "Where nnslashdot will save its files.")
45
46 (defvoo nnslashdot-active-url "http://slashdot.org/search.pl?section=&min=%d"
47   "Where nnslashdot will fetch the active file from.")
48
49 (defvoo nnslashdot-comments-url "http://slashdot.org/comments.pl?sid=%s&threshold=%d&commentsort=%d&mode=flat&startat=%d"
50   "Where nnslashdot will fetch comments from.")
51
52 (defvoo nnslashdot-article-url
53     "http://slashdot.org/article.pl?sid=%s&mode=nocomment"
54   "Where nnslashdot will fetch the article from.")
55
56 (defvoo nnslashdot-backslash-url "http://slashdot.org/slashdot.xml"
57   "Where nnslashdot will fetch the stories from.")
58
59 (defvoo nnslashdot-threshold -1
60   "The article threshold.")
61
62 (defvoo nnslashdot-threaded t
63   "Whether the nnslashdot groups should be threaded or not.")
64
65 (defvoo nnslashdot-group-number 0
66   "The number of non-fresh groups to keep updated.")
67
68 (defvoo nnslashdot-login-name ""
69   "The login name to use when posting.")
70
71 (defvoo nnslashdot-password ""
72   "The password to use when posting.")
73
74 ;;; Internal variables
75
76 (defvar nnslashdot-groups nil)
77 (defvar nnslashdot-buffer nil)
78 (defvar nnslashdot-headers nil)
79
80 ;;; Interface functions
81
82 (nnoo-define-basics nnslashdot)
83
84 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
85   (nnslashdot-possibly-change-server group server)
86   (condition-case why
87       (unless gnus-nov-is-evil
88         (nnslashdot-retrieve-headers-1 articles group))
89     (search-failed (nnslashdot-lose why))))
90
91 (deffoo nnslashdot-retrieve-headers-1 (articles group)
92   (let* ((last (car (last articles)))
93          (start (if nnslashdot-threaded 1 (pop articles)))
94          (entry (assoc group nnslashdot-groups))
95          (sid (nth 2 entry))
96          (first-comments t)
97          headers article subject score from date lines parent point cid 
98          s startats changed)
99     (save-excursion
100       (set-buffer nnslashdot-buffer)
101       (let ((case-fold-search t))
102         (erase-buffer)
103         (when (= start 1)
104           (mm-url-insert (format nnslashdot-article-url
105                                 (nnslashdot-sid-strip sid)) t)
106           (goto-char (point-min))
107           (re-search-forward "Posted by[ \t\r\n]+")
108           (when (looking-at "\\(<a[^>]+>\\)?[ \t\r\n]*\\([^<\r\n]+\\)")
109             (setq from (mm-url-decode-entities-string (match-string 2))))
110           (search-forward "on ")
111           (setq date (nnslashdot-date-to-date
112                       (buffer-substring (point) (1- (search-forward "<")))))
113           (setq lines (/ (- (point)
114                             (progn (forward-line 1) (point)))
115                          60))
116           (push
117            (cons
118             1
119             (make-full-mail-header
120              1 group from date
121              (concat "<" (nnslashdot-sid-strip sid) "%1@slashdot>")
122              "" 0 lines nil nil))
123            headers)
124           (setq start (if nnslashdot-threaded 2 (pop articles))))
125         (while (and start (<= start last))
126           (setq point (goto-char (point-max)))
127           (mm-url-insert
128            (format nnslashdot-comments-url
129                    (nnslashdot-sid-strip sid)
130                    nnslashdot-threshold 0 (- start 2))
131            t)
132           (when (and nnslashdot-threaded first-comments)
133             (setq first-comments nil)
134             (goto-char (point-max))
135             (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
136               (setq s (string-to-number (match-string 1)))
137               (unless (memq s startats)
138                 (push s startats)))
139             (setq startats (sort startats '<)))
140           (setq article (if (and article (< start article)) article start))
141           (goto-char point)
142           (while (re-search-forward
143                   "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
144                   nil t)
145             (setq cid (match-string 1)
146                   subject (match-string 3)
147                   score (match-string 5))
148             (unless (assq article (nth 4 entry))
149               (setcar (nthcdr 4 entry) (cons (cons article cid) (nth 4 entry)))
150               (setq changed t))
151             (when (string-match "^Re: *" subject)
152               (setq subject (concat "Re: " (substring subject (match-end 0)))))
153             (setq subject (mm-url-decode-entities-string subject))
154             (search-forward "<BR>")
155             (if (looking-at
156                  "by[ \t\n]+<a[^>]+>\\([^<]+\\)</a>[ \t\n]*(\\(<[^>]+>\\)*\\([^<>)]+\\))")
157                 (progn
158                   (goto-char (- (match-end 0) 5))
159                   (setq from (concat
160                               (mm-url-decode-entities-string (match-string 1))
161                               " <" (match-string 3) ">")))
162               (setq from "")
163               (when (looking-at "by \\([^<>]*\\) on ")
164                 (goto-char (- (match-end 0) 5))
165                 (setq from (mm-url-decode-entities-string (match-string 1)))))
166             (search-forward " on ")
167             (setq date
168                   (nnslashdot-date-to-date
169                    (buffer-substring (point) (progn (skip-chars-forward "^()<>\n\r") (point)))))
170             (setq lines (/ (abs (- (search-forward "<td")
171                                    (search-forward "</td>")))
172                            70))
173             (if (not
174                  (re-search-forward ".*cid=\\([0-9]+\\)\">Parent</A>" nil t))
175                 (setq parent nil)
176               (setq parent (match-string 1))
177               (when (string= parent "0")
178                 (setq parent nil)))
179             (push
180              (cons
181               article
182               (make-full-mail-header
183                article
184                (concat subject " (" score ")")
185                from date
186                (concat "<" (nnslashdot-sid-strip sid) "%" cid "@slashdot>")
187                (if parent
188                    (concat "<" (nnslashdot-sid-strip sid) "%" 
189                            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                               "&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         sid elem description articles gname)
300     (condition-case why
301         ;; First we do the Ultramode to get info on all the latest groups.
302         (progn
303           (mm-with-unibyte-buffer
304             (mm-url-insert nnslashdot-backslash-url t)
305             (goto-char (point-min))
306             (while (search-forward "<story>" nil t)
307               (narrow-to-region (point) (search-forward "</story>"))
308               (goto-char (point-min))
309               (re-search-forward "<title>\\([^<]+\\)</title>")
310               (setq description
311                     (mm-url-decode-entities-string (match-string 1)))
312               (re-search-forward "<url>\\([^<]+\\)</url>")
313               (setq sid (match-string 1))
314               (string-match "sid=\\([0-9/]+\\)\\(.shtml\\|$\\)" sid)
315               (setq sid (match-string 1 sid))
316               (re-search-forward "<comments>\\([^<]+\\)</comments>")
317               (setq articles (string-to-number (match-string 1)))
318               (setq gname (concat description " (" sid ")"))
319               (if (setq elem (assoc gname nnslashdot-groups))
320                   (setcar (cdr elem) articles)
321                 (push (list gname articles sid (current-time) nil)
322                       nnslashdot-groups))
323               (goto-char (point-max))
324               (widen)))
325           ;; Then do the older groups.
326           (while (> (- nnslashdot-group-number number) 0)
327             (mm-with-unibyte-buffer
328               (let ((case-fold-search t))
329                 (mm-url-insert (format nnslashdot-active-url number) t)
330                 (goto-char (point-min))
331                 (while (re-search-forward
332                         "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>"
333                         nil t)
334                   (setq sid (match-string 1)
335                         description
336                         (mm-url-decode-entities-string (match-string 2)))
337                   (forward-line 1)
338                   (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
339                     (setq articles (string-to-number (match-string 1))))
340                   (setq gname (concat description " (" sid ")"))
341                   (if (setq elem (assoc gname nnslashdot-groups))
342                       (setcar (cdr elem) articles)
343                     (push (list gname articles sid (current-time) nil)
344                           nnslashdot-groups)))))
345             (incf number 30)))
346       (search-failed (nnslashdot-lose why)))
347     (nnslashdot-write-groups)
348     (nnslashdot-generate-active)
349     t))
350
351 (deffoo nnslashdot-request-newgroups (date &optional server)
352   (nnslashdot-possibly-change-server nil server)
353   (nnslashdot-generate-active)
354   t)
355
356 (deffoo nnslashdot-request-post (&optional server)
357   (nnslashdot-possibly-change-server nil server)
358   (let ((sid (nnslashdot-sid-strip (message-fetch-field "newsgroups")))
359         (subject (message-fetch-field "subject"))
360         (references (car (last (split-string
361                                 (message-fetch-field "references")))))
362         body quoted pid)
363     (string-match "%\\([0-9]+\\)@slashdot" references)
364     (setq pid (match-string 1 references))
365     (message-goto-body)
366     (narrow-to-region (point) (progn (message-goto-signature) (point)))
367     (goto-char (point-min))
368     (while (not (eobp))
369       (if (looking-at "> ")
370           (progn
371             (delete-region (point) (+ (point) 2))
372             (unless quoted
373               (insert "<blockquote>\n"))
374             (setq quoted t))
375         (when quoted
376           (insert "</blockquote>\n")
377           (setq quoted nil)))
378       (forward-line 1))
379     (goto-char (point-min))
380     (while (re-search-forward "^ *\n" nil t)
381       (replace-match "<p>\n"))
382     (widen)
383     (when (message-goto-signature)
384       (forward-line -1)
385       (insert "<p>\n")
386       (while (not (eobp))
387         (end-of-line)
388         (insert "<br>")
389         (forward-line 1)))
390     (message-goto-body)
391     (setq body (buffer-substring (point) (point-max)))
392     (erase-buffer)
393     (mm-url-fetch-form
394      "http://slashdot.org/comments.pl"
395      `(("sid" . ,sid)
396        ("pid" . ,pid)
397        ("rlogin" . "userlogin")
398        ("unickname" . ,nnslashdot-login-name)
399        ("upasswd" . ,nnslashdot-password)
400        ("postersubj" . ,subject)
401        ("op" . "Submit")
402        ("postercomment" . ,body)
403        ("posttype" . "html")))))
404
405 (deffoo nnslashdot-request-delete-group (group &optional force server)
406   (nnslashdot-possibly-change-server group server)
407   (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
408                                 nnslashdot-groups))
409   (nnslashdot-write-groups))
410
411 (deffoo nnslashdot-request-close ()
412   (setq nnslashdot-headers nil
413         nnslashdot-groups nil))
414
415 (deffoo nnslashdot-request-expire-articles
416     (articles group &optional server force)
417   (nnslashdot-possibly-change-server group server)
418   (let ((item (assoc group nnslashdot-groups)))
419     (when item
420       (if (fourth item)
421           (when (and (>= (length articles) (cadr item)) ;; All are expirable.
422                      (nnmail-expired-article-p
423                       group
424                       (fourth item)
425                       force))
426             (setq nnslashdot-groups (delq item nnslashdot-groups))
427             (nnslashdot-write-groups)
428             (setq articles nil)) ;; all expired.
429         (setcdr (cddr item) (list (current-time)))
430         (nnslashdot-write-groups))))
431   articles)
432
433 (nnoo-define-skeleton nnslashdot)
434
435 ;;; Internal functions
436
437 (defun nnslashdot-possibly-change-server (&optional group server)
438   (nnslashdot-init server)
439   (when (and server
440              (not (nnslashdot-server-opened server)))
441     (nnslashdot-open-server server))
442   (unless nnslashdot-groups
443     (nnslashdot-read-groups)))
444
445 (defun nnslashdot-make-tuple (tuple n)
446   (prog1
447       tuple
448     (while (> n 1)
449       (unless (cdr tuple)
450         (setcdr tuple (list nil)))
451       (setq tuple (cdr tuple)
452             n (1- n)))))
453
454 (defun nnslashdot-read-groups ()
455   (let ((file (expand-file-name "groups" nnslashdot-directory)))
456     (when (file-exists-p file)
457       (mm-with-unibyte-buffer
458         (insert-file-contents file)
459         (goto-char (point-min))
460         (setq nnslashdot-groups (read (current-buffer))))
461       (if (and nnslashdot-groups (< (length (car nnslashdot-groups)) 5))
462           (let ((groups nnslashdot-groups))
463             (while groups
464               (nnslashdot-make-tuple (car groups) 5)
465               (setq groups (cdr groups))))))))
466
467 (defun nnslashdot-write-groups ()
468   (with-temp-file (expand-file-name "groups" nnslashdot-directory)
469     (gnus-prin1 nnslashdot-groups)))
470
471 (defun nnslashdot-init (server)
472   "Initialize buffers and such."
473   (unless (file-exists-p nnslashdot-directory)
474     (gnus-make-directory nnslashdot-directory))
475   (unless (gnus-buffer-live-p nnslashdot-buffer)
476     (setq nnslashdot-buffer
477           (save-excursion
478             (nnheader-set-temp-buffer
479              (format " *nnslashdot %s*" server))))
480     (push nnslashdot-buffer gnus-buffers)))
481
482 (defun nnslashdot-date-to-date (sdate)
483   (condition-case err
484       (let ((elem (delete "" (split-string sdate))))
485         (concat (substring (nth 0 elem) 0 3) " "
486                 (substring (nth 1 elem) 0 3) " "
487                 (substring (nth 2 elem) 0 2) " "
488                 (substring (nth 3 elem) 1 6) " "
489                 (format-time-string "%Y") " "
490                 (nth 4 elem)))
491     (error "")))
492
493 (defun nnslashdot-generate-active ()
494   (save-excursion
495     (set-buffer nntp-server-buffer)
496     (erase-buffer)
497     (dolist (elem nnslashdot-groups)
498       (insert (prin1-to-string (car elem))
499               " " (number-to-string (cadr elem)) " 1 y\n"))))
500
501 (defun nnslashdot-lose (why)
502   (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
503
504 (defalias 'nnslashdot-sid-strip 'identity)
505
506 (provide 'nnslashdot)
507
508 ;;; nnslashdot.el ends here