Skip to content

Commit

Permalink
Merge pull request #40 from fl42/fix/message-looks-as-arg
Browse files Browse the repository at this point in the history
Fix issue when message looks like a CLI arg (like "--test")
  • Loading branch information
Bernhard B authored Nov 22, 2020
2 parents 418b934 + b9df80d commit 12a16c3
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func cleanupTmpFiles(paths []string) {

func send(c *gin.Context, attachmentTmpDir string, signalCliConfig string, number string, message string,
recipients []string, base64Attachments []string, isGroup bool) {
cmd := []string{"--config", signalCliConfig, "-u", number, "send", "-m", message}
cmd := []string{"--config", signalCliConfig, "-u", number, "send"}

if len(recipients) == 0 {
c.JSON(400, gin.H{"error": "Please specify at least one recipient"})
Expand Down Expand Up @@ -163,7 +163,7 @@ func send(c *gin.Context, attachmentTmpDir string, signalCliConfig string, numbe
cmd = append(cmd, attachmentTmpPaths...)
}

_, err := runSignalCli(true, cmd)
_, err := runSignalCli(true, cmd, message)
if err != nil {
cleanupTmpFiles(attachmentTmpPaths)
c.JSON(400, gin.H{"error": err.Error()})
Expand All @@ -177,7 +177,7 @@ func send(c *gin.Context, attachmentTmpDir string, signalCliConfig string, numbe
func getGroups(number string, signalCliConfig string) ([]GroupEntry, error) {
groupEntries := []GroupEntry{}

out, err := runSignalCli(true, []string{"--config", signalCliConfig, "-u", number, "listGroups", "-d"})
out, err := runSignalCli(true, []string{"--config", signalCliConfig, "-u", number, "listGroups", "-d"}, "")
if err != nil {
return groupEntries, err
}
Expand Down Expand Up @@ -234,8 +234,11 @@ func getGroups(number string, signalCliConfig string) ([]GroupEntry, error) {
return groupEntries, nil
}

func runSignalCli(wait bool, args []string) (string, error) {
func runSignalCli(wait bool, args []string, stdin string) (string, error) {
cmd := exec.Command("signal-cli", args...)
if stdin != "" {
cmd.Stdin = strings.NewReader(stdin)
}
if wait {
var errBuffer bytes.Buffer
var outBuffer bytes.Buffer
Expand Down Expand Up @@ -338,7 +341,7 @@ func (a *Api) RegisterNumber(c *gin.Context) {
command = append(command, "--voice")
}

_, err := runSignalCli(true, command)
_, err := runSignalCli(true, command, "")
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -391,7 +394,7 @@ func (a *Api) VerifyRegisteredNumber(c *gin.Context) {
cmd = append(cmd, pin)
}

_, err := runSignalCli(true, cmd)
_, err := runSignalCli(true, cmd, "")
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -495,7 +498,7 @@ func (a *Api) Receive(c *gin.Context) {
timeout := c.DefaultQuery("timeout", "1")

command := []string{"--config", a.signalCliConfig, "-u", number, "receive", "-t", timeout, "--json"}
out, err := runSignalCli(true, command)
out, err := runSignalCli(true, command, "")
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -544,7 +547,7 @@ func (a *Api) CreateGroup(c *gin.Context) {
cmd := []string{"--config", a.signalCliConfig, "-u", number, "updateGroup", "-n", req.Name, "-m"}
cmd = append(cmd, req.Members...)

out, err := runSignalCli(true, cmd)
out, err := runSignalCli(true, cmd, "")
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -600,7 +603,7 @@ func (a *Api) DeleteGroup(c *gin.Context) {
return
}

_, err = runSignalCli(true, []string{"--config", a.signalCliConfig, "-u", number, "quitGroup", "-g", string(groupId)})
_, err = runSignalCli(true, []string{"--config", a.signalCliConfig, "-u", number, "quitGroup", "-g", string(groupId)}, "")
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
Expand All @@ -624,7 +627,7 @@ func (a *Api) GetQrCodeLink(c *gin.Context) {

command := []string{"--config", a.signalCliConfig, "link", "-n", deviceName}

tsdeviceLink, err := runSignalCli(false, command)
tsdeviceLink, err := runSignalCli(false, command, "")
if err != nil {
log.Error("Couldn't create QR code: ", err.Error())
c.JSON(400, Error{Msg: "Couldn't create QR code: " + err.Error()})
Expand Down

0 comments on commit 12a16c3

Please sign in to comment.