aboutsummaryrefslogtreecommitdiff
path: root/common_imap.go
blob: 3d791240321d93796a61e99d87540211efa53cf9 (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
package main

import (
	"errors"
	"strings"
)

var err_imap_generic = errors.New("Error in IMAP command received by server.")

func parse_imap_line(line string) (tag, cmd, param string, err error) {
	line = strings.TrimSuffix(line, "\n")
	line = strings.TrimSuffix(line, "\r")
	tag_end := strings.IndexByte(line, ' ')
	if tag_end == -1 {
		err = err_imap_generic
		return
	}
	tag = line[:tag_end]
	rest := line[tag_end+1:]
	cmd_end := strings.IndexByte(rest, ' ')
	var param_start int
	if cmd_end == -1 {
		cmd_end = len(rest)
		param_start = len(rest)
	} else {
		param_start = cmd_end + 1
	}
	cmd = strings.ToUpper(rest[:cmd_end])
	param = rest[param_start:]
	return
}