2019-07-19 18:15:48 +00:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-07-19 18:34:13 +00:00
|
|
|
"errors"
|
2019-07-19 18:15:48 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
2022-02-25 15:03:43 +00:00
|
|
|
"os"
|
2019-07-19 18:15:48 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2022-03-22 08:52:27 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2019-07-19 18:15:48 +00:00
|
|
|
"github.com/kyoh86/xdg"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AercServer struct {
|
|
|
|
listener net.Listener
|
|
|
|
OnMailto func(addr *url.URL) error
|
2022-07-11 18:11:19 +00:00
|
|
|
OnMbox func(source string) error
|
2019-07-19 18:15:48 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 20:31:51 +00:00
|
|
|
func StartServer() (*AercServer, error) {
|
2019-07-19 18:15:48 +00:00
|
|
|
sockpath := path.Join(xdg.RuntimeDir(), "aerc.sock")
|
2022-02-25 15:03:43 +00:00
|
|
|
// remove the socket if it already exists
|
|
|
|
os.Remove(sockpath)
|
2022-07-19 20:31:51 +00:00
|
|
|
logging.Infof("Starting Unix server: %s", sockpath)
|
2019-07-19 18:15:48 +00:00
|
|
|
l, err := net.Listen("unix", sockpath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-19 20:31:51 +00:00
|
|
|
as := &AercServer{listener: l}
|
2019-07-19 18:15:48 +00:00
|
|
|
// TODO: stash clients and close them on exit... bleh racey
|
|
|
|
go func() {
|
2022-03-22 08:52:27 +00:00
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
2019-07-19 18:15:48 +00:00
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: Something more useful, in some cases, on wednesdays,
|
|
|
|
// after 2 PM, I guess?
|
2022-07-19 20:31:51 +00:00
|
|
|
logging.Errorf("Closing Unix server: %v", err)
|
2019-07-19 18:15:48 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-22 08:52:27 +00:00
|
|
|
go func() {
|
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
|
|
|
as.handleClient(conn)
|
|
|
|
}()
|
2019-07-19 18:15:48 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return as, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *AercServer) Close() {
|
|
|
|
as.listener.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastId int64 = 0 // access via atomic
|
|
|
|
|
|
|
|
func (as *AercServer) handleClient(conn net.Conn) {
|
|
|
|
clientId := atomic.AddInt64(&lastId, 1)
|
2022-07-19 20:31:51 +00:00
|
|
|
logging.Debugf("unix:%d accepted connection", clientId)
|
2019-07-19 18:15:48 +00:00
|
|
|
scanner := bufio.NewScanner(conn)
|
2022-07-29 20:31:54 +00:00
|
|
|
err := conn.SetDeadline(time.Now().Add(1 * time.Minute))
|
|
|
|
if err != nil {
|
|
|
|
logging.Errorf("failed to set deadline: %v", err)
|
|
|
|
}
|
2019-07-19 18:15:48 +00:00
|
|
|
for scanner.Scan() {
|
2022-07-29 20:31:54 +00:00
|
|
|
err = conn.SetDeadline(time.Now().Add(1 * time.Minute))
|
|
|
|
if err != nil {
|
|
|
|
logging.Errorf("failed to update deadline: %v", err)
|
|
|
|
}
|
2019-07-19 18:15:48 +00:00
|
|
|
msg := scanner.Text()
|
2022-07-19 20:31:51 +00:00
|
|
|
logging.Debugf("unix:%d got message %s", clientId, msg)
|
2019-07-19 18:15:48 +00:00
|
|
|
if !strings.ContainsRune(msg, ':') {
|
2022-07-29 20:31:54 +00:00
|
|
|
_, innererr := conn.Write([]byte("error: invalid command\n"))
|
|
|
|
if innererr != nil {
|
|
|
|
logging.Errorf("failed to write error message: %v", innererr)
|
|
|
|
}
|
2020-02-13 09:25:55 +00:00
|
|
|
continue
|
2019-07-19 18:15:48 +00:00
|
|
|
}
|
|
|
|
prefix := msg[:strings.IndexRune(msg, ':')]
|
2022-07-29 20:31:54 +00:00
|
|
|
var err error
|
2019-07-19 18:15:48 +00:00
|
|
|
switch prefix {
|
|
|
|
case "mailto":
|
|
|
|
mailto, err := url.Parse(msg)
|
|
|
|
if err != nil {
|
2022-07-29 20:31:54 +00:00
|
|
|
_, innererr := conn.Write([]byte(fmt.Sprintf("error: %v\n", err)))
|
|
|
|
if innererr != nil {
|
|
|
|
logging.Errorf("failed to write error message: %v", innererr)
|
|
|
|
}
|
2019-07-19 18:15:48 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if as.OnMailto != nil {
|
|
|
|
err = as.OnMailto(mailto)
|
2022-07-29 20:31:54 +00:00
|
|
|
if err != nil {
|
|
|
|
logging.Errorf("mailto failed: %v", err)
|
|
|
|
}
|
2019-09-29 11:22:36 +00:00
|
|
|
}
|
2022-07-11 18:11:19 +00:00
|
|
|
case "mbox":
|
|
|
|
if as.OnMbox != nil {
|
|
|
|
err = as.OnMbox(msg)
|
|
|
|
}
|
2022-07-29 20:31:54 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
_, err = conn.Write([]byte(fmt.Sprintf("result: %v\n", err)))
|
|
|
|
if err != nil {
|
|
|
|
logging.Errorf("failed to send error: %v")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, err = conn.Write([]byte("result: success\n"))
|
2022-07-11 18:11:19 +00:00
|
|
|
if err != nil {
|
2022-07-29 20:31:54 +00:00
|
|
|
logging.Errorf("failed to send successmessage: %v")
|
2022-07-11 18:11:19 +00:00
|
|
|
}
|
2019-07-19 18:15:48 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-19 20:31:51 +00:00
|
|
|
logging.Debugf("unix:%d closed connection", clientId)
|
2019-07-19 18:15:48 +00:00
|
|
|
}
|
2019-07-19 18:34:13 +00:00
|
|
|
|
|
|
|
func ConnectAndExec(msg string) error {
|
|
|
|
sockpath := path.Join(xdg.RuntimeDir(), "aerc.sock")
|
|
|
|
conn, err := net.Dial("unix", sockpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-29 20:31:54 +00:00
|
|
|
_, err = conn.Write([]byte(msg + "\n"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to send message: %w", err)
|
|
|
|
}
|
2019-07-19 18:34:13 +00:00
|
|
|
scanner := bufio.NewScanner(conn)
|
|
|
|
if !scanner.Scan() {
|
|
|
|
return errors.New("No response from server")
|
|
|
|
}
|
|
|
|
result := scanner.Text()
|
|
|
|
fmt.Println(result)
|
|
|
|
return nil
|
|
|
|
}
|