diff --git a/mods/macro.go b/mods/macro.go
new file mode 100644
index 0000000..5f9c9ef
--- /dev/null
+++ b/mods/macro.go
@@ -0,0 +1,76 @@
+package main
+
+import (
+ "encoding/json"
+ "fmt"
+ "log"
+ "os"
+ "strings"
+ "time"
+
+ "github.com/hamcha/clessy/tg"
+)
+
+type Macro struct {
+ Value string
+ Author tg.APIUser
+ Time time.Time
+}
+
+var macropath *string
+var macros map[string]Macro
+
+func initmacro() {
+ macros = make(map[string]Macro)
+ file, err := os.Open(*macropath)
+ if err != nil {
+ return
+ }
+ defer file.Close()
+ err = json.NewDecoder(file).Decode(¯os)
+ if err != nil {
+ log.Println("WARN: Could not load macros (malformed or unreadable file): " + err.Error())
+ return
+ }
+}
+
+func macro(broker *tg.Broker, update tg.APIMessage) {
+ if isCommand(update, "macro") {
+ parts := strings.SplitN(*(update.Text), " ", 3)
+ switch len(parts) {
+ case 2:
+ var out string
+ macroname := strings.ToLower(parts[1])
+ item, ok := macros[macroname]
+ if ok {
+ out = fmt.Sprintf("%s\n%s\n%s - %s", macroname, item.Value, item.Author.Username, item.Time.Format("02-01-06 15:04"))
+ } else {
+ out = fmt.Sprintf("%s\nmacro inesistente\n(configura con /macro %s contenuto)", macroname, macroname)
+ }
+ broker.SendTextMessage(update.Chat, out, &update.MessageID)
+ case 3:
+ macroname := strings.ToLower(parts[1])
+ macros[macroname] = Macro{
+ Value: parts[2],
+ Author: update.User,
+ Time: time.Now(),
+ }
+ savemacros()
+ broker.SendTextMessage(update.Chat, fmt.Sprintf("%s → %s", macroname, parts[2]), &update.MessageID)
+ default:
+ broker.SendTextMessage(update.Chat, "Sintassi\nLeggi: /macro nome-macro\nScrivi: /macro nome-macro contenuto macro", &update.MessageID)
+ }
+ }
+}
+
+func savemacros() {
+ file, err := os.Create(*macropath)
+ if err != nil {
+ log.Println("WARN: Could not open macro db: " + err.Error())
+ return
+ }
+ err = json.NewEncoder(file).Encode(macros)
+ if err != nil {
+ log.Println("WARN: Could not save macros into file: " + err.Error())
+ }
+}
diff --git a/mods/main.go b/mods/main.go
index ef341f4..87b17ae 100644
--- a/mods/main.go
+++ b/mods/main.go
@@ -11,6 +11,7 @@ func initmods() {
initviaggi()
initmeme()
initunsplash()
+ initmacro()
}
func dispatch(broker *tg.Broker, update tg.APIMessage) {
@@ -18,6 +19,7 @@ func dispatch(broker *tg.Broker, update tg.APIMessage) {
viaggi(broker, update)
memegen(broker, update)
unsplash(broker, update)
+ macro(broker, update)
}
func isCommand(update tg.APIMessage, cmdname string) bool {
@@ -38,6 +40,7 @@ func main() {
botname = flag.String("botname", "maudbot", "Bot name for /targetet@commands")
impact = flag.String("impact", "impact.ttf", "Path to impact.ttf (Impact font)")
gillmt = flag.String("gillmt", "gill.ttf", "Path to gill.ttf (Gill Sans MT font)")
+ macropath = flag.String("macropath", "macros.json", "Path to macros db (JSON)")
flag.Parse()
initmods()