about summary refs log tree commit diff stats
path: root/atom/grammars/subx.json
blob: 8ba8c0770f32f7661acd3fe558d4c9adeb26d824 (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
{
    "scopeName": "source.subx",
    "name": "SubX",
    "fileTypes": ["subx"],
    "patterns": [
        {
            "name": "header-comment.subx",
            "match": "# - .*"
        },
        {
            "name": "comment-sub-2.subx",
            "match": "# \\. \\. .*"
        },
        {
            "name": "comment-sub-1.subx",
            "match": "# \\. .*"
        },
        {
            "name": "comment.subx",
            "match": "#.*"
        },
        {
            "name": "strings.subx",
            "match": "\"[^\"]*\""
        },
        {
            "name": "test.subx",
            "match": "^test-[^ ]*:"
        },
        {
            "name": "global.subx",
            "match": "^[A-Z][^ ]*:"
        },
        {
            "name": "function.subx",
            "match": "^[^_$ #][^ ]*:"
        },
        {
            "name": "label.subx",
            "match": "^[_$][^ ]*:"
        }
    ]
}
de'>^
026e8a1 ^






a0be5e8 ^
d603bbe ^
1710c90 ^
d603bbe ^
1710c90 ^


6394e38 ^
1710c90 ^








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











a9aebe1 ^
1710c90 ^



a9aebe1 ^
1710c90 ^

















b0bf09b ^

1710c90 ^





d603bbe ^

cc03f6f ^
2750f99 ^

b389647 ^

de36484 ^

27b2517 ^

95875b1 ^

312a53e ^

db213fd ^

b0bf09b ^

1710c90 ^

6394e38 ^
026e8a1 ^






1710c90 ^
6394e38 ^

2750f99 ^




b389647 ^


2750f99 ^
2750f99 ^
4b350dd ^

2750f99 ^




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


            
                    


                 
 
                                     
                                          

                                            
                                               

 



                                                      
                             

 
                        






                                      
                            

                              


                                   

                                                                   

 
                                                      
                           


                                                       


         
                                                                   






                                                                           
                                  
                                
                        
                              


                                                      
                 








                                                                                          
                                                              
                 
 
                                      
                            











                                                           
                                                                                 



                                                  
                                                                             

















                                                                                    

                                                    





                                                                          

                                                                            
                                            

                                          

                                                   

                                                

                                                 

                                              

                                           

                                         

                                          

                                     
         






                                                                                   
                  

 




                                                             


                                                   
                                                          
                                               

                                                  




                                                     






                                                                  


         
                            

                        

                                                         
                                                                               
                                                                        
                                                                      
                                       
                                              
                                                                  

                                                                      
                                       

                                           
                                                  


                 
package imap

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

	"github.com/emersion/go-imap"
	"github.com/emersion/go-imap-idle"
	"github.com/emersion/go-imap/client"

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

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
	}

	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) *IMAPWorker {
	return &IMAPWorker{
		idleDone: make(chan error),
		updates:  make(chan client.Update, 50),
		worker:   worker,
	}
}

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
		}

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

		w.config.user = u.User
	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 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.FetchMessageHeaders:
		w.handleFetchMessageHeaders(msg)
	case *types.FetchMessageBodyPart:
		w.handleFetchMessageBodyPart(msg)
	case *types.FetchFullMessages:
		w.handleFetchFullMessages(msg)
	case *types.DeleteMessages:
		w.handleDeleteMessages(msg)
	case *types.CopyMessages:
		w.handleCopyMessages(msg)
	case *types.AppendMessage:
		w.handleAppendMessage(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{
			Flags:    status.Flags,
			Name:     status.Name,
			ReadOnly: status.ReadOnly,

			Exists: int(status.Messages),
			Recent: int(status.Recent),
			Unseen: int(status.Unseen),
		}, 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)
		}
	}
}