Skip to content

Commit

Permalink
Add support for user registration
Browse files Browse the repository at this point in the history
This is an admin command.
  • Loading branch information
mremond committed Jul 24, 2016
1 parent 97a7267 commit adeea7c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
46 changes: 46 additions & 0 deletions api.go
Expand Up @@ -56,6 +56,8 @@ type command interface {
params() (HTTPParams, error)
}

//==============================================================================

type GetStats struct {
Name string `json:"name"`
}
Expand Down Expand Up @@ -86,6 +88,50 @@ func (g *GetStats) params() (HTTPParams, error) {
}, nil
}

//==============================================================================

type RegisterUser struct {
JID string `json:"jid"`
Password string `json:"password"`
}

func (r *RegisterUser) params() (HTTPParams, error) {
var query url.Values

jid, err := parseJID(r.JID)
if err != nil {
return HTTPParams{}, err
}

type register struct {
User string `json:"user"`
Host string `json:"host"`
Password string `json:"password"`
}

data := register{
User: jid.username,
Host: jid.domain,
Password: r.Password,
}

body, err := json.Marshal(data)
if err != nil {
return HTTPParams{}, err
}

return HTTPParams{
version: 1,
admin: true,
method: "POST",
path: "register/",
query: query,
body: body,
}, nil
}

//==============================================================================

func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
Expand Down
33 changes: 32 additions & 1 deletion cmd/ejabberd/main.go
Expand Up @@ -26,6 +26,12 @@ var (
// ========= stats =========
stats = app.Command("stats", "Get ejabberd statistics.")
statsName = stats.Arg("name", "Name of stats to query.").Required().Enum("registeredusers", "onlineusers", "onlineusersnode", "uptimeseconds", "processes")

// ========= user =========
user = app.Command("user", "Operations to perform on users.")
userOperation = user.Arg("operation", "Operation").Required().Enum("register")
userJID = user.Flag("jid", "JID of the user to perform operation on.").Short('j').Required().String()
userPassword = user.Flag("password", "User password").Short('p').Required().String()
)

func main() {
Expand Down Expand Up @@ -55,6 +61,8 @@ func execute(command string) {
switch command {
case stats.FullCommand():
statsCommand(c)
case user.FullCommand():
userCommand(c, *userOperation)
}
}

Expand Down Expand Up @@ -85,7 +93,7 @@ func getToken() {
fmt.Println("Successfully saved token in file", *file)
}

// =============================================================================
//==============================================================================

func statsCommand(c ejabberd.Client) {
command := ejabberd.GetStats{
Expand All @@ -98,3 +106,26 @@ func statsCommand(c ejabberd.Client) {
}
fmt.Println(string(resp))
}

//==============================================================================

func userCommand(c ejabberd.Client, op string) {
switch op {
case "register":
registerCommand(c, *userJID, *userPassword)
}
}

func registerCommand(c ejabberd.Client, j, p string) {
// TODO Should we create a v2 command with only two parameters (JID, Password)
command := ejabberd.RegisterUser{
JID: j,
Password: p}
resp, err := c.Call(&command)
if err != nil {
kingpin.Fatalf("register command error %v: %s", command, err)
}
fmt.Println(string(resp))
}

// TODO Interface for command result formatting

0 comments on commit adeea7c

Please sign in to comment.