Example #1
0
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))
		}})
}
Example #2
0
func (db *DB) initList(c core.Core) {
	c.RegisterCommand(core.Command{
		[]string{"list", "ls"},
		"Searches and lists items from the database",
		map[string]string{
			"text":  "(Optional) Search string for full text search",
			"query": "(Optional) Input for SQL-ish query",
			"tags":  "(Optional) Comma-separated list of tags to include",
		},
		core.AuthGuest,
		db.list})
}
Example #3
0
func (db *DB) initLogic(c core.Core) {
	c.RegisterCommand(core.Command{
		[]string{"trackplayed"},
		"Inserts a track playback into the database statistics",
		map[string]string{
			"title":         "Title",
			"artist":        "Artist",
			"album":         "Album",
			"album_artist":  "Album Artist",
			"length":        "Track length in ms",
			"date":          "Time the listening occurred, as Unix timestamp",
			"length_played": "The time the track was listened to (when fully played: length) in ms",
			"scrobbled":     "Whether the track was scrobbled to last.fm/libre.fm"},
		core.AuthUser,
		db.TrackPlayed})
}
Example #4
0
func (d *DB) initStats(c core.Core) {
	c.RegisterCommand(core.Command{
		[]string{"stats"},
		"Prints some statistics about the database",
		map[string]string{},
		core.AuthUser,
		func(_ core.CommandContext) core.Result {
			res := map[string]interface{}{
				"items_total":      d.TitlesTotal(),
				"folders_total":    d.FoldersTotal(),
				"items_per_folder": d.AvgFilesPerFolder(),
			}
			//TODO more stats
			//TODO stats per user level
			return core.Result{Status: core.StatusOK, Result: res}
		}})
}
Example #5
0
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))
		}})
}
Example #6
0
func (d *DB) Open(c core.Core) error {
	if d.open {
		return errrs.New("DB is already opened!")
	}
	d.c = c

	var err error
	file := config.Current.DbFile

	d.db, err = gorm.Open("sqlite3", file)
	if err != nil {
		return err
	}
	d.open = true

	d.db.LogMode(true)
	if err := d.checkTables(); err != nil {
		return err
	}

	err = d.checkSanity()
	if err != nil {
		//TODO what now?
		return err
	}

	d.initStats(c)
	d.initLogic(c) // hear the difference?
	d.initLogin(c) // it's subtle but it could save your life
	d.initList(c)

	c.RegisterCommand(core.Command{
		[]string{"rescan"},
		"Refreshes the database by re-scanning the media folder.",
		map[string]string{},
		core.AuthAdmin,
		func(_ core.CommandContext) core.Result {
			go d.Update()
			return core.ResultOK
		}})

	return nil
}
Example #7
0
func (a *AS) Start(c core.Core, d *db.DB) {
	a.db = d

	//TODO get base url
	baseURL := "http://localhost/api/"
	a.nowPlayingURL = baseURL + "as_nowplaying"
	a.submissionURL = baseURL + "as_scrobble"

	c.RegisterCommand(core.Command{
		[]string{"audioscrobbler", "as"},
		`Interface for clients using the Last.fm Submissions Protocol v1.2.1.
In your favourite scrobbler, use this as handshake URL:
http://cfmedias-server-ip/api/audioscrobbler/?hs=true
See also http://www.last.fm/api/submissions.`,
		map[string]string{
			"hs": "hs=true marks a handshake request. requires p, c, v, u, t, a.",
			"p":  "must be 1.2.1",
			"c":  "identifier for the client software",
			"v":  "version of the client software",
			"u":  "username",
			"t":  "timestamp of the request",
			"a":  "authentication message: md5(md5(auth_token) + timestamp)",
			"__response": `The server will respond with:
OK
<session ID>
<url to be used for a now-playing request>
<url to be used for a submission request>

or one of these:
BANNED: Your client is doing bad things and is therefore banned.
BADAUTH: Authentication details are incorrect.
BADTIME: Time difference between server and client time too high.
 Correct your system clock.
FAILED <reason>: Your request failed for the specified reason.`},
		core.AuthGuest,
		a.LoginAS})
}
Example #8
0
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)
		}})
}