mirror of
https://github.com/aljazceru/signal-cli-rest-api.git
synced 2025-12-20 16:14:29 +01:00
@@ -10,6 +10,8 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"github.com/gabriel-vasile/mimetype"
|
||||||
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
const supervisorctlConfigTemplate = `
|
const supervisorctlConfigTemplate = `
|
||||||
@@ -30,7 +32,38 @@ stdout_logfile_backups=10
|
|||||||
numprocs=1
|
numprocs=1
|
||||||
`
|
`
|
||||||
|
|
||||||
|
func isSignalCliLinkedNumberConfigFile(filename string) (bool, error) {
|
||||||
|
fileExtension := filepath.Ext(filename)
|
||||||
|
if fileExtension != "" {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
mimetype, err := mimetype.DetectFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if mimetype.String() == "application/json" {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func getUsernameFromLinkedNumberConfigFile(filename string) (string, error) {
|
||||||
|
type LinkedNumberConfigFile struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
}
|
||||||
|
bytes, err := ioutil.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
var linkedNumberConfigFile LinkedNumberConfigFile
|
||||||
|
err = json.Unmarshal(bytes, &linkedNumberConfigFile)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return linkedNumberConfigFile.Username, nil
|
||||||
|
}
|
||||||
func main() {
|
func main() {
|
||||||
signalCliConfigDir := "/home/.local/share/signal-cli/"
|
signalCliConfigDir := "/home/.local/share/signal-cli/"
|
||||||
signalCliConfigDirEnv := utils.GetEnv("SIGNAL_CLI_CONFIG_DIR", "")
|
signalCliConfigDirEnv := utils.GetEnv("SIGNAL_CLI_CONFIG_DIR", "")
|
||||||
@@ -58,9 +91,27 @@ func main() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
filename := filepath.Base(item.Name())
|
filename := filepath.Base(item.Name())
|
||||||
if strings.HasPrefix(filename, "+") {
|
isSignalCliLinkedNumberConfigFile, err := isSignalCliLinkedNumberConfigFile(signalCliConfigDataDir + "/" + filename)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Couldn't determine whether file ", filename, " is a signal-cli config file: ", err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(filename, "+") || isSignalCliLinkedNumberConfigFile {
|
||||||
|
var number string = ""
|
||||||
if utils.IsPhoneNumber(filename) {
|
if utils.IsPhoneNumber(filename) {
|
||||||
number := filename
|
number = filename
|
||||||
|
} else if isSignalCliLinkedNumberConfigFile {
|
||||||
|
number, err = getUsernameFromLinkedNumberConfigFile(signalCliConfigDataDir + "/" + filename)
|
||||||
|
if err != nil {
|
||||||
|
log.Debug("Skipping ", filename, " as it is not a valid signal-cli config file: ", err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Error("Skipping ", filename, " as it is not a valid phone number!")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
fifoPathname := fifoBasePathName + strconv.FormatInt(ctr, 10)
|
fifoPathname := fifoBasePathName + strconv.FormatInt(ctr, 10)
|
||||||
tcpPort := tcpBasePort + ctr
|
tcpPort := tcpBasePort + ctr
|
||||||
jsonRpc2ClientConfig.AddEntry(number, utils.ConfigEntry{TcpPort: tcpPort, FifoPathname: fifoPathname})
|
jsonRpc2ClientConfig.AddEntry(number, utils.ConfigEntry{TcpPort: tcpPort, FifoPathname: fifoPathname})
|
||||||
@@ -97,9 +148,6 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Couldn't write ", supervisorctlConfigFilename, ": ", err.Error())
|
log.Fatal("Couldn't write ", supervisorctlConfigFilename, ": ", err.Error())
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
log.Error("Skipping ", filename, " as it is not a valid phone number!")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user