about summary refs log tree commit diff stats
path: root/dwm.1
Commit message (Expand)AuthorAgeFilesLines
* regarding to http://plan9.bell-labs.com/sources/contrib/rsc/man.ps the BUGS s...Anselm R. Garbe2007-01-261-2/+2
* renamed CAVEATS into BUGSAnselm R. Garbe2007-01-261-1/+1
* moved BORDERPX to config.*.hAnselm R. Garbe2007-01-171-1/+1
* removed client title barAnselm R. Garbe2007-01-141-1/+0
* added Button{4,5} description to dwm.1Anselm R. Garbe2007-01-101-0/+6
* small grammar fix of dwm.1Anselm R. Garbe2007-01-071-2/+2
* changed a term in dwm.1Anselm R. Garbe2007-01-071-1/+1
* removed the term clients from dwm.1Anselm R. Garbe2007-01-051-3/+3
* changed shortcuts to M-i and M-dAnselm R. Garbe2007-01-051-2/+2
* changed plus key to MODKEY-Shift-plus (US-layout restrictions ;)Anselm R. Garbe2007-01-051-1/+1
* added MODKEY-{plus,minus} shortcuts (increasing/decreasing master clients)Anselm R. Garbe2007-01-051-0/+6
* applied Sanders patch 2.7arg@mig292006-12-131-3/+3
* fixed man page, status bar displays EOF instead of "broken pipe"arg@mig292006-12-131-1/+1
* added the java odyssee to CAVEATS sectionarg@mig292006-12-131-2/+10
* we don't use the term clients anymore, windows is the better termarg@mig292006-12-071-1/+1
* and the winner isarg@mig292006-12-011-2/+3
* less obtrusive indicator (only a top line)arg@mig292006-12-011-1/+1
* final2 indicatorarg@mig292006-12-011-1/+1
* final indicatorsarg@mig292006-12-011-2/+1
* removed viewall(), replaced with view(-1); added tag(-1) to tag a client with...arg@mig292006-11-301-0/+3
* extended dwm.1 with last change 2.4arg@mig292006-11-281-1/+1
* added man page entryarg@mig292006-11-271-0/+3
* applied Manuels patch (thanks to Manuel!)arg@mig292006-11-251-1/+1
* small fix of man pagearg@mig292006-11-241-1/+1
* using the term 'indicated' instead of 'higlighted' 2.3arg@mig292006-11-231-3/+3
* updated man page with the bottom right corner indicatorarg@mig292006-11-231-2/+4
* applied Gottox patch to simplify the resizing of col, instead of resizing the...arg@mig292006-10-311-2/+2
* removed the stack position stuffAnselm R. Garbe2006-10-051-5/+1
* final fix for man pageAnselm R. Garbe2006-10-051-3/+3
* Button3 click on mode label toggles stack position nowAnselm R. Garbe2006-10-051-2/+3
* documented the new behavior in man pageAnselm R. Garbe2006-09-291-0/+3
* renamed column into areaAnselm R. Garbe2006-09-281-7/+7
* man page fixarg@mmvi2006-09-221-1/+1
* fixed issue pointed out by Jukkaarg@mmvi2006-09-221-1/+1
* small man page fixarg@mmvi2006-09-221-0/+1
* updated man pagearg@mmvi2006-09-221-5/+1
* fixed a nuance in dwm.1arg@mmvi2006-09-201-1/+1
* really small changes to dwm.1Anselm R. Garbe2006-09-081-4/+3
* made markups in dwm.1 more consistentAnselm R. Garbe2006-09-071-95/+38
* small addition to dwm.1Anselm R. Garbe2006-09-071-4/+4
* well ok, added Mod-s to default bindings (however, I don't need it)Anselm R. Garbe2006-09-051-0/+6
* I really need column growing, now pushing upstreamAnselm R. Garbe2006-09-051-0/+6
* applied sanders patchAnselm R. Garbe2006-09-011-6/+12
* changed shortcut into Mod1-0Anselm R. Garbe2006-08-311-5/+5
* added viewall to mainstream (only Ross Mohns version, not the toggle)Anselm R. Garbe2006-08-311-0/+4
* implemented Button2 press on tags for toggletag on the focused clientAnselm R. Garbe2006-08-311-0/+6
* applied sanders somepatches.patchAnselm R. Garbe2006-08-281-1/+6
* updated man page of dwmAnselm R. Garbe2006-08-251-3/+3
* yet another fixAnselm R. Garbe2006-08-241-1/+1
* small man page fixAnselm R. Garbe2006-08-241-1/+2
ref='/akspecs/aerc/commit/worker/imap/worker.go?h=0.2.1&id=1710c9054898e820700d673e21e7c7a90a3f67b1'>1710c90 ^
d603bbe ^
1710c90 ^


6394e38 ^
1710c90 ^






b0eaf51 ^













1710c90 ^

2dc4365 ^
6394e38 ^
1710c90 ^
1710c90 ^
626f91c ^
d603bbe ^
1710c90 ^











a9aebe1 ^
1710c90 ^



a9aebe1 ^
1710c90 ^












b0eaf51 ^





1710c90 ^



b0bf09b ^

1710c90 ^





d603bbe ^

cc03f6f ^
2750f99 ^

b389647 ^

acfe7d7 ^

de36484 ^

27b2517 ^

95875b1 ^

312a53e ^

6271d45 ^

db213fd ^

b0bf09b ^

198661b ^

1710c90 ^

6394e38 ^
026e8a1 ^






1710c90 ^
6394e38 ^

2750f99 ^




b389647 ^


2750f99 ^
cce7cb4 ^








2750f99 ^
05f00f0 ^





cce7cb4 ^
c610c3c ^


cce7cb4 ^


05f00f0 ^
3ace4ef ^






2750f99 ^


6394e38 ^
6394e38 ^

d24e471 ^

1710c90 ^
d603bbe ^
1710c90 ^
d24e471 ^
1710c90 ^
d603bbe ^
1710c90 ^

d24e471 ^
1710c90 ^

2750f99 ^
6394e38 ^


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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254


            
                    


                 
 
                                     
                                               
                                            
                             
 
                                      
                                         
                                                  
                                               

 




                                                              



                                                      
                             

 
                        
                       





                                           

         
                            

                              


                                   

                                                                   

 
                                                                 
                           


                                                       
              

 
                                                                   






                                                                           
                                  
                                
                        
                              


                                                      
                 






                                                                                          













                                                                                                       

                                                              
                                                              
                 
 
                                      
                                                     
                            











                                                           
                                                                                 



                                                  
                                                                             












                                                                                    





                                                                                                                



                                          

                                                    





                                                                          

                                                                            
                                            

                                          

                                                   

                                            

                                                

                                                 

                                              

                                           

                                         

                                         

                                          

                                            

                                     
         






                                                                                   
                  

 




                                                             


                                                   
                                                          








                                                             
                       





                                                        
                                                  


                                                                                         


                                                                
                       






                                                                  


         
                            

                        

                                                         
                                                                               
                                                                        
                                                                      
                                       
                                              
                                                                  

                                                                      
                                       

                                           
                                                  


                 
package imap

import (
	"crypto/tls"
	"fmt"
	"net/url"
	"strings"

	"github.com/emersion/go-imap"
	idle "github.com/emersion/go-imap-idle"
	"github.com/emersion/go-imap/client"
	"golang.org/x/oauth2"

	"git.sr.ht/~sircmpwn/aerc/lib"
	"git.sr.ht/~sircmpwn/aerc/models"
	"git.sr.ht/~sircmpwn/aerc/worker/handlers"
	"git.sr.ht/~sircmpwn/aerc/worker/types"
)

func init() {
	handlers.RegisterWorkerFactory("imap", NewIMAPWorker)
	handlers.RegisterWorkerFactory("imaps", NewIMAPWorker)
}

var errUnsupported = fmt.Errorf("unsupported command")

type imapClient struct {
	*client.Client
	idle *idle.IdleClient
}

type IMAPWorker struct {
	config struct {
		scheme      string
		insecure    bool
		addr        string
		user        *url.Userinfo
		folders     []string
		oauthBearer lib.OAuthBearer
	}

	client   *imapClient
	idleStop chan struct{}
	idleDone chan error
	selected imap.MailboxStatus
	updates  chan client.Update
	worker   *types.Worker
	// Map of sequence numbers to UIDs, index 0 is seq number 1
	seqMap []uint32
}

func NewIMAPWorker(worker *types.Worker) (types.Backend, error) {
	return &IMAPWorker{
		idleDone: make(chan error),
		updates:  make(chan client.Update, 50),
		worker:   worker,
	}, nil
}

func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
	if w.idleStop != nil {
		close(w.idleStop)
		if err := <-w.idleDone; err != nil {
			w.worker.PostMessage(&types.Error{Error: err}, nil)
		}
	}

	switch msg := msg.(type) {
	case *types.Unsupported:
		// No-op
	case *types.Configure:
		u, err := url.Parse(msg.Config.Source)
		if err != nil {
			return err
		}

		w.config.scheme = u.Scheme
		if strings.HasSuffix(w.config.scheme, "+insecure") {
			w.config.scheme = strings.TrimSuffix(w.config.scheme, "+insecure")
			w.config.insecure = true
		}

		if strings.HasSuffix(w.config.scheme, "+oauthbearer") {
			w.config.scheme = strings.TrimSuffix(w.config.scheme, "+oauthbearer")
			w.config.oauthBearer.Enabled = true
			q := u.Query()
			if q.Get("token_endpoint") != "" {
				w.config.oauthBearer.OAuth2 = &oauth2.Config{
					ClientID:     q.Get("client_id"),
					ClientSecret: q.Get("client_secret"),
					Scopes:       []string{q.Get("scope")},
				}
				w.config.oauthBearer.OAuth2.Endpoint.TokenURL = q.Get("token_endpoint")
			}
		}

		w.config.addr = u.Host
		if !strings.ContainsRune(w.config.addr, ':') {
			w.config.addr += ":" + w.config.scheme
		}

		w.config.user = u.User
		w.config.folders = msg.Config.Folders
	case *types.Connect:
		var (
			c   *client.Client
			err error
		)
		switch w.config.scheme {
		case "imap":
			c, err = client.Dial(w.config.addr)
			if err != nil {
				return err
			}

			if !w.config.insecure {
				if err := c.StartTLS(&tls.Config{}); err != nil {
					return err
				}
			}
		case "imaps":
			c, err = client.DialTLS(w.config.addr, &tls.Config{})
			if err != nil {
				return err
			}
		default:
			return fmt.Errorf("Unknown IMAP scheme %s", w.config.scheme)
		}

		if w.config.user != nil {
			username := w.config.user.Username()
			password, hasPassword := w.config.user.Password()
			if !hasPassword {
				// TODO: ask password
			}

			if w.config.oauthBearer.Enabled {
				if err := w.config.oauthBearer.Authenticate(username, password, c); err != nil {
					return err
				}
			} else if err := c.Login(username, password); err != nil {
				return err
			}
		}

		c.SetDebug(w.worker.Logger.Writer())

		if _, err := c.Select(imap.InboxName, false); err != nil {
			return err
		}

		c.Updates = w.updates
		w.client = &imapClient{c, idle.NewClient(c)}
		w.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
	case *types.ListDirectories:
		w.handleListDirectories(msg)
	case *types.OpenDirectory:
		w.handleOpenDirectory(msg)
	case *types.FetchDirectoryContents:
		w.handleFetchDirectoryContents(msg)
	case *types.CreateDirectory:
		w.handleCreateDirectory(msg)
	case *types.FetchMessageHeaders:
		w.handleFetchMessageHeaders(msg)
	case *types.FetchMessageBodyPart:
		w.handleFetchMessageBodyPart(msg)
	case *types.FetchFullMessages:
		w.handleFetchFullMessages(msg)
	case *types.DeleteMessages:
		w.handleDeleteMessages(msg)
	case *types.ReadMessages:
		w.handleReadMessages(msg)
	case *types.CopyMessages:
		w.handleCopyMessages(msg)
	case *types.AppendMessage:
		w.handleAppendMessage(msg)
	case *types.SearchDirectory:
		w.handleSearchDirectory(msg)
	default:
		return errUnsupported
	}

	if w.idleStop != nil {
		w.idleStop = make(chan struct{})
		go func() {
			w.idleDone <- w.client.idle.IdleWithFallback(w.idleStop, 0)
		}()
	}
	return nil
}

