1// This program is free software: you can redistribute it and/or modify 2// it under the terms of the GNU Affero General Public License as 3// published by the Free Software Foundation, either version 3 of the 4// License, or (at your option) any later version. 5// 6// This program is distributed in the hope that it will be useful, but 7// WITHOUT ANY WARRANTY; without even the implied warranty of 8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 9// Affero General Public License for more details.10//11// You should have received a copy of the GNU Affero General Public12// License along with this program. If not, see <http://www.gnu.org/licenses/>.1314package remind1516import (17 "github.com/nmeum/marvin/irc"18 "github.com/nmeum/marvin/modules"19 "strings"20 "time"21)2223type Module struct {24 TimeLimit int `json:"time_limit"`25 UserLimit int `json:"user_limit"`26}2728func Init(moduleSet *modules.ModuleSet) {29 moduleSet.Register(new(Module))30}3132func (m *Module) Name() string {33 return "remind"34}3536func (m *Module) Help() string {37 return "USAGE: !remind DURATION MSG"38}3940func (m *Module) Defaults() {41 m.TimeLimit = 1042 m.UserLimit = 343}4445func (m *Module) Load(client *irc.Client) error {46 users := make(map[string]int)47 client.CmdHook("privmsg", func(c *irc.Client, msg irc.Message) error {48 splited := strings.Fields(msg.Data)49 if len(splited) < 3 || splited[0] != "!remind" {50 return nil51 }5253 duration, err := time.ParseDuration(splited[1])54 if err != nil {55 return c.Write("NOTICE %s :ERROR: %s", msg.Receiver, err.Error())56 }5758 limit := time.Duration(m.TimeLimit) * time.Hour59 if duration > limit {60 return c.Write("NOTICE %s :%v hours exceeds the limit of %v hours",61 msg.Receiver, duration.Hours(), limit.Hours())62 }6364 if users[msg.Sender.Host] >= m.UserLimit {65 return c.Write("NOTICE %s :You can only run %d reminders at a time",66 msg.Receiver, m.UserLimit)67 }6869 users[msg.Sender.Host]++70 reminder := strings.Join(splited[2:], " ")71 time.AfterFunc(duration, func() {72 users[msg.Sender.Host]--73 c.Write("PRIVMSG %s :Reminder: %s",74 msg.Sender.Name, reminder)75 })7677 return c.Write("NOTICE %s :Reminder setup for %s",78 msg.Receiver, duration.String())79 })8081 return nil82}