1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Fixed OTR decryption check

This commit is contained in:
James Booth 2015-08-27 00:37:48 +01:00
parent ef52840d91
commit 1484e94b35
8 changed files with 30 additions and 18 deletions

View File

@ -193,7 +193,7 @@ str_replace(const char *string, const char *substr,
} }
gboolean gboolean
str_contains_str(char *searchstr, char *substr) str_contains_str(const char * const searchstr, const char * const substr)
{ {
return g_strrstr(searchstr, substr) != NULL; return g_strrstr(searchstr, substr) != NULL;
} }

View File

@ -104,7 +104,7 @@ gboolean create_dir(char *name);
gboolean mkdir_recursive(const char *dir); gboolean mkdir_recursive(const char *dir);
char * str_replace(const char *string, const char *substr, char * str_replace(const char *string, const char *substr,
const char *replacement); const char *replacement);
gboolean str_contains_str(char *searchstr, char *substr); gboolean str_contains_str(const char * const searchstr, const char * const substr);
int str_contains(const char str[], int size, char ch); int str_contains(const char str[], int size, char ch);
gboolean strtoi_range(char *str, int *saveptr, int min, int max, char **err_msg); gboolean strtoi_range(char *str, int *saveptr, int min, int max, char **err_msg);
int utf8_display_len(const char * const str); int utf8_display_len(const char * const str);

View File

