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