1 ;;; gnus-gl.el --- an interface to GroupLens for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
4 ;; Free Software Foundation, Inc.
6 ;; Author: Brad Miller <bmiller@cs.umn.edu>
7 ;; Keywords: news, score
9 ;; This file is part of GNU Emacs.
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)
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.
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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; GroupLens software and documentation is copyright (c) 1995 by Paul
30 ;; Resnick (Massachusetts Institute of Technology); Brad Miller, John
31 ;; Riedl, Jon Herlocker, and Joseph Konstan (University of Minnesota),
32 ;; and David Maltz (Carnegie-Mellon University).
34 ;; Permission to use, copy, modify, and distribute this documentation
35 ;; for non-commercial and commercial purposes without fee is hereby
36 ;; granted provided that this copyright notice and permission notice
37 ;; appears in all copies and that the names of the individuals and
38 ;; institutions holding this copyright are not used in advertising or
39 ;; publicity pertaining to this software without specific, written
40 ;; prior permission. The copyright holders make no representations
41 ;; about the suitability of this software and documentation for any
42 ;; purpose. It is provided ``as is'' without express or implied
45 ;; The copyright holders request that they be notified of
46 ;; modifications of this code. Please send electronic mail to
47 ;; grouplens@cs.umn.edu for more information or to announce derived
49 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
50 ;; Author: Brad Miller
52 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
54 ;; User Documentation:
55 ;; To use GroupLens you must load this file.
56 ;; You must also register a pseudonym with the Better Bit Bureau.
57 ;; http://www.cs.umn.edu/Research/GroupLens
59 ;; ---------------- For your .emacs or .gnus file ----------------
61 ;; As of version 2.5, grouplens now works as a minor mode of
62 ;; gnus-summary-mode. To get make that work you just need a couple of
64 ;; (setq gnus-use-grouplens t)
65 ;; (setq grouplens-pseudonym "")
66 ;; (setq grouplens-bbb-host "grouplens.cs.umn.edu")
68 ;; (setq gnus-summary-default-score 0)
71 ;; How do I Rate an article??
72 ;; Before you type n to go to the next article, hit a number from 1-5
73 ;; Type r in the summary buffer and you will be prompted.
74 ;; Note that when you're in grouplens-minor-mode 'r' masks the
75 ;; usual reply binding for 'r'
77 ;; What if, Gasp, I find a bug???
78 ;; Please type M-x gnus-gl-submit-bug-report. This will set up a
79 ;; mail buffer with the state of variables and buffers that will help
80 ;; me debug the problem. A short description up front would help too!
82 ;; How do I display the prediction for an article:
83 ;; If you set the gnus-summary-line-format as shown above, the score
84 ;; (prediction) will be shown automatically.
87 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
90 ;; gnus-scores-articles contains the articles
91 ;; When scoring is done, the call tree looks something like:
92 ;; gnus-possibly-score-headers
93 ;; ==> gnus-score-headers
94 ;; ==> gnus-score-load-file
95 ;; ==> get-all-mids (from the eval form)
97 ;; it would be nice to have one that gets called after all the other
98 ;; headers have been scored.
99 ;; we may want a variable gnus-grouplens-scale-factor
100 ;; and gnus-grouplens-offset this would probably be either -3 or 0
101 ;; to make the scores centered around zero or not.
103 ;; According to Lars, Norse god of gnus, the simple way to insert a
104 ;; call to an external function is to have a function added to the
105 ;; variable gnus-score-find-files-function This new function
106 ;; gnus-grouplens-score-alist will return a core alist that
107 ;; has (("message-id" ("<message-id-xxxx>" score) ("<message-id-xxxy>" score))
108 ;; This seems like it would be pretty inefficient, though workable.
110 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
112 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
114 ;; 3. Add some more ways to rate messages
115 ;; 4. Better error handling for token timeouts.
117 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
119 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124 (eval-when-compile (require 'cl))
126 (require 'gnus-score)
129 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
133 (defvar gnus-summary-grouplens-line-format
134 "%U\%R\%z%l%I\%(%[%4L: %-20,20n%]%) %s\n"
135 "*The line format spec in summary GroupLens mode buffers.")
137 (defvar grouplens-pseudonym ""
139 This pseudonym is obtained during the registration process")
141 (defvar grouplens-bbb-host "grouplens.cs.umn.edu"
142 "Host where the bbbd is running.")
144 (defvar grouplens-bbb-port 9000
145 "Port where the bbbd is listening.")
147 (defvar grouplens-newsgroups
148 '("comp.groupware" "comp.human-factors" "comp.lang.c++"
149 "comp.lang.java" "comp.os.linux.admin" "comp.os.linux.advocacy"
150 "comp.os.linux.announce" "comp.os.linux.answers"
151 "comp.os.linux.development" "comp.os.linux.development.apps"
152 "comp.os.linux.development.system" "comp.os.linux.hardware"
153 "comp.os.linux.help" "comp.os.linux.m68k" "comp.os.linux.misc"
154 "comp.os.linux.networking" "comp.os.linux.setup" "comp.os.linux.x"
155 "mn.general" "rec.arts.movies" "rec.arts.movies.current-films"
156 "rec.food.recipes" "rec.humor")
157 "*Groups that are part of the GroupLens experiment.")
159 (defvar grouplens-prediction-display 'prediction-spot
161 prediction-spot -- an * corresponding to the prediction between 1 and 5,
162 confidence-interval -- a numeric confidence interval
163 prediction-bar -- |##### | the longer the bar, the better the article,
164 confidence-bar -- | ----- } the prediction is in the middle of the bar,
165 confidence-spot -- ) * | the spot gets bigger with more confidence,
166 prediction-num -- plain-old numeric value,
167 confidence-plus-minus -- prediction +/i confidence")
169 (defvar grouplens-score-offset 0
170 "Offset the prediction by this value.
171 Setting this variable to -2 would have the following effect on
180 The reason is that a user might want to do this is to combine
181 GroupLens predictions with scores calculated by other score methods.")
183 (defvar grouplens-score-scale-factor 1
184 "This variable allows the user to magnify the effect of GroupLens scores.
185 The scale factor is applied after the offset.")
187 (defvar gnus-grouplens-override-scoring 'override
188 "Tell GroupLens to override the normal Gnus scoring mechanism.
189 GroupLens scores can be combined with gnus scores in one of three ways.
190 'override -- just use grouplens predictions for grouplens groups
191 'combine -- combine grouplens scores with gnus scores
192 'separate -- treat grouplens scores completely separate from gnus")
195 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
196 ;;;; Program global variables
197 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
198 (defvar grouplens-bbb-token nil
199 "Current session token number.")
201 (defvar grouplens-bbb-process nil
202 "Process Id of current bbbd network stream process.")
204 (defvar grouplens-bbb-buffer nil
205 "Buffer associated with the BBBD process.")
207 (defvar grouplens-rating-alist nil
208 "Current set of message-id rating pairs.")
210 (defvar grouplens-current-hashtable nil
211 "A hashtable to hold predictions from the BBB.")
213 (defvar grouplens-current-group nil)
215 ;;(defvar bbb-alist nil)
217 (defvar bbb-timeout-secs 10
218 "Number of seconds to wait for some response from the BBB.
219 If this times out we give up and assume that something has died..." )
221 (defvar grouplens-previous-article nil
222 "Message-ID of the last article read.")
224 (defvar bbb-read-point)
225 (defvar bbb-response-point)
227 (defun bbb-renew-hash-table ()
228 (setq grouplens-current-hashtable (make-vector 100 0)))
230 (bbb-renew-hash-table)
232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
233 ;;;; Utility Functions
234 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
236 (defun bbb-connect-to-bbbd (host port)
237 (unless grouplens-bbb-buffer
238 (setq grouplens-bbb-buffer
239 (gnus-get-buffer-create (format " *BBBD trace: %s*" host)))
241 (set-buffer grouplens-bbb-buffer)
242 (make-local-variable 'bbb-read-point)
243 (make-local-variable 'bbb-response-point)
244 (setq bbb-read-point (point-min))))
246 ;; if an old process is still running for some reason, kill it
247 (when grouplens-bbb-process
249 (when (eq 'open (process-status grouplens-bbb-process))
250 (set-process-buffer grouplens-bbb-process nil)
251 (delete-process grouplens-bbb-process))))
253 ;; clear the trace buffer of old output
255 (set-buffer grouplens-bbb-buffer)
258 ;; open the connection to the server
260 (condition-case error
261 (setq grouplens-bbb-process
262 (open-network-stream-as-binary
263 "BBBD" grouplens-bbb-buffer host port))
264 (error (gnus-message 3 "Error: Failed to connect to BBB")
266 (and (null grouplens-bbb-process)
269 (set-buffer grouplens-bbb-buffer)
270 (setq bbb-read-point (point-min))
271 (or (bbb-read-response grouplens-bbb-process)
274 ;; return the process
275 grouplens-bbb-process)
277 (defun bbb-send-command (process command)
278 (goto-char (point-max))
281 (setq bbb-read-point (point))
282 (setq bbb-response-point (point))
283 (set-marker (process-mark process) (point)) ; process output also comes here
284 (process-send-string process command)
285 (process-send-string process "\r\n")
286 (process-send-eof process))
288 (defun bbb-read-response (process)
289 "This function eats the initial response of OK or ERROR from the BBB."
290 (let ((case-fold-search nil)
292 (goto-char bbb-read-point)
293 (while (and (not (search-forward "\r\n" nil t))
294 (accept-process-output process bbb-timeout-secs))
295 (goto-char bbb-read-point))
296 (setq match-end (point))
297 (goto-char bbb-read-point)
298 (setq bbb-read-point match-end)
301 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
303 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
305 "return the token number if login is successful, otherwise return nil."
307 (setq grouplens-bbb-token nil)
308 (if (not (equal grouplens-pseudonym ""))
310 (bbb-connect-to-bbbd grouplens-bbb-host grouplens-bbb-port)))
313 (set-buffer (process-buffer bbb-process))
314 (bbb-send-command bbb-process
315 (concat "login " grouplens-pseudonym))
316 (if (bbb-read-response bbb-process)
317 (setq grouplens-bbb-token (bbb-extract-token-number))
318 (gnus-message 3 "Error: GroupLens login failed")))))
319 (gnus-message 3 "Error: you must set a pseudonym"))
322 (defun bbb-extract-token-number ()
323 (let ((token-pos (search-forward "token=" nil t)))
324 (when (looking-at "[0-9]+")
325 (buffer-substring token-pos (match-end 0)))))
327 (gnus-add-shutdown 'bbb-logout 'gnus)
330 "logout of bbb session."
331 (when grouplens-bbb-token
333 (bbb-connect-to-bbbd grouplens-bbb-host grouplens-bbb-port)))
336 (set-buffer (process-buffer bbb-process))
337 (bbb-send-command bbb-process (concat "logout " grouplens-bbb-token))
338 (bbb-read-response bbb-process))))))
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
342 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
344 (defun bbb-build-mid-scores-alist (groupname)
345 "this function can be called as part of the function to return the list of score files to use.
346 See the gnus variable gnus-score-find-score-files-function.
348 *Note:* If you want to use grouplens scores along with calculated scores,
349 you should see the offset and scale variables. At this point, I don't
350 recommend using both scores and grouplens predictions together."
351 (setq grouplens-current-group groupname)
352 (when (member groupname grouplens-newsgroups)
353 (setq grouplens-previous-article nil)
354 ;; scores-alist should be a list of lists:
355 ;; ((("message-id" ("<mid1>" score1 nil s) ("<mid2> score2 nil s))))
356 ;;`((("message-id" . ,predict-list))) ; Yes, this is the return value
359 (list (append (list "message-id")
360 (bbb-get-predictions (bbb-get-all-mids) groupname)))))))
362 (defun bbb-get-predictions (midlist groupname)
363 "Ask the bbb for predictions, and build up the score alist."
364 (gnus-message 5 "Fetching Predictions...")
365 (if grouplens-bbb-token
366 (let ((bbb-process (bbb-connect-to-bbbd grouplens-bbb-host
367 grouplens-bbb-port)))
370 (set-buffer (process-buffer bbb-process))
371 (bbb-send-command bbb-process
372 (bbb-build-predict-command midlist groupname
373 grouplens-bbb-token))
374 (if (bbb-read-response bbb-process)
375 (bbb-get-prediction-response bbb-process)
376 (gnus-message 1 "Invalid Token, login and try again")
378 (gnus-message 3 "Error: You are not logged in to a BBB")
381 (defun bbb-get-all-mids ()
382 (mapcar (function (lambda (x) (mail-header-id x))) gnus-newsgroup-headers))
384 (defun bbb-build-predict-command (mlist grpname token)
385 (concat "getpredictions " token " " grpname "\r\n"
386 (mapconcat 'identity mlist "\r\n") "\r\n.\r\n"))
388 (defun bbb-get-prediction-response (process)
389 (let ((case-fold-search nil))
390 (goto-char bbb-read-point)
391 (while (and (not (search-forward ".\r\n" nil t))
392 (accept-process-output process bbb-timeout-secs))
393 (goto-char bbb-read-point))
394 (goto-char (+ bbb-response-point 4));; we ought to be right before OK
395 (bbb-build-response-alist)))
397 ;; build-response-alist assumes that the cursor has been positioned at
398 ;; the first line of the list of mid/rating pairs.
399 (defun bbb-build-response-alist ()
403 ((looking-at "\\(<.*>\\) :nopred=")
404 ;;(push `(,(bbb-get-mid) ,gnus-summary-default-score nil s) resp)
407 ((looking-at "\\(<.*>\\) :pred=\\([0-9]\.[0-9][0-9]\\) :conflow=\\([0-9]\.[0-9][0-9]\\) :confhigh=\\([0-9]\.[0-9][0-9]\\)")
408 (setq mid (bbb-get-mid)
410 (push `(,mid ,pred nil s) resp)
411 (gnus-sethash mid (list pred (bbb-get-confl) (bbb-get-confh))
412 grouplens-current-hashtable)
415 ((looking-at "\\(<.*>\\) :pred=\\([0-9]\.[0-9][0-9]\\)")
416 (setq mid (bbb-get-mid)
418 (push `(,mid ,pred nil s) resp)
419 (gnus-sethash mid (list pred 0 0) grouplens-current-hashtable)
425 ;; these "get" functions assume that there is an active match lying
426 ;; around. Where the first parenthesized expression is the
427 ;; message-id, and the second is the prediction, the third and fourth
428 ;; are the confidence interval
430 ;; Since gnus assumes that scores are integer values?? we round the
432 (defun bbb-get-mid ()
433 (buffer-substring (match-beginning 1) (match-end 1)))
435 (defun bbb-get-pred ()
436 (let ((tpred (string-to-number (buffer-substring (match-beginning 2)
439 (round (* grouplens-score-scale-factor
440 (+ grouplens-score-offset tpred)))
443 (defun bbb-get-confl ()
444 (string-to-number (buffer-substring (match-beginning 4) (match-end 4))))
446 (defun bbb-get-confh ()
447 (string-to-number (buffer-substring (match-beginning 4) (match-end 4))))
449 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
450 ;;;; Prediction Display
451 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
452 (defconst grplens-rating-range 4.0)
453 (defconst grplens-maxrating 5)
454 (defconst grplens-minrating 1)
455 (defconst grplens-predstringsize 12)
457 (defvar gnus-tmp-score)
458 (defun bbb-grouplens-score (header)
459 (if (eq gnus-grouplens-override-scoring 'separate)
460 (bbb-grouplens-other-score header)
461 (let* ((rate-string (make-string 12 ?\ ))
462 (mid (mail-header-id header))
463 (hashent (gnus-gethash mid grouplens-current-hashtable))
464 (iscore gnus-tmp-score)
465 (low (car (cdr hashent)))
466 (high (car (cdr (cdr hashent)))))
467 (aset rate-string 0 ?|)
468 (aset rate-string 11 ?|)
469 (unless (member grouplens-current-group grouplens-newsgroups)
470 (unless (equal grouplens-prediction-display 'prediction-num)
477 (if (and (bbb-valid-score iscore)
481 ((equal grouplens-prediction-display 'prediction-spot)
482 (setq rate-string (bbb-fmt-prediction-spot rate-string iscore)))
483 ;; confidence-interval
484 ((equal grouplens-prediction-display 'confidence-interval)
485 (setq rate-string (bbb-fmt-confidence-interval iscore low high)))
487 ((equal grouplens-prediction-display 'prediction-bar)
488 (setq rate-string (bbb-fmt-prediction-bar rate-string iscore)))
490 ((equal grouplens-prediction-display 'confidence-bar)
491 (setq rate-string (format "| %4.2f |" iscore)))
493 ((equal grouplens-prediction-display 'confidence-spot)
494 (setq rate-string (format "| %4.2f |" iscore)))
496 ((equal grouplens-prediction-display 'prediction-num)
497 (setq rate-string (bbb-fmt-prediction-num iscore)))
498 ;; confidence-plus-minus
499 ((equal grouplens-prediction-display 'confidence-plus-minus)
500 (setq rate-string (bbb-fmt-confidence-plus-minus iscore low high))
502 (t (gnus-message 3 "Invalid prediction display type")))
503 (aset rate-string 5 ?N) (aset rate-string 6 ?A))
506 ;; Gnus user format function that doesn't depend on
507 ;; bbb-build-mid-scores-alist being used as the score function, but is
508 ;; instead called from gnus-select-group-hook. -- LAB
509 (defun bbb-grouplens-other-score (header)
510 (if (not (member grouplens-current-group grouplens-newsgroups))
511 ;; Return an empty string
513 (let* ((rate-string (make-string 12 ?\ ))
514 (mid (mail-header-id header))
515 (hashent (gnus-gethash mid grouplens-current-hashtable))
516 (pred (or (nth 0 hashent) 0))
517 (low (nth 1 hashent))
518 (high (nth 2 hashent)))
520 (aset rate-string 0 ?|)
521 (aset rate-string 11 ?|)
522 (unless (equal grouplens-prediction-display 'prediction-num)
527 ;; If no entry in BBB hash mark rate string as NA and return
530 (aset rate-string 5 ?N)
531 (aset rate-string 6 ?A)
534 ((equal grouplens-prediction-display 'prediction-spot)
535 (bbb-fmt-prediction-spot rate-string pred))
537 ((equal grouplens-prediction-display 'confidence-interval)
538 (bbb-fmt-confidence-interval pred low high))
540 ((equal grouplens-prediction-display 'prediction-bar)
541 (bbb-fmt-prediction-bar rate-string pred))
543 ((equal grouplens-prediction-display 'confidence-bar)
544 (format "| %4.2f |" pred))
546 ((equal grouplens-prediction-display 'confidence-spot)
547 (format "| %4.2f |" pred))
549 ((equal grouplens-prediction-display 'prediction-num)
550 (bbb-fmt-prediction-num pred))
552 ((equal grouplens-prediction-display 'confidence-plus-minus)
553 (bbb-fmt-confidence-plus-minus pred low high))
556 (gnus-message 3 "Invalid prediction display type")
557 (aset rate-string 0 ?|)
558 (aset rate-string 11 ?|)
561 (defun bbb-valid-score (score)
562 (or (equal grouplens-prediction-display 'prediction-num)
563 (and (>= score grplens-minrating)
564 (<= score grplens-maxrating))))
566 (defun bbb-requires-confidence (format-type)
567 (or (equal format-type 'confidence-plus-minus)
568 (equal format-type 'confidence-spot)
569 (equal format-type 'confidence-interval)))
571 (defun bbb-have-confidence (clow chigh)
575 (defun bbb-fmt-prediction-spot (rate-string score)
577 (round (* (/ (- score grplens-minrating) grplens-rating-range)
578 (+ (- grplens-predstringsize 4) 1.49)))
582 (defun bbb-fmt-confidence-interval (score low high)
583 (if (bbb-have-confidence low high)
584 (format "|%4.2f-%4.2f |" low high)
585 (bbb-fmt-prediction-num score)))
587 (defun bbb-fmt-confidence-plus-minus (score low high)
588 (if (bbb-have-confidence low high)
589 (format "|%3.1f+/-%4.2f|" score (/ (- high low) 2.0))
590 (bbb-fmt-prediction-num score)))
592 (defun bbb-fmt-prediction-bar (rate-string score)
594 (step (/ grplens-rating-range (- grplens-predstringsize 4)))
595 (half-step (/ step 2))
596 (loc (- grplens-minrating half-step)))
597 (while (< i (- grplens-predstringsize 2))
599 (aset rate-string i ?#)
600 (aset rate-string i ?\ ))
602 (setq loc (+ loc step)))
606 (defun bbb-fmt-prediction-num (score)
607 (format "| %4.2f |" score))
609 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
611 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
613 (defun bbb-put-ratings ()
614 (if (and grouplens-bbb-token
615 grouplens-rating-alist
616 (member gnus-newsgroup-name grouplens-newsgroups))
617 (let ((bbb-process (bbb-connect-to-bbbd grouplens-bbb-host
619 (rate-command (bbb-build-rate-command grouplens-rating-alist)))
622 (set-buffer (process-buffer bbb-process))
623 (gnus-message 5 "Sending Ratings...")
624 (bbb-send-command bbb-process rate-command)
625 (if (bbb-read-response bbb-process)
626 (setq grouplens-rating-alist nil)
628 "Token timed out: call bbb-login and quit again")
630 (gnus-message 5 "Sending Ratings...Done"))
631 (gnus-message 3 "No BBB connection")))
632 (setq grouplens-rating-alist nil)))
634 (defun bbb-build-rate-command (rate-alist)
635 (concat "putratings " grouplens-bbb-token " " grouplens-current-group " \r\n"
636 (mapconcat '(lambda (this) ; form (mid . (score . time))
638 " :rating=" (cadr this) ".00"
639 " :time=" (cddr this)))
643 ;; Interactive rating functions.
644 (defun bbb-summary-rate-article (rating &optional midin)
645 (interactive "nRating: ")
646 (when (member gnus-newsgroup-name grouplens-newsgroups)
647 (let ((mid (or midin (bbb-get-current-id))))
649 (>= rating grplens-minrating)
650 (<= rating grplens-maxrating)
652 (let ((oldrating (assoc mid grouplens-rating-alist)))
654 (setcdr oldrating (cons rating 0))
655 (push `(,mid . (,rating . 0)) grouplens-rating-alist))
656 (gnus-summary-mark-article nil (int-to-string rating)))
657 (gnus-message 3 "Invalid rating")))))
659 (defun grouplens-next-unread-article (rating)
660 "Select unread article after current one."
663 (bbb-summary-rate-article rating))
664 (gnus-summary-next-unread-article))
666 (defun grouplens-best-unread-article (rating)
667 "Select unread article after current one."
670 (bbb-summary-rate-article rating))
671 (gnus-summary-best-unread-article))
673 (defun grouplens-summary-catchup-and-exit (rating)
674 "Mark all articles not marked as unread in this newsgroup as read, then exit.
675 If prefix argument ALL is non-nil, all articles are marked as read."
678 (bbb-summary-rate-article rating))
680 (gnus-summary-catchup-and-exit)
681 (gnus-summary-catchup-and-exit rating)))
683 (defun grouplens-score-thread (score)
684 "Raise the score of the articles in the current thread with SCORE."
685 (interactive "nRating: ")
688 (let ((articles (gnus-summary-articles-in-thread))
690 (while (setq article (pop articles))
691 (gnus-summary-goto-subject article)
692 (bbb-summary-rate-article score
694 (gnus-summary-article-header article)))))
696 (let ((gnus-summary-check-current t))
697 (or (zerop (gnus-summary-next-subject 1 t))
699 (gnus-summary-recenter)
700 (gnus-summary-position-point)
701 (gnus-set-mode-line 'summary))
703 (defun bbb-exit-group ()
705 (bbb-renew-hash-table))
707 (defun bbb-get-current-id ()
708 (if gnus-current-headers
709 (mail-header-id gnus-current-headers)
710 (gnus-message 3 "You must select an article before you rate it")))
712 (defun bbb-grouplens-group-p (group)
713 "Say whether GROUP is a GroupLens group."
714 (if (member group grouplens-newsgroups) " (GroupLens Enhanced)" ""))
716 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
717 ;; TIME SPENT READING
718 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
719 (defvar grouplens-current-starting-time nil)
721 (defun grouplens-start-timer ()
722 (setq grouplens-current-starting-time (current-time)))
724 (defun grouplens-elapsed-time ()
725 (let ((et (bbb-time-float (current-time))))
726 (- et (bbb-time-float grouplens-current-starting-time))))
728 (defun bbb-time-float (timeval)
729 (+ (* (car timeval) 65536)
732 (defun grouplens-do-time ()
733 (when (member gnus-newsgroup-name grouplens-newsgroups)
734 (when grouplens-previous-article
735 (let ((elapsed-time (grouplens-elapsed-time))
736 (oldrating (assoc grouplens-previous-article
737 grouplens-rating-alist)))
739 (push `(,grouplens-previous-article . (0 . ,elapsed-time))
740 grouplens-rating-alist)
741 (setcdr oldrating (cons (cadr oldrating) elapsed-time)))))
742 (grouplens-start-timer)
743 (setq grouplens-previous-article (bbb-get-current-id))))
745 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
747 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
749 (defconst gnus-gl-version "gnus-gl.el 2.50")
750 (defconst gnus-gl-maintainer-address "grouplens-bug@cs.umn.edu")
751 (defun gnus-gl-submit-bug-report ()
752 "Submit via mail a bug report on gnus-gl."
755 (reporter-submit-bug-report gnus-gl-maintainer-address
756 (concat "gnus-gl.el " gnus-gl-version)
757 (list 'grouplens-pseudonym
760 'grouplens-newsgroups
762 'grouplens-bbb-process
763 'grouplens-current-group
764 'grouplens-previous-article)
768 (defun gnus-gl-get-trace ()
769 "Insert the contents of the BBBD trace buffer."
770 (when grouplens-bbb-buffer
771 (insert-buffer-substring grouplens-bbb-buffer)))
774 ;; GroupLens minor mode
777 (defvar gnus-grouplens-mode nil
778 "Minor mode for providing a GroupLens interface in Gnus summary buffers.")
780 (defvar gnus-grouplens-mode-map nil)
782 (unless gnus-grouplens-mode-map
783 (setq gnus-grouplens-mode-map (make-keymap))
785 gnus-grouplens-mode-map
786 "n" grouplens-next-unread-article
787 "r" bbb-summary-rate-article
788 "k" grouplens-score-thread
789 "c" grouplens-summary-catchup-and-exit
790 "," grouplens-best-unread-article))
792 (defun gnus-grouplens-make-menu-bar ()
793 (unless (boundp 'gnus-grouplens-menu)
795 gnus-grouplens-menu gnus-grouplens-mode-map ""
797 ["Login" bbb-login t]
798 ["Rate" bbb-summary-rate-article t]
799 ["Next article" grouplens-next-unread-article t]
800 ["Best article" grouplens-best-unread-article t]
801 ["Raise thread" grouplens-score-thread t]
802 ["Report bugs" gnus-gl-submit-bug-report t]))))
804 (defun gnus-grouplens-mode (&optional arg)
805 "Minor mode for providing a GroupLens interface in Gnus summary buffers."
807 (when (and (eq major-mode 'gnus-summary-mode)
808 (member gnus-newsgroup-name grouplens-newsgroups))
809 (make-local-variable 'gnus-grouplens-mode)
810 (setq gnus-grouplens-mode
811 (if (null arg) (not gnus-grouplens-mode)
812 (> (prefix-numeric-value arg) 0)))
813 (when gnus-grouplens-mode
814 (make-local-hook 'gnus-select-article-hook)
815 (add-hook 'gnus-select-article-hook 'grouplens-do-time nil 'local)
816 (make-local-hook 'gnus-exit-group-hook)
817 (add-hook 'gnus-exit-group-hook 'bbb-exit-group nil 'local)
818 (make-local-variable 'gnus-score-find-score-files-function)
821 ((eq gnus-grouplens-override-scoring 'combine)
822 ;; either add bbb-buld-mid-scores-alist to a list
824 (if (listp gnus-score-find-score-files-function)
825 (setq gnus-score-find-score-files-function
826 (append 'bbb-build-mid-scores-alist
827 gnus-score-find-score-files-function))
828 (setq gnus-score-find-score-files-function
829 (list gnus-score-find-score-files-function
830 'bbb-build-mid-scores-alist))))
831 ;; leave the gnus-score-find-score-files variable alone
832 ((eq gnus-grouplens-override-scoring 'separate)
833 (add-hook 'gnus-select-group-hook
835 (bbb-get-predictions (bbb-get-all-mids)
836 gnus-newsgroup-name))))
837 ;; default is to override
839 (setq gnus-score-find-score-files-function
840 'bbb-build-mid-scores-alist)))
842 ;; Change how summary lines look
843 (make-local-variable 'gnus-summary-line-format)
844 (make-local-variable 'gnus-summary-line-format-spec)
845 (setq gnus-summary-line-format gnus-summary-grouplens-line-format)
846 (setq gnus-summary-line-format-spec nil)
847 (gnus-update-format-specifications nil 'summary)
848 (gnus-update-summary-mark-positions)
851 (when (and menu-bar-mode
852 (gnus-visual-p 'grouplens-menu 'menu))
853 (gnus-grouplens-make-menu-bar))
855 'gnus-grouplens-mode " GroupLens" gnus-grouplens-mode-map)
856 (gnus-run-hooks 'gnus-grouplens-mode-hook))))
860 ;;; gnus-gl.el ends here