Update of /project/net-nittin-irc/cvsroot/net-nittin-irc/example In directory common-lisp.net:/tmp/cvs-serv9160
Modified Files: cliki.lisp Log Message: Fixed handling in the case of an end-of-file.
Date: Sat Dec 13 09:03:40 2003 Author: bmastenbrook
Index: net-nittin-irc/example/cliki.lisp diff -u net-nittin-irc/example/cliki.lisp:1.2 net-nittin-irc/example/cliki.lisp:1.3 --- net-nittin-irc/example/cliki.lisp:1.2 Sat Dec 13 08:11:34 2003 +++ net-nittin-irc/example/cliki.lisp Sat Dec 13 09:03:39 2003 @@ -1,4 +1,4 @@ -;;;; $Id: cliki.lisp,v 1.2 2003/12/13 13:11:34 bmastenbrook Exp $ +;;;; $Id: cliki.lisp,v 1.3 2003/12/13 14:03:39 bmastenbrook Exp $ ;;;; $Source: /project/net-nittin-irc/cvsroot/net-nittin-irc/example/cliki.lisp,v $
;;;; cliki.lisp - CLiki as an infobot; only works on SBCL. @@ -33,7 +33,7 @@ (let ((stream (socket-make-stream s :input t :output t :buffering :full))) ;; we are exceedingly unportable about proper line-endings here. ;; Anyone wishing to run this under non-SBCL should take especial care - (format stream "GET ~A HTTP/1.0~%Host: ~A~%~%" url host) + (format stream "GET ~A HTTP/1.0~%Host: ~A~%User-Agent: CLiki Bot~%~%" url host) (force-output stream) (list (let* ((l (read-line stream)) @@ -52,7 +52,7 @@ (setf str (regex-replace-all " " str "%20")) (setf str (regex-replace-all "," str "%2C")) (setf str (regex-replace-all "`" str "%60")) - (format t "hi ~A~%" str) + ;(format t "hi ~A~%" str) str)
(defun cliki-first-sentence (term) @@ -76,19 +76,23 @@ (let ((first-line "")) (loop for i from 1 to 5 do ;; scan the first 5 lines (progn - (setf first-line (concatenate 'string first-line (read-line stream))) + (multiple-value-bind (next-line missing-newline-p) + (read-line stream nil) + (if next-line + (setf first-line (concatenate 'string first-line next-line (string #\newline))) + (return-from cliki-return (format nil "The end of the page was reached before a definition was found in ~A" cliki-url)))) (setf first-line (regex-replace-all "\r" first-line " ")) (setf first-line (regex-replace-all "\n" first-line " ")) (setf first-line (regex-replace-all "_\(([^)]*)\)" first-line "\1")) (setf first-line (regex-replace-all "\*\(([^)]*)\)" first-line "\1")) (setf first-line (regex-replace-all "<[^>]+>" first-line "")) - (setf first-line (regex-replace-all "^([^.]+)\..*$" first-line "\1.")) + (setf first-line (regex-replace-all "^(([^.]|\.\S)+)\.\s+.*$" first-line "\1.")) (setf first-line (regex-replace-all "(\s)\s+" first-line "\1")) (setf first-line (regex-replace-all "^\s(.+)$" first-line "\1")) - (when (scan "^[^.]+\.$" first-line) + (when (scan "^([^.]|\.\S)+\.$" first-line) (setf first-line (concatenate 'string first-line " " cliki-url)) (return-from cliki-return first-line)))) - (format nil "No definition was found in the first 5 lines of ~A." cliki-url))) + (format nil "No definition was found in the first 5 lines of ~A" cliki-url))) (if stream (close stream))))) (condition (c &rest whatever) (return-from cliki-return (format nil "An error was encountered in lookup.")))))))
net-nittin-irc-cvs@common-lisp.net