func (w *IMAPWorker) handleImapUpdate(update client.Update) {
	w.worker.Logger.Printf("(= %T", update)
	switch update := update.(type) {
	case *client.MailboxUpdate:
		status := update.Mailbox
		if w.selected.Name == status.Name {
			w.selected = *status
		}
		w.worker.PostMessage(&types.DirectoryInfo{
			Info: &models.DirectoryInfo{
				Flags:    status.Flags,
				Name:     status.Name,
				ReadOnly: status.ReadOnly,

				Exists: int(status.Messages),
				Recent: int(status.Recent),
				Unseen: int(status.Unseen),
			},
		}, nil)
	case *client.MessageUpdate:
		msg := update.Message
		if msg.Uid == 0 {
			msg.Uid = w.seqMap[msg.SeqNum-1]
		}
		w.worker.PostMessage(&types.MessageInfo{
			Info: &models.MessageInfo{
				BodyStructure: translateBodyStructure(msg.BodyStructure),
				Envelope:      translateEnvelope(msg.Envelope),
				Flags:         translateFlags(msg.Flags),
				InternalDate:  msg.InternalDate,
				Uid:           msg.Uid,
			},
		}, nil)
	case *client.ExpungeUpdate:
		i := update.SeqNum - 1
		uid := w.seqMap[i]
		w.seqMap = append(w.seqMap[:i], w.seqMap[i+1:]...)
		w.worker.PostMessage(&types.MessagesDeleted{
			Uids: []uint32{uid},
		}, nil)
	}
}

func (w *IMAPWorker) Run() {
	for {
		select {
		case msg := <-w.worker.Actions:
			msg = w.worker.ProcessAction(msg)
			if err := w.handleMessage(msg); err == errUnsupported {
				w.worker.PostMessage(&types.Unsupported{
					Message: types.RespondTo(msg),
				}, nil)
			} else if err != nil {
				w.worker.PostMessage(&types.Error{
					Message: types.RespondTo(msg),
					Error:   err,
				}, nil)
			}
		case update := <-w.updates:
			w.handleImapUpdate(update)
		}
	}
}