<commit_before>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                                %
%                                                                                %
% Author: <NAME>                                                    %
% E-mail: <EMAIL>                                          %
% IRC Nick: eazar001                                                             %
% Title: Yes-Bot chat-logging extension                                          %
% Descripton: A chat-logging extension for the Yes-Bot IRC Bot                   %
%                                                                                %
%                                                                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

:- module(chat_log, [chat_log/4]).

:- use_module(parser).
:- thread_local known/3.


% XXX TODO : This is still not thread-safe, serialize chat-logs at this point.

%% chat_log(+Rest, +Nick, +Recip, +Chan) is semidet.
%
% Attempt to parse a reply from the server and determine whether or not the
% current line is a valid line to store in an irc chat transcript. If it is, the
% user's nick is ascertained along with the recipient and the corresponding
% message. The message will also be time-stamped. The entire day's worth of chat
% logs will be stored in one file. The next day's chat transript will spawn the
% existence of a new text file.
%
% This will succeed only if Nick and Recip are identical.

chat_log(Rest, Nick, Chan, Chan) :-
  chat_log(Rest, Log),
  get_time(Time),
  stamp_date_time(Time, Date, local),
  once(write_chat_line(Date, Nick, Log)).


%% write_chat_line(+Date, +Nick, +Log) is det.
%
% This predicate is only deterministic due to the fact that it is meant to be
% called once. It's only purpose is to write the chat logs to the correct files
% using the correct naming scheme that reflects the point in time observed.

write_chat_line(Date, Nick, Log) :-
  format_time(atom(Filename), '%d-%b-%Y.txt', Date, posix),
  format_time(atom(Stamp), '%a, %d %b %Y %T', Date, posix),
  date_time_value(day, Date, Current_Day),
  (
     \+known(_, _, _),
     asserta(known(yes, Current_Day, Filename))
  ;
     known(yes, Stored_Day, Filename),
       Current_Day = Stored_Day ->
         true
     ;
       retractall(known(_, _, _)),
       asserta(known(yes, Current_Day, Filename))
   
  ),
  known(yes, _, Filename),
  open(Filename, append, Fstream, []),
  format(Fstream, '<~a> ~s | ~s~n', [Stamp, Nick, Log]),
  flush_output(Fstream),
  close(Fstream).
  <commit_msg>Change known/3 from thread_local to dynamic. Clean up code and document.  Changes to be committed: 	modified:   extensions/chat_log.pl<commit_after>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                                %
%                                                                                %
% Author: <NAME>                                                    %
% E-mail: <EMAIL>                                          %
% IRC Nick: eazar001                                                             %
% Title: Yes-Bot chat-logging extension                                          %
% Descripton: A chat-logging extension for the Yes-Bot IRC Bot                   %
%                                                                                %
%                                                                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

:- module(chat_log, [chat_log/4]).

:- use_module(parser).
:- dynamic known/3.


%% chat_log(+Rest, +Nick, +Recip, +Chan) is semidet.
%
% Attempt to parse a reply from the server and determine whether or not the
% current line is a valid line to store in an irc chat transcript. If it is, the
% user's nick is ascertained along with the recipient and the corresponding
% message. The message will also be time-stamped. The entire day's worth of chat
% logs will be stored in one file. The next day's chat transript will spawn the
% existence of a new text file.
%
% This will succeed only if Nick and Recip are identical.

chat_log(Rest, Nick, Chan, Chan) :-
  chat_log(Rest, Log),
  get_time(Time),
  stamp_date_time(Time, Date, local),
  write_chat_line(Date, Nick, Log).


%% write_chat_line(+Date, +Nick, +Log) is det.
%
% Write chat logs to the correct files using the correct naming scheme that
% reflects the point in time observed. If the current day is not known then cut
% and assert the current day. Afterwards, open a file for appending logs to. If
% the day is known, then compared the stored day to the ascertained day. If they
% match, proceed with the normal course of action. If they do not, assert the
% newly ascertained day and retract the old one. Open a new file for appending
% the new day's logs to.

write_chat_line(Date, Nick, Log) :-
  format_time(atom(Filename), '%d-%b-%Y.txt', Date, posix),
  format_time(atom(Stamp), '%a, %d %b %Y %T', Date, posix),
  date_time_value(day, Date, Current_Day),
  (
     \+known(_, _, _),
     asserta(known(yes, Current_Day, Filename)), !
  ;
     known(yes, Stored_Day, Filename),
       Current_Day = Stored_Day ->
         true
     ;
       retractall(known(_, _, _)),
       asserta(known(yes, Current_Day, Filename))
   
  ),
  known(yes, _, Filename),
  open(Filename, append, Fstream, []),
  format(Fstream, '<~a> ~s | ~s~n', [Stamp, Nick, Log]),
  flush_output(Fstream),
  close(Fstream).
  
{'fraction_non_alphanumeric': 0.14924322421682507, 'fraction_numerical': 0.0019359380499824005, 'mean_word_length': 1.8965341488277268, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7353851', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1428, 'n_words': 524}