anchor.diff:  ;;;;    You should have received a copy of the GNU Affero General Public License
   ;;;;    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   ;;;;**************************************************************************
  -
   (defpackage "COM.INFORMATIMAGO.SMALL-CL-PGMS.BOTIHN"
  -  (:use "COMMON-LISP" "CL-IRC" "CL-JSON" "DRAKMA" 
  -        "COM.INFORMATIMAGO.COMMON-LISP.CESARUM.LIST")
  +  (:use "COMMON-LISP" "CL-IRC" "CL-JSON" "DRAKMA"  "SPLIT-SEQUENCE"
  +        "COM.INFORMATIMAGO.COMMON-LISP.CESARUM.LIST"
  +        "COM.INFORMATIMAGO.COMMON-LISP.CESARUM.UTILITY")
  +  (:import-from "COM.INFORMATIMAGO.COMMON-LISP.INTERACTIVE.INTERACTIVE"
  +                "DATE" "UPTIME")
     (:export "MAIN")
     (:documentation "
   Botihn is a simple IRC bot monitoring Hacker News, and writing to
  
positive.code:   "The URL where the sources of this ircbot can be found.")
  (defvar *connection* nil
    "The current IRC server connection.")
  
  (defun msg-hook (message)
    "Answers to PRIVMSG sent directly to this bot."
    (when (string= *nickname* (first (arguments message)))
      (privmsg *connection* (source message)
               (format nil "I'm an IRC bot forwarding HackerNews news to ~A; ~
                            under AGPL3 license, my sources are available at <~A>."
                       *channel*
                       *sources-url*))))
  
  
  (defun call-with-retry (delay thunk)
  
negative.code:   "The main program of the botihn IRC bot.
  We connect and reconnect to the *SERVER* under the *NICKNAME*,
  and join to the *CHANNEL* where HackerNews are published."
    (with-simple-restart (quit "Quit")
      (catch :gazongues
        (with-retry (sleep (+ 10 (random 30)))
          (with-simple-restart (reconnect "Reconnect")
            (catch :petites-gazongues
              (unwind-protect
                   (progn
                     (setf *connection* (connect :nickname *nickname* :server *server*))
                     (add-hook *connection* 'irc::irc-privmsg-message 'msg-hook)
                     (join *connection* *channel*)
                     (monitor-initialize)
                     (loop
                       :with next-time = (+ *period* (get-universal-time))
                       :for time = (get-universal-time)
                       :do (if (<= next-time time)
                               (progn
                                 (monitor-hacker-news (lambda (message) (privmsg *connection* *channel* message)))
                                 (incf next-time *period*))
                               (read-message *connection*) #|there's a 10 s timeout in here.|#)))
                (when *connection*
                  (quit *connection*)
                  (setf *connection* nil)))))))))
  
  
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  