23 #include "mrmailbox_internal.h" 24 #include "mrapeerstate.h" 25 #include "mraheader.h" 33 static void mrapeerstate_empty(mrapeerstate_t* ths)
40 ths->m_last_seen_autocrypt = 0;
41 ths->m_prefer_encrypt = 0;
47 if( ths->m_public_key->m_binary ) {
48 mrkey_unref(ths->m_public_key);
49 ths->m_public_key = mrkey_new();
54 int mrapeerstate_load_from_db__(mrapeerstate_t* ths, mrsqlite3_t* sql,
const char* addr)
59 if( ths==NULL || sql == NULL || addr == NULL ) {
63 mrapeerstate_empty(ths);
65 stmt = mrsqlite3_predefine__(sql, SELECT_aclpp_FROM_acpeerstates_WHERE_a,
66 "SELECT addr, last_seen, last_seen_autocrypt, prefer_encrypted, public_key FROM acpeerstates WHERE addr=? COLLATE NOCASE;");
67 sqlite3_bind_text(stmt, 1, addr, -1, SQLITE_STATIC);
68 if( sqlite3_step(stmt) != SQLITE_ROW ) {
71 ths->m_addr = safe_strdup((
char*)sqlite3_column_text (stmt, 0));
72 ths->m_last_seen = sqlite3_column_int64 (stmt, 1);
73 ths->m_last_seen_autocrypt = sqlite3_column_int64 (stmt, 2);
74 ths->m_prefer_encrypt = sqlite3_column_int (stmt, 3);
75 mrkey_set_from_stmt (ths->m_public_key, stmt, 4, MR_PUBLIC);
84 int mrapeerstate_save_to_db__(
const mrapeerstate_t* ths, mrsqlite3_t* sql,
int create)
89 if( ths==NULL || sql==NULL
90 || ths->m_addr==NULL || ths->m_public_key->m_binary==NULL || ths->m_public_key->m_bytes<=0 ) {
95 stmt = mrsqlite3_predefine__(sql, INSERT_INTO_acpeerstates_a,
"INSERT INTO acpeerstates (addr) VALUES(?);");
96 sqlite3_bind_text(stmt, 1, ths->m_addr, -1, SQLITE_STATIC);
100 if( (ths->m_to_save&MRA_SAVE_ALL) || create )
102 stmt = mrsqlite3_predefine__(sql, UPDATE_acpeerstates_SET_lcpp_WHERE_a,
103 "UPDATE acpeerstates SET last_seen=?, last_seen_autocrypt=?, prefer_encrypted=?, public_key=? WHERE addr=?;");
104 sqlite3_bind_int64(stmt, 1, ths->m_last_seen);
105 sqlite3_bind_int64(stmt, 2, ths->m_last_seen_autocrypt);
106 sqlite3_bind_int64(stmt, 3, ths->m_prefer_encrypt);
107 sqlite3_bind_blob (stmt, 4, ths->m_public_key->m_binary, ths->m_public_key->m_bytes, SQLITE_STATIC);
108 sqlite3_bind_text (stmt, 5, ths->m_addr, -1, SQLITE_STATIC);
109 if( sqlite3_step(stmt) != SQLITE_DONE ) {
113 else if( ths->m_to_save&MRA_SAVE_LAST_SEEN )
115 stmt = mrsqlite3_predefine__(sql, UPDATE_acpeerstates_SET_l_WHERE_a,
116 "UPDATE acpeerstates SET last_seen=?, last_seen_autocrypt=? WHERE addr=?;");
117 sqlite3_bind_int64(stmt, 1, ths->m_last_seen);
118 sqlite3_bind_int64(stmt, 2, ths->m_last_seen_autocrypt);
119 sqlite3_bind_text (stmt, 3, ths->m_addr, -1, SQLITE_STATIC);
120 if( sqlite3_step(stmt) != SQLITE_DONE ) {
137 mrapeerstate_t* mrapeerstate_new()
139 mrapeerstate_t* ths = NULL;
141 if( (ths=calloc(1,
sizeof(mrapeerstate_t)))==NULL ) {
145 ths->m_public_key = mrkey_new();
151 void mrapeerstate_unref(mrapeerstate_t* ths)
158 mrkey_unref(ths->m_public_key);
168 int mrapeerstate_init_from_header(mrapeerstate_t* ths,
const mraheader_t* header, time_t message_time)
170 if( ths == NULL || header == NULL ) {
174 mrapeerstate_empty(ths);
175 ths->m_addr = safe_strdup(header->m_addr);
176 ths->m_last_seen = message_time;
177 ths->m_last_seen_autocrypt = message_time;
178 ths->m_to_save = MRA_SAVE_ALL;
179 ths->m_prefer_encrypt = header->m_prefer_encrypt;
180 mrkey_set_from_key(ths->m_public_key, header->m_public_key);
185 int mrapeerstate_degrade_encryption(mrapeerstate_t* ths, time_t message_time)
191 ths->m_prefer_encrypt = MRA_PE_RESET;
192 ths->m_last_seen = message_time;
193 ths->m_to_save = MRA_SAVE_ALL;
198 int mrapeerstate_apply_header(mrapeerstate_t* ths,
const mraheader_t* header, time_t message_time)
200 if( ths==NULL || header==NULL
202 || header->m_addr==NULL || header->m_public_key->m_binary==NULL
203 || strcasecmp(ths->m_addr, header->m_addr)!=0 ) {
207 if( message_time > ths->m_last_seen_autocrypt )
209 ths->m_last_seen = message_time;
210 ths->m_last_seen_autocrypt = message_time;
211 ths->m_to_save |= MRA_SAVE_LAST_SEEN;
213 if( (header->m_prefer_encrypt==MRA_PE_MUTUAL || header->m_prefer_encrypt==MRA_PE_NOPREFERENCE)
214 && header->m_prefer_encrypt != ths->m_prefer_encrypt )
216 ths->m_prefer_encrypt = header->m_prefer_encrypt;
217 ths->m_to_save |= MRA_SAVE_ALL;
220 if( !mrkey_equals(ths->m_public_key, header->m_public_key) )
222 mrkey_set_from_key(ths->m_public_key, header->m_public_key);
223 ths->m_to_save |= MRA_SAVE_ALL;
227 return ths->m_to_save? 1 : 0;