Exemple #1
0
func (ag *AddPattern) Do(ctx coa.Context) error {
	mctx := ctx.(*ctxs.MsgContext)
	text := mctx.Msg.Text
	args := getAddPatternArgs(text)
	if len(args) < 1 {
		return errors.New(fmt.Sprintf("error: args = %v", args))
	}

	re, err := regexp.Compile(args[0])
	if err != nil {
		return err
	}

	ptn := &models.Pattern{Pattern: args[0], Strs: args[1:]}
	ag.DB().Create(ptn)
	if ag.DB().Error != nil {
		return ag.DB().Error
	}

	models.CompiledPatterns[re] = ptn.Strs

	ag.SendMsg.Msg = fmt.Sprintf("パッターン %s: %s", args[0], strings.Join(args[1:], ", "))
	ag.SendMsg.Channel = store.ChanByID(mctx.Msg.Channel).Name

	return nil
}
Exemple #2
0
func (ag *RemovePattern) Do(ctx coa.Context) error {
	mctx := ctx.(*ctxs.MsgContext)
	text := mctx.Msg.Text

	args := getRmPatternArgs(text)

	ag.DB().Where("id in (?)", args).Delete(models.Pattern{})

	ag.SendMsg.Msg = fmt.Sprint("パッターン リムーブ!!")
	ag.SendMsg.Channel = store.ChanByID(mctx.Msg.Channel).Name

	return nil
}
Exemple #3
0
func (a *Filtering) Do(ctx coa.Context) error {
	log.Action("==> filtering")
	if ag, ok := ctx.ActionGroup().(HasMsg); ok {
		m := ag.GetMsg()
		chanName := store.ChanByID(m.Channel).Name
		userName := store.UserByID(m.User).Name
		toUser := m.ToUser
		if len(a.PermitChannel) > 0 {
			for _, c := range a.PermitChannel {
				if c == chanName {
					log.Info("filtering: permit channel ok")
					return nil
				}
			}
			log.Info("filtering: permit channel bad")
			return eh.ErrNonError
		}

		if len(a.DenyChannel) > 0 {
			for _, c := range a.DenyChannel {
				if c == chanName {
					log.Info("filtering: deny channel bad")
					return eh.ErrNonError
				}
			}
			log.Info("filtering: deny channel ok")
			return nil
		}

		if len(a.FromPermitUser) > 0 {
			for _, u := range a.FromPermitUser {
				if u == userName {
					log.Info("filtering: from permit user ok")
					return nil
				}
			}
			log.Info("filtering: from permit user bad")
			return eh.ErrNonError
		}

		if len(a.FromDenyUser) > 0 {
			for _, u := range a.FromDenyUser {
				if u == userName {
					log.Info("filtering: from deny user bad")
					return eh.ErrNonError
				}
			}
			log.Info("filtering: from deny user ok")
			return nil
		}

		if len(a.ToPermitUser) > 0 {
			if toUser == nil {
				log.Info("filtering: to permit user bad")
				return eh.ErrNonError
			}
			for _, u := range a.ToPermitUser {
				if u == toUser.Name {
					log.Info("filtering: to permit user ok")
					return nil
				}
			}
			log.Info("filtering: to permit user bad")
			return eh.ErrNonError
		}

		if len(a.ToDenyUser) > 0 {
			if toUser == nil {
				log.Info("filtering: to permit user bad")
				return eh.ErrNonError
			}
			for _, u := range a.ToDenyUser {
				if u == toUser.Name {
					log.Info("filtering: to deny user bad")
					return eh.ErrNonError
				}
			}
			log.Info("filtering: to deny user ok")
			return nil
		}

		if a.Ratio > 0.0 {
			if rand.Float64() <= a.Ratio {
				log.Info("filtering: ratio ok")
				return nil
			}
			log.Info("filtering: ratio bad")
			return eh.ErrNonError
		}

		if a.Func != nil && !a.Func(m) {
			log.Info("filtering: func bad")
			return eh.ErrNonError
		}
	}
	log.Info("filtering: ok")
	return nil
}