<commit_before>// Package util contains definitions for filtering and posting kills to Slack from zKillboard.
package util

import (
	"bytes"
	"log"
	"strconv"
	"text/template"
	"time"

	"github.com/dustin/go-humanize"
	"github.com/nlopes/slack"
	"github.com/spf13/viper"
	"github.com/vivace-io/evelib/zkill"
)

/* util/util.go
 * Defines functions for loading the configuration file, as well as formating
 * and posting kills to Slack.
 */

var t = template.Must(template.ParseGlob("response.tmpl"))

// postData is passed to the executed template for formatting.
type postData struct {
	SoloKill       bool
	AttackerName   string
	VictimName     string
	VictimShipName string
	DamageTaken    int
	TotalValue     string
	NumberInvolved int
}

// LoadConfig reads the configuration file and returns it,
// marshalled in to Config
func LoadConfig() (*viper.Viper, error) {
	v := viper.New()
	v.AddConfigPath(".")
	v.SetConfigFile("zk2s.config.json")
	err := v.ReadInConfig()
	return v, err
}

// PostKill applys the filter(s) to the kill, and posts the kill to slack
// only if the kill is within the configured filters.
func PostKill(kill *zkill.ZKill, bot *slack.Client, config *viper.Viper) {
	if isWithinFilters(kill, config) {
		format(kill, bot, config)
	}
}

// format loads the formatting template and applies formatting
// rules from the Configuration object.
func format(kill *zkill.ZKill, bot *slack.Client, config *viper.Viper) {
	title := new(bytes.Buffer)
	body := new(bytes.Buffer)
	var err error

	data := postData{}
	getData(kill, &data)
	err = t.ExecuteTemplate(title, "killtitle", data)
	if err != nil {
		log.Println(err)
	}
	err = t.ExecuteTemplate(body, "killbody", data)
	if err != nil {
		log.Println(err)
	}

	attch := slack.Attachment{}
	attch.MarkdownIn = []string{"pretext", "text"}
	attch.Title = title.String()
	attch.TitleLink = "https://zkillboard.com/kills/" + strconv.Itoa(kill.KillID) + "/"
	attch.ThumbURL = "http://image.eveonline.com/render/" + strconv.Itoa(kill.Killmail.Victim.ShipType.ID) + "_64.png"
	attch.Text = body.String()
	if withinCorpFilter(kill.Killmail.Victim.Corporation.ID, config) {
		attch.Color = "danger"
	} else if withinAllianceFilter(kill.Killmail.Victim.Alliance.ID, config) {
		attch.Color = "danger"
	} else {
		attch.Color = "good"
	}
	messageParams := slack.PostMessageParameters{}<commit_msg>fixed typeo where post link did not direct to the correct location<commit_after>// Package util contains definitions for filtering and posting kills to Slack from zKillboard.
package util

import (
	"bytes"
	"log"
	"strconv"
	"text/template"
	"time"

	"github.com/dustin/go-humanize"
	"github.com/nlopes/slack"
	"github.com/spf13/viper"
	"github.com/vivace-io/evelib/zkill"
)

/* util/util.go
 * Defines functions for loading the configuration file, as well as formating
 * and posting kills to Slack.
 */

var t = template.Must(template.ParseGlob("response.tmpl"))

// postData is passed to the executed template for formatting.
type postData struct {
	SoloKill       bool
	AttackerName   string
	VictimName     string
	VictimShipName string
	DamageTaken    int
	TotalValue     string
	NumberInvolved int
}

// LoadConfig reads the configuration file and returns it,
// marshalled in to Config
func LoadConfig() (*viper.Viper, error) {
	v := viper.New()
	v.AddConfigPath(".")
	v.SetConfigFile("zk2s.config.json")
	err := v.ReadInConfig()
	return v, err
}

// PostKill applys the filter(s) to the kill, and posts the kill to slack
// only if the kill is within the configured filters.
func PostKill(kill *zkill.ZKill, bot *slack.Client, config *viper.Viper) {
	if isWithinFilters(kill, config) {
		format(kill, bot, config)
	}
}

// format loads the formatting template and applies formatting
// rules from the Configuration object.
func format(kill *zkill.ZKill, bot *slack.Client, config *viper.Viper) {
	title := new(bytes.Buffer)
	body := new(bytes.Buffer)
	var err error

	data := postData{}
	getData(kill, &data)
	err = t.ExecuteTemplate(title, "killtitle", data)
	if err != nil {
		log.Println(err)
	}
	err = t.ExecuteTemplate(body, "killbody", data)
	if err != nil {
		log.Println(err)
	}

	attch := slack.Attachment{}
	attch.MarkdownIn = []string{"pretext", "text"}
	attch.Title = title.String()
	attch.TitleLink = "https://zkillboard.com/kill/" + strconv.Itoa(kill.KillID) + "/"
	attch.ThumbURL = "http://image.eveonline.com/render/" + strconv.Itoa(kill.Killmail.Victim.ShipType.ID) + "_64.png"
	attch.Text = body.String()
	if withinCorpFilter(kill.Killmail.Victim.Corporation.ID, config) {
		attch.Color = "danger"
	} else if withinAllianceFilter(kill.Killmail.Victim.Alliance.ID, config) {
		attch.Color = "danger"
	} else {
		attch.Color = "good"
	}
	messageParams := slack.PostMessageParameters{}
{'fraction_non_alphanumeric': 0.12709731543624161, 'fraction_numerical': 0.0020973154362416107, 'mean_word_length': 4.116952789699571, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1246111', 'n_tokens_mistral': 1726, 'n_tokens_neox': 1667, 'n_words': 432}