-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b62c401
commit b00397f
Showing
13 changed files
with
246 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
*.so | ||
compile_plugins | ||
main | ||
fedai | ||
database.db | ||
app.db | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.