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