1 ;;; gnus-gl.el --- an interface to GroupLens for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
4 ;; Author: Brad Miller <bmiller@cs.umn.edu>
5 ;; Keywords: news, score
7 ;; This file is part of GNU Emacs.
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)
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.
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.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; GroupLens software and documentation is copyright (c) 1995 by Paul
28 ;; Resnick (Massachusetts Institute of Technology); Brad Miller, John
29 ;; Riedl, Jon Herlocker, and Joseph Konstan (University of Minnesota),
30 ;; and David Maltz (Carnegie-Mellon University).
32 ;; Permission to use, copy, modify, and distribute this documentation
33 ;; for non-commercial and commercial purposes without fee is hereby
34 ;; granted provided that this copyright notice and permission notice
35 ;; appears in all copies and that the names of the individuals and
36 ;; institutions holding this copyright are not used in advertising or
37 ;; publicity pertaining to this software without specific, written
38 ;; prior permission. The copyright holders make no representations
39 ;; about the suitability of this software and documentation for any
40 ;; purpose. It is provided ``as is'' without express or implied
43 ;; The copyright holders request that they be notified of
44 ;; modifications of this code. Please send electronic mail to
45 ;; grouplens@cs.umn.edu for more information or to announce derived
47 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48 ;; Author: Brad Miller
50 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
52 ;; User Documentation:
53 ;; To use GroupLens you must load this file.
54 ;; You must also register a pseudonym with the Better Bit Bureau.
55 ;; http://www.cs.umn.edu/Research/GroupLens
57 ;; ---------------- For your .emacs or .gnus file ----------------
59 ;; As of version 2.5, grouplens now works as a minor mode of
60 ;; gnus-summary-mode. To get make that work you just need a couple of
62 ;; (setq gnus-use-grouplens t)
63 ;; (setq grouplens-pseudonym "")
64 ;; (setq grouplens-bbb-host "grouplens.cs.umn.edu")
66 ;; (setq gnus-summary-default-score 0)
69 ;; How do I Rate an article??
70 ;; Before you type n to go to the next article, hit a number from 1-5
71 ;; Type r in the summary buffer and you will be prompted.
72 ;; Note that when you're in grouplens-minor-mode 'r' masks the
73 ;; usual reply binding for 'r'
75 ;; What if, Gasp, I find a bug???
76 ;; Please type M-x gnus-gl-submit-bug-report. This will set up a
77 ;; mail buffer with the state of variables and buffers that will help
78 ;; me debug the problem. A short description up front would help too!
80 ;; How do I display the prediction for an article:
81 ;; If you set the gnus-summary-line-format as shown above, the score
82 ;; (prediction) will be shown automatically.
85 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
88 ;; gnus-scores-articles contains the articles
89 ;; When scoring is done, the call tree looks something like:
90 ;; gnus-possibly-score-headers
91 ;; ==> gnus-score-headers
92 ;; ==> gnus-score-load-file
93 ;; ==> get-all-mids (from the eval form)
95 ;; it would be nice to have one that gets called after all the other
96 ;; headers have been scored.
97 ;; we may want a variable gnus-grouplens-scale-factor
98 ;; and gnus-grouplens-offset this would probably be either -3 or 0
99 ;; to make the scores centered around zero or not.
101 ;; According to Lars, Norse god of gnus, the simple way to insert a
102 ;; call to an external function is to have a function added to the
103 ;; variable gnus-score-find-files-function This new function
104 ;; gnus-grouplens-score-alist will return a core alist that
105 ;; has (("message-id" ("<message-id-xxxx>" score) ("<message-id-xxxy>" score))
106 ;; This seems like it would be pretty inefficient, though workable.
108 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
110 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
112 ;; 3. Add some more ways to rate messages
113 ;; 4. Better error handling for token timeouts.
115 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
117 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122 (eval-when-compile (require 'cl))
124 (require 'gnus-score)
127 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
129 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131 (defvar gnus-summary-grouplens-line-format
132 "%U\%R\%z%l%I\%(%[%4L: %-20,20n%]%) %s\n"
133 "*The line format spec in summary GroupLens mode buffers.")
135 (defvar grouplens-pseudonym ""
137 This pseudonym is obtained during the registration process")
139 (defvar grouplens-bbb-host "grouplens.cs.umn.edu"
140 "Host where the bbbd is running.")
142 (defvar grouplens-bbb-port 9000
143 "Port where the bbbd is listening.")
145 (defvar grouplens-newsgroups
146 '("comp.groupware" "comp.human-factors" "comp.lang.c++"
147 "comp.lang.java" "comp.os.linux.admin" "comp.os.linux.advocacy"
148 "comp.os.linux.announce" "comp.os.linux.answers"
149 "comp.os.linux.development" "comp.os.linux.development.apps"
150 "comp.os.linux.development.system" "comp.os.linux.hardware"
151 "comp.os.linux.help" "comp.os.linux.m68k" "comp.os.linux.misc"
152 "comp.os.linux.networking" "comp.os.linux.setup" "comp.os.linux.x"
153 "mn.general" "rec.arts.movies" "rec.arts.movies.current-films"
154 "rec.food.recipes" "rec.humor")
155 "*Groups that are part of the GroupLens experiment.")
157 (defvar grouplens-prediction-display 'prediction-spot
159 prediction-spot -- an * corresponding to the prediction between 1 and 5,
160 confidence-interval -- a numeric confidence interval
161 prediction-bar -- |##### | the longer the bar, the better the article,
162 confidence-bar -- | ----- } the prediction is in the middle of the bar,
163 confidence-spot -- ) * | the spot gets bigger with more confidence,
164 prediction-num -- plain-old numeric value,
165 confidence-plus-minus -- prediction +/i confidence")
167 (defvar grouplens-score-offset 0
168 "Offset the prediction by this value.
169 Setting this variable to -2 would have the following effect on
178 The reason is that a user might want to do this is to combine
179 GroupLens predictions with scores calculated by other score methods.")
181 (defvar grouplens-score-scale-factor 1
182 "This variable allows the user to magnify the effect of GroupLens scores.
183 The scale factor is applied after the offset.")
185 (defvar gnus-grouplens-override-scoring 'override
186 "Tell GroupLens to override the normal Gnus scoring mechanism.
187 GroupLens scores can be combined with gnus scores in one of three ways.
188 'override -- just use grouplens predictions for grouplens groups
189 'combine -- combine grouplens scores with gnus scores
190 'separate -- treat grouplens scores completely separate from gnus")
193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
194 ;;;; Program global variables
195 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
196 (defvar grouplens-bbb-token nil
197 "Current session token number.")
199 (defvar grouplens-bbb-process nil
200 "Process Id of current bbbd network stream process.")
202 (defvar grouplens-bbb-buffer nil
203 "Buffer associated with the BBBD process.")
205 (defvar grouplens-rating-alist nil
206 "Current set of message-id rating pairs.")
208 (defvar grouplens-current-hashtable nil
209 "A hashtable to hold predictions from the BBB.")
211 (defvar grouplens-current-group nil)
213 ;;(defvar bbb-alist nil)
215 (defvar bbb-timeout-secs 10
216 "Number of seconds to wait for some response from the BBB.
217 If this times out we give up and assume that something has died..." )
219 (defvar grouplens-previous-article nil
220 "Message-ID of the last article read.")
222 (defvar bbb-read-point)
223 (defvar bbb-response-point)
225 (defun bbb-renew-hash-table ()
226 (setq grouplens-current-hashtable (make-vector 100 0)))
228 (bbb-renew-hash-table)
230 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231 ;;;; Utility Functions
232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
234 (defun bbb-connect-to-bbbd (host port)
235 (unless grouplens-bbb-buffer
236 (setq grouplens-bbb-buffer
237 (gnus-get-buffer-create (format " *BBBD trace: %s*" host)))
239 (set-buffer grouplens-bbb-buffer)
240 (make-local-variable 'bbb-read-point)
241 (make-local-variable 'bbb-response-point)
242 (setq bbb-read-point (point-min))))
244 ;; if an old process is still running for some reason, kill it
245 (when grouplens-bbb-process
247 (when (eq 'open (process-status grouplens-bbb-process))
248 (set-process-buffer grouplens-bbb-process nil)
249 (delete-process grouplens-bbb-process))))
251 ;; clear the trace buffer of old output
253 (set-buffer grouplens-bbb-buffer)
256 ;; open the connection to the server
258 (condition-case error
259 (setq grouplens-bbb-process
260 (open-network-stream-as-binary
261 "BBBD" grouplens-bbb-buffer host port))
262 (error (gnus-message 3 "Error: Failed to connect to BBB")
264 (and (null grouplens-bbb-process)
267 (set-buffer grouplens-bbb-buffer)
268 (setq bbb-read-point (point-min))
269 (or (bbb-read-response grouplens-bbb-process)
272 ;; return the process
273 grouplens-bbb-process)
275 (defun bbb-send-command (process command)
276 (goto-char (point-max))
279 (setq bbb-read-point (point))
280 (setq bbb-response-point (point))
281 (set-marker (process-mark process) (point)) ; process output also comes here
282 (process-send-string process command)
283 (process-send-string process "\r\n")
284 (process-send-eof process))
286 (defun bbb-read-response (process)
287 "This function eats the initial response of OK or ERROR from the BBB."
288 (let ((case-fold-search nil)
290 (goto-char bbb-read-point)
291 (while (and (not (search-forward "\r\n" nil t))
292 (accept-process-output process bbb-timeout-secs))
293 (goto-char bbb-read-point))
294 (setq match-end (point))
295 (goto-char bbb-read-point)
296 (setq bbb-read-point match-end)
299 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
301 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
303 "return the token number if login is successful, otherwise return nil."
305 (setq grouplens-bbb-token nil)
306 (if (not (equal grouplens-pseudonym ""))
308 (bbb-connect-to-bbbd grouplens-bbb-host grouplens-bbb-port)))
311 (set-buffer (process-buffer bbb-process))
312 (bbb-send-command bbb-process
313 (concat "login " grouplens-pseudonym))
314 (if (bbb-read-response bbb-process)
315 (setq grouplens-bbb-token (bbb-extract-token-number))
316 (gnus-message 3 "Error: GroupLens login failed")))))
317 (gnus-message 3 "Error: you must set a pseudonym"))
320 (defun bbb-extract-token-number ()
321 (let ((token-pos (search-forward "token=" nil t)))
322 (when (looking-at "[0-9]+")
323 (buffer-substring token-pos (match-end 0)))))
325 (gnus-add-shutdown 'bbb-logout 'gnus)
328 "logout of bbb session."
329 (when grouplens-bbb-token
331 (bbb-connect-to-bbbd grouplens-bbb-host grouplens-bbb-port)))
334 (set-buffer (process-buffer bbb-process))
335 (bbb-send-command bbb-process (concat "logout " grouplens-bbb-token))
336 (bbb-read-response bbb-process))))))
338 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
342 (defun bbb-build-mid-scores-alist (groupname)
343 "this function can be called as part of the function to return the list of score files to use.
344 See the gnus variable gnus-score-find-score-files-function.
346 *Note:* If you want to use grouplens scores along with calculated scores,
347 you should see the offset and scale variables. At this point, I don't
348 recommend using both scores and grouplens predictions together."
349 (setq grouplens-current-group groupname)
350 (when (member groupname grouplens-newsgroups)
351 (setq grouplens-previous-article nil)
352 ;; scores-alist should be a list of lists:
353 ;; ((("message-id" ("<mid1>" score1 nil s) ("<mid2> score2 nil s))))
354 ;;`((("message-id" . ,predict-list))) ; Yes, this is the return value
357 (list (append (list "message-id")
358 (bbb-get-predictions (bbb-get-all-mids) groupname)))))))
360 (defun bbb-get-predictions (midlist groupname)
361 "Ask the bbb for predictions, and build up the score alist."
362 (gnus-message 5 "Fetching Predictions...")
363 (if grouplens-bbb-token
364 (let ((bbb-process (bbb-connect-to-bbbd grouplens-bbb-host
365 grouplens-bbb-port)))
368 (set-buffer (process-buffer bbb-process))
369 (bbb-send-command bbb-process
370 (bbb-build-predict-command midlist groupname
371 grouplens-bbb-token))
372 (if (bbb-read-response bbb-process)
373 (bbb-get-prediction-response bbb-process)
374 (gnus-message 1 "Invalid Token, login and try again")
376 (gnus-message 3 "Error: You are not logged in to a BBB")
379 (defun bbb-get-all-mids ()
380 (mapcar (function (lambda (x) (mail-header-id x))) gnus-newsgroup-headers))
382 (defun bbb-build-predict-command (mlist grpname token)
383 (concat "getpredictions " token " " grpname "\r\n"
384 (mapconcat 'identity mlist "\r\n") "\r\n.\r\n"))
386 (defun bbb-get-prediction-response (process)
387 (let ((case-fold-search nil))
388 (goto-char bbb-read-point)
389 (while (and (not (search-forward ".\r\n" nil t))
390 (accept-process-output process bbb-timeout-secs))
391 (goto-char bbb-read-point))
392 (goto-char (+ bbb-response-point 4));; we ought to be right before OK
393 (bbb-build-response-alist)))
395 ;; build-response-alist assumes that the cursor has been positioned at
396 ;; the first line of the list of mid/rating pairs.
397 (defun bbb-build-response-alist ()
401 ((looking-at "\\(<.*>\\) :nopred=")
402 ;;(push `(,(bbb-get-mid) ,gnus-summary-default-score nil s) resp)
405 ((looking-at "\\(<.*>\\) :pred=\\([0-9]\.[0-9][0-9]\\) :conflow=\\([0-9]\.[0-9][0-9]\\) :confhigh=\\([0-9]\.[0-9][0-9]\\)")
406 (setq mid (bbb-get-mid)
408 (push `(,mid ,pred nil s) resp)
409 (gnus-sethash mid (list pred (bbb-get-confl) (bbb-get-confh))
410 grouplens-current-hashtable)
413 ((looking-at "\\(<.*>\\) :pred=\\([0-9]\.[0-9][0-9]\\)")
414 (setq mid (bbb-get-mid)
416 (push `(,mid ,pred nil s) resp)
417 (gnus-sethash mid (list pred 0 0) grouplens-current-hashtable)
423 ;; these "get" functions assume that there is an active match lying
424 ;; around. Where the first parenthesized expression is the
425 ;; message-id, and the second is the prediction, the third and fourth
426 ;; are the confidence interval
428 ;; Since gnus assumes that scores are integer values?? we round the
430 (defun bbb-get-mid ()
431 (buffer-substring (match-beginning 1) (match-end 1)))
433 (defun bbb-get-pred ()
434 (let ((tpred (string-to-number (buffer-substring (match-beginning 2)
437 (round (* grouplens-score-scale-factor
438 (+ grouplens-score-offset tpred)))
441 (defun bbb-get-confl ()
442 (string-to-number (buffer-substring (match-beginning 4) (match-end 4))))
444 (defun bbb-get-confh ()
445 (string-to-number (buffer-substring (match-beginning 4) (match-end 4))))
447 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
448 ;;;; Prediction Display
449 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
450 (defconst grplens-rating-range 4.0)
451 (defconst grplens-maxrating 5)
452 (defconst grplens-minrating 1)
453 (defconst grplens-predstringsize 12)
455 (defvar gnus-tmp-score)
456 (defun bbb-grouplens-score (header)
457 (if (eq gnus-grouplens-override-scoring 'separate)
458 (bbb-grouplens-other-score header)
459 (let* ((rate-string (make-string 12 ?\ ))
460 (mid (mail-header-id header))
461 (hashent (gnus-gethash mid grouplens-current-hashtable))
462 (iscore gnus-tmp-score)
463 (low (car (cdr hashent)))
464 (high (car (cdr (cdr hashent)))))
465 (aset rate-string 0 ?|)
466 (aset rate-string 11 ?|)
467 (unless (member grouplens-current-group grouplens-newsgroups)
468 (unless (equal grouplens-prediction-display 'prediction-num)
475 (if (and (bbb-valid-score iscore)
479 ((equal grouplens-prediction-display 'prediction-spot)
480 (setq rate-string (bbb-fmt-prediction-spot rate-string iscore)))
481 ;; confidence-interval
482 ((equal grouplens-prediction-display 'confidence-interval)
483 (setq rate-string (bbb-fmt-confidence-interval iscore low high)))
485 ((equal grouplens-prediction-display 'prediction-bar)
486 (setq rate-string (bbb-fmt-prediction-bar rate-string iscore)))
488 ((equal grouplens-prediction-display 'confidence-bar)
489 (setq rate-string (format "| %4.2f |" iscore)))
491 ((equal grouplens-prediction-display 'confidence-spot)
492 (setq rate-string (format "| %4.2f |" iscore)))
494 ((equal grouplens-prediction-display 'prediction-num)
495 (setq rate-string (bbb-fmt-prediction-num iscore)))
496 ;; confidence-plus-minus
497 ((equal grouplens-prediction-display 'confidence-plus-minus)
498 (setq rate-string (bbb-fmt-confidence-plus-minus iscore low high))
500 (t (gnus-message 3 "Invalid prediction display type")))
501 (aset rate-string 5 ?N) (aset rate-string 6 ?A))
504 ;; Gnus user format function that doesn't depend on
505 ;; bbb-build-mid-scores-alist being used as the score function, but is
506 ;; instead called from gnus-select-group-hook. -- LAB
507 (defun bbb-grouplens-other-score (header)
508 (if (not (member grouplens-current-group grouplens-newsgroups))
509 ;; Return an empty string
511 (let* ((rate-string (make-string 12 ?\ ))
512 (mid (mail-header-id header))
513 (hashent (gnus-gethash mid grouplens-current-hashtable))
514 (pred (or (nth 0 hashent) 0))
515 (low (nth 1 hashent))
516 (high (nth 2 hashent)))
518 (aset rate-string 0 ?|)
519 (aset rate-string 11 ?|)
520 (unless (equal grouplens-prediction-display 'prediction-num)
525 ;; If no entry in BBB hash mark rate string as NA and return
528 (aset rate-string 5 ?N)
529 (aset rate-string 6 ?A)
532 ((equal grouplens-prediction-display 'prediction-spot)
533 (bbb-fmt-prediction-spot rate-string pred))
535 ((equal grouplens-prediction-display 'confidence-interval)
536 (bbb-fmt-confidence-interval pred low high))
538 ((equal grouplens-prediction-display 'prediction-bar)
539 (bbb-fmt-prediction-bar rate-string pred))
541 ((equal grouplens-prediction-display 'confidence-bar)
542 (format "| %4.2f |" pred))
544 ((equal grouplens-prediction-display 'confidence-spot)
545 (format "| %4.2f |" pred))
547 ((equal grouplens-prediction-display 'prediction-num)
548 (bbb-fmt-prediction-num pred))
550 ((equal grouplens-prediction-display 'confidence-plus-minus)
551 (bbb-fmt-confidence-plus-minus pred low high))
554 (gnus-message 3 "Invalid prediction display type")
555 (aset rate-string 0 ?|)
556 (aset rate-string 11 ?|)
559 (defun bbb-valid-score (score)
560 (or (equal grouplens-prediction-display 'prediction-num)
561 (and (>= score grplens-minrating)
562 (<= score grplens-maxrating))))
564 (defun bbb-requires-confidence (format-type)
565 (or (equal format-type 'confidence-plus-minus)
566 (equal format-type 'confidence-spot)
567 (equal format-type 'confidence-interval)))
569 (defun bbb-have-confidence (clow chigh)
573 (defun bbb-fmt-prediction-spot (rate-string score)
575 (round (* (/ (- score grplens-minrating) grplens-rating-range)
576 (+ (- grplens-predstringsize 4) 1.49)))
580 (defun bbb-fmt-confidence-interval (score low high)
581 (if (bbb-have-confidence low high)
582 (format "|%4.2f-%4.2f |" low high)
583 (bbb-fmt-prediction-num score)))
585 (defun bbb-fmt-confidence-plus-minus (score low high)
586 (if (bbb-have-confidence low high)
587 (format "|%3.1f+/-%4.2f|" score (/ (- high low) 2.0))
588 (bbb-fmt-prediction-num score)))
590 (defun bbb-fmt-prediction-bar (rate-string score)
592 (step (/ grplens-rating-range (- grplens-predstringsize 4)))
593 (half-step (/ step 2))
594 (loc (- grplens-minrating half-step)))
595 (while (< i (- grplens-predstringsize 2))
597 (aset rate-string i ?#)
598 (aset rate-string i ?\ ))
600 (setq loc (+ loc step)))
604 (defun bbb-fmt-prediction-num (score)
605 (format "| %4.2f |" score))
607 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
609 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
611 (defun bbb-put-ratings ()
612 (if (and grouplens-bbb-token
613 grouplens-rating-alist
614 (member gnus-newsgroup-name grouplens-newsgroups))
615 (let ((bbb-process (bbb-connect-to-bbbd grouplens-bbb-host
617 (rate-command (bbb-build-rate-command grouplens-rating-alist)))
620 (set-buffer (process-buffer bbb-process))
621 (gnus-message 5 "Sending Ratings...")
622 (bbb-send-command bbb-process rate-command)
623 (if (bbb-read-response bbb-process)
624 (setq grouplens-rating-alist nil)
626 "Token timed out: call bbb-login and quit again")
628 (gnus-message 5 "Sending Ratings...Done"))
629 (gnus-message 3 "No BBB connection")))
630 (setq grouplens-rating-alist nil)))
632 (defun bbb-build-rate-command (rate-alist)
633 (concat "putratings " grouplens-bbb-token " " grouplens-current-group " \r\n"
634 (mapconcat '(lambda (this) ; form (mid . (score . time))
636 " :rating=" (cadr this) ".00"
637 " :time=" (cddr this)))
641 ;; Interactive rating functions.
642 (defun bbb-summary-rate-article (rating &optional midin)
643 (interactive "nRating: ")
644 (when (member gnus-newsgroup-name grouplens-newsgroups)
645 (let ((mid (or midin (bbb-get-current-id))))
647 (>= rating grplens-minrating)
648 (<= rating grplens-maxrating)
650 (let ((oldrating (assoc mid grouplens-rating-alist)))
652 (setcdr oldrating (cons rating 0))
653 (push `(,mid . (,rating . 0)) grouplens-rating-alist))
654 (gnus-summary-mark-article nil (int-to-string rating)))
655 (gnus-message 3 "Invalid rating")))))
657 (defun grouplens-next-unread-article (rating)
658 "Select unread article after current one."
661 (bbb-summary-rate-article rating))
662 (gnus-summary-next-unread-article))
664 (defun grouplens-best-unread-article (rating)
665 "Select unread article after current one."
668 (bbb-summary-rate-article rating))
669 (gnus-summary-best-unread-article))
671 (defun grouplens-summary-catchup-and-exit (rating)
672 "Mark all articles not marked as unread in this newsgroup as read, then exit.
673 If prefix argument ALL is non-nil, all articles are marked as read."
676 (bbb-summary-rate-article rating))
678 (gnus-summary-catchup-and-exit)
679 (gnus-summary-catchup-and-exit rating)))
681 (defun grouplens-score-thread (score)
682 "Raise the score of the articles in the current thread with SCORE."
683 (interactive "nRating: ")
686 (let ((articles (gnus-summary-articles-in-thread))
688 (while (setq article (pop articles))
689 (gnus-summary-goto-subject article)
690 (bbb-summary-rate-article score
692 (gnus-summary-article-header article)))))
694 (let ((gnus-summary-check-current t))
695 (or (zerop (gnus-summary-next-subject 1 t))
697 (gnus-summary-recenter)
698 (gnus-summary-position-point)
699 (gnus-set-mode-line 'summary))
701 (defun bbb-exit-group ()
703 (bbb-renew-hash-table))
705 (defun bbb-get-current-id ()
706 (if gnus-current-headers
707 (mail-header-id gnus-current-headers)
708 (gnus-message 3 "You must select an article before you rate it")))
710 (defun bbb-grouplens-group-p (group)
711 "Say whether GROUP is a GroupLens group."
712 (if (member group grouplens-newsgroups) " (GroupLens Enhanced)" ""))
714 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
715 ;; TIME SPENT READING
716 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
717 (defvar grouplens-current-starting-time nil)
719 (defun grouplens-start-timer ()
720 (setq grouplens-current-starting-time (current-time)))
722 (defun grouplens-elapsed-time ()
723 (let ((et (bbb-time-float (current-time))))
724 (- et (bbb-time-float grouplens-current-starting-time))))
726 (defun bbb-time-float (timeval)
727 (+ (* (car timeval) 65536)
730 (defun grouplens-do-time ()
731 (when (member gnus-newsgroup-name grouplens-newsgroups)
732 (when grouplens-previous-article
733 (let ((elapsed-time (grouplens-elapsed-time))
734 (oldrating (assoc grouplens-previous-article
735 grouplens-rating-alist)))
737 (push `(,grouplens-previous-article . (0 . ,elapsed-time))
738 grouplens-rating-alist)
739 (setcdr oldrating (cons (cadr oldrating) elapsed-time)))))
740 (grouplens-start-timer)
741 (setq grouplens-previous-article (bbb-get-current-id))))
743 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
745 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
747 (defconst gnus-gl-version "gnus-gl.el 2.50")
748 (defconst gnus-gl-maintainer-address "grouplens-bug@cs.umn.edu")
749 (defun gnus-gl-submit-bug-report ()
750 "Submit via mail a bug report on gnus-gl."
753 (reporter-submit-bug-report gnus-gl-maintainer-address
754 (concat "gnus-gl.el " gnus-gl-version)
755 (list 'grouplens-pseudonym
758 'grouplens-newsgroups
760 'grouplens-bbb-process
761 'grouplens-current-group
762 'grouplens-previous-article)
766 (defun gnus-gl-get-trace ()
767 "Insert the contents of the BBBD trace buffer."
768 (when grouplens-bbb-buffer
769 (insert-buffer-substring grouplens-bbb-buffer)))
772 ;; GroupLens minor mode
775 (defvar gnus-grouplens-mode nil
776 "Minor mode for providing a GroupLens interface in Gnus summary buffers.")
778 (defvar gnus-grouplens-mode-map nil)
780 (unless gnus-grouplens-mode-map
781 (setq gnus-grouplens-mode-map (make-keymap))
783 gnus-grouplens-mode-map
784 "n" grouplens-next-unread-article
785 "r" bbb-summary-rate-article
786 "k" grouplens-score-thread
787 "c" grouplens-summary-catchup-and-exit
788 "," grouplens-best-unread-article))
790 (defun gnus-grouplens-make-menu-bar ()
791 (unless (boundp 'gnus-grouplens-menu)
793 gnus-grouplens-menu gnus-grouplens-mode-map ""
795 ["Login" bbb-login t]
796 ["Rate" bbb-summary-rate-article t]
797 ["Next article" grouplens-next-unread-article t]
798 ["Best article" grouplens-best-unread-article t]
799 ["Raise thread" grouplens-score-thread t]
800 ["Report bugs" gnus-gl-submit-bug-report t]))))
802 (defun gnus-grouplens-mode (&optional arg)
803 "Minor mode for providing a GroupLens interface in Gnus summary buffers."
805 (when (and (eq major-mode 'gnus-summary-mode)
806 (member gnus-newsgroup-name grouplens-newsgroups))
807 (make-local-variable 'gnus-grouplens-mode)
808 (setq gnus-grouplens-mode
809 (if (null arg) (not gnus-grouplens-mode)
810 (> (prefix-numeric-value arg) 0)))
811 (when gnus-grouplens-mode
812 (make-local-hook 'gnus-select-article-hook)
813 (add-hook 'gnus-select-article-hook 'grouplens-do-time nil 'local)
814 (make-local-hook 'gnus-exit-group-hook)
815 (add-hook 'gnus-exit-group-hook 'bbb-exit-group nil 'local)
816 (make-local-variable 'gnus-score-find-score-files-function)
819 ((eq gnus-grouplens-override-scoring 'combine)
820 ;; either add bbb-buld-mid-scores-alist to a list
822 (if (listp gnus-score-find-score-files-function)
823 (setq gnus-score-find-score-files-function
824 (append 'bbb-build-mid-scores-alist
825 gnus-score-find-score-files-function))
826 (setq gnus-score-find-score-files-function
827 (list gnus-score-find-score-files-function
828 'bbb-build-mid-scores-alist))))
829 ;; leave the gnus-score-find-score-files variable alone
830 ((eq gnus-grouplens-override-scoring 'separate)
831 (add-hook 'gnus-select-group-hook
833 (bbb-get-predictions (bbb-get-all-mids)
834 gnus-newsgroup-name))))
835 ;; default is to override
837 (setq gnus-score-find-score-files-function
838 'bbb-build-mid-scores-alist)))
840 ;; Change how summary lines look
841 (make-local-variable 'gnus-summary-line-format)
842 (make-local-variable 'gnus-summary-line-format-spec)
843 (setq gnus-summary-line-format gnus-summary-grouplens-line-format)
844 (setq gnus-summary-line-format-spec nil)
845 (gnus-update-format-specifications nil 'summary)
846 (gnus-update-summary-mark-positions)
849 (when (and menu-bar-mode
850 (gnus-visual-p 'grouplens-menu 'menu))
851 (gnus-grouplens-make-menu-bar))
853 'gnus-grouplens-mode " GroupLens" gnus-grouplens-mode-map)
854 (gnus-run-hooks 'gnus-grouplens-mode-hook))))
858 ;;; gnus-gl.el ends here