aboutsummaryrefslogtreecommitdiffstats
path: root/worker/middleware/foldermapper.go
blob: ba0981917a025a3501c26e18adc54569fcf91ead (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package middleware

import (
	"fmt"
	"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) Unwrap() types.WorkerInteractor {
	return f.WorkerInteractor
}

func (f *folderMapper) incoming(msg types.WorkerMessage, dir string) string {
	f.Lock()
	defer f.Unlock()
	mapped, ok := f.table[dir]
	if !ok {
		return 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, error) {
	f.Lock()
	defer f.Unlock()
	backend := createFolder(f.table, s)
	if _, exists := f.table[s]; exists {
		return s, fmt.Errorf("folder already exists: %s", s)
	}
	f.table[s] = backend
	f.Tracef("create display folder '%s' as '%s'", s, backend)
	return backend, nil
}

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:
		var err error
		msg.Directory, err = f.create(msg.Directory)
		if err != nil {
			f.Errorf("error creating new directory: %v", err)
		}
	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.CheckMailDirectories:
		for i := range msg.Directories {
			msg.Directories[i] = f.outgoing(msg, msg.Directories[i])
		}
	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) {
			term := strings.TrimPrefix(s, v)
			if strings.Contains(k, "*") && !strict {
				prefix := k
				for strings.Contains(prefix, "**") {
					prefix = strings.ReplaceAll(prefix, "**", "*")
				}
				s = strings.Replace(prefix, "*", term, 1)
			} else {
				s = k + term
			}
		}
	}
	return s
}

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