Ejemplo n.º 1
0
// Fetch a message from storage, delete if it is a one-time message
func (store Store) Fetch(messageID *[message.MessageIDSize]byte) ([]byte, error) {
	data := store.messages.Index(messageID[:]).GetLast()
	if data == nil || len(data) < MaxMessageSize+structs.MessageStructSize {
		return nil, ErrNotFound
	}
	msgStruct := structs.MessageStructDecode(data[:structs.MessageStructSize])
	if msgStruct.OneTime {
		store.messages.Index(messageID[:]).Truncate()
		signerLoaded := structs.SignerStructDecode(store.signers.Index(msgStruct.SignerPub[:]).GetLast())
		if signerLoaded != nil {
			signerLoaded.MessagesRetained--
			store.signers.Index(msgStruct.SignerPub[:]).Append(signerLoaded.Encode().Fill())
		}
	}
	return data[structs.MessageStructSize:], nil
}
Ejemplo n.º 2
0
Archivo: put.go Proyecto: mewbak/repbin
// updateKeyIndex updates a key index by writing the new struct with an increased counter
func updateKeyIndex(tx fileback.Tx, msgStruct *structs.MessageStruct, msgStructEncoded structs.MessageStructEncoded) error { // Ignore errors
	prev := tx.GetLast()
	if prev == nil {
		log.Errors("messagestore, globalindex getLast: NIL")
		err := tx.Append(msgStructEncoded)
		return err
	}
	prevStr := structs.MessageStructDecode(prev)
	if prevStr == nil {
		log.Errors("messagestore, globalindex decode: NIL")
		err := tx.Append(msgStructEncoded)
		return err
	}
	msgStruct.Counter = prevStr.Counter + 1 // Increase counter
	msgStructEncoded = msgStruct.Encode().Fill()
	err := tx.Append(msgStructEncoded)
	return err
}
Ejemplo n.º 3
0
// PreExpire expires a message in the next expire run
func (store Store) PreExpire(messageID *[message.MessageIDSize]byte, pubkey *message.Curve25519Key) error {
	data := store.messages.Index(messageID[:]).GetLast()
	if data == nil || len(data) < MaxMessageSize+structs.MessageStructSize {
		return ErrNotFound
	}
	msgStruct := structs.MessageStructDecode(data[:structs.MessageStructSize])
	if msgStruct.ReceiverConstantPubKey == *pubkey {
		// Add to expire
		expire := &structs.ExpireStruct{
			ExpireTime:   uint64(time.Now().Unix()),
			MessageID:    msgStruct.MessageID,
			SignerPubKey: msgStruct.SignerPub,
		}
		expireTime := (uint64(time.Now().Unix()) / uint64(ExpireRun)) + uint64(ExpireRun)
		return store.expireindex.Index(utils.EncodeUInt64(expireTime)).CreateAppend(expire.Encode().Fill())
	}
	return ErrNotFound
}
Ejemplo n.º 4
0
func lineToMsgStruct(d []byte) *structs.MessageStruct {
	return structs.MessageStructDecode(d)
}