Example #1
0
func (ce *CtrlEngine) msgSend(
	c *cli.Context,
	id string,
	all bool,
	failDelivery bool,
) error {
	nyms, err := ce.getNyms(id, all)
	if err != nil {
		return err
	}
	for _, nym := range nyms {
		// clear resend status for old messages in outqueue
		if err := ce.msgDB.ClearResendOutQueue(nym); err != nil {
			return err
		}

		// process old messages in outqueue
		if err := ce.procOutQueue(c, nym, failDelivery); err != nil {
			return err
		}

		/*
			ids, err := ce.msgDB.GetMsgIDs(nym)
			if err != nil {
				return err
			}
			for _, id := range ids {
				log.Debugf("id=%d, to=%s", id.MsgID, id.To)
			}
		*/

		// add all undelivered messages to outqueue
		var recvNymAddress string
		for {
			msgID, peer, msg, sign, minDelay, maxDelay, err :=
				ce.msgDB.GetUndeliveredMessage(nym)
			if err != nil {
				return err
			}
			if peer == "" {
				log.Debug("break")
				break // no more undelivered messages
			}

			// determine recipient nymaddress for encryption, if necessary
			if recvNymAddress == "" {
				// TODO! (implement more accounts? delay settings?)
				privkey, server, secret, minDelay, maxDelay, _, err :=
					ce.msgDB.GetAccount(nym, "")
				if err != nil {
					return err
				}
				_, domain, err := identity.Split(nym)
				if err != nil {
					return err
				}
				expire := times.ThirtyDaysLater() // TODO: make this settable
				singleUse := false                // TODO correct?
				var pubkey [ed25519.PublicKeySize]byte
				copy(pubkey[:], privkey[32:])
				_, recvNymAddress, err = util.NewNymAddress(domain, secret[:],
					expire, singleUse, minDelay, maxDelay, nym, &pubkey, server,
					def.CACert)
				if err != nil {
					return err
				}
			}

			// encrypt
			enc, nymaddress, err := mutecryptEncrypt(c, nym, peer,
				ce.passphrase, msg, sign, recvNymAddress)
			if err != nil {
				return log.Error(err)
			}
			// add to outqueue
			log.Debug("add")
			err = ce.msgDB.AddOutQueue(nym, msgID, enc, nymaddress,
				minDelay, maxDelay)
			if err != nil {
				return log.Error(err)
			}
		}

		// process new messages in outqueue
		if err := ce.procOutQueue(c, nym, failDelivery); err != nil {
			return err
		}
	}
	return nil
}
Example #2
0
func (ce *CtrlEngine) uidNew(
	c *cli.Context,
	minDelay, maxDelay int32,
	host string,
) error {
	// make sure the ID is well-formed
	unmapped := c.String("id")
	id, domain, err := identity.MapPlus(unmapped)
	if err != nil {
		return err
	}

	// sync corresponding hashchain
	if id != "keyserver" {
		if err := ce.upkeepHashchain(c, domain, c.String("host")); err != nil {
			return err
		}
	}

	// check that ID has not been registered already by the same user
	unmappedID, _, err := ce.msgDB.GetNym(id)
	if err != nil {
		return err
	}
	if unmappedID != "" {
		return log.Error(ErrUserIDOwned)
	}

	// check that ID has not been registered already by other user
	err = mutecryptHashchainSearch(c, id, c.String("host"), ce.passphrase)
	if err == nil {
		return log.Error(ErrUserIDTaken)
	}

	// get token from wallet
	token, err := wallet.GetToken(ce.client, def.AccdUsage, def.AccdOwner)
	if err != nil {
		return err
	}

	// register account for  UID
	_, privkey, err := ed25519.GenerateKey(cipher.RandReader)
	if err != nil {
		return log.Error(err)
	}
	server, err := mixclient.PayAccount(privkey, token.Token, "", def.CACert)
	if err != nil {
		ce.client.UnlockToken(token.Hash)
		return log.Error(err)
	}
	ce.client.DelToken(token.Hash)

	// generate secret for account
	var secret [64]byte
	if _, err := io.ReadFull(cipher.RandReader, secret[:]); err != nil {
		return err
	}

	// get mixaddress and nymaddress for KeyInit message
	expire := times.ThirtyDaysLater() // TODO: make this settable
	singleUse := false                // TODO correct?
	var pubkey [ed25519.PublicKeySize]byte
	copy(pubkey[:], privkey[32:])
	mixaddress, nymaddress, err := util.NewNymAddress(domain, secret[:], expire,
		singleUse, minDelay, maxDelay, id, &pubkey, server, def.CACert)
	if err != nil {
		return err
	}

	// generate UID
	err = mutecryptNewUID(c, ce.passphrase, id, domain, host, mixaddress,
		nymaddress, ce.client)
	if err != nil {
		return err
	}

	// save name mapping
	if err := ce.msgDB.AddNym(id, unmapped, c.String("full-name")); err != nil {
		return err
	}

	// register account for UID
	err = ce.msgDB.AddAccount(id, "", privkey, server, &secret,
		minDelay, maxDelay)
	if err != nil {
		return err
	}

	// set active UID, if this was the first UID
	active, err := ce.msgDB.GetValue(msgdb.ActiveUID)
	if err != nil {
		return err
	}
	if active == "" {
		if err := ce.msgDB.AddValue(msgdb.ActiveUID, unmapped); err != nil {
			return err
		}
	}
	return nil
}