@ -219,7 +219,7 @@ _sv_ev_incoming_otr(ProfChatWin *chatwin, gboolean new_win, char *barejid, char
gboolean decrypted = FALSE; gboolean decrypted = FALSE;
char *otr_res = otr_on_message_recv(barejid, resource, message, &decrypted); char *otr_res = otr_on_message_recv(barejid, resource, message, &decrypted);
if (otr_res) { if (otr_res) {
if (decrypted && g_strrstr(otr_res, message) == NULL) { if (decrypted) {
ui_incoming_msg(chatwin, resource, otr_res, NULL, new_win, PROF_ENC_OTR); ui_incoming_msg(chatwin, resource, otr_res, NULL, new_win, PROF_ENC_OTR);
} else { } else {
ui_incoming_msg(chatwin, resource, otr_res, NULL, new_win, PROF_ENC_NONE); ui_incoming_msg(chatwin, resource, otr_res, NULL, new_win, PROF_ENC_NONE);

View File

@ -274,7 +274,7 @@ otr_on_connect(ProfAccount *account)
} }
char* char*
otr_on_message_recv(const char * const barejid, const char * const resource, const char * const message, gboolean *was_decrypted) otr_on_message_recv(const char * const barejid, const char * const resource, const char * const message, gboolean *decrypted)
{ {
prof_otrpolicy_t policy = otr_get_policy(barejid); prof_otrpolicy_t policy = otr_get_policy(barejid);
char *whitespace_base = strstr(message, OTRL_MESSAGE_TAG_BASE); char *whitespace_base = strstr(message, OTRL_MESSAGE_TAG_BASE);
@ -298,19 +298,19 @@ otr_on_message_recv(const char * const barejid, const char * const resource, con
} }
} }
char *decrypted = otr_decrypt_message(barejid, message, was_decrypted); char *newmessage = otr_decrypt_message(barejid, message, decrypted);
if (!decrypted) { // internal OTR message if (!newmessage) { // internal OTR message
return NULL; return NULL;
} }
if (policy == PROF_OTRPOLICY_ALWAYS && *was_decrypted == FALSE && !whitespace_base) { if (policy == PROF_OTRPOLICY_ALWAYS && *decrypted == FALSE && !whitespace_base) {
char *otr_query_message = otr_start_query(); char *otr_query_message = otr_start_query();
cons_show("Attempting to start OTR session..."); cons_show("Attempting to start OTR session...");
char *id = message_send_chat_otr(barejid, otr_query_message); char *id = message_send_chat_otr(barejid, otr_query_message);
free(id); free(id);
} }
return decrypted; return newmessage;
} }
gboolean gboolean
@ -717,12 +717,12 @@ _otr_tlv_free(OtrlTLV *tlvs)
} }
char * char *
otr_decrypt_message(const char * const from, const char * const message, gboolean *was_decrypted) otr_decrypt_message(const char * const from, const char * const message, gboolean *decrypted)
{ {
char *decrypted = NULL; char *newmessage = NULL;
OtrlTLV *tlvs = NULL; OtrlTLV *tlvs = NULL;
int result = otrlib_decrypt_message(user_state, &ops, jid, from, message, &decrypted, &tlvs); int result = otrlib_decrypt_message(user_state, &ops, jid, from, message, &newmessage, &tlvs);
// internal libotr message // internal libotr message
if (result == 1) { if (result == 1) {
@ -743,16 +743,18 @@ otr_decrypt_message(const char * const from, const char * const message, gboolea
return NULL; return NULL;
// message was decrypted, return to user // message was processed, return to user
} else if (decrypted) { } else if (newmessage) {
_otr_tlv_free(tlvs); _otr_tlv_free(tlvs);
*was_decrypted = TRUE; if (g_str_has_prefix(message, "?OTR:")) {
return decrypted; *decrypted = TRUE;
}
return newmessage;
// normal non OTR message // normal non OTR message
} else { } else {
_otr_tlv_free(tlvs); _otr_tlv_free(tlvs);
*was_decrypted = FALSE; *decrypted = FALSE;
return strdup(message); return strdup(message);
} }
} }

View File

@ -58,7 +58,7 @@ char* otr_start_query(void);
void otr_poll(void); void otr_poll(void);
void otr_on_connect(ProfAccount *account); void otr_on_connect(ProfAccount *account);
char* otr_on_message_recv(const char * const barejid, const char * const resource, const char * const message, gboolean *was_decrypted); char* otr_on_message_recv(const char * const barejid, const char * const resource, const char * const message, gboolean *decrypted);
gboolean otr_on_message_send(ProfChatWin *chatwin, const char * const message); gboolean otr_on_message_send(ProfChatWin *chatwin, const char * const message);
void otr_keygen(ProfAccount *account); void otr_keygen(ProfAccount *account);
@ -83,7 +83,7 @@ char * otr_get_their_fingerprint(const char * const recipient);
char * otr_encrypt_message(const char * const to, const char * const message); char * otr_encrypt_message(const char * const to, const char * const message);
char * otr_decrypt_message(const char * const from, const char * const message, char * otr_decrypt_message(const char * const from, const char * const message,
gboolean *was_decrypted); gboolean *decrypted);
void otr_free_message(char *message); void otr_free_message(char *message);

View File

@ -663,6 +663,14 @@ void str_contains_str_in_middle(void **state)
assert_true(str_contains_str(main, occur)); assert_true(str_contains_str(main, occur));
} }
void str_contains_str_whole(void **state)
{
char *main = "somestring";
char *occur = "somestring";
assert_true(str_contains_str(main, occur));
}
void str_empty_not_contains_str(void **state) void str_empty_not_contains_str(void **state)
{ {
char *main = NULL; char *main = NULL;

View File

@ -60,6 +60,7 @@ void str_not_contains_str(void **state);
void str_contains_str_at_start(void **state); void str_contains_str_at_start(void **state);
void str_contains_str_at_end(void **state); void str_contains_str_at_end(void **state);
void str_contains_str_in_middle(void **state); void str_contains_str_in_middle(void **state);
void str_contains_str_whole(void **state);
void str_empty_not_contains_str(void **state); void str_empty_not_contains_str(void **state);
void str_not_contains_str_empty(void **state); void str_not_contains_str_empty(void **state);
void str_empty_not_contains_str_empty(void **state); void str_empty_not_contains_str_empty(void **state);

View File

@ -99,6 +99,7 @@ int main(int argc, char* argv[]) {
unit_test(str_contains_str_at_start), unit_test(str_contains_str_at_start),
unit_test(str_contains_str_at_end), unit_test(str_contains_str_at_end),
unit_test(str_contains_str_in_middle), unit_test(str_contains_str_in_middle),
unit_test(str_contains_str_whole),
unit_test(str_empty_not_contains_str), unit_test(str_empty_not_contains_str),
unit_test(str_not_contains_str_empty), unit_test(str_not_contains_str_empty),
unit_test(str_empty_not_contains_str_empty), unit_test(str_empty_not_contains_str_empty),