commands

package
v0.0.0-...-f701b90 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2025 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultPerms = 0755

Default level of permissions that should be used

Variables

View Source
var (
	ErrorInsuficientArgs       error = fmt.Errorf("not enough arguments")                           // not enough arguments
	ErrorNotConnected          error = fmt.Errorf("not connected to a server")                      // not connected to a server
	ErrorAlreadyConnected      error = fmt.Errorf("already connected to a server")                  // already connected to a server
	ErrorNotLoggedIn           error = fmt.Errorf("you are not logged in")                          // you are not logged in
	ErrorAlreadyLoggedIn       error = fmt.Errorf("you are already logged in")                      // you are already logged in
	ErrorWrongCredentials      error = fmt.Errorf("wrong credentials")                              // wrong credentials
	ErrorUnknownUSRSOption     error = fmt.Errorf("unknown usrs option provided")                   // unknown usrs option provided
	ErrorUsernameEmpty         error = fmt.Errorf("username cannot be empty")                       // username cannot be empty
	ErrorUserExists            error = fmt.Errorf("local user exists")                              // local user exists
	ErrorPasswordsDontMatch    error = fmt.Errorf("passwords do not match")                         // passwords do not match
	ErrorUserNotFound          error = fmt.Errorf("user not found")                                 // user not found
	ErrorUnknownTLSOption      error = fmt.Errorf("unknown tls option provided")                    // unknown tls option provided
	ErrorOfflineRequired       error = fmt.Errorf("you must be offline")                            // you must be offline
	ErrorInvalidSkipVerify     error = fmt.Errorf("cannot skip verification on a non-TLS endpoint") // cannot skip verification on a non-TLS endpoint
	ErrorRequestToSelf         error = fmt.Errorf("cannot request yourself")                        // cannot request yourself
	ErrorUnknownHookOption     error = fmt.Errorf("invalid hook provided")                          // invalid hook provided
	ErrorInvalidAdminOperation error = fmt.Errorf("invalid admin operation")                        // invalid admin operation
	ErrorRecoveryPassword      error = fmt.Errorf("could not recover during password checking")     // could not recover during password checking
	ErrorInvalidTarget         error = fmt.Errorf("provided object is not an appropiate type")      // provided object is not an appropiate type
	ErrorInvalidField          error = fmt.Errorf("provided field is non-existant")                 // provided field is non-existant
	ErrorCannotSet             error = fmt.Errorf("failed to set a value on the given field")       // failed to set a value on the given field
	ErrorNoReusableToken       error = fmt.Errorf("reusable token is empty")                        // reusable token is empty
)
View Source
var BuildCode string

Specifies the build code

Functions

func ADMIN

func ADMIN(ctx context.Context, cmd Command, op string, args ...[]byte) error

Sends an ADMIN packet that performs an specific ADMIN operation.

func CONFIG

func CONFIG(objs ...ConfigObj) [][]byte

Returns the current configuration values for the given objects. Only needs the object and prefix. Passed objects as "any" can or not be pointers

func CONN

func CONN(cmd Command, server db.Server, noverify bool) error

Starts a connection with a server. If noverify is set, in case of TLS connections, certificate origins wont be checked. This command does not spawn a listening thread.

func DEREG

func DEREG(ctx context.Context, cmd Command, username, pass string) error

Deregisters a user from the server and also removes it locally.

func DISCN

func DISCN(cmd Command) error

Disconnects a client from a server.

func DefaultWaitlist

func DefaultWaitlist() models.Waitlist[spec.Command]

Returns an appropiate waitlist

func EXPORT

func EXPORT(cmd Command, username, pass string) error

Exports a local user as a private RSA key in the "export" folder using the spec PEM format.

func Find

func Find(id spec.ID, ops ...spec.Action) func(cmd spec.Command) bool

Returns a function that returns true if the received command fulfills the given conditions in the arguments (ID and operations). This is used to dinamically create functions that retrieve commands from the waitlist with waitlist.Get()

func GetPermissions

func GetPermissions(ctx context.Context, cmd Command, uname string) (uint, error)

Requests the user logged in to get its permissions

func IMPORT

func IMPORT(cmd Command, username, pass, dir string) error

Imports a private RSA key for a new local user from the "import" directory using the specification PEM format.

func LOGIN

func LOGIN(ctx context.Context, cmd Command, username, pass string) error

Logs a user to a server, also performs the verification.

func LOGOUT

func LOGOUT(ctx context.Context, cmd Command) error

Logs out a user from a server.

func ListenPackets

func ListenPackets(cmd Command, cleanup func())

Listens for incoming server packets. When a packet is received, it is stored in the packet waitlist A cleanup function that cleans up resources can be passed.

func MSG

func MSG(ctx context.Context, cmd Command, username, message string) error

Sends a message to a user with the current time stamp and stores it in the database.

func PreventIdle

func PreventIdle(ctx context.Context, data *Data, d time.Duration)

Sends a KEEP packet every x time

func RECIV

func RECIV(ctx context.Context, cmd Command) error

Asks the server to retrieve all messages while the user was offline. This function is not responsible for receiving the messages, only request them.

func RECOVER

func RECOVER(cmd Command, username, pass string, cleanup bool) error

Recovers the private key and messages for a specified user Does not require a Data struct in Command

