-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (101 loc) · 2.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"fmt"
C "muhammedkpln/fedai/core"
S "muhammedkpln/fedai/shared"
_ "github.com/mattn/go-sqlite3"
"go.mau.fi/whatsmeow/types"
"go.mau.fi/whatsmeow/types/events"
)
func CommandCatcher(message string) (*S.Plugin, S.RegexpMatches) {
var pl *S.Plugin
var foundMatches S.RegexpMatches
for _, plugin := range C.LoadedPlugins {
var matches = plugin.CommandRegex.FindStringSubmatch(message)
if len(matches) > 0 {
if len(matches) <= 1 {
pl = &plugin
foundMatches = S.RegexpMatches{
Match: matches[0],
}
} else if len(matches) > 1 && len(matches) == 3 {
pl = &plugin
foundMatches = S.RegexpMatches{
Match: matches[0],
Action: &matches[1],
Payload: &matches[2],
}
}
}
// if len(matches) > 0 {
// pl = &plugin
// foundMatches = matches
// break
// }
}
if pl != nil {
return pl, foundMatches
}
return pl, foundMatches
}
func handleMessageEvent(message *events.Message) {
// set as unavailable to not see(?) the message
go C.GetClient().SendChatPresence(message.Info.Chat, "unavailable", types.ChatPresenceMediaText)
var m = message.Message.ExtendedTextMessage
var textMessage = message.Message.GetConversation()
// Catch only messages
if message.Message.Conversation != nil || m != nil {
var ci = m.GetContextInfo()
var context S.PluginRunOptions = S.PluginRunOptions{
IsQuoted: false,
Message: message,
ChatJID: message.Info.Chat,
SenderJID: message.Info.Sender,
StanzaID: message.Info.ID,
}
// Checks if message has context, which can mean that it has a quoted message.
if ci != nil {
quotedMessage := ci.QuotedMessage
if quotedMessage != nil {
context = S.PluginRunOptions{
IsQuoted: true,
QuotedMessage: quotedMessage,
Message: message,
ChatJID: message.Info.Chat,
SenderJID: message.Info.Sender,
StanzaID: message.Info.ID,
}
}
}
textMessageWrapper := S.If(m != nil, *m.Text, textMessage)
pl, matches := CommandCatcher(textMessageWrapper)
if pl != nil {
fmt.Println(context.SenderJID, C.GetClient().Store.ID)
// fmt.Println(bool(*pl.IsPublic))
if pl.IsPublic == nil {
if message.Info.Sender.ToNonAD() != C.GetClient().Store.ID.ToNonAD() {
// Plugin is not allowed to be used of other users, return
return
}
}
fmt.Println("selam")
go pl.CommandFn(&context, matches)
}
}
}
func eventHandler(evt interface{}) {
switch v := evt.(type) {
case *events.Message:
go handleMessageEvent(v)
case *events.Connected:
C.AppLog().Infof("Connection established")
}
}
func main() {
C.LoadDotenv()
C.LoadDatabase()
C.LoadModules()
C.AppLog().Infof("Modules loaded")
C.AppLog().Infof("Database loaded")
C.EstablishConnection(eventHandler)
}