<commit_before>package main

import "github.com/cjoudrey/irc"
import "github.com/yuin/gopher-lua"
import "os"
import "fmt"
import "flag"

func main() {
	var host = flag.String("host", "irc.freenode.net", "host to connect to")
	var port = flag.Int("port", 6667, "port to connect to")
	var nickname = flag.String("nickname", "go-irc-bot", "nickname")
	var ident = flag.String("ident", "go-irc-bot", "ident")
	var realname = flag.String("realname", "go-irc-bot", "realname")

	flag.Parse()

	if flag.Arg(0) == "" {
		fmt.Fprintf(os.Stderr, "Usage: %s [options...] <script.lua>\n", os.Args[0])
		flag.PrintDefaults()
		return
	}

	handler := *irc.NewEventHandler()

	client := irc.Client{
		Host: *host,
		Port: *port,
		Nickname: *nickname,
		Ident:    *ident,
		Realname: *realname,
		Handler:  handler,
	}

	l := lua.NewState()
	defer l.Close()

	registerBotTable(l, &client, &handler)
	registerBotFunctions(l)

	if err := l.DoFile(flag.Arg(0)); err != nil {
		panic(err)
	}

	go client.Connect()

	select {}
}

func registerBotTable(l *lua.LState, client *irc.Client, handler *irc.EventHandler) {
	bot := l.NewTable()
	bot.RawSet(lua.LString("write"), l.NewFunction(func(l *lua.LState) int {
		data := l.ToString(1)
		client.Write(data)
		return 1
	}))
	bot.RawSet(lua.LString("on"), l.NewFunction(func(l *lua.LState) int {
		command := l.ToString(1)
		function := l.ToFunction(2)

		handler.On(command, func(client *irc.Client, message *irc.Message) {
			lparams := l.NewTable()
			for _, param := range message.Params {
				lparams.Append(lua.LString(param))
			}

			l.CallByParam(lua.P{
				Fn:      function,
				NRet:    0,
				Protect: true,
			}, lua.LString(message.Prefix), lparams)
		})

		return 1
	}))
	l.SetGlobal("bot", bot)
}

func registerBotFunctions(l *lua.LState) {
	if err := l.DoString(`
bot.join = function(channel)
	bot.write("JOIN " .. channel)
end

bot.nick = function(new_nick)
	bot.write("NICK " .. new_nick)
end

bot.privmsg = function(target, message)
	bot.write("PRIVMSG " .. target .. " :" .. message)
end

bot.on("PING", function(prefix, params)
	print("PING? PONG!")
	bot.write("PONG " .. params[1])
end)
	`); err != nil {
		panic(err)
	}
}<commit_msg>Remove ping log<commit_after>package main

import "github.com/cjoudrey/irc"
import "github.com/yuin/gopher-lua"
import "os"
import "fmt"
import "flag"

func main() {
	var host = flag.String("host", "irc.freenode.net", "host to connect to")
	var port = flag.Int("port", 6667, "port to connect to")
	var nickname = flag.String("nickname", "go-irc-bot", "nickname")
	var ident = flag.String("ident", "go-irc-bot", "ident")
	var realname = flag.String("realname", "go-irc-bot", "realname")

	flag.Parse()

	if flag.Arg(0) == "" {
		fmt.Fprintf(os.Stderr, "Usage: %s [options...] <script.lua>\n", os.Args[0])
		flag.PrintDefaults()
		return
	}

	handler := *irc.NewEventHandler()

	client := irc.Client{
		Host: *host,
		Port: *port,
		Nickname: *nickname,
		Ident:    *ident,
		Realname: *realname,
		Handler:  handler,
	}

	l := lua.NewState()
	defer l.Close()

	registerBotTable(l, &client, &handler)
	registerBotFunctions(l)

	if err := l.DoFile(flag.Arg(0)); err != nil {
		panic(err)
	}

	go client.Connect()

	select {}
}

func registerBotTable(l *lua.LState, client *irc.Client, handler *irc.EventHandler) {
	bot := l.NewTable()
	bot.RawSet(lua.LString("write"), l.NewFunction(func(l *lua.LState) int {
		data := l.ToString(1)
		client.Write(data)
		return 1
	}))
	bot.RawSet(lua.LString("on"), l.NewFunction(func(l *lua.LState) int {
		command := l.ToString(1)
		function := l.ToFunction(2)

		handler.On(command, func(client *irc.Client, message *irc.Message) {
			lparams := l.NewTable()
			for _, param := range message.Params {
				lparams.Append(lua.LString(param))
			}

			l.CallByParam(lua.P{
				Fn:      function,
				NRet:    0,
				Protect: true,
			}, lua.LString(message.Prefix), lparams)
		})

		return 1
	}))
	l.SetGlobal("bot", bot)
}

func registerBotFunctions(l *lua.LState) {
	if err := l.DoString(`
bot.join = function(channel)
	bot.write("JOIN " .. channel)
end

bot.nick = function(new_nick)
	bot.write("NICK " .. new_nick)
end

bot.privmsg = function(target, message)
	bot.write("PRIVMSG " .. target .. " :" .. message)
end

bot.on("PING", function(prefix, params)
	bot.write("PONG " .. params[1])
end)
	`); err != nil {
		panic(err)
	}
}
{'fraction_non_alphanumeric': 0.18290441176470587, 'fraction_numerical': 0.006433823529411764, 'mean_word_length': 3.7940528634361232, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2284499', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1659, 'n_words': 348}