* gnus.el (gnus-valid-select-methods): nnslashdot is news.
[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           (forward-line 1)
244           (if (looking-at
245                "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
246               (setq from (concat (match-string 1) " <" (match-string 2) ">"))
247             (looking-at "by \\(.+\\) on ")
248             (setq from (match-string 1)))
249           (goto-char (- (match-end 0) 5))
250           (search-forward " on ")
251           (setq date
252                 (nnslashdot-date-to-date
253                  (buffer-substring (point) (progn (end-of-line) (point)))))
254           (setq lines (/ (abs (- (search-forward "<td ")
255                                  (search-forward "</td>")))
256                          70))
257           (forward-line 2)
258           (setq parent
259                 (if (looking-at ".*cid=\\([0-9]+\\)")
260                     (match-string 1)
261                   nil))
262           (setq did t)
263           (push
264            (cons
265             (1+ article)
266             (make-full-mail-header
267              (1+ article) (concat subject " (" score ")")
268              from date
269              (concat "<" sid "%"
270                      (number-to-string (1+ article)) 
271                      "@slashdot>")
272              (if parent
273                  (concat "<" sid "%"
274                          (number-to-string (1+ (string-to-number parent)))
275                          "@slashdot>")
276                "")
277              0 lines nil nil))
278            headers))))
279     (setq nnslashdot-headers
280           (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
281     (save-excursion
282       (set-buffer nntp-server-buffer)
283       (erase-buffer)
284       (dolist (header nnslashdot-headers)
285         (nnheader-insert-nov (cdr header))))
286     'nov))
287
288 (deffoo nnslashdot-request-group (group &optional server dont-check)
289   (nnslashdot-possibly-change-server nil server)
290   (let ((elem (assoc group nnslashdot-groups)))
291     (cond
292      ((not elem)
293       (nnheader-report 'nnslashdot "Group does not exist"))
294      (t
295       (nnheader-report 'nnslashdot "Opened group %s" group)
296       (nnheader-insert
297        "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
298        (prin1-to-string group))))))
299
300 (deffoo nnslashdot-close-group (group &optional server)
301   (nnslashdot-possibly-change-server group server)
302   (when (gnus-buffer-live-p nnslashdot-buffer)
303     (save-excursion
304       (set-buffer nnslashdot-buffer)
305       (kill-buffer nnslashdot-buffer)))
306   t)
307
308 (deffoo nnslashdot-request-article (article &optional group server buffer)
309   (nnslashdot-possibly-change-server group server)
310   (let (contents)
311     (save-excursion
312       (set-buffer nnslashdot-buffer)
313       (let ((case-fold-search t))
314         (goto-char (point-min))
315         (when (and (stringp article)
316                    (string-match "%\\([0-9]+\\)@" article))
317           (setq article (string-to-number (match-string 1 article))))
318         (when (numberp article)
319           (if (= article 1)
320               (progn
321                 (re-search-forward "Posted by .* on ")
322                 (forward-line 1)
323                 (setq contents
324                       (buffer-substring
325                        (point)
326                        (progn
327                          (re-search-forward
328                           "<p>.*A href=http://slashdot.org/article.pl")
329                          (match-beginning 0)))))
330             (search-forward (format "<a name=\"%d\">" (1- article)))
331             (setq contents
332                   (buffer-substring
333                    (re-search-forward "<td[^>]+>")
334                    (search-forward "</td>")))))))
335     (when contents
336       (save-excursion
337         (set-buffer (or buffer nntp-server-buffer))
338         (erase-buffer)
339         (insert contents)
340         (goto-char (point-min))
341         (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
342         (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
343                 "\n")
344         (let ((header (cdr (assq article nnslashdot-headers))))
345           (nnheader-insert-header header))
346         (nnheader-report 'nnslashdot "Fetched article %s" article)
347         (cons group article)))))
348
349 (deffoo nnslashdot-close-server (&optional server)
350   (when (and (nnslashdot-server-opened server)
351              (gnus-buffer-live-p nnslashdot-buffer))
352     (save-excursion
353       (set-buffer nnslashdot-buffer)
354       (kill-buffer nnslashdot-buffer)))
355   (nnoo-close-server 'nnslashdot server))
356
357 (deffoo nnslashdot-request-list (&optional server)
358   (nnslashdot-possibly-change-server nil server)
359   (let ((number 0)
360         sid elem description articles gname)
361     ;; First we do the Ultramode to get info on all the latest groups.
362     (with-temp-buffer
363       (nnweb-insert "http://slashdot.org/slashdot.xml")
364       (goto-char (point-min))
365       (while (search-forward "<story>" nil t)
366         (narrow-to-region (point) (search-forward "</story>"))
367         (goto-char (point-min))
368         (re-search-forward "<title>\\([^<]+\\)</title>")
369         (setq description (match-string 1))
370         (re-search-forward "<url>\\([^<]+\\)</url>")
371         (setq sid (match-string 1))
372         (string-match "/\\([0-9/]+\\).shtml" sid)
373         (setq sid (match-string 1 sid))
374         (re-search-forward "<comments>\\([^<]+\\)</comments>")
375         (setq articles (string-to-number (match-string 1)))
376         (setq gname (concat description " (" sid ")"))
377         (if (setq elem (assoc gname nnslashdot-groups))
378             (setcar (cdr elem) articles)
379           (push (list gname articles sid) nnslashdot-groups))
380         (goto-char (point-max))
381         (widen)))
382     ;; Then do the older groups.
383     (while (> (- nnslashdot-group-number number) 0)
384       (with-temp-buffer
385         (let ((case-fold-search t))
386           (nnweb-insert (format nnslashdot-active-url number))
387           (goto-char (point-min))
388           (while (re-search-forward
389                   "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
390             (setq sid (match-string 1)
391                   description (match-string 2))
392             (forward-line 1)
393             (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
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       (incf number 30))
400     (nnslashdot-write-groups)
401     (nnslashdot-generate-active)
402     t))
403
404 (deffoo nnslashdot-request-newgroups (date &optional server)
405   (nnslashdot-possibly-change-server nil server)
406   (nnslashdot-generate-active)
407   t)
408
409 (deffoo nnslashdot-request-post (&optional server)
410   (nnslashdot-possibly-change-server nil server)
411   (let ((sid (message-fetch-field "newsgroups"))
412         (subject (message-fetch-field "subject"))
413         (references (car (last (split-string
414                                 (message-fetch-field "references")))))
415         body quoted pid)
416     (string-match "%\\([0-9]+\\)@slashdot" references)
417     (setq pid (match-string 1 references))
418     (message-goto-body)
419     (narrow-to-region (point) (progn (message-goto-signature) (point)))
420     (goto-char (point-min))
421     (while (not (eobp))
422       (if (looking-at "> ")
423           (progn
424             (delete-region (point) (+ (point) 2))
425             (unless quoted
426               (insert "<blockquote>\n"))
427             (setq quoted t))
428         (when quoted
429           (insert "</blockquote>\n")
430           (setq quoted nil)))
431       (forward-line 1))
432     (widen)
433     (when (message-goto-signature)
434       (forward-line -1)
435       (insert "<p>\n")
436       (while (not (eobp))
437         (end-of-line)
438         (insert "<br>")
439         (forward-line 1)))
440     (message-goto-body)
441     (setq body (buffer-substring (point) (point-max)))
442     (erase-buffer)
443     (nnweb-fetch-form
444      "http://slashdot.org/comments.pl"
445      `(("sid" . ,sid)
446        ("pid" . ,pid)
447        ("rlogin" . "userlogin")
448        ("unickname" . ,nnslashdot-login-name)
449        ("upasswd" . ,nnslashdot-password)
450        ("postersubj" . ,subject)
451        ("op" . "Submit")
452        ("postercomment" . ,body)
453        ("posttype" . "html")))))
454
455 (nnoo-define-skeleton nnslashdot)
456
457 ;;; Internal functions
458
459 (defun nnslashdot-possibly-change-server (&optional group server)
460   (nnslashdot-init server)
461   (when (and server
462              (not (nnslashdot-server-opened server)))
463     (nnslashdot-open-server server))
464   (unless nnslashdot-groups
465     (nnslashdot-read-groups)))
466
467 (defun nnslashdot-read-groups ()
468   (let ((file (expand-file-name "groups" nnslashdot-directory)))
469     (when (file-exists-p file)
470       (with-temp-buffer
471         (insert-file-contents file)
472         (goto-char (point-min))
473         (setq nnslashdot-groups (read (current-buffer)))))))
474
475 (defun nnslashdot-write-groups ()
476   (with-temp-file (expand-file-name "groups" nnslashdot-directory)
477     (prin1 nnslashdot-groups (current-buffer))))
478     
479 (defun nnslashdot-init (server)
480   "Initialize buffers and such."
481   (unless (file-exists-p nnslashdot-directory)
482     (gnus-make-directory nnslashdot-directory))
483   (unless (gnus-buffer-live-p nnslashdot-buffer)
484     (setq nnslashdot-buffer
485           (save-excursion
486             (nnheader-set-temp-buffer
487              (format " *nnslashdot %s*" server))))))
488
489 (defun nnslashdot-date-to-date (sdate)
490   (let ((elem (delete "" (split-string sdate))))
491     (concat (substring (nth 0 elem) 0 3) " "
492             (substring (nth 1 elem) 0 3) " "
493             (substring (nth 2 elem) 0 2) " "
494             (substring (nth 3 elem) 1 6) " "
495             (format-time-string "%Y") " "
496             (nth 4 elem))))
497
498 (defun nnslashdot-generate-active ()
499   (save-excursion
500     (set-buffer nntp-server-buffer)
501     (erase-buffer)
502     (dolist (elem nnslashdot-groups)
503       (insert (prin1-to-string (car elem))
504               " " (number-to-string (cadr elem)) " 1 y\n"))))
505
506 (provide 'nnslashdot)
507
508 ;;; nnslashdot.el ends here