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