tgbot_misaka_5882f7/botcmd_dig.go

56 lines
1.2 KiB
Go
Raw Normal View History

2023-03-20 16:22:46 +09:00
package main
import (
"fmt"
"html"
"strings"
"text/tabwriter"
tele "gopkg.in/telebot.v3"
"git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/hostcmds"
)
func handleDigCmd(c tele.Context) error {
msg := c.Message()
if msg == nil {
return nil
}
req, err := hostcmds.NewDigRequest(msg.Payload)
if err != nil {
return c.Reply(
"Invalid arguments.\nUsage: `/dig <name> [type]`",
&tele.SendOptions{ParseMode: tele.ModeMarkdown},
tele.Silent,
)
}
resp, err := hostcmds.Dig(req)
if err != nil {
_ = c.Reply(stickerFromID(stickerPanic), tele.Silent)
return err
}
replyBuf := &strings.Builder{}
tw := tabwriter.NewWriter(replyBuf, 0, 0, 2, ' ', 0)
// Write header
if len(resp.Records) > 0 {
_, _ = tw.Write([]byte("Name\tTTL\tType\tData\n"))
}
// Write data
for _, r := range resp.Records {
_, _ = fmt.Fprintf(tw, "%s\t%d\t%s\t%s\n", r.Name, r.TTL, r.Type, r.Data)
}
_ = tw.Flush()
replyText := []string{
fmt.Sprintf("<i>Status: <b>%s</b></i>\n", resp.Status),
fmt.Sprintf("<i>Query Time: <b>%s</b></i>\n\n", resp.QueryTime),
"<pre>",
html.EscapeString(replyBuf.String()),
"</pre>",
}
return c.Reply(strings.Join(replyText, ""), &tele.SendOptions{ParseMode: tele.ModeHTML}, tele.Silent)
}