1package otr3
2
3import "fmt"
4
5var errCantAuthenticateWithoutEncryption = newOtrError("can't authenticate a peer without a secure conversation established")
6var errCorruptEncryptedSignature = newOtrError("corrupt encrypted signature")
7var errEncryptedMessageWithNoSecureChannel = newOtrError("encrypted message received without encrypted session established")
8var errUnexpectedPlainMessage = newOtrError("plain message received when encryption was required")
9var errInvalidOTRMessage = newOtrError("invalid OTR message")
10var errInvalidVersion = newOtrError("no valid version agreement could be found") //libotr ignores this situation
11var errNotWaitingForSMPSecret = newOtrError("not expected SMP secret to be provided now")
12var errReceivedMessageForOtherInstance = newOtrError("received message for other OTR instance") //not exactly an error - we should ignore these messages by default
13var errShortRandomRead = newOtrError("short read from random source")
14var errUnexpectedMessage = newOtrError("unexpected SMP message")
15var errUnsupportedOTRVersion = newOtrError("unsupported OTR version")
16var errWrongProtocolVersion = newOtrError("wrong protocol version")
17var errMessageNotInPrivate = newOtrError("message not in private")
18var errCannotSendUnencrypted = newOtrConflictError("cannot send message in unencrypted state")
19
20// OtrError is an error in the OTR library
21type OtrError struct {
22	msg      string
23	conflict bool
24}
25
26func newOtrError(s string) error {
27	return OtrError{msg: s, conflict: false}
28}
29
30func newOtrConflictError(s string) error {
31	return OtrError{msg: s, conflict: true}
32}
33
34func newOtrErrorf(format string, a ...interface{}) error {
35	return OtrError{msg: fmt.Sprintf(format, a...), conflict: false}
36}
37
38func (oe OtrError) Error() string {
39	return "otr: " + oe.msg
40}
41
42func firstError(es ...error) error {
43	for _, e := range es {
44		if e != nil {
45			return e
46		}
47	}
48	return nil
49}
50
51func isConflict(e error) bool {
52	if oe, ok := e.(OtrError); ok {
53		return oe.conflict
54	}
55	return false
56}
57