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