<commit_before>;;; Copyright 2012 <NAME> All Rights Reserved.
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;;     http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.

(in-package #:orcabot)

(defmodule memo memo-module ("memo")
  (memos :accessor memos-of :initform (make-hash-table :test 'equalp)
         :documentation "Container for memo information.  Memos are
stored as NICK UNIVERSAL-TIME MESSAGE, with the destination user as
the key.
"))

(defmethod initialize-module ((module memo-module) config)
  (clrhash (memos-of module))
  (with-open-file (inf (orcabot-path "data/memos.lisp")
                       :direction :input
                       :if-does-not-exist nil)
    (when inf
      (loop for tuple = (read inf nil)
         while tuple
         do (setf (gethash (first tuple) (memos-of module))
                  (rest tuple))))))

(defun save-memos (module)
  (with-open-file (ouf (orcabot-path "data/memos.lisp")
                       :direction :output
                       :if-exists :supersede
                       :if-does-not-exist :create)
    (maphash (lambda (k v)
               (write (list* k v) :stream ouf)
               (terpri ouf))
             (memos-of module))))

(defun send-pending-memos (module nick)
  (let* ((user (normalize-nick nick))
         (memos (gethash user (memos-of module))))
    (when memos
      (dolist (memo memos)
        (irc:notice (conn-of module)
                    nick
                    (format nil "~a said ~a ago: ~a"
                            (first memo)
                            (describe-duration (- (get-universal-time)
                                                  (second memo)))
                            (third memo))))
      (remhash user (memos-of module))
      (save-memos module))))

(defun add-new-memo (module from-nick to-nick message)
  (push (list from-nick
              (get-universal-time)
              message)
        (gethash (normalize-nick to-nick) (memos-of module)))
  (save-memos module))

(defmethod examine-message ((module memo-module) (type (eql 'irc:ctcp-action-message))
                           message)
  (send-pending-memos module (source message)))

(defmethod examine-message ((module memo-module) (type (eql 'irc:irc-nick-message))
                           message)
  (send-pending-memos module (source message)))

(defmethod examine-message ((module memo-module) (type (eql 'irc:irc-part-message))
                           message)
  (send-pending-memos module (source message)))

(defmethod examine-message ((module memo-module) (type (eql 'irc:irc-join-message))
                           message)<commit_msg>Memos are now sent using privmsg instead of notice<commit_after>;;; Copyright 2012 <NAME> All Rights Reserved.
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;;     http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.

(in-package #:orcabot)

(defmodule memo memo-module ("memo")
  (memos :accessor memos-of :initform (make-hash-table :test 'equalp)
         :documentation "Container for memo information.  Memos are
stored as NICK UNIVERSAL-TIME MESSAGE, with the destination user as
the key.
"))

(defmethod initialize-module ((module memo-module) config)
  (clrhash (memos-of module))
  (with-open-file (inf (orcabot-path "data/memos.lisp")
                       :direction :input
                       :if-does-not-exist nil)
    (when inf
      (loop for tuple = (read inf nil)
         while tuple
         do (setf (gethash (first tuple) (memos-of module))
                  (rest tuple))))))

(defun save-memos (module)
  (with-open-file (ouf (orcabot-path "data/memos.lisp")
                       :direction :output
                       :if-exists :supersede
                       :if-does-not-exist :create)
    (maphash (lambda (k v)
               (write (list* k v) :stream ouf)
               (terpri ouf))
             (memos-of module))))

(defun send-pending-memos (module nick)
  (let* ((user (normalize-nick nick))
         (memos (gethash user (memos-of module))))
    (when memos
      (dolist (memo memos)
        (irc:privmsg (conn-of module)
                     nick
                     (format nil "~a said ~a ago: ~a"
                             (first memo)
                             (describe-duration (- (get-universal-time)
                                                   (second memo)))
                             (third memo))))
      (remhash user (memos-of module))
      (save-memos module))))

(defun add-new-memo (module from-nick to-nick message)
  (push (list from-nick
              (get-universal-time)
              message)
        (gethash (normalize-nick to-nick) (memos-of module)))
  (save-memos module))

(defmethod examine-message ((module memo-module) (type (eql 'irc:ctcp-action-message))
                           message)
  (send-pending-memos module (source message)))

(defmethod examine-message ((module memo-module) (type (eql 'irc:irc-nick-message))
                           message)
  (send-pending-memos module (source message)))

(defmethod examine-message ((module memo-module) (type (eql 'irc:irc-part-message))
                           message)
  (send-pending-memos module (source message)))

(defmethod examine-message ((module memo-module) (type (eql 'irc:irc-join-message))
                           message)
{'fraction_non_alphanumeric': 0.11811524992040752, 'fraction_numerical': 0.0025469595670168737, 'mean_word_length': 2.1336658354114713, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7339814', 'n_tokens_mistral': 1834, 'n_tokens_neox': 1780, 'n_words': 575}