// Remove any object from the database and hash list. func RemoveHash(log chan string, hashObj objects.Hash) error { mutex.Lock() defer mutex.Unlock() hash := hashObj.GetBytes() if hashList == nil || dbConn == nil { return DBError(EUNINIT) } var sql string switch Contains(hashObj) { case PUBKEY: sql = "DELETE FROM pubkey WHERE hash=?" case MSG: sql = "DELETE FROM msg WHERE hash=?" case PURGE: sql = "DELETE FROM purge WHERE hash=?" default: return nil } err := dbConn.Exec(sql, hash) if err != nil { log <- fmt.Sprintf("Error deleting hash from db... %s", err) return nil } Delete(hashObj) return nil }
// Get Encrypted Public Key from database. func GetPubkey(log chan string, addrHash objects.Hash) *objects.EncryptedPubkey { mutex.Lock() defer mutex.Unlock() hash := addrHash.GetBytes() if hashList == nil || dbConn == nil { return nil } if hashList[string(hash)] != PUBKEY { return nil } for s, err := dbConn.Query("SELECT payload FROM pubkey WHERE hash=?", hash); err == nil; err = s.Next() { var payload []byte s.Scan(&payload) // Assigns 1st column to rowid, the rest to row pub := new(objects.EncryptedPubkey) pub.AddrHash = addrHash copy(pub.IV[:], payload[:16]) pub.Payload = payload[16:] return pub } // Not Found return nil }
// Get basic message from database. func GetMessage(log chan string, txidHash objects.Hash) *objects.Message { mutex.Lock() defer mutex.Unlock() hash := txidHash.GetBytes() if hashList == nil || dbConn == nil { return nil } if hashList[string(hash)] != MSG && hashList[string(hash)] != PUB { return nil } msg := new(objects.Message) for s, err := dbConn.Query("SELECT * FROM msg WHERE hash=?", hash); err == nil; err = s.Next() { var timestamp int64 encrypted := make([]byte, 0, 0) txidhash := make([]byte, 0, 0) addrhash := make([]byte, 0, 0) s.Scan(&txidhash, &addrhash, ×tamp, &encrypted) msg.TxidHash.FromBytes(txidhash) msg.AddrHash.FromBytes(addrhash) msg.Timestamp = time.Unix(timestamp, 0) msg.Content.FromBytes(encrypted) return msg } // Not Found return nil }
func DeleteAddress(addrHash *objects.Hash) error { localMutex.Lock() defer localMutex.Unlock() if Contains(*addrHash) > ADDRESS { return errors.New("Error Deleting Message: Not Found!") } return LocalDB.Exec("DELETE FROM addressbook WHERE hash=?", addrHash.GetBytes()) }
func DeleteMessage(txidHash *objects.Hash) error { localMutex.Lock() defer localMutex.Unlock() if Contains(*txidHash) > SENDBOX { return errors.New("Error Deleting Message: Not Found!") } return LocalDB.Exec("DELETE FROM msg WHERE txid_hash=?", txidHash.GetBytes()) }
// Get type of object in Hash List func Contains(hashObj objects.Hash) int { hash := string(hashObj.GetBytes()) if hashList != nil { hashType, ok := hashList[hash] if ok { return hashType } else { return NOTFOUND } } return NOTFOUND }
func GetMessageDetail(txidHash objects.Hash) (*objects.FullMessage, error) { localMutex.Lock() defer localMutex.Unlock() if Contains(txidHash) > SENDBOX { return nil, errors.New("Message not found!") } ret := new(objects.FullMessage) ret.Encrypted = new(encryption.EncryptedMessage) ret.Decrypted = new(objects.DecryptedMessage) s, err := LocalDB.Query("SELECT * FROM msg WHERE txid_hash=?", txidHash.GetBytes()) if err == nil { recipient := make([]byte, 0, 0) sender := make([]byte, 0, 0) encrypted := make([]byte, 0, 0) decrypted := make([]byte, 0, 0) txidHash := make([]byte, 0, 0) var timestamp int64 var purged bool var box int s.Scan(&txidHash, &recipient, ×tamp, &box, &encrypted, &decrypted, &purged, &sender) ret.MetaMessage.TxidHash.FromBytes(txidHash) ret.MetaMessage.Recipient = encryption.AddressToString(recipient) ret.MetaMessage.Sender = encryption.AddressToString(sender) ret.MetaMessage.Timestamp = time.Unix(timestamp, 0) ret.MetaMessage.Purged = purged ret.Encrypted.FromBytes(encrypted) if len(decrypted) > 0 { ret.Decrypted.FromBytes(decrypted) } else { ret.Decrypted = nil } return ret, nil } return nil, err }
func DeleteObject(obj objects.Hash) error { var err error switch Contains(obj) { case INBOX: fallthrough case SENDBOX: fallthrough case OUTBOX: err = LocalDB.Exec("DELETE FROM msg WHERE txid_hash=?", obj.GetBytes()) case ADDRESS: err = LocalDB.Exec("DELETE FROM addressbook WHERE hash=?", obj.GetBytes()) default: err = errors.New("Hash not found!") } if err == nil { Del(obj) } return err }
// Get purge token from the database. func GetPurge(log chan string, txidHash objects.Hash) *objects.Purge { mutex.Lock() defer mutex.Unlock() hash := txidHash.GetBytes() if hashList == nil || dbConn == nil { return nil } if hashList[string(hash)] != PURGE { return nil } for s, err := dbConn.Query("SELECT txid FROM purge WHERE hash=?", hash); err == nil; err = s.Next() { var txid []byte s.Scan(&txid) // Assigns 1st column to rowid, the rest to row p := new(objects.Purge) p.FromBytes(txid) return p } // Not Found return nil }
// Delete hash from Hash List func Del(hashObj objects.Hash) { hash := string(hashObj.GetBytes()) if hashList != nil { delete(hashList, hash) } }
// Add to global Hash List. func Add(hashObj objects.Hash, hashType int) { hash := string(hashObj.GetBytes()) if hashList != nil { hashList[hash] = hashType } }
func GetAddressDetail(addrHash objects.Hash) (*objects.AddressDetail, error) { localMutex.Lock() defer localMutex.Unlock() if Contains(addrHash) != ADDRESS { return nil, errors.New("Address not found!") } ret := new(objects.AddressDetail) s, err := LocalDB.Query("SELECT address, registered, pubkey, privkey, label, subscribed, encprivkey FROM addressbook WHERE hash=?", addrHash.GetBytes()) if err == nil { s.Scan(&ret.Address, &ret.IsRegistered, &ret.Pubkey, &ret.Privkey, &ret.Label, &ret.IsSubscribed, &ret.EncPrivkey) ret.String = encryption.AddressToString(ret.Address) return ret, nil } return nil, err }