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