<commit_before>// The MIT License (MIT)
//
// Copyright (c) 2015 <NAME>
//
// See LICENSE file.
package memo

import (
	"database/sql"
	"fmt"
	"github.com/romainletendart/goxxx/core"
	"github.com/thoj/go-ircevent"
	"log"
	"strings"
)

var _database *sql.DB

type memoData struct {
	id        int
	date      string
	message   string
	user_from string
	user_to   string
}

func Init(db *sql.DB) {
	_database = db
	sqlStmt := `CREATE TABLE IF NOT EXISTS Memo (
    id integer NOT NULL PRIMARY KEY,
    user_to TEXT,
    user_from TEXT,
    message TEXT,
    date DATETIME DEFAULT CURRENT_TIMESTAMP);`

	_, err := db.Exec(sqlStmt)
	if err != nil {
		log.Fatalf("%q: %s\n", err, sqlStmt)
	}
}

func HandleMemoCmd(event *irc.Event, callback func(*core.ReplyCallbackData)) bool {
	fields := strings.Fields(event.Message())
	// fields[0]  => Command
	// fields[1]  => recipient's nick
	// fields[2:] => message
	if len(fields) == 0 || fields[0] != "!memo" {
		return false
	}

	if len(fields) < 3 {
		if callback != nil {
			callback(&core.ReplyCallbackData{
				Message: fmt.Sprintf("Memo usage: %s \"recipient's nick\" \"message\"", fields[0])})
		}
		return false
	}

	memo := memoData{
		user_to:   fields[1],
		user_from: event.Nick,
		message:   strings.Join(fields[2:], " ")}

	sqlStmt := "INSERT INTO Memo (user_to, user_from, message) VALUES ($1, $2, $3)"
	_, err := _database.Exec(sqlStmt, memo.user_to, memo.user_from, memo.message)
	if err != nil {
		log.Fatalf("%q: %s\n", err, sqlStmt)
	}

	if callback != nil {
		callback(&core.ReplyCallbackData{
			// Message: fmt.Sprintf("Memo for %s from %s: \"%s\"", fields[1], user_from, message)})
			Message: fmt.Sprintf("%s: memo for %s saved", memo.user_from, memo.user_from)})
	}
	return true
}

func SendMemo(event *irc.Event, callback func(*core.ReplyCallbackData)) bool {
	if callback == nil {
		log.Println("Callback nil for the SendMemo function, unable to send the memo")
	}

	user := event.Nick
	sqlQuery := "SELECT id, user_to, user_from, message, strftime('%d/%m/%Y @ %H:%M', datetime(date, 'localtime')) FROM Memo WHERE user_to = $1;"<commit_msg>Memo: Added "help" message for !memostat command<commit_after>// The MIT License (MIT)
//
// Copyright (c) 2015 <NAME>
//
// See LICENSE file.
package memo

import (
	"database/sql"
	"fmt"
	"github.com/romainletendart/goxxx/core"
	"github.com/thoj/go-ircevent"
	"log"
	"strings"
)

var _database *sql.DB

type memoData struct {
	id        int
	date      string
	message   string
	user_from string
	user_to   string
}

func Init(db *sql.DB) {
	_database = db
	sqlStmt := `CREATE TABLE IF NOT EXISTS Memo (
    id integer NOT NULL PRIMARY KEY,
    user_to TEXT,
    user_from TEXT,
    message TEXT,
    date DATETIME DEFAULT CURRENT_TIMESTAMP);`

	_, err := db.Exec(sqlStmt)
	if err != nil {
		log.Fatalf("%q: %s\n", err, sqlStmt)
	}
}

func HandleMemoCmd(event *irc.Event, callback func(*core.ReplyCallbackData)) bool {
	fields := strings.Fields(event.Message())
	// fields[0]  => Command
	// fields[1]  => recipient's nick
	// fields[2:] => message
	if len(fields) == 0 || fields[0] != "!memo" {
		return false
	}

	if len(fields) < 3 {
		if callback != nil {
			callback(&core.ReplyCallbackData{
				Message: fmt.Sprintf("Memo usage: %s \"recipient's nick\" \"message\"", fields[0])})
			callback(&core.ReplyCallbackData{
				Message: "Memo usage: !memostat to list unread memos"})
		}
		return false
	}

	memo := memoData{
		user_to:   fields[1],
		user_from: event.Nick,
		message:   strings.Join(fields[2:], " ")}

	sqlStmt := "INSERT INTO Memo (user_to, user_from, message) VALUES ($1, $2, $3)"
	_, err := _database.Exec(sqlStmt, memo.user_to, memo.user_from, memo.message)
	if err != nil {
		log.Fatalf("%q: %s\n", err, sqlStmt)
	}

	if callback != nil {
		callback(&core.ReplyCallbackData{
			// Message: fmt.Sprintf("Memo for %s from %s: \"%s\"", fields[1], user_from, message)})
			Message: fmt.Sprintf("%s: memo for %s saved", memo.user_from, memo.user_from)})
	}
	return true
}

func SendMemo(event *irc.Event, callback func(*core.ReplyCallbackData)) bool {
	if callback == nil {
		log.Println("Callback nil for the SendMemo function, unable to send the memo")
	}

	user := event.Nick
	sqlQuery := "SELECT id, user_to, user_from, message, strftime('%d/%m/%Y @ %H:%M', datetime(date, 'localtime')) FROM Memo WHERE user_to = $1;"
{'fraction_non_alphanumeric': 0.15853658536585366, 'fraction_numerical': 0.008283479061205707, 'mean_word_length': 3.6842672413793105, 'pattern_counts': {'":': 0, '<': 7, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1488280', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1587, 'n_words': 424}