logmania/bot/hostname.go

68 lines
1.7 KiB
Go
Raw Permalink Normal View History

2018-09-11 22:57:09 +02:00
package bot
import (
"fmt"
2019-08-08 15:51:55 +02:00
"dev.sum7.eu/sum7/logmania/database"
2018-09-11 22:57:09 +02:00
timeago "github.com/ararog/timeago"
)
func NewHostname(db *database.DB) *Command {
return &Command{
Name: "hostname",
2019-06-20 10:25:52 +02:00
Description: "alternative short (host)names for long IP-Addresses or URLs (and time of last received input)",
2018-09-11 22:57:09 +02:00
Commands: []*Command{
2018-11-06 00:52:45 +01:00
{
2018-09-11 22:57:09 +02:00
Name: "set",
Description: "set or replace a hostname: IPAddress/Hostname NewHostname",
Action: func(from string, params []string) string {
if len(params) != 2 {
return "invalid: IPAddress/Hostname NewHostname"
}
addr := params[0]
name := params[1]
h := db.GetHost(addr)
if h == nil {
h = db.NewHost(addr)
}
db.ChangeHostname(h, name)
return fmt.Sprintf("set for %s the hostname %s", addr, name)
},
},
2018-11-06 00:52:45 +01:00
{
2018-09-11 22:57:09 +02:00
Name: "del",
Description: "delete a hostname: IPAddress/Hostname",
Action: func(from string, params []string) string {
if len(params) != 1 {
return "invalid: IPAddress/Hostname"
}
addr := params[0]
h := db.GetHost(addr)
if h != nil {
db.DeleteHost(h)
if h.Name != "" {
return fmt.Sprintf("remove host %s with hostname %s", h.Address, h.Name)
}
return fmt.Sprintf("remove host %s", h.Address)
}
return "could not found host"
},
},
},
Action: func(from string, params []string) string {
msg := "hostnames:\n"
for _, host := range db.Hosts {
if host.Lastseen.Year() > 1 {
got, _ := timeago.TimeAgoFromNowWithTime(host.Lastseen)
msg = fmt.Sprintf("%s%s - %s (%s)\n", msg, host.Address, host.Name, got)
} else {
msg = fmt.Sprintf("%s%s - %s\n", msg, host.Address, host.Name)
}
}
return msg
},
}
}