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