// Factoid add: 'key := value' or 'key :is value' func insert(line *base.Line) { if !line.Addressed || !util.IsFactoidAddition(line.Args[1]) { return } var key, val string if strings.Index(line.Args[1], ":=") != -1 { kv := strings.SplitN(line.Args[1], ":=", 2) key = ToKey(kv[0], false) val = strings.TrimSpace(kv[1]) } else { // we use :is to add val = "key is val" kv := strings.SplitN(line.Args[1], ":is", 2) key = ToKey(kv[0], false) val = strings.Join([]string{strings.TrimSpace(kv[0]), "is", strings.TrimSpace(kv[1])}, " ") } n, c := line.Storable() fact := factoids.NewFactoid(key, val, n, c) // The "randomwoot" factoid contains random positive phrases for success. joy := "Woo" if rand := fc.GetPseudoRand("randomwoot"); rand != nil { joy = rand.Value } if err := fc.Insert(fact); err == nil { count := fc.GetCount(key) bot.ReplyN(line, "%s, I now know %d things about '%s'.", joy, count, key) } else { bot.ReplyN(line, "Error storing factoid: %s.", err) } }
func bot_command(l *base.Line) { // This is a dirty hack to treat factoid additions as a special // case, since they may begin with command string prefixes. if util.IsFactoidAddition(l.Args[1]) { return } if cmd, ln := commands.Match(l.Args[1]); l.Addressed && cmd != nil { // Cut command off, trim and compress spaces. l.Args[1] = strings.Join(strings.Fields(l.Args[1][ln:]), " ") cmd.Execute(l) } }
// Implement client.Handler so commandSet can Handle things directly. func (cs *commandSet) Handle(conn *client.Conn, line *client.Line) { // This is a dirty hack to treat factoid additions as a special // case, since they may begin with command string prefixes. ctx := context(conn, line) if ctx == nil || util.IsFactoidAddition(line.Text()) { return } if r, ln := cs.match(ctx.Text()); ctx.Addressed && r != nil { // Cut command off, trim and compress spaces. ctx.Args[1] = strings.Join(strings.Fields(ctx.Args[1][ln:]), " ") r.Run(ctx) } }