about summary refs log tree commit diff stats
path: root/draw.c
Commit message (Expand)AuthorAgeFilesLines
* removed TLast tag enum, now tags is simple defined as char *[] array, the res...arg@10ksloc.org2006-08-031-2/+2
* changed Client->tags and Rule->tags to be Bool (I'll also try to remove the T...arg@10ksloc.org2006-08-031-4/+4
* made fullscreen apps working fine in floating mode (there is no sane way to m...arg@10ksloc.org2006-08-021-1/+0
* committed a patch which fixes the hints of Jukkaarg@10ksloc.org2006-08-011-1/+1
* made status bar drawing more robust, implemented togglemax and togglemode, wo...arg@10ksloc.org2006-07-201-7/+6
* cleaned up codearg@10ksloc.org2006-07-201-2/+4
* added heretag command which allows to tag a client of a foreign tag with curr...Anselm R. Garbe2006-07-181-1/+1
* ordered variables in structs and source files alphabeticallyAnselm R. Garbe2006-07-171-2/+2
* fixed XSync handling and finished man pageAnselm R. Garbe2006-07-151-4/+3
* sanitized other stuffAnselm R. Garbe2006-07-151-52/+52
* proceeded with cleaning up, sorting functions, etcAnselm R. Garbe2006-07-151-72/+74
* rearranged several stuffAnselm R. Garbe2006-07-151-13/+24
* sanitized namesAnselm R. Garbe2006-07-141-5/+5
* rearrangedAnselm R. Garbe2006-07-141-1/+66
* removed a bunch of lines through swap removalAnselm R. Garbe2006-07-141-4/+4
* implemented bar for dwm (I miss status text), I plan that status text is read...Anselm R. Garbe2006-07-141-1/+1
* changed default colorsAnselm R. Garbe2006-07-131-51/+38
* added logo+descriptionAnselm R. Garbe2006-07-131-37/+36
* new stuff (some warning elimination)Anselm R. Garbe2006-07-131-14/+13
* removed unnecessary crapAnselm R. Garbe2006-07-131-2/+3
* new stuff, fixed several issuesAnselm R. Garbe2006-07-121-0/+1
* simplified several portions of code through replacing rect structs with x,y,h...Anselm R. Garbe2006-07-121-22/+23
* new stuffAnselm R. Garbe2006-07-111-3/+3
* added several other stuffAnselm R. Garbe2006-07-101-0/+6
* several new changes, made gridmenu workingAnselm R. Garbe2006-07-101-20/+21
* added new stuffAnselm R. Garbe2006-07-101-0/+163
                                                                                





















                                                                 
                                                                          

                                                                      
                                
                 










                                                                                   




                                                                                    



                                                               















                                                            
package lib

import (
	"bufio"
	"errors"
	"fmt"
	"log"
	"net"
	"net/url"
	"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")
	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?
				as.logger.Printf("Closing Unix server: %v", err)
				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()
		as.logger.Printf("unix:%d: got message %s", clientId, msg)
		if !strings.ContainsRune(msg, ':') {
			conn.Write([]byte("error: invalid command\n"))
			continue
		}
		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)
			}
			if err != nil {
				conn.Write([]byte(fmt.Sprintf("result: %v\n", err)))
			} else {
				conn.Write([]byte(fmt.Sprint("result: success\n")))
			}
		}
	}
	as.logger.Printf("Closed Unix connection %d", clientId)
}

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
}