func REG

func REG(ctx context.Context, cmd Command, username, pass string) error

Registers a user to a server and also adds it to the client database.

func REQ

func REQ(ctx context.Context, cmd Command, username string) ([][]byte, error)

Requests the information of an external user to add it to the client database. Returns the arguments of a REQ as by specification.

func SET

func SET(cmd Command, target, value string, objs ...ConfigObj) error

Sets a variable on an object as configuration. Passed objects must be pointers. Does not require a Data struct in "Command"

func SUB

func SUB(ctx context.Context, cmd Command, name string) error

Subscribes to a specific hook to the server.

func SocketConnect

func SocketConnect(address string, port uint16, useTLS bool, noVerify bool) (con net.Conn, err error)

Performs the socket connection to the server.

func UNSUB

func UNSUB(ctx context.Context, cmd Command, name string) error

Unsubscribes from a specific hook on the server.

func USRS

func USRS(ctx context.Context, cmd Command, usrsType USRSType) ([][]byte, error)

Requests a list of users depending on the type specified, which may or not require an active connection. Returns a the received usernames in an array if the request was correct.

func Version

func Version(major float32) string

Returns the full version string

func WaitConnect

func WaitConnect(data Command, endpoint net.Conn, server db.Server) error

Listens for a HELLO packet from the server when starting the connection, which determines that the client/server connection was started successfully.

Types

type Command

type Command struct {
	Output OutputFunc  // Custom output-printing function
	Static *StaticData // Static Data (mostly)
	Data   *Data       // Modifiable Data
}

Specifies all structs necessary for a command

type ConfigObj

type ConfigObj struct {
	Prefix       string       // Should start with capital letter
	Object       any          // Can be or not a pointer (read the function precondition)
	Precondition func() error // Condition needed for it to be able to set a value
	Update       ConfigUpdate // Called to update values on the database
	Finish       func()       // Run a function once it has been updated
}

Represents a config struct to be passed to modify

type ConfigUpdate

type ConfigUpdate func(db *gorm.DB, obj any, column string, val any) error

Represents a function to update an object on the database

type Data

type Data struct {
	Conn     net.Conn                      // Specifies the connection to the server
	Logout   context.CancelFunc            // Specifies the function to call on a logout for context propagation
	Waitlist models.Waitlist[spec.Command] // Stores all packets to be retrieved later

	// Using pointers so that "nil" can be used
	Server    *db.Server    // Specifies the database server
	LocalUser *db.LocalUser // Specifies the logged in user
	// contains filtered or unexported fields
}

Struct that contains all the data necessary to run a command Requires fields may change between commands Commands may alter the data if necessary

func NewEmptyData

func NewEmptyData() Data

Creates a new empty but initialised struct for Data

func (*Data) ClearToken

func (d *Data) ClearToken()

Empties the reusabke token

func (*Data) GetToken

func (d *Data) GetToken() (string, bool)

Gets a reusable token if it exists

func (*Data) IsConnected

func (data *Data) IsConnected() bool

Whether the connection is or not established

func (*Data) IsLoggedIn

func (data *Data) IsLoggedIn() bool

Whether the connection is logged in or not

func (*Data) NextID

func (data *Data) NextID() spec.ID

Incremenents the next ID to be used and returns it

func (*Data) SetToken

func (d *Data) SetToken(t string)

Sets a new reusable token

type Message

type Message struct {
	Sender    string    // Who is sending the message
	Content   string    // What the message contains
	Timestamp time.Time // When the message was sent
}

Specifies a message that is going through the connection

func StoreMessage

func StoreMessage(ctx context.Context, reciv spec.Command, cmd Command) (Message, error)

Performs the necessary operations to store a RECIV packet in the database (decryption, REQ (if necessary) insert...), then returns the decrypted message

type OutputFunc

type OutputFunc func(text string, outputType OutputType)

Represents the function that will be called when outputting information

type OutputType

type OutputType uint

Represents the type of a command output This eases specific output printing actions

const (
	INTERMEDIATE OutputType = iota // Intermediate status messages
	PACKET                         // Packet information messages
	PROMPT                         // Prompt message when input is asked
	RESULT                         // Messages that show the result of a command
	ERROR                          // Error messages that may be printed additionaly in error cases
	INFO                           // Message that representes generic info not asocciated to a command
	USRSRESPONSE                   // Specific for user printing
	COLOR                          // Special output for shell colors
	PLAIN                          // Output type that should be printed as-is, with no prefix
	SECONDARY                      // Optional text to show after the result
)

type StaticData

type StaticData struct {
	Verbose bool     // Whether or not to print detailed information
	DB      *gorm.DB // Connection to the database
}

Static data that should only be assigned in specific cases

type USRSType

type USRSType uint

Represents the different USRS command types

const (
	ALL          USRSType = 0 // Users in the server (as spec)
	ONLINE       USRSType = 1 // Online users in the server (as spec)
	ALLPERMS     USRSType = 2 // All users with perms (as spec)
	ONLINEPERMS  USRSType = 3 // Online users with perms (as spec)
	LOCAL_SERVER USRSType = 4 // Registered local users for a server
	LOCAL_ALL    USRSType = 5 // All local users
	REQUESTED    USRSType = 6 // All external users whose public key has been saved
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL