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