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