~shunter/cordcgi

0706e9f81a0c1e0bb7f1ee1afe4ed9e1616edffe — Samuel Hunter 3 years ago 8e697e0
Run external commands from a cgi-bin directory.
2 files changed, 31 insertions(+), 1 deletions(-)

M .gitignore
M discordcgi.go
M .gitignore => .gitignore +1 -0
@@ 1,2 1,3 @@
discordcgi
settings.json
cgi-bin

M discordcgi.go => discordcgi.go +30 -1
@@ 4,12 4,16 @@
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/bwmarrin/discordgo"
	"log"
	"os"
	"os/exec"
	"os/signal"
	"path"
	"strings"
	"syscall"
)



@@ 19,9 23,10 @@ var logger = log.New(os.Stdout, "Discordcgi: ",
	log.Ldate|log.Ltime|log.Lshortfile)

var Settings struct {
	CgiBin   string
	ClientID string
	Token    string
	Prefix   string
	Token    string
}

func loadSettingsOrPanic() {


@@ 71,5 76,29 @@ func main() {
}

func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
	if m.Author.Bot || !strings.HasPrefix(m.Content, Settings.Prefix) {
		// Disregard all bot comments and non-prefix messages
		return
	}

	// Log all messages being processed.
	logger.Println("New message: ", m.Content)

	content := m.Content[len(Settings.Prefix):]
	args := strings.Split(content, " ")
	command := path.Join(Settings.CgiBin, args[0])
	cmd := exec.Command(command, args[1:]...)
	var out bytes.Buffer

	cmd.Stdout = &out
	cmd.Env = append(os.Environ(),
		"DISCORD_MESSAGE_AUTHOR="+m.Author.ID,
		"DISCORD_MESSAGE="+m.ID,
		// Yadda yadda put literally everything here
	)
	if err := cmd.Run(); err != nil {
		log.Println(err)
	}

	s.ChannelMessageSend(m.ChannelID, out.String())
}