func (db *DB) list(ctx core.CommandContext) core.Result { //TODO args := ctx.Args var err error text, err := util.GetArg(args, "text", false, err) query, err := util.GetArg(args, "query", false, err) tags, err := util.GetArg(args, "tags", false, err) if text != nil { return core.ResultByError(core.ErrorNotImplemented) } if query != nil { res, err := db.ListQuery(*query) if err != nil { return core.ResultByError(errrs.New(err.Error())) } return core.Result{core.StatusOK, ItemToInterfaceSlice(res), err, false} } if tags != nil { return core.ResultByError(core.ErrorNotImplemented) } res, err := db.ListAll() if err != nil { return core.ResultByError(errrs.New(err.Error())) } return core.Result{core.StatusOK, ItemToInterfaceSlice(res), err, false} }
func (s *Sync) Start(c core.Core, db *db.DB) { s.core = c s.db = db s.config = config.Current.Plugins[PluginName].(*Config) c.RegisterCommand(core.Command{[]string{"sync", "s"}, "Syncs media with a device or folder. By default, lossles files are converted to MP3 V0.", map[string]string{ "path": "Target path", "convert": "boolean, default is true"}, core.AuthAdmin, func(ctx core.CommandContext) core.Result { args := ctx.Args var err error pathS, err := util.GetArg(args, "path", true, err) convertS, err := util.GetArg(args, "convert", true, err) doConvert, err := util.CastBool(convertS, err) if err != nil { return core.ResultByError(err) } convert := true if doConvert != nil { convert = *doConvert } return core.ResultByError(s.Sync(*pathS, convert)) }}) }
func (c *impl) Cmd(ctx core.CommandContext) core.Result { command, ok := c.commandMap[ctx.Cmd] if !ok { return core.ResultByError(core.ErrorCmdNotFound) } if ctx.AuthLevel < command.MinAuthLevel { if ctx.AuthLevel == core.AuthGuest { return core.ResultByError(core.ErrorNotLoggedIn) } return core.ResultByError(core.ErrorNotAllowed) } return command.Handler(ctx) }
func (p *IPod) Start(c core.Core, db *db.DB, s *sync.Sync) { p.core = c p.db = db p.sync = s p.config = config.Current.Plugins[PluginName].(*Config) c.RegisterCommand(core.Command{[]string{"ipod"}, "Syncs media with an iPod device. By default, Lossles files are converted to MP3 V0.", map[string]string{"mountpoint": "mountpoint of the iPod"}, core.AuthAdmin, func(ctx core.CommandContext) core.Result { path, err := util.GetArg(ctx.Args, "mountpoint", true, nil) if err != nil { return core.ResultByError(err) } return core.ResultByError(p.Sync(*path)) }}) }
func (c *impl) registerBaseCommands() { c.RegisterCommand(core.Command{ []string{"quit", "q", "close", "exit"}, "Shuts down and exits.", map[string]string{}, core.AuthRoot, func(_ core.CommandContext) core.Result { return core.ResultByError(c.Shutdown()) }}) c.RegisterCommand(core.Command{ []string{"help", "h", "?"}, "Prints help.", map[string]string{"c": "(Optional) the command to get help for"}, core.AuthGuest, func(ctx core.CommandContext) core.Result { res := make(map[string]interface{}) mk := func(k string, v core.Command) { if ctx.AuthLevel >= v.MinAuthLevel { res[k] = CmdHelp{v.MinAuthLevel, v.Verbs, v.Description, v.ArgsHelp} } } var err error cmdArg, err := util.GetArg(ctx.Args, "c", false, err) if err != nil { return core.ResultByError(err) } if cmdArg != nil { cmd, ok := c.commandMap[*cmdArg] if !ok { return core.ResultByError(core.ErrorCmdNotFound) } mk(*cmdArg, cmd) } else { for k, v := range c.commandSet { mk(k, v) } } return core.Result{Status: core.StatusOK, Result: res} }}) }
func (db *DB) initLogin(c core.Core) { c.RegisterCommand(core.Command{ []string{"create_user"}, "Creates a user in the database", map[string]string{ "name": "Username", "email": "E-Mail", "auth_level": fmt.Sprintf("User Rank: Guest(%d), User(%d), "+ "Admin(%d), Root(%d)", core.AuthGuest, core.AuthUser, core.AuthAdmin, core.AuthRoot), "password": "******"}, core.AuthAdmin, func(ctx core.CommandContext) core.Result { args := ctx.Args var err error name, err := util.GetArg(args, "name", true, err) email, err := util.GetArg(args, "email", true, err) authLevelS, err := util.GetArg(args, "auth_level", true, err) password, err := util.GetArg(args, "password", true, err) authLevelI, err := util.CastUint(authLevelS, err) if err != nil { return core.Result{Status: core.StatusError, Error: err} } authLevel := core.AuthLevel(*authLevelI) if authLevel >= ctx.AuthLevel { return core.ResultByError(errrs.New("You cannot create a user" + " with that level!")) } user, err := db.CreateUser(*name, *email, authLevel, *password) if err == nil { return core.Result{Status: core.StatusOK, Result: user} } return core.Result{Status: core.StatusError, Error: err} }}) c.RegisterCommand(core.Command{ []string{"login"}, "Logs in with user/password and returns the auth token", map[string]string{ "name": "Username", "password": "******"}, core.AuthGuest, func(ctx core.CommandContext) core.Result { args := ctx.Args var err error name, err := util.GetArg(args, "name", true, err) password, err := util.GetArg(args, "password", true, err) if err != nil { return core.ResultByError(err) } success, authToken, err := db.Login(*name, *password) if err == nil && success { return core.Result{Status: core.StatusOK, Result: map[string]string{"auth_token": authToken}} } if err == nil { return core.Result{Status: core.StatusError, Error: errrs.New("Wrong username or password")} } return core.Result{Status: core.StatusError, Error: err} }}) c.RegisterCommand(core.Command{ []string{"change_authtoken", "logout"}, `Changes the authentication token of the user, thereby logging out all clients`, map[string]string{ "name": `(Optional) username of the user to log out. Leave empty to use current user`, }, core.AuthUser, func(ctx core.CommandContext) core.Result { args := ctx.Args var err error name, err := util.GetArg(args, "name", false, err) if err != nil { return core.ResultByError(err) } var user *User if name != nil { if ctx.AuthLevel < core.AuthAdmin { return core.ResultByError(core.ErrorNotAllowed) } user, err = db.GetUserByName(*name) } else { if ctx.UserId == nil { return core.ResultByError(core.ErrorNotLoggedIn) } user, err = db.GetUser(*ctx.UserId) } if user == nil { return core.ResultByError(core.ErrorUserNotFound) } _, err = db.ChangeAuthToken(user) return core.ResultByError(err) }}) }
func (a *AS) scrobbleAS(ctx core.CommandContext) core.Result { //TODO implement return core.ResultByError(core.ErrorNotImplemented) }