aboutsummaryrefslogtreecommitdiffstats
path: root/worker/middleware/foldermapper.go
blob: c78833a228b34e70deeefccd744bbbb1ce39ce0d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package middleware

import (
	"strings"
	"sync"

	"git.sr.ht/~rjarry/aerc/worker/types"
)

type folderMapper struct {
	sync.Mutex
	types.WorkerInteractor
	fm    folderMap
	table map[string]string
}

func NewFolderMapper(base types.WorkerInteractor, mapping map[string]string,
	order []string,
) types.WorkerInteractor {
	base.Infof("loading worker middleware: foldermapper")
	return &folderMapper{
		WorkerInteractor: base,
		fm:               folderMap{mapping, order},
		table:            make(map[string]string),
	}
}

func (f *folderMapper) incoming(msg types.WorkerMessage, dir string) string {
	f.Lock()
	defer f.Unlock()
	mapped := f.table[dir]
	return mapped
}

func (f *folderMapper) outgoing(msg types.WorkerMessage, dir string) string {
	f.Lock()
	defer f.Unlock()
	for k, v := range f.table {
		if v == dir {
			mapped := k
			return mapped
		}
	}
	return dir
}

func (f *folderMapper) store(s string) {
	f.Lock()
	defer f.Unlock()
	display := f.fm.Apply(s)
	f.table[display] = s
	f.Tracef("store display folder '%s' to '%s'", display, s)
}

func (f *folderMapper) create(s string) string {
	f.Lock()
	defer f.Unlock()
	backend := f.fm.Create(s)
	f.table[s] = backend
	f.Tracef("create display folder '%s' as '%s'", s, backend)
	return backend
}

func (f *folderMapper) ProcessAction(msg types.WorkerMessage) types.WorkerMessage {
	switch msg := msg.(type) {
	case *types.CheckMail:
		for i := range msg.Directories {
			msg.Directories[i] = f.incoming(msg, msg.Directories[i])
		}
	case *types.CopyMessages:
		msg.Destination = f.incoming(msg, msg.Destination)
	case *types.AppendMessage:
		msg.Destination = f.incoming(msg, msg.Destination)
	case *types.MoveMessages:
		msg.Destination = f.incoming(msg, msg.Destination)
	case *types.CreateDirectory:
		msg.Directory = f.create(msg.Directory)
	case *types.RemoveDirectory:
		msg.Directory = f.incoming(msg, msg.Directory)
	case *types.OpenDirectory:
		msg.Directory = f.incoming(msg, msg.Directory)
	}

	return f.WorkerInteractor.ProcessAction(msg)
}

func (f *folderMapper) PostMessage(msg types.WorkerMessage, cb func(m types.WorkerMessage)) {
	switch msg := msg.(type) {
	case *types.Done:
		switch msg := msg.InResponseTo().(type) {
		case *types.CheckMail:
			for i := range msg.Directories {
				msg.Directories[i] = f.outgoing(msg, msg.Directories[i])
			}
		case *types.CopyMessages:
			msg.Destination = f.outgoing(msg, msg.Destination)
		case *types.AppendMessage:
			msg.Destination = f.outgoing(msg, msg.Destination)
		case *types.MoveMessages:
			msg.Destination = f.outgoing(msg, msg.Destination)
		case *types.CreateDirectory:
			msg.Directory = f.outgoing(msg, msg.Directory)
		case *types.RemoveDirectory:
			msg.Directory = f.outgoing(msg, msg.Directory)
		case *types.OpenDirectory:
			msg.Directory = f.outgoing(msg, msg.Directory)
		}
	case *types.Directory:
		f.store(msg.Dir.Name)
		msg.Dir.Name = f.outgoing(msg, msg.Dir.Name)
	case *types.DirectoryInfo:
		msg.Info.Name = f.outgoing(msg, msg.Info.Name)
	}
	f.WorkerInteractor.PostMessage(msg, cb)
}

// folderMap contains the mapping between the ui and backend folder names
type folderMap struct {
	mapping map[string]string
	order   []string
}

// Apply applies the mapping from the folder map to the backend folder
func (f *folderMap) Apply(s string) string {
	for _, k := range f.order {
		v := f.mapping[k]
		strict := true
		if strings.HasSuffix(v, "*") {
			v = strings.TrimSuffix(v, "*")
			strict = false
		}
		if (strings.HasPrefix(s, v) && !strict) || (s == v && strict) {
			s = k + strings.TrimPrefix(s, v)
		}
	}
	return s
}

// Create reverses the mapping of a new folder name
func (f *folderMap) Create(s string) string {
	max, key := 0, ""
	for k := range f.mapping {
		if strings.HasPrefix(s, k) && len(k) > max {
			max, key = len(k), k
		}
	}
	if max > 0 && key != "" {
		s = strings.TrimSuffix(f.mapping[key], "*") +
			strings.TrimPrefix(s, key)
	}
	return s
}