| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/url" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // OficinaApp is one of the team's Oficina apps, listed on Home. |
| 11 | type OficinaApp struct { |
| 12 | ID string // one of OficinaIDs |
| 13 | Name string // the name Base gives it |
| 14 | Origin string // https and a hostname, no trailing slash |
| 15 | } |
| 16 | |
| 17 | // oficinaApps are the Oficina apps Base knows, with their names. |
| 18 | var oficinaApps = []struct{ id, name string }{ |
| 19 | {"calendar", "Calendar"}, {"docs", "Docs"}, {"sheets", "Sheets"}, {"slides", "Slides"}, |
| 20 | {"memoria", "Memoria"}, {"calls", "Calls"}, {"fichas", "Fichas"}, {"equipo", "Equipo"}, |
| 21 | } |
| 22 | |
| 23 | // BundleID is the app's native bundle ID on the Mac and iPhone: every |
| 24 | // Oficina native app is works.oficina.<id> (ruled 2026-11-14). Base's own |
| 25 | // native app opens the app by it when it is installed. |
| 26 | func (o OficinaApp) BundleID() string { return "works.oficina." + o.ID } |
| 27 | |
| 28 | // oficinaSchemes are the URL schemes that open an Oficina app's native app |
| 29 | // on the iPhone, for the apps whose app can be opened by one today. Docs, |
| 30 | // Sheets, Calendar and Memoria answer only …://signin until their apps take |
| 31 | // the kit's …://open, so they open on the web until then. |
| 32 | var oficinaSchemes = map[string]string{"calls": "oficina-calls", "equipo": "works.oficina.equipo"} |
| 33 | |
| 34 | // Scheme is the URL scheme that opens the app's native app, or "" when it |
| 35 | // opens on the web. |
| 36 | func (o OficinaApp) Scheme() string { return oficinaSchemes[o.ID] } |
| 37 | |
| 38 | // OficinaIDs is every app id BASE_OFICINA_APPS accepts. |
| 39 | func OficinaIDs() []string { |
| 40 | ids := make([]string, len(oficinaApps)) |
| 41 | for i, a := range oficinaApps { |
| 42 | ids[i] = a.id |
| 43 | } |
| 44 | return ids |
| 45 | } |
| 46 | |
| 47 | // hostRE is a DNS hostname with at least two labels and a top-level label |
| 48 | // that starts with a letter, so no IP address passes. |
| 49 | var hostRE = regexp.MustCompile(`^([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z]([a-z0-9-]*[a-z0-9])?$`) |
| 50 | |
| 51 | // parseOficinaApps reads BASE_OFICINA_APPS: comma-separated id=origin |
| 52 | // pairs, each id one Base knows and each origin https and a hostname only. |
| 53 | // Empty is no apps. |
| 54 | func parseOficinaApps(raw string) ([]OficinaApp, error) { |
| 55 | var out []OficinaApp |
| 56 | seen := map[string]bool{} |
| 57 | for _, item := range strings.Split(raw, ",") { |
| 58 | item = strings.TrimSpace(item) |
| 59 | if item == "" { |
| 60 | continue |
| 61 | } |
| 62 | id, origin, ok := strings.Cut(item, "=") |
| 63 | id, origin = strings.TrimSpace(id), strings.ToLower(strings.TrimRight(strings.TrimSpace(origin), "/")) |
| 64 | if !ok || id == "" { |
| 65 | return nil, fmt.Errorf("BASE_OFICINA_APPS: %q must be an app and its address, such as calendar=https://calendar.example.com", item) |
| 66 | } |
| 67 | name := "" |
| 68 | for _, a := range oficinaApps { |
| 69 | if a.id == id { |
| 70 | name = a.name |
| 71 | } |
| 72 | } |
| 73 | if name == "" { |
| 74 | return nil, fmt.Errorf("BASE_OFICINA_APPS: unknown app %q; Base knows %s", id, strings.Join(OficinaIDs(), ", ")) |
| 75 | } |
| 76 | if seen[id] { |
| 77 | return nil, fmt.Errorf("BASE_OFICINA_APPS lists %s twice", id) |
| 78 | } |
| 79 | seen[id] = true |
| 80 | u, err := url.Parse(origin) |
| 81 | if err != nil || u.Scheme != "https" || u.User != nil || u.Path != "" || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" || |
| 82 | u.Port() != "" || !hostRE.MatchString(u.Host) { |
| 83 | return nil, fmt.Errorf("BASE_OFICINA_APPS: %s's address %q must be https:// and a hostname only", id, origin) |
| 84 | } |
| 85 | out = append(out, OficinaApp{ID: id, Name: name, Origin: origin}) |
| 86 | } |
| 87 | return out, nil |
| 88 | } |
| 89 | |