init: pristine aerc 0.20.0 source
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package ipc
|
||||
|
||||
type Handler interface {
|
||||
Command(args []string) error
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package ipc
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// Request contains all parameters needed for the main instance to respond to
|
||||
// a request.
|
||||
type Request struct {
|
||||
// Arguments contains the commandline arguments. The detection of what
|
||||
// action to take is left to the receiver.
|
||||
Arguments []string `json:"arguments"`
|
||||
}
|
||||
|
||||
// Response is used to report the results of a command.
|
||||
type Response struct {
|
||||
// Error contains the success-state of the command. Error is an empty
|
||||
// string if everything ran successfully.
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
// Encode transforms the message in an easier to transfer format
|
||||
func (msg *Request) Encode() ([]byte, error) {
|
||||
return json.Marshal(msg)
|
||||
}
|
||||
|
||||
// DecodeMessage consumes a raw message and returns the message contained
|
||||
// within.
|
||||
func DecodeMessage(data []byte) (*Request, error) {
|
||||
msg := new(Request)
|
||||
err := json.Unmarshal(data, msg)
|
||||
return msg, err
|
||||
}
|
||||
|
||||
// Encode transforms the message in an easier to transfer format
|
||||
func (msg *Response) Encode() ([]byte, error) {
|
||||
return json.Marshal(msg)
|
||||
}
|
||||
|
||||
// DecodeRequest consumes a raw message and returns the message contained
|
||||
// within.
|
||||
func DecodeRequest(data []byte) (*Request, error) {
|
||||
msg := new(Request)
|
||||
err := json.Unmarshal(data, msg)
|
||||
return msg, err
|
||||
}
|
||||
|
||||
// DecodeResponse consumes a raw message and returns the message contained
|
||||
// within.
|
||||
func DecodeResponse(data []byte) (*Response, error) {
|
||||
msg := new(Response)
|
||||
err := json.Unmarshal(data, msg)
|
||||
return msg, err
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package ipc
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"git.sr.ht/~rjarry/aerc/lib/log"
|
||||
"git.sr.ht/~rjarry/aerc/lib/xdg"
|
||||
)
|
||||
|
||||
type AercServer struct {
|
||||
listener net.Listener
|
||||
handler Handler
|
||||
startup context.Context
|
||||
}
|
||||
|
||||
func StartServer(handler Handler, startup context.Context) (*AercServer, error) {
|
||||
sockpath := xdg.RuntimePath("aerc.sock")
|
||||
// remove the socket if it is not connected to a session
|
||||
if _, err := ConnectAndExec(nil); err != nil {
|
||||
os.Remove(sockpath)
|
||||
}
|
||||
log.Debugf("Starting Unix server: %s", sockpath)
|
||||
l, err := net.Listen("unix", sockpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
as := &AercServer{listener: l, handler: handler, startup: startup}
|
||||
go as.Serve()
|
||||
|
||||
return as, nil
|
||||
}
|
||||
|
||||
func (as *AercServer) Close() {
|
||||
as.listener.Close()
|
||||
}
|
||||
|
||||
var lastId int64 = 0 // access via atomic
|
||||
|
||||
func (as *AercServer) Serve() {
|
||||
defer log.PanicHandler()
|
||||
|
||||
<-as.startup.Done()
|
||||
|
||||
for {
|
||||
conn, err := as.listener.Accept()
|
||||
switch {
|
||||
case errors.Is(err, net.ErrClosed):
|
||||
log.Infof("shutting down UNIX listener")
|
||||
return
|
||||
case err != nil:
|
||||
log.Errorf("ipc: accepting connection failed: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
defer conn.Close()
|
||||
clientId := atomic.AddInt64(&lastId, 1)
|
||||
log.Debugf("unix:%d accepted connection", clientId)
|
||||
scanner := bufio.NewScanner(conn)
|
||||
err = conn.SetDeadline(time.Now().Add(1 * time.Minute))
|
||||
if err != nil {
|
||||
log.Errorf("unix:%d failed to set deadline: %v", clientId, err)
|
||||
}
|
||||
for scanner.Scan() {
|
||||
// allow up to 1 minute between commands
|
||||
err = conn.SetDeadline(time.Now().Add(1 * time.Minute))
|
||||
if err != nil {
|
||||
log.Errorf("unix:%d failed to update deadline: %v", clientId, err)
|
||||
}
|
||||
msg, err := DecodeRequest(scanner.Bytes())
|
||||
log.Tracef("unix:%d got message %s", clientId, scanner.Text())
|
||||
if err != nil {
|
||||
log.Errorf("unix:%d failed to parse request: %v", clientId, err)
|
||||
continue
|
||||
}
|
||||
|
||||
response := as.handleMessage(msg)
|
||||
result, err := response.Encode()
|
||||
if err != nil {
|
||||
log.Errorf("unix:%d failed to encode result: %v", clientId, err)
|
||||
continue
|
||||
}
|
||||
_, err = conn.Write(append(result, '\n'))
|
||||
if err != nil {
|
||||
log.Errorf("unix:%d failed to send response: %v", clientId, err)
|
||||
break
|
||||
}
|
||||
}
|
||||
log.Tracef("unix:%d closed connection", clientId)
|
||||
}
|
||||
}
|
||||
|
||||
func (as *AercServer) handleMessage(req *Request) *Response {
|
||||
err := as.handler.Command(req.Arguments)
|
||||
if err != nil {
|
||||
return &Response{Error: err.Error()}
|
||||
}
|
||||
return &Response{}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package ipc
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"git.sr.ht/~rjarry/aerc/lib/xdg"
|
||||
)
|
||||
|
||||
func ConnectAndExec(args []string) (*Response, error) {
|
||||
sockpath := xdg.RuntimePath("aerc.sock")
|
||||
conn, err := net.Dial("unix", sockpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
req, err := (&Request{Arguments: args}).Encode()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to encode request: %w", err)
|
||||
}
|
||||
|
||||
_, err = conn.Write(append(req, '\n'))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to send message: %w", err)
|
||||
}
|
||||
scanner := bufio.NewScanner(conn)
|
||||
if !scanner.Scan() {
|
||||
return nil, errors.New("No response from server")
|
||||
}
|
||||
resp, err := DecodeResponse(scanner.Bytes())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user