63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
|
package botlib
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"os/exec"
|
||
|
)
|
||
|
|
||
|
type MessageType int
|
||
|
|
||
|
const (
|
||
|
MessageRequest = 1
|
||
|
MessageResponse = 2
|
||
|
)
|
||
|
|
||
|
type InputPair struct {
|
||
|
Param string `json:"param"`
|
||
|
Argument string `json:"argument"`
|
||
|
}
|
||
|
|
||
|
type Message struct {
|
||
|
Type MessageType `json:"type"`
|
||
|
Inputs []InputPair `json:"inputs"`
|
||
|
Sender string `json:"sender"`
|
||
|
Receiver string `json:"receiver"`
|
||
|
Body string `json:"body"`
|
||
|
}
|
||
|
|
||
|
func Knock(name string) error {
|
||
|
path, exists := Registry[name]
|
||
|
if !exists {
|
||
|
return errors.New("Unknown knock target")
|
||
|
}
|
||
|
/*knock_msg := Message{
|
||
|
Type: MessageRequest,
|
||
|
Sender: "One-Who-Knocks",
|
||
|
Receiver: name,
|
||
|
}*/
|
||
|
if path.Hostname() == "localhost" || path.Hostname() == "127.0.0.1" {
|
||
|
//Local delivery
|
||
|
if path.EscapedPath() == "" {
|
||
|
if path.Port() == "" {
|
||
|
//no path means it's not a binary but no port means it's not listening
|
||
|
//invalid, can't knock
|
||
|
return errors.New("Can't knock, invalid URL")
|
||
|
} else {
|
||
|
//no path but port, local network connection
|
||
|
return errors.New("Unsupposed: Local network") //TODO
|
||
|
}
|
||
|
} else {
|
||
|
//path on localhost means local bin file
|
||
|
binpath := path.EscapedPath()
|
||
|
cmd := exec.Command(binpath)
|
||
|
err := cmd.Run()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
return errors.New("Network messaging not supported yet")
|
||
|
}
|
||
|
return nil
|
||
|
}
|