2019-07-19 20:15:48 +02:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-07-19 20:34:13 +02:00
|
|
|
"errors"
|
2019-07-19 20:15:48 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
2022-02-25 16:03:43 +01:00
|
|
|
"os"
|
2019-07-19 20:15:48 +02:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/kyoh86/xdg"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AercServer struct {
|
|
|
|
logger *log.Logger
|
|
|
|
listener net.Listener
|
|
|
|
OnMailto func(addr *url.URL) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartServer(logger *log.Logger) (*AercServer, error) {
|
|
|
|
sockpath := path.Join(xdg.RuntimeDir(), "aerc.sock")
|
2022-02-25 16:03:43 +01:00
|
|
|
// remove the socket if it already exists
|
|
|
|
os.Remove(sockpath)
|
2019-07-19 20:15:48 +02:00
|
|
|
l, err := net.Listen("unix", sockpath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
as := &AercServer{
|
|
|
|
logger: logger,
|
|
|
|
listener: l,
|
|
|
|
}
|
|
|
|
// TODO: stash clients and close them on exit... bleh racey
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: Something more useful, in some cases, on wednesdays,
|
|
|
|
// after 2 PM, I guess?
|
2019-07-29 21:44:28 +02:00
|
|
|
as.logger.Printf("Closing Unix server: %v", err)
|
2019-07-19 20:15:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
go as.handleClient(conn)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
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)
|
|
|
|
as.logger.Printf("Accepted Unix connection %d", clientId)
|
|
|
|
scanner := bufio.NewScanner(conn)
|
|
|
|
conn.SetDeadline(time.Now().Add(1 * time.Minute))
|
|
|
|
for scanner.Scan() {
|
|
|
|
conn.SetDeadline(time.Now().Add(1 * time.Minute))
|
|
|
|
msg := scanner.Text()
|
2020-02-13 10:25:55 +01:00
|
|
|
as.logger.Printf("unix:%d: got message %s", clientId, msg)
|
2019-07-19 20:15:48 +02:00
|
|
|
if !strings.ContainsRune(msg, ':') {
|
|
|
|
conn.Write([]byte("error: invalid command\n"))
|
2020-02-13 10:25:55 +01:00
|
|
|
continue
|
2019-07-19 20:15:48 +02:00
|
|
|
}
|
|
|
|
prefix := msg[:strings.IndexRune(msg, ':')]
|
|
|
|
switch prefix {
|
|
|
|
case "mailto":
|
|
|
|
mailto, err := url.Parse(msg)
|
|
|
|
if err != nil {
|
|
|
|
conn.Write([]byte(fmt.Sprintf("error: %v\n", err)))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if as.OnMailto != nil {
|
|
|
|
err = as.OnMailto(mailto)
|
|
|
|
}
|
2019-09-29 13:22:36 +02:00
|
|
|
if err != nil {
|
|
|
|
conn.Write([]byte(fmt.Sprintf("result: %v\n", err)))
|
|
|
|
} else {
|
2022-03-09 22:48:00 +01:00
|
|
|
conn.Write([]byte("result: success\n"))
|
2019-09-29 13:22:36 +02:00
|
|
|
}
|
2019-07-19 20:15:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
as.logger.Printf("Closed Unix connection %d", clientId)
|
|
|
|
}
|
2019-07-19 20:34:13 +02:00
|
|
|
|
|
|
|
func ConnectAndExec(msg string) error {
|
|
|
|
sockpath := path.Join(xdg.RuntimeDir(), "aerc.sock")
|
|
|
|
conn, err := net.Dial("unix", sockpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn.Write([]byte(msg + "\n"))
|
|
|
|
scanner := bufio.NewScanner(conn)
|
|
|
|
if !scanner.Scan() {
|
|
|
|
return errors.New("No response from server")
|
|
|
|
}
|
|
|
|
result := scanner.Text()
|
|
|
|
fmt.Println(result)
|
|
|
|
return nil
|
|
|
|
}
|