From c37ed1c9825483670bf298dcebe6aab98c61e261 Mon Sep 17 00:00:00 2001 From: shoopea Date: Mon, 6 Jan 2020 12:04:40 +0800 Subject: [PATCH] fix rules --- rules.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/rules.go b/rules.go index 7b7fb8d..1482a8b 100644 --- a/rules.go +++ b/rules.go @@ -73,3 +73,53 @@ func resetMsgParsingRules() error { return nil } + +func loadMsgParsingRules() (m map[int]MessageParsingRule, err error) { + var ( + id int32 + priority int32 + descn string + rule string + msgTypeID64 int64 + chatID64 int64 + userID64 int64 + ) + + log.Println("Loading message parsing rules...") + m = make(map[int]MessageParsingRule) + count := int(0) + + defer func() { + if rec := recover(); rec != nil { + log.Println("Error parsing rules : ", rec) + err = errors.New("panic") + } + }() + + rules, err := db.Query(`SELECT r.id, r.prio, r.descn, r.rule, r.msg_type_id, r.chat_id, r.user_id FROM msg_rules r ORDER BY r.prio DESC;`) + if err != nil { + return m, err + } + defer rules.Close() + + for rules.Next() { + err = rules.Scan(&id, &priority, &descn, &rule, &msgTypeID64, &chatID64, &userID64) + if err != nil { + return m, err + } + i := new(MessageParsingRule) + i.ID = id + i.Priority = priority + i.Description = descn + i.Rule = rule + i.MsgTypeID64 = msgTypeID64 + i.ChatID64 = chatID64 + i.SenderUserID64 = userID64 + i.re = regexp.MustCompile(rule) + m[count] = *i + // log.Printf("New rule : %s\n", rule) + count++ + } + + return m, nil +}