author | Mikael Berthe <mikael@lilotux.net> |
Sat, 04 Feb 2023 13:17:17 +0100 | |
changeset 268 | 4dd196a4ee7c |
parent 265 | 05c40b36d3b2 |
parent 267 | 5b91a65ba95a |
permissions | -rw-r--r-- |
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
239
diff
changeset
|
1 |
// Copyright © 2017-2023 Mikael Berthe <mikael@lilotux.net> |
0 | 2 |
// |
3 |
// Licensed under the MIT license. |
|
4 |
// Please see the LICENSE file is this directory. |
|
5 |
||
6 |
package cmd |
|
7 |
||
8 |
import ( |
|
9 |
"strings" |
|
10 |
||
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
239
diff
changeset
|
11 |
"github.com/McKael/madon/v3" |
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
12 |
"github.com/pkg/errors" |
0 | 13 |
"github.com/spf13/viper" |
14 |
) |
|
15 |
||
16 |
var scopes = []string{"read", "write", "follow"} |
|
17 |
||
18 |
func madonInit(signIn bool) error { |
|
19 |
if gClient == nil { |
|
20 |
if err := madonInitClient(); err != nil { |
|
21 |
return err |
|
22 |
} |
|
23 |
} |
|
24 |
if signIn { |
|
25 |
return madonLogin() |
|
26 |
} |
|
27 |
return nil |
|
28 |
} |
|
29 |
||
30 |
func madonInitClient() error { |
|
31 |
if gClient != nil { |
|
32 |
return nil |
|
33 |
} |
|
34 |
var err error |
|
35 |
||
36 |
// Overwrite variables using Viper |
|
37 |
instanceURL = viper.GetString("instance") |
|
38 |
appID = viper.GetString("app_id") |
|
39 |
appSecret = viper.GetString("app_secret") |
|
40 |
||
41 |
if instanceURL == "" { |
|
42 |
return errors.New("no instance provided") |
|
43 |
} |
|
44 |
||
46
13717ea83333
Improve init logging/error message
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
45 |
if verbose { |
13717ea83333
Improve init logging/error message
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
46 |
errPrint("Instance: '%s'", instanceURL) |
13717ea83333
Improve init logging/error message
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
47 |
} |
13717ea83333
Improve init logging/error message
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
48 |
|
0 | 49 |
if appID != "" && appSecret != "" { |
50 |
// We already have an app key/secret pair |
|
51 |
gClient, err = madon.RestoreApp(AppName, instanceURL, appID, appSecret, nil) |
|
52 |
if err != nil { |
|
53 |
return err |
|
54 |
} |
|
55 |
// Check instance |
|
56 |
if _, err := gClient.GetCurrentInstance(); err != nil { |
|
53
4088535c4c4c
Be more verbose when connection fails
Mikael Berthe <mikael@lilotux.net>
parents:
46
diff
changeset
|
57 |
return errors.Wrap(err, "could not connect to server with provided app ID/secret") |
0 | 58 |
} |
59 |
if verbose { |
|
46
13717ea83333
Improve init logging/error message
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
60 |
errPrint("Using provided app ID/secret") |
0 | 61 |
} |
62 |
return nil |
|
63 |
} |
|
64 |
||
65 |
if appID != "" || appSecret != "" { |
|
66 |
errPrint("Warning: provided app id/secrets incomplete -- registering again") |
|
67 |
} |
|
68 |
||
55
e9df533a1c4f
Sync w/ madon 1.4.0 -- add website parameter to application registration
Mikael Berthe <mikael@lilotux.net>
parents:
53
diff
changeset
|
69 |
gClient, err = madon.NewApp(AppName, AppWebsite, scopes, madon.NoRedirect, instanceURL) |
0 | 70 |
if err != nil { |
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
71 |
return errors.Wrap(err, "app registration failed") |
0 | 72 |
} |
73 |
||
158 | 74 |
errPrint("Registered new application.") |
0 | 75 |
return nil |
76 |
} |
|
77 |
||
78 |
func madonLogin() error { |
|
79 |
if gClient == nil { |
|
158 | 80 |
return errors.New("application not registered") |
0 | 81 |
} |
82 |
||
83 |
token = viper.GetString("token") |
|
84 |
login = viper.GetString("login") |
|
85 |
password = viper.GetString("password") |
|
86 |
||
87 |
if token != "" { // TODO check token validity? |
|
88 |
if verbose { |
|
89 |
errPrint("Reusing existing token.") |
|
90 |
} |
|
91 |
gClient.SetUserToken(token, login, password, []string{}) |
|
92 |
return nil |
|
93 |
} |
|
94 |
||
95 |
err := gClient.LoginBasic(login, password, scopes) |
|
96 |
if err == nil { |
|
97 |
return nil |
|
98 |
} |
|
11
3884f5276808
Work around server sending HTML error page on failed login
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
99 |
if !verbose && err.Error() == "cannot unmarshal server response: invalid character '<' looking for beginning of value" { |
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
100 |
return errors.New("login failed (server did not return a JSON response - check your credentials)") |
11
3884f5276808
Work around server sending HTML error page on failed login
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
101 |
} |
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
102 |
return errors.Wrap(err, "login failed") |
0 | 103 |
} |
104 |
||
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
105 |
// splitIDs splits a list of IDs into an int64 array |
267 | 106 |
func splitIDs(ids string) (list []madon.ActivityID, err error) { |
0 | 107 |
if ids == "" { |
108 |
return |
|
109 |
} |
|
110 |
l := strings.Split(ids, ",") |
|
111 |
for _, s := range l { |
|
267 | 112 |
if s == "" { |
0 | 113 |
return |
114 |
} |
|
267 | 115 |
list = append(list, s) |
0 | 116 |
} |
117 |
return |
|
118 |
} |