1package app
2
3import "strings"
4
5// parseRemoteAttachTarget distinguishes remote attach syntax from ordinary
6// local session names. Only values with a user@host prefix are remote; local
7// names may still contain ':' when they do not contain '@'.
8func parseRemoteAttachTarget(s string) (target, session string, ok bool) {
9 if strings.Count(s, "@") != 1 {
10 return "", "", false
11 }
12 at := strings.IndexByte(s, '@')
13 if at <= 0 || at == len(s)-1 {
14 return "", "", false
15 }
16 afterAt := s[at+1:]
17 if strings.HasPrefix(afterAt, "[") {
18 closeBracket := strings.IndexByte(afterAt, ']')
19 if closeBracket <= 1 || strings.Contains(afterAt[closeBracket+1:], "]") {
20 return "", "", false
21 }
22 suffix := afterAt[closeBracket+1:]
23 switch {
24 case suffix == "":
25 return s, "", true
26 case strings.HasPrefix(suffix, ":"):
27 targetEnd := at + 1 + closeBracket + 1
28 return s[:targetEnd], suffix[1:], true
29 default:
30 return "", "", false
31 }
32 }
33 if strings.ContainsAny(afterAt, "[]") || strings.Count(afterAt, ":") > 1 {
34 return "", "", false
35 }
36 colon := strings.LastIndexByte(afterAt, ':')
37 if colon < 0 {
38 return s, "", true
39 }
40 if colon == 0 {
41 return "", "", false
42 }
43 return s[:at+1+colon], afterAt[colon+1:], true
44}