| | @@ -0,0 +1,182 @@ |
| 1 | +package ergo
|
| 2 | +
|
| 3 | +import (
|
| 4 | + "bytes"
|
| 5 | + "encoding/json"
|
| 6 | + "fmt"
|
| 7 | + "net/http"
|
| 8 | + "time"
|
| 9 | +)
|
| 10 | +
|
| 11 | +// APIClient is an HTTP client for Ergo's management API.
|
| 12 | +type APIClient struct {
|
| 13 | + baseURL string
|
| 14 | + token string
|
| 15 | + http *http.Client
|
| 16 | +}
|
| 17 | +
|
| 18 | +// NewAPIClient returns a new APIClient pointed at addr with the given bearer token.
|
| 19 | +func NewAPIClient(addr, token string) *APIClient {
|
| 20 | + return &APIClient{
|
| 21 | + baseURL: "http://" + addr,
|
| 22 | + token: token,
|
| 23 | + http: &http.Client{Timeout: 10 * time.Second},
|
| 24 | + }
|
| 25 | +}
|
| 26 | +
|
| 27 | +// Status returns the Ergo server status.
|
| 28 | +func (c *APIClient) Status() (*StatusResponse, error) {
|
| 29 | + var resp StatusResponse
|
| 30 | + if err := c.post("/v1/status", nil, &resp); err != nil {
|
| 31 | + return nil, fmt.Errorf("ergo api: status: %w", err)
|
| 32 | + }
|
| 33 | + return &resp, nil
|
| 34 | +}
|
| 35 | +
|
| 36 | +// Rehash reloads Ergo's configuration file.
|
| 37 | +func (c *APIClient) Rehash() error {
|
| 38 | + var resp successResponse
|
| 39 | + if err := c.post("/v1/rehash", nil, &resp); err != nil {
|
| 40 | + return fmt.Errorf("ergo api: rehash: %w", err)
|
| 41 | + }
|
| 42 | + if !resp.Success {
|
| 43 | + return fmt.Errorf("ergo api: rehash failed: %s", resp.Error)
|
| 44 | + }
|
| 45 | + return nil
|
| 46 | +}
|
| 47 | +
|
| 48 | +// RegisterAccount creates a NickServ account via saregister.
|
| 49 | +func (c *APIClient) RegisterAccount(name, passphrase string) error {
|
| 50 | + var resp registerResponse
|
| 51 | + if err := c.post("/v1/ns/saregister", map[string]string{
|
| 52 | + "accountName": name,
|
| 53 | + "passphrase": passphrase,
|
| 54 | + }, &resp); err != nil {
|
| 55 | + return fmt.Errorf("ergo api: register account %q: %w", name, err)
|
| 56 | + }
|
| 57 | + if !resp.Success {
|
| 58 | + return fmt.Errorf("ergo api: register account %q: %s", name, resp.ErrorCode)
|
| 59 | + }
|
| 60 | + return nil
|
| 61 | +}
|
| 62 | +
|
| 63 | +// ChangePassword updates the passphrase of an existing NickServ account.
|
| 64 | +func (c *APIClient) ChangePassword(name, passphrase string) error {
|
| 65 | + var resp passwdResponse
|
| 66 | + if err := c.post("/v1/ns/passwd", map[string]string{
|
| 67 | + "accountName": name,
|
| 68 | + "passphrase": passphrase,
|
| 69 | + }, &resp); err != nil {
|
| 70 | + return fmt.Errorf("ergo api: change password %q: %w", name, err)
|
| 71 | + }
|
| 72 | + if !resp.Success {
|
| 73 | + return fmt.Errorf("ergo api: change password %q: %s", name, resp.ErrorCode)
|
| 74 | + }
|
| 75 | + return nil
|
| 76 | +}
|
| 77 | +
|
| 78 | +// AccountInfo fetches details about a NickServ account.
|
| 79 | +func (c *APIClient) AccountInfo(name string) (*AccountInfoResponse, error) {
|
| 80 | + var resp AccountInfoResponse
|
| 81 | + if err := c.post("/v1/ns/info", map[string]string{
|
| 82 | + "accountName": name,
|
| 83 | + }, &resp); err != nil {
|
| 84 | + return nil, package ergo
|
| 85 | +
|
| 86 | +import (
|
| 87 | + "bytes"
|
| 88 | + "encoding/json"
|
| 89 | + "fmt"
|
| 90 | + "net/http"
|
| 91 | + "time"
|
| 92 | +)
|
| 93 | +
|
| 94 | +// APIClient is an HTTP client for Ergo's management API.
|
| 95 | +type APIClient struct {
|
| 96 | + baseURL string
|
| 97 | + token string
|
| 98 | + http *http.Client
|
| 99 | +}
|
| 100 | +
|
| 101 | +// NewAPIClient returns a new APIClient pointed at addr with the given bearer token.
|
| 102 | +func NewAPIClient(addr, token string) *APIClient {
|
| 103 | + return &APIClient{
|
| 104 | + baseURL: "http://" + addr,
|
| 105 | + token: token,
|
| 106 | + http: &http.Client{Timeout: 10 * time.Second},
|
| 107 | + }
|
| 108 | +}
|
| 109 | +
|
| 110 | +// Status returns the Ergo server status.
|
| 111 | +func (c *APIClient) Status() (*StatusResponse, error) {
|
| 112 | + var resp StatusResponse
|
| 113 | + if err := c.post("/v1/status", nil, &resp); err != nil {
|
| 114 | + return nil, fmt.Errorf("ergo api: status: %w", err)
|
| 115 | + }
|
| 116 | + return &resp, nil
|
| 117 | +}
|
| 118 | +
|
| 119 | +// Rehash reloads Ergo's configuration f(
|
| 120 | + "bytes"
|
| 121 | + "encoding/json"
|
| 122 | + "fmt"
|
| 123 | + "net/http"
|
| 124 | + "time"
|
| 125 | +)
|
| 126 | +
|
| 127 | +// APIClient is an HTTP client for Ergo's management API.
|
| 128 | +type APIClient struct {
|
| 129 | + baseURL string
|
| 130 | + token string
|
| 131 | + http *http.Client
|
| 132 | +}
|
| 133 | +
|
| 134 | +// NewAPIClient returns a new APIClient pointed at addr with the given bearer token.
|
| 135 | +func NewAPIClient(addr, token string) *APIClient {
|
| 136 | + return &APIClient{
|
| 137 | + baseURL: "http://" + addr,
|
| 138 | + token: token,
|
| 139 | + http: &http.Client{Timeout: 10 * time.Second},
|
| 140 | + }
|
| 141 | +}
|
| 142 | +
|
| 143 | +// Status returns the Ergo server status.
|
| 144 | +func (c *APIClient) Status() (*StatusResponse, error) {
|
| 145 | + var resp StatusResponse
|
| 146 | + if err := c.post("/v1/status", nil, &resp); err != nil {
|
| 147 | + return nil, fmt.Errorf("ergo api: status: %w", err)
|
| 148 | + }
|
| 149 | + return &resp, nil
|
| 150 | +}
|
| 151 | +
|
| 152 | +// Rehash reloads Ergo's configuration file.
|
| 153 | +func (c *APIClient) Rehash() error {
|
| 154 | + var resp successResponse
|
| 155 | + if err := c.post("/v1/rehash", nil, &resp); err != nil {
|
| 156 | + return fmt.Errorf("ergo api: rehash: %w", err)
|
| 157 | + }
|
| 158 | + if !resp.Success {
|
| 159 | + return fmt.Errorf("ergo api: rehash failed: %s", resp.Error)
|
| 160 | + }
|
| 161 | + return nil
|
| 162 | +}
|
| 163 | +
|
| 164 | +// RegisterAccount creates a NickServ account via saregister.
|
| 165 | +func (c *APIClient) RegisterAccount(name, passphrase string) error {
|
| 166 | + var resp registerResponse
|
| 167 | + if err := c.post("/v1/ns/saregister", map[string]string{
|
| 168 | + "accountName": name,
|
| 169 | + "passphrase": passphrase,
|
| 170 | + }, &resp); err != nil {
|
| 171 | + return fmt.Errorf("ergo api: register account %q: %w", name, err)
|
| 172 | + }
|
| 173 | + if !resp.Success {
|
| 174 | + return fmt.Errorf("ergo api: register account %q: %s", name, resp.ErrorCode)
|
| 175 | + }
|
| 176 | + return nil
|
| 177 | +}
|
| 178 | +
|
| 179 | +// ChangePassword updates the passphrase of an existing NickServ account.
|
| 180 | +func (c *APIClient) ChangePassword(name, passphrase string) error {
|
| 181 | + var resp passwdResponse
|
| 182 | + if err := c.post("/v1/ns/passwd", map[stri |