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

Merge branch 'master' into osx-functional

This commit is contained in:
James Booth 2015-08-27 00:42:27 +01:00
commit d7b56a468d
9 changed files with 163 additions and 78 deletions

View File

@ -192,6 +192,12 @@ str_replace(const char *string, const char *substr,
return newstr;
}
gboolean
str_contains_str(const char * const searchstr, const char * const substr)
{
return g_strrstr(searchstr, substr) != NULL;
}
int
str_contains(const char str[], int size, char ch)
{

View File

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

View File

@ -191,8 +191,55 @@ sv_ev_incoming_carbon(char *barejid, char *resource, char *message)
chat_log_msg_in(barejid, message);
}
#ifdef HAVE_LIBGPGME
static void
_sv_ev_incoming_pgp(ProfChatWin *chatwin, gboolean new_win, char *barejid, char *resource, char *message, char *pgp_message)
{
char *decrypted = p_gpg_decrypt(pgp_message);
if (decrypted) {
if (chatwin->enc_mode == PROF_ENC_NONE) {
win_println((ProfWin*)chatwin, 0, "PGP encryption enabled.");
}
ui_incoming_msg(chatwin, resource, decrypted, NULL, new_win, PROF_ENC_PGP);
chat_log_pgp_msg_in(barejid, decrypted);
chatwin->enc_mode = PROF_ENC_PGP;
p_gpg_free_decrypted(decrypted);
} else {
ui_incoming_msg(chatwin, resource, message, NULL, new_win, PROF_ENC_NONE);
chat_log_msg_in(barejid, message);
chatwin->enc_mode = PROF_ENC_NONE;
}
}
#endif
#ifdef HAVE_LIBOTR
static void
_sv_ev_incoming_otr(ProfChatWin *chatwin, gboolean new_win, char *barejid, char *resource, char *message)
{
gboolean decrypted = FALSE;
char *otr_res = otr_on_message_recv(barejid, resource, message, &decrypted);
if (otr_res) {
if (decrypted) {
ui_incoming_msg(chatwin, resource, otr_res, NULL, new_win, PROF_ENC_OTR);
} else {
ui_incoming_msg(chatwin, resource, otr_res, NULL, new_win, PROF_ENC_NONE);
}
chat_log_otr_msg_in(barejid, otr_res, decrypted);
otr_free_message(otr_res);
}
}
#endif
static void
_sv_ev_incoming_plain(ProfChatWin *chatwin, gboolean new_win, char *barejid, char *resource, char *message)
{
ui_incoming_msg(chatwin, resource, message, NULL, new_win, PROF_ENC_NONE);
chat_log_msg_in(barejid, message);
chatwin->enc_mode = PROF_ENC_NONE;
}
void
sv_ev_incoming_message(char *barejid, char *resource, char *message, char *enc_message)
sv_ev_incoming_message(char *barejid, char *resource, char *message, char *pgp_message)
{
gboolean new_win = FALSE;
ProfChatWin *chatwin = wins_get_chat(barejid);
@ -205,44 +252,18 @@ sv_ev_incoming_message(char *barejid, char *resource, char *message, char *enc_m
// OTR suported, PGP supported
#ifdef HAVE_LIBOTR
#ifdef HAVE_LIBGPGME
prof_enc_t enc_mode = chatwin->enc_mode;
if (enc_message) {
if (enc_mode == PROF_ENC_OTR) {
if (pgp_message) {
if (chatwin->enc_mode == PROF_ENC_OTR) {
win_println((ProfWin*)chatwin, 0, "PGP encrypted message received whilst in OTR session.");
} else { // PROF_ENC_NONE, PROF_ENC_PGP
char *decrypted = p_gpg_decrypt(enc_message);
if (decrypted) {
if (enc_mode == PROF_ENC_NONE) {
win_println((ProfWin*)chatwin, 0, "PGP encryption enabled.");
}
ui_incoming_msg(chatwin, resource, decrypted, NULL, new_win, PROF_ENC_PGP);
chat_log_pgp_msg_in(barejid, decrypted);
chatwin->enc_mode = PROF_ENC_PGP;
p_gpg_free_decrypted(decrypted);
} else {
ui_incoming_msg(chatwin, resource, message, NULL, new_win, PROF_ENC_NONE);
chat_log_msg_in(barejid, message);
chatwin->enc_mode = PROF_ENC_NONE;
}
_sv_ev_incoming_pgp(chatwin, new_win, barejid, resource, message, pgp_message);
}
} else {
if (enc_mode == PROF_ENC_PGP) {
if (chatwin->enc_mode == PROF_ENC_PGP) {
win_println((ProfWin*)chatwin, 0, "PGP encryption disabled.");
ui_incoming_msg(chatwin, resource, message, NULL, new_win, PROF_ENC_NONE);
chat_log_msg_in(barejid, message);
chatwin->enc_mode = PROF_ENC_NONE;
_sv_ev_incoming_plain(chatwin, new_win, barejid, resource, message);
} else {
gboolean decrypted = FALSE;
char *otr_res = otr_on_message_recv(barejid, resource, message, &decrypted);
if (otr_res) {
if (decrypted && g_strrstr(otr_res, message) == NULL) {
ui_incoming_msg(chatwin, resource, otr_res, NULL, new_win, PROF_ENC_OTR);
} else {
ui_incoming_msg(chatwin, resource, otr_res, NULL, new_win, PROF_ENC_NONE);
}
chat_log_otr_msg_in(barejid, otr_res, decrypted);
otr_free_message(otr_res);
}
_sv_ev_incoming_otr(chatwin, new_win, barejid, resource, message);
}
}
return;
@ -252,17 +273,7 @@ sv_ev_incoming_message(char *barejid, char *resource, char *message, char *enc_m
// OTR supported, PGP unsupported
#ifdef HAVE_LIBOTR
#ifndef HAVE_LIBGPGME
gboolean decrypted = FALSE;
char *otr_res = otr_on_message_recv(barejid, resource, message, &decrypted);
if (otr_res) {
if (decrypted && g_strrstr(otr_res, message) == NULL) {
ui_incoming_msg(chatwin, resource, otr_res, NULL, new_win, PROF_ENC_OTR);
} else {
ui_incoming_msg(chatwin, resource, otr_res, NULL, new_win, PROF_ENC_NONE);
}
chat_log_otr_msg_in(barejid, otr_res, decrypted);
otr_free_message(otr_res);
}
_sv_ev_incoming_otr(chatwin, new_win, barejid, resource, message);
return;
#endif
#endif
@ -270,22 +281,10 @@ sv_ev_incoming_message(char *barejid, char *resource, char *message, char *enc_m
// OTR unsupported, PGP supported
#ifndef HAVE_LIBOTR
#ifdef HAVE_LIBGPGME
if (enc_message) {
char *decrypted = p_gpg_decrypt(enc_message);
if (decrypted) {
ui_incoming_msg(chatwin, resource, decrypted, NULL, new_win, PROF_ENC_PGP);
chat_log_pgp_msg_in(barejid, decrypted);
chatwin->enc_mode = PROF_ENC_PGP;
p_gpg_free_decrypted(decrypted);
} else {
ui_incoming_msg(chatwin, resource, message, NULL, new_win, PROF_ENC_NONE);
chat_log_msg_in(barejid, message);
chatwin->enc_mode = PROF_ENC_NONE;
}
if (pgp_message) {
_sv_ev_incoming_pgp(chatwin, new_win, barejid, resource, message, pgp_message);
} else {
ui_incoming_msg(chatwin, resource, message, NULL, new_win, PROF_ENC_NONE);
chat_log_msg_in(barejid, message);
chatwin->enc_mode = PROF_ENC_NONE;
_sv_ev_incoming_plain(chatwin, new_win, barejid, resource, message);
}
return;
#endif
@ -294,9 +293,7 @@ sv_ev_incoming_message(char *barejid, char *resource, char *message, char *enc_m
// OTR unsupported, PGP unsupported
#ifndef HAVE_LIBOTR
#ifndef HAVE_LIBGPGME
ui_incoming_msg(chatwin, resource, message, NULL, new_win, PROF_ENC_NONE);
chat_log_msg_in(barejid, message);
chatwin->enc_mode = PROF_ENC_NONE;
_sv_ev_incoming_plain(chatwin, new_win, barejid, resource, message);
return;
#endif
#endif

View File

@ -50,7 +50,7 @@ void sv_ev_room_history(const char * const room_jid, const char * const nick,
GDateTime *timestamp, const char * const message);
void sv_ev_room_message(const char * const room_jid, const char * const nick,
const char * const message);
void sv_ev_incoming_message(char *barejid, char *resource, char *message, char *enc_message);
void sv_ev_incoming_message(char *barejid, char *resource, char *message, char *pgp_message);
void sv_ev_incoming_private_message(const char * const fulljid, char *message);
void sv_ev_delayed_message(char *fulljid, char *message, GDateTime *timestamp);
void sv_ev_delayed_private_message(const char * const fulljid, char *message, GDateTime *timestamp);

View File

@ -274,7 +274,7 @@ 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)
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);
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);
if (!decrypted) { // internal OTR message
char *newmessage = otr_decrypt_message(barejid, message, decrypted);
if (!newmessage) { // internal OTR message
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();
cons_show("Attempting to start OTR session...");
char *id = message_send_chat_otr(barejid, otr_query_message);
free(id);
}
return decrypted;
return newmessage;
}
gboolean
@ -717,12 +717,12 @@ _otr_tlv_free(OtrlTLV *tlvs)
}
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;
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
if (result == 1) {
@ -743,16 +743,18 @@ otr_decrypt_message(const char * const from, const char * const message, gboolea
return NULL;
// message was decrypted, return to user
} else if (decrypted) {
// message was processed, return to user
} else if (newmessage) {
_otr_tlv_free(tlvs);
*was_decrypted = TRUE;
return decrypted;
if (g_str_has_prefix(message, "?OTR:")) {
*decrypted = TRUE;
}
return newmessage;
// normal non OTR message
} else {
_otr_tlv_free(tlvs);
*was_decrypted = FALSE;
*decrypted = FALSE;
return strdup(message);
}
}

View File

@ -58,7 +58,7 @@ char* otr_start_query(void);
void otr_poll(void);
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);
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_decrypt_message(const char * const from, const char * const message,
gboolean *was_decrypted);
gboolean *decrypted);
void otr_free_message(char *message);

View File

@ -631,3 +631,66 @@ void strip_quotes_strips_both(void **state)
free(result);
}
void str_not_contains_str(void **state)
{
char *main = "somestring";
char *occur = "not";
assert_false(str_contains_str(main, occur));
}
void str_contains_str_at_start(void **state)
{
char *main = "somestring";
char *occur = "some";
assert_true(str_contains_str(main, occur));
}
void str_contains_str_at_end(void **state)
{
char *main = "somestring";
char *occur = "string";
assert_true(str_contains_str(main, occur));
}
void str_contains_str_in_middle(void **state)
{
char *main = "somestring";
char *occur = "str";
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)
{
char *main = NULL;
char *occur = "str";
assert_false(str_contains_str(main, occur));
}
void str_not_contains_str_empty(void **state)
{
char *main = "somestring";
char *occur = NULL;
assert_false(str_contains_str(main, occur));
}
void str_empty_not_contains_str_empty(void **state)
{
char *main = NULL;
char *occur = NULL;
assert_false(str_contains_str(main, occur));
}

View File

@ -56,3 +56,11 @@ void strip_quotes_does_nothing_when_no_quoted(void **state);
void strip_quotes_strips_first(void **state);
void strip_quotes_strips_last(void **state);
void strip_quotes_strips_both(void **state);
void str_not_contains_str(void **state);
void str_contains_str_at_start(void **state);
void str_contains_str_at_end(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_not_contains_str_empty(void **state);
void str_empty_not_contains_str_empty(void **state);

View File

@ -95,6 +95,14 @@ int main(int argc, char* argv[]) {
unit_test(strip_quotes_strips_first),
unit_test(strip_quotes_strips_last),
unit_test(strip_quotes_strips_both),
unit_test(str_not_contains_str),
unit_test(str_contains_str_at_start),
unit_test(str_contains_str_at_end),
unit_test(str_contains_str_in_middle),
unit_test(str_contains_str_whole),
unit_test(str_empty_not_contains_str),
unit_test(str_not_contains_str_empty),
unit_test(str_empty_not_contains_str_empty),
unit_test(clear_empty),
unit_test(reset_after_create),