Skip to content

Commit

Permalink
wip: add plugin manager
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammedKpln committed Oct 14, 2024
1 parent b62c401 commit b00397f
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 208 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*.so
compile_plugins
main
fedai
database.db
app.db
.env
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ COPY . .

RUN go build --buildmode=plugin --trimpath -o /help.so /app/pl/help.go
RUN go build --buildmode=plugin --trimpath -o /voicy.so /app/pl/voicy.go
RUN go build --buildmode=plugin --trimpath -o /plugin.so /app/pl/plugin.go
RUN go build --buildmode=plugin --trimpath -o /plugins.so /app/pl/plugins.go


FROM golang:latest as serverBuilder
Expand All @@ -20,6 +22,8 @@ RUN apt update && apt install -y ffmpeg
COPY --from=serverBuilder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=pluginBuilder /help.so ./pl/help.so
COPY --from=pluginBuilder /voicy.so ./pl/voicy.so
COPY --from=pluginBuilder /plugin.so ./pl/plugin.so
COPY --from=pluginBuilder /plugins.so ./pl/plugins.so
COPY --from=serverBuilder /fedai .

CMD [ "/fedai" ]
30 changes: 30 additions & 0 deletions core/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package core

import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

type Plugin struct {
gorm.Model
Name string
Url string
}

var Database *gorm.DB

func GetDatabase() *gorm.DB {
return Database
}

func LoadDatabase() {
db, err := gorm.Open(sqlite.Open("app.db"), &gorm.Config{})

if err != nil {
panic("failed to connect database")
}

db.AutoMigrate(&Plugin{})

Database = db
}
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
services:
fedai:
build:
build:
context: .
dockerfile: Dockerfile
command: ./fedai
command: ./start.sh
env_file:
- .env
volumes:
- data:/opt/fedai

volumes:
data:
driver: local
driver: local
34 changes: 6 additions & 28 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,35 @@ require (
github.com/mattn/go-sqlite3 v1.14.24
github.com/mdp/qrterminal/v3 v3.2.0
go.mau.fi/whatsmeow v0.0.0-20241011190419-de8326a9d38d
gorm.io/gorm v1.25.12
)

require (
github.com/VividCortex/ewma v1.1.1 // indirect
github.com/alessio/shellescape v1.4.1 // indirect
github.com/aws/aws-sdk-go v1.55.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cbroglie/mustache v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cheggaaa/pb/v3 v3.0.5 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/lib/pq v1.7.0 // indirect
github.com/mattn/go-runewidth v0.0.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.3.2 // indirect
github.com/montanaflynn/stats v0.6.3 // indirect
github.com/prometheus/client_golang v1.11.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
github.com/u2takey/go-utils v0.3.1 // indirect
github.com/yuin/gluamapper v0.0.0-20150323120927-d836955830e7 // indirect
gopkg.in/xmlpath.v2 v2.0.0-20150820204837-860cbeca3ebc // indirect
golang.org/x/text v0.18.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Shopify/go-lua v0.0.0-20240527182111-9ab1540f3f5f
github.com/cjoudrey/gluahttp v0.0.0-20201111170219-25003d9adfa9
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/joho/godotenv v1.5.1
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/rs/zerolog v1.33.0 // indirect
github.com/u2takey/ffmpeg-go v0.5.0
github.com/vadv/gopher-lua-libs v0.5.0
github.com/wit-ai/wit-go/v2 v2.1.0 // indirect
github.com/yuin/gopher-lua v1.1.1
go.mau.fi/libsignal v0.1.1 // indirect
go.mau.fi/util v0.8.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
google.golang.org/protobuf v1.34.2
gorm.io/driver/sqlite v1.5.6
rsc.io/qr v0.2.0 // indirect
)
175 changes: 11 additions & 164 deletions go.sum

Large diffs are not rendered by default.

40 changes: 30 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,41 @@ import (
"go.mau.fi/whatsmeow/types/events"
)

func CommandCatcher(message string) *S.Plugin {
func CommandCatcher(message string) (*S.Plugin, S.RegexpMatches) {
var pl *S.Plugin

var foundMatches S.RegexpMatches
for _, plugin := range M.LoadedPlugins {
var matches = plugin.CommandRegex.MatchString(message)
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 matches {
pl = &plugin
break
}

// if len(matches) > 0 {
// pl = &plugin
// foundMatches = matches
// break
// }
}

if pl != nil {
return pl
return pl, foundMatches
}

return pl
return pl, foundMatches
}

func handleMessageEvent(message *events.Message) {
Expand Down Expand Up @@ -67,7 +85,7 @@ func handleMessageEvent(message *events.Message) {

textMessageWrapper := S.If(m != nil, *m.Text, textMessage)

pl := CommandCatcher(textMessageWrapper)
pl, matches := CommandCatcher(textMessageWrapper)
if pl != nil {
fmt.Println(context.SenderJID, C.GetClient().Store.ID)
// fmt.Println(bool(*pl.IsPublic))
Expand All @@ -80,7 +98,7 @@ func handleMessageEvent(message *events.Message) {

}
fmt.Println("selam")
go pl.CommandFn(&context)
go pl.CommandFn(&context, matches)

}
}
Expand All @@ -102,6 +120,8 @@ func main() {
C.LoadDotenv()
M.LoadModules()
C.AppLog().Infof("Modules loaded")
C.LoadDatabase()
C.AppLog().Infof("Database loaded")
C.EstablishConnection(eventHandler)

}
2 changes: 1 addition & 1 deletion pl/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Plugin S.Plugin = S.Plugin{
CommandFn: Run,
}

func Run(message *S.PluginRunOptions) {
func Run(message *S.PluginRunOptions, payload S.RegexpMatches) {
var helpString string
client := C.GetClient()
for _, plugin := range M.LoadedPlugins {
Expand Down
113 changes: 113 additions & 0 deletions pl/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"fmt"
"io"
Cx "muhammedkpln/fedai/context"
C "muhammedkpln/fedai/core"
"muhammedkpln/fedai/shared"
S "muhammedkpln/fedai/shared"
"net/http"
"os"
"regexp"
"strings"
"time"
)

var Plugin S.Plugin = S.Plugin{
Name: "Plugin Manager",
CommandRegex: regexp.MustCompile(`^\.plugin (add|del) (https:\/\/[a-zA-Z0-9\-\.]+(?:\/[a-zA-Z0-9\-._~:\/?#@!$&'()*+,;=]*)?\.so)$`),
CommandInfo: "Handles plugins",
CommandFn: Run,
}

func Run(message *shared.PluginRunOptions, Payload S.RegexpMatches) {
if Payload.Action == nil && Payload.Payload == nil {
go Cx.EditMessage(Cx.ErrorMessage("Action or Payload is missing!"), message)
return
}

var action string = *Payload.Action

switch action {
case "add":
go AddPlugin(message, Payload)
break

case "del":
go DelPlugin(message, Payload)
break

}

}

func DelPlugin(message *shared.PluginRunOptions, Payload S.RegexpMatches) {
splittedUrl := strings.Split(*Payload.Payload, "/")
file := splittedUrl[len(splittedUrl)-1]

os.Remove(fmt.Sprintf("./pl/%s", file))

database := C.GetDatabase()
var plugin C.Plugin
database.Where(C.Plugin{Name: file, Url: *Payload.Payload}).Attrs(C.Plugin{
Name: file,
Url: *Payload.Payload,
}).Delete(&plugin)

go Cx.EditMessage(Cx.InfoMessage(fmt.Sprintf("Deleted %s, restarting in 5 seconds...", file)), message)

time.Sleep(5 * time.Second)

os.Exit(0)
}

func AddPlugin(message *shared.PluginRunOptions, Payload S.RegexpMatches) {
splittedUrl := strings.Split(*Payload.Payload, "/")
file := splittedUrl[len(splittedUrl)-1]

go Cx.EditMessage(Cx.InfoMessage(fmt.Sprintf("Downloading %s...", file)), message)

client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}

resp, err := client.Get(*Payload.Payload)
defer resp.Body.Close()

if err != nil {
panic(err)
}

go Cx.EditMessage(Cx.InfoMessage(fmt.Sprintf("Writing %s...", file)), message)

out, err := os.Create(fmt.Sprintf("./pl/%s", file))
defer out.Close()
n, err := io.Copy(out, resp.Body)

if err != nil {
panic(err)
}

database := C.GetDatabase()
var plugin C.Plugin
database.Where(C.Plugin{Name: file, Url: *Payload.Payload}).Attrs(C.Plugin{
Name: file,
Url: *Payload.Payload,
}).FirstOrCreate(&plugin)

// database.Create(C.Plugin{
// Name: file,
// Url: *Payload.Payload,
// })

go Cx.EditMessage(Cx.SuccessMessage("Download Complete, restarting in 5 seconds..."), message)

fmt.Printf("Written %s bytes", n)

time.Sleep(5 * time.Second)

os.Exit(0)
}
37 changes: 37 additions & 0 deletions pl/plugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"fmt"
Cx "muhammedkpln/fedai/context"
C "muhammedkpln/fedai/core"
S "muhammedkpln/fedai/shared"
"regexp"
)

var Plugin S.Plugin = S.Plugin{
Name: "Plugins",
CommandRegex: regexp.MustCompile(".plugins$"),
CommandInfo: "List Plugins",
CommandFn: Run,
}

func Run(message *S.PluginRunOptions, payload S.RegexpMatches) {
database := C.GetDatabase()
var plugins []C.Plugin
database.Take(&plugins)

if len(plugins) < 1 {
go Cx.EditMessage(Cx.InfoMessage("No plugins available."), message)

return
}

var messages string

for _, plugin := range plugins {
messages += fmt.Sprintf("_*%s*_ \n *URL*: %s \n _*To Delete*_: `.plugin del %s` \n\n", plugin.Name, plugin.Url, plugin.Url)
}

go Cx.EditMessage(messages, message)

}
2 changes: 1 addition & 1 deletion pl/voicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Feedback(msg string, message *S.PluginRunOptions) {

}

func Run(message *S.PluginRunOptions) {
func Run(message *S.PluginRunOptions, Payload S.RegexpMatches) {

if os.Getenv("WITAI_TOKEN") == "" {
Feedback(Context.ErrorMessage("Token ekle."), message)
Expand Down
Loading

0 comments on commit b00397f

Please sign in to comment.