2021-04-22 17:08:24 -06:00
|
|
|
package chats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-07-18 20:53:18 -06:00
|
|
|
"io"
|
2021-04-22 17:08:24 -06:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-07-18 20:53:18 -06:00
|
|
|
"net/url"
|
2021-04-22 17:08:24 -06:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"suah.dev/mcchunkie/plugins"
|
|
|
|
)
|
|
|
|
|
|
|
|
func smsCanSend(number string, numbers []string) bool {
|
|
|
|
for _, s := range numbers {
|
|
|
|
if number == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-07-18 20:53:18 -06:00
|
|
|
type voipms struct {
|
|
|
|
did string
|
|
|
|
dst string
|
|
|
|
message string
|
|
|
|
method string
|
|
|
|
apiUser string
|
|
|
|
apiPassword string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v voipms) UrlStr() string {
|
|
|
|
base, _ := url.Parse("https://voip.ms/api/v1/rest.php")
|
|
|
|
|
|
|
|
params := url.Values{}
|
|
|
|
params.Add("did", v.did)
|
|
|
|
params.Add("dst", v.dst)
|
|
|
|
params.Add("method", v.method)
|
|
|
|
params.Add("api_username", v.apiUser)
|
|
|
|
params.Add("api_password", v.apiPassword)
|
|
|
|
params.Add("message", v.message)
|
|
|
|
|
|
|
|
base.RawQuery = params.Encode()
|
|
|
|
return base.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendVoipmsResp(v voipms) error {
|
|
|
|
resp, err := http.Get(v.UrlStr())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2024-07-18 21:18:39 -06:00
|
|
|
str, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-18 21:24:50 -06:00
|
|
|
log.Println(string(str))
|
2024-07-18 20:53:18 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-22 17:08:24 -06:00
|
|
|
// SMSListen listens for our incoming sms
|
|
|
|
func SMSListen(store ChatStore, plugins *plugins.Plugins) {
|
2024-06-18 14:46:32 -06:00
|
|
|
var (
|
2024-06-21 14:16:55 -06:00
|
|
|
smsPort, _ = store.Get("sms_listen")
|
|
|
|
smsAllowed, _ = store.Get("sms_users")
|
|
|
|
smsUsers = strings.Split(smsAllowed, ",")
|
2024-07-18 20:53:18 -06:00
|
|
|
voipmsUser, _ = store.Get("voipms_user")
|
|
|
|
voipmsPass, _ = store.Get("voipms_api_pass")
|
2024-06-18 14:46:32 -06:00
|
|
|
)
|
2021-04-22 17:08:24 -06:00
|
|
|
|
|
|
|
if smsPort != "" {
|
|
|
|
var htpass, _ = store.Get("sms_htpass")
|
|
|
|
|
|
|
|
log.Printf("SMS: listening on %q\n", smsPort)
|
|
|
|
|
|
|
|
http.HandleFunc("/_sms", func(w http.ResponseWriter, r *http.Request) {
|
2024-06-21 14:16:55 -06:00
|
|
|
var msg, from string
|
2021-04-22 17:08:24 -06:00
|
|
|
user, pass, ok := r.BasicAuth()
|
2024-05-28 15:35:05 -06:00
|
|
|
if !ok {
|
|
|
|
log.Println("SMS: basic auth no ok")
|
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="sms notify"`)
|
|
|
|
http.Error(w, "auth error", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user != "sms" {
|
|
|
|
log.Printf("SMS: failed auth for invalid user: %q, %q\n", user, pass)
|
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="sms notify"`)
|
|
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-22 17:08:24 -06:00
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(htpass), []byte(pass))
|
2024-05-28 15:35:05 -06:00
|
|
|
if err != nil {
|
2021-04-22 17:08:24 -06:00
|
|
|
log.Printf("SMS: failed auth %q %q\n", user, pass)
|
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="sms notify"`)
|
|
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodPost:
|
2024-06-17 19:27:38 -06:00
|
|
|
err = r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "invalid request", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-04-22 17:08:24 -06:00
|
|
|
msg = r.Form.Get("Body")
|
|
|
|
from = r.Form.Get("From")
|
2024-06-17 19:27:38 -06:00
|
|
|
case http.MethodGet:
|
2024-07-18 20:53:18 -06:00
|
|
|
// voip.ms
|
2024-06-18 14:46:32 -06:00
|
|
|
// to={TO}&from={FROM}&message={MESSAGE}&id={ID}&date={TIMESTAMP}
|
2024-06-17 19:27:38 -06:00
|
|
|
msg = r.URL.Query().Get("message")
|
|
|
|
from = r.URL.Query().Get("from")
|
2024-07-18 20:53:18 -06:00
|
|
|
to := r.URL.Query().Get("to")
|
|
|
|
|
|
|
|
for _, p := range *plugins {
|
|
|
|
if p.Match(from, msg) {
|
|
|
|
log.Printf("%s: responding to '%s'", p.Name(), from)
|
|
|
|
p.SetStore(store)
|
|
|
|
|
|
|
|
resp := p.Process(from, msg)
|
|
|
|
err := sendVoipmsResp(voipms{
|
2024-07-18 21:38:02 -06:00
|
|
|
did: to,
|
|
|
|
dst: from,
|
2024-07-18 20:53:18 -06:00
|
|
|
message: resp,
|
|
|
|
method: "sendSMS",
|
|
|
|
apiUser: voipmsUser,
|
|
|
|
apiPassword: voipmsPass,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2021-04-22 17:08:24 -06:00
|
|
|
default:
|
|
|
|
http.Error(
|
|
|
|
w,
|
|
|
|
fmt.Sprintf("method %q not implemented", r.Method),
|
|
|
|
http.StatusMethodNotAllowed,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if smsCanSend(from, smsUsers) {
|
|
|
|
msg = strings.TrimSuffix(msg, "\n")
|
|
|
|
|
|
|
|
if msg == "" {
|
|
|
|
fmt.Fprintf(w, "empty message")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range *plugins {
|
|
|
|
if p.Match(from, msg) {
|
|
|
|
log.Printf("%s: responding to '%s'", p.Name(), from)
|
|
|
|
p.SetStore(store)
|
|
|
|
|
|
|
|
resp := p.Process(from, msg)
|
2024-06-21 14:16:55 -06:00
|
|
|
fmt.Fprint(w, resp)
|
2021-04-22 17:08:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("number not allowed (%q)", from)
|
|
|
|
http.Error(
|
|
|
|
w,
|
|
|
|
fmt.Sprintf("number not allowed (%q)", from),
|
|
|
|
http.StatusMethodNotAllowed,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
log.Fatal(http.ListenAndServe(smsPort, nil))
|
|
|
|
}
|
|
|
|
}
|