* spam-report.el (spam-report-gmane-ham): Renamed from
[gnus] / lisp / nnslashdot.el
1 ;;; nnslashdot.el --- interfacing with Slashdot
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'nnoo)
33 (require 'message)
34 (require 'gnus-util)
35 (require 'gnus)
36 (require 'nnmail)
37 (require 'mm-util)
38 (require 'mm-url)
39
40 (nnoo-declare nnslashdot)
41
42 (defvoo nnslashdot-directory (nnheader-concat gnus-directory "slashdot/")
43   "Where nnslashdot will save its files.")
44
45 (defvoo nnslashdot-active-url "http://slashdot.org/search.pl?section=&min=%d"
46   "Where nnslashdot will fetch the active file from.")
47
48 (defvoo nnslashdot-comments-url "http://slashdot.org/comments.pl?sid=%s&threshold=%d&commentsort=%d&mode=flat&startat=%d"
49   "Where nnslashdot will fetch comments from.")
50
51 (defvoo nnslashdot-article-url
52     "http://slashdot.org/article.pl?sid=%s&mode=nocomment"
53   "Where nnslashdot will fetch the article from.")
54
55 (defvoo nnslashdot-backslash-url "http://slashdot.org/slashdot.xml"
56   "Where nnslashdot will fetch the stories from.")
57
58 (defvoo nnslashdot-use-front-page nil
59   "Use the front page in addition to the backslash page.")
60
61 (defvoo nnslashdot-threshold -1
62   "The article threshold.")
63
64 (defvoo nnslashdot-threaded t
65   "Whether the nnslashdot groups should be threaded or not.")
66
67 (defvoo nnslashdot-group-number 0
68   "The number of non-fresh groups to keep updated.")
69
70 (defvoo nnslashdot-login-name ""
71   "The login name to use when posting.")
72
73 (defvoo nnslashdot-password ""
74   "The password to use when posting.")
75
76 ;;; Internal variables
77
78 (defvar nnslashdot-groups nil)
79 (defvar nnslashdot-buffer nil)
80 (defvar nnslashdot-headers nil)
81
82 ;;; Interface functions
83
84 (nnoo-define-basics nnslashdot)
85
86 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
87   (nnslashdot-possibly-change-server group server)
88   (condition-case why
89       (unless gnus-nov-is-evil
90         (nnslashdot-retrieve-headers-1 articles group))
91     (search-failed (nnslashdot-lose why))))
92
93 (deffoo nnslashdot-retrieve-headers-1 (articles group)
94   (let* ((last (car (last articles)))
95          (start (if nnslashdot-threaded 1 (pop articles)))
96          (entry (assoc group nnslashdot-groups))
97          (sid (nth 2 entry))
98          (first-comments t)
99          headers article subject score from date lines parent point cid
100          s startats changed)
101     (save-excursion
102       (set-buffer nnslashdot-buffer)
103       (let ((case-fold-search t))
104         (erase-buffer)
105         (when (= start 1)
106           (mm-url-insert (format nnslashdot-article-url sid) t)
107           (goto-char (point-min))
108           (if (eobp)
109               (error "Couldn't open connection to slashdot"))
110           (re-search-forward "Posted by[ \t\r\n]+")
111           (when (looking-at "\\(<a[^>]+>\\)?[ \t\r\n]*\\([^<\r\n]+\\)")
112             (setq from (mm-url-decode-entities-string (match-string 2))))
113           (search-forward "on ")
114           (setq date (nnslashdot-date-to-date
115                       (buffer-substring (point) (1- (search-forward "<")))))
116           (setq lines (/ (- (point)
117                             (progn (forward-line 1) (point)))
118                          60))
119           (push
120            (cons
121             1
122             (make-full-mail-header
123              1 group from date
124              (concat "<" sid "%1@slashdot>")
125              "" 0 lines nil nil))
126            headers)
127           (setq start (if nnslashdot-threaded 2 (pop articles))))
128         (while (and start (<= start last))
129           (setq point (goto-char (point-max)))
130           (mm-url-insert
131            (format nnslashdot-comments-url sid
132                    nnslashdot-threshold 0 (- start 2))
133            t)
134           (when (and nnslashdot-threaded first-comments)
135             (setq first-comments nil)
136             (goto-char (point-max))
137             (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
138               (setq s (string-to-number (match-string 1)))
139               (unless (memq s startats)
140                 (push s startats)))
141             (setq startats (sort startats '<)))
142           (setq article (if (and article (< start article)) article start))
143           (goto-char point)
144           (while (re-search-forward
145                   "<a name=\"\\([0-9]+\\)\"><\\(b\\|H4\\)>\\([^<]+\\)</\\(b\\|H4\\)>.*score:\\([^)]+\\))"
146                   nil t)
147             (setq cid (match-string 1)
148                   subject (match-string 3)
149                   score (match-string 5))
150             (unless (assq article (nth 4 entry))
151               (setcar (nthcdr 4 entry) (cons (cons article cid) (nth 4 entry)))
152               (setq changed t))
153             (when (string-match "^Re: *" subject)
154               (setq subject (concat "Re: " (substring subject (match-end 0)))))
155             (setq subject (mm-url-decode-entities-string subject))
156             (search-forward "<BR>")
157             (cond 
158              ((looking-at
159                "by[ \t\n]+<a[^>]+>\\([^<]+\\)</a>[ \t\n]*(\\(<[^>]+>\\)*\\([^<>)]+\\))")
160               (goto-char (- (match-end 0) 5))
161               (setq from (concat
162                           (mm-url-decode-entities-string (match-string 1))
163                           " <" (match-string 3) ">")))
164              ((looking-at "by[ \t\n]+<a[^>]+>\\([^<(]+\\) (\\([0-9]+\\))</a>")
165               (goto-char (- (match-end 0) 5))
166               (setq from (concat 
167                           (mm-url-decode-entities-string (match-string 1))
168                           " <" (match-string 2) ">")))
169              ((looking-at "by \\([^<>]*\\)[\t\n\r ]+on ")
170               (goto-char (- (match-end 0) 5))
171               (setq from (mm-url-decode-entities-string (match-string 1))))
172              (t
173               (setq from "")))
174             (search-forward "on ")
175             (setq date
176                   (nnslashdot-date-to-date
177                    (buffer-substring (point) (progn (skip-chars-forward "^()<>\n\r") (point)))))
178             (setq lines (/ (abs (- (search-forward "<td")
179                                    (search-forward "</td>")))
180                            70))
181             (if (not
182                  (re-search-forward ".*cid=\\([0-9]+\\)\">Parent</A>" nil t))
183                 (setq parent nil)
184               (setq parent (match-string 1))
185               (when (string= parent "0")
186                 (setq parent nil)))
187             (push
188              (cons
189               article
190               (make-full-mail-header
191                article
192                (concat subject " (" score ")")
193                from date
194                (concat "<" sid "%" cid "@slashdot>")
195                (if parent
196                    (concat "<" sid "%" parent "@slashdot>")
197                  "")
198                0 lines nil nil))
199              headers)
200             (while (and articles (<= (car articles) article))
201               (pop articles))
202             (setq article (1+ article)))
203           (if nnslashdot-threaded
204               (progn
205                 (setq start (pop startats))
206                 (if start (setq start (+ start 2))))
207             (setq start (pop articles))))))
208     (if changed (nnslashdot-write-groups))
209     (setq nnslashdot-headers (sort headers 'car-less-than-car))
210     (save-excursion
211       (set-buffer nntp-server-buffer)
212       (erase-buffer)
213       (mm-with-unibyte-current-buffer
214        (dolist (header nnslashdot-headers)
215          (nnheader-insert-nov (cdr header)))))
216     'nov))
217
218 (deffoo nnslashdot-request-group (group &optional server dont-check)
219   (nnslashdot-possibly-change-server nil server)
220   (let ((elem (assoc group nnslashdot-groups)))
221     (cond
222      ((not elem)
223       (nnheader-report 'nnslashdot "Group does not exist"))
224      (t
225       (nnheader-report 'nnslashdot "Opened group %s" group)
226       (nnheader-insert
227        "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
228        (prin1-to-string group))))))
229
230 (deffoo nnslashdot-close-group (group &optional server)
231   (nnslashdot-possibly-change-server group server)
232   (when (gnus-buffer-live-p nnslashdot-buffer)
233     (save-excursion
234       (set-buffer nnslashdot-buffer)
235       (kill-buffer nnslashdot-buffer)))
236   t)
237
238 (deffoo nnslashdot-request-article (article &optional group server buffer)
239   (nnslashdot-possibly-change-server group server)
240   (let (contents cid)
241     (condition-case why
242         (save-excursion
243           (set-buffer nnslashdot-buffer)
244           (let ((case-fold-search t))
245             (goto-char (point-min))
246             (when (and (stringp article)
247                        (string-match "%\\([0-9]+\\)@" article))
248               (setq cid (match-string 1 article))
249               (let ((map (nth 4 (assoc group nnslashdot-groups))))
250                 (while map
251                   (if (equal (cdar map) cid)
252                       (setq article (caar map)
253                             map nil)
254                     (setq map (cdr map))))))
255             (when (numberp article)
256               (if (= article 1)
257                   (progn
258                     (search-forward "Posted by")
259                     (search-forward "<BR>")
260                     (setq contents
261                           (buffer-substring
262                            (point)
263                            (progn
264                              (re-search-forward
265                               "<IFRAME\\|<SCRIPT LANGUAGE=\"JAVASCRIPT\">\\|<!-- no ad 6 -->\\|&lt;&nbsp;[ \t\r\n]*<A HREF=\"\\(\\(http:\\)?//slashdot\\.org\\)?/article")
266                              (match-beginning 0)))))
267                 (setq cid (cdr (assq article
268                                      (nth 4 (assoc group nnslashdot-groups)))))
269                 (search-forward (format "<a name=\"%s\">" cid))
270                 (setq contents
271                       (buffer-substring
272                        (re-search-forward "<td[^>]*>")
273                        (search-forward "</td>")))))))
274       (search-failed (nnslashdot-lose why)))
275
276     (when contents
277       (save-excursion
278         (set-buffer (or buffer nntp-server-buffer))
279         (erase-buffer)
280         (mm-with-unibyte-current-buffer
281           (insert contents)
282           (goto-char (point-min))
283           (while (re-search-forward "\\(<br>\r?\\)+" nil t)
284             (replace-match "<p>" t t))
285           (goto-char (point-min))
286           (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
287           (insert "Newsgroups: " (caddr (assoc group nnslashdot-groups))
288                   "\n")
289           (let ((header (cdr (assq article nnslashdot-headers))))
290             (nnheader-insert-header header))
291           (nnheader-report 'nnslashdot "Fetched article %s" article))
292         (cons group article)))))
293
294 (deffoo nnslashdot-close-server (&optional server)
295   (when (and (nnslashdot-server-opened server)
296              (gnus-buffer-live-p nnslashdot-buffer))
297     (save-excursion
298       (set-buffer nnslashdot-buffer)
299       (kill-buffer nnslashdot-buffer)))
300   (nnoo-close-server 'nnslashdot server))
301
302 (deffoo nnslashdot-request-list (&optional server)
303   (nnslashdot-possibly-change-server nil server)
304   (let ((number 0)
305         (first nnslashdot-use-front-page)
306         sid elem description articles gname)
307     (condition-case why
308         ;; First we do the Ultramode to get info on all the latest groups.
309         (progn
310           (mm-with-unibyte-buffer
311             (mm-url-insert nnslashdot-backslash-url t)
312             (goto-char (point-min))
313             (if (eobp)
314                 (error "Couldn't open connection to slashdot"))
315             (while (search-forward "<story>" nil t)
316               (narrow-to-region (point) (search-forward "</story>"))
317               (goto-char (point-min))
318               (re-search-forward "<title>\\([^<]+\\)</title>")
319               (setq description
320                     (mm-url-decode-entities-string (match-string 1)))
321               (re-search-forward "<url>\\([^<]+\\)</url>")
322               (setq sid (match-string 1))
323               (string-match "sid=\\([0-9/]+\\)\\(.shtml\\|$\\)" sid)
324               (setq sid (match-string 1 sid))
325               (re-search-forward "<comments>\\([^<]+\\)</comments>")
326               (setq articles (string-to-number (match-string 1)))
327               (setq gname (concat description " (" sid ")"))
328               (if (setq elem (assoc gname nnslashdot-groups))
329                   (setcar (cdr elem) articles)
330                 (push (list gname articles sid (current-time) nil)
331                       nnslashdot-groups))
332               (goto-char (point-max))
333               (widen)))
334           ;; Then do the older groups.
335           (while (or first
336                      (> (- nnslashdot-group-number number) 0))
337             (setq first nil)
338             (mm-with-unibyte-buffer
339               (let ((case-fold-search t))
340                 (mm-url-insert (format nnslashdot-active-url number) t)
341                 (goto-char (point-min))
342                 (while (re-search-forward
343                         "article.pl\\?sid=\\([^&]+\\).*>\\([^<]+\\)</a>"
344                         nil t)
345                   (setq sid (match-string 1)
346                         description
347                         (mm-url-decode-entities-string (match-string 2)))
348                   (forward-line 1)
349                   (when (re-search-forward "with \\([0-9]+\\) comment" nil t)
350                     (setq articles (1+ (string-to-number (match-string 1)))))
351                   (setq gname (concat description " (" sid ")"))
352                   (if (setq elem (assoc gname nnslashdot-groups))
353                       (setcar (cdr elem) articles)
354                     (push (list gname articles sid (current-time) nil)
355                           nnslashdot-groups)))))
356             (incf number 30)))
357       (search-failed (nnslashdot-lose why)))
358     (nnslashdot-write-groups)
359     (nnslashdot-generate-active)
360     t))
361
362 (deffoo nnslashdot-request-newgroups (date &optional server)
363   (nnslashdot-possibly-change-server nil server)
364   (nnslashdot-generate-active)
365   t)
366
367 (deffoo nnslashdot-request-post (&optional server)
368   (nnslashdot-possibly-change-server nil server)
369   (let ((sid (message-fetch-field "newsgroups"))
370         (subject (message-fetch-field "subject"))
371         (references (car (last (split-string
372                                 (message-fetch-field "references")))))
373         body quoted pid)
374     (string-match "%\\([0-9]+\\)@slashdot" references)
375     (setq pid (match-string 1 references))
376     (message-goto-body)
377     (narrow-to-region (point) (progn (message-goto-signature) (point)))
378     (goto-char (point-min))
379     (while (not (eobp))
380       (if (looking-at "> ")
381           (progn
382             (delete-region (point) (+ (point) 2))
383             (unless quoted
384               (insert "<blockquote>\n"))
385             (setq quoted t))
386         (when quoted
387           (insert "</blockquote>\n")
388           (setq quoted nil)))
389       (forward-line 1))
390     (goto-char (point-min))
391     (while (re-search-forward "^ *\n" nil t)
392       (replace-match "<p>\n"))
393     (widen)
394     (when (message-goto-signature)
395       (forward-line -1)
396       (insert "<p>\n")
397       (while (not (eobp))
398         (end-of-line)
399         (insert "<br>")
400         (forward-line 1)))
401     (message-goto-body)
402     (setq body (buffer-substring (point) (point-max)))
403     (erase-buffer)
404     (mm-url-fetch-form
405      "http://slashdot.org/comments.pl"
406      `(("sid" . ,sid)
407        ("pid" . ,pid)
408        ("rlogin" . "userlogin")
409        ("unickname" . ,nnslashdot-login-name)
410        ("upasswd" . ,nnslashdot-password)
411        ("postersubj" . ,subject)
412        ("op" . "Submit")
413        ("postercomment" . ,body)
414        ("posttype" . "html")))))
415
416 (deffoo nnslashdot-request-delete-group (group &optional force server)
417   (nnslashdot-possibly-change-server group server)
418   (setq nnslashdot-groups (delq (assoc group nnslashdot-groups)
419                                 nnslashdot-groups))
420   (nnslashdot-write-groups))
421
422 (deffoo nnslashdot-request-close ()
423   (setq nnslashdot-headers nil
424         nnslashdot-groups nil))
425
426 (deffoo nnslashdot-request-expire-articles
427     (articles group &optional server force)
428   (nnslashdot-possibly-change-server group server)
429   (let ((item (assoc group nnslashdot-groups)))
430     (when item
431       (if (fourth item)
432           (when (and (>= (length articles) (cadr item)) ;; All are expirable.
433                      (nnmail-expired-article-p
434                       group
435                       (fourth item)
436                       force))
437             (setq nnslashdot-groups (delq item nnslashdot-groups))
438             (nnslashdot-write-groups)
439             (setq articles nil)) ;; all expired.
440         (setcdr (cddr item) (list (current-time)))
441         (nnslashdot-write-groups))))
442   articles)
443
444 (nnoo-define-skeleton nnslashdot)
445
446 ;;; Internal functions
447
448 (defun nnslashdot-possibly-change-server (&optional group server)
449   (nnslashdot-init server)
450   (when (and server
451              (not (nnslashdot-server-opened server)))
452     (nnslashdot-open-server server))
453   (unless nnslashdot-groups
454     (nnslashdot-read-groups)))
455
456 (defun nnslashdot-make-tuple (tuple n)
457   (prog1
458       tuple
459     (while (> n 1)
460       (unless (cdr tuple)
461         (setcdr tuple (list nil)))
462       (setq tuple (cdr tuple)
463             n (1- n)))))
464
465 (defun nnslashdot-read-groups ()
466   (let ((file (expand-file-name "groups" nnslashdot-directory)))
467     (when (file-exists-p file)
468       (mm-with-unibyte-buffer
469         (insert-file-contents file)
470         (goto-char (point-min))
471         (setq nnslashdot-groups (read (current-buffer))))
472       (when (and nnslashdot-groups (< (length (car nnslashdot-groups)) 5))
473         (dolist (group nnslashdot-groups)
474           (nnslashdot-make-tuple group 5))))))
475
476 (defun nnslashdot-write-groups ()
477   (with-temp-file (expand-file-name "groups" nnslashdot-directory)
478     (gnus-prin1 nnslashdot-groups)))
479
480 (defun nnslashdot-init (server)
481   "Initialize buffers and such."
482   (unless (file-exists-p nnslashdot-directory)
483     (gnus-make-directory nnslashdot-directory))
484   (unless (gnus-buffer-live-p nnslashdot-buffer)
485     (setq nnslashdot-buffer
486           (save-excursion
487             (nnheader-set-temp-buffer
488              (format " *nnslashdot %s*" server))))
489     (push nnslashdot-buffer gnus-buffers)))
490
491 (defun nnslashdot-date-to-date (sdate)
492   (condition-case err
493       (let ((elem (delete "" (split-string sdate))))
494         (concat (substring (nth 0 elem) 0 3) " "
495                 (substring (nth 1 elem) 0 3) " "
496                 (substring (nth 2 elem) 0 2) " "
497                 (substring (nth 3 elem) 1 6) " "
498                 (format-time-string "%Y") " "
499                 (nth 4 elem)))
500     (error "")))
501
502 (defun nnslashdot-generate-active ()
503   (save-excursion
504     (set-buffer nntp-server-buffer)
505     (erase-buffer)
506     (dolist (elem nnslashdot-groups)
507       (when (numberp (cadr elem))
508         (insert (prin1-to-string (car elem))
509                 " " (number-to-string (cadr elem)) " 1 y\n")))))
510
511 (defun nnslashdot-lose (why)
512   (error "Slashdot HTML has changed; please get a new version of nnslashdot"))
513
514 (provide 'nnslashdot)
515
516 ;;; arch-tag: aa73df7a-f7e6-4eef-bdea-5ce2f8c691b3
517 ;;; nnslashdot.el ends here