diff --git a/python/conftest.py b/python/conftest.py index 83edbcc1..a2ef2205 100644 --- a/python/conftest.py +++ b/python/conftest.py @@ -34,6 +34,7 @@ def acfactory(pytestconfig, tmpdir, request): configdict = self.configlist.pop(0) tmpdb = tmpdir.join("testdb%d" % self.count) ac = Account(tmpdb.strpath) + ac._evlogger.set_timeout(10) ac.set_config(**configdict) if started: ac.start() diff --git a/python/setup.cfg b/python/setup.cfg new file mode 100644 index 00000000..5326e01e --- /dev/null +++ b/python/setup.cfg @@ -0,0 +1,2 @@ +[devpi:upload] +formats = sdist.tgz,bdist_wheel diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index 84817e42..cff6722e 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -42,21 +42,26 @@ class EventLogger: self._event_queue = Queue() self._debug = debug self._ctxinfo = str(self.dc_context).strip(">").split()[-1] + self._timeout = None def __call__(self, evt_name, data1, data2): self._log_event(evt_name, data1, data2) self._event_queue.put((evt_name, data1, data2)) + def set_timeout(self, timeout): + self._timeout = timeout + def get(self, timeout=None, check_error=True): + timeout = timeout or self._timeout ev = self._event_queue.get(timeout=timeout) if check_error: assert ev[0] != "DC_EVENT_ERROR" return ev - def get_matching(self, event_name_regex, timeout=None): - rex = re.compile(event_name_regex + ".*") + def get_matching(self, event_name_regex): + rex = re.compile("(?:{}).*".format(event_name_regex)) while 1: - ev = self.get(timeout=timeout) + ev = self.get() if rex.match(ev[0]): return ev diff --git a/python/tests/test_account.py b/python/tests/test_account.py index ed8dda0f..37de687e 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -30,10 +30,10 @@ class TestLive: imap_ok = smtp_ok = False while not imap_ok or not smtp_ok: evt_name, data1, data2 = \ - ac1._evlogger.get_matching("DC_EVENT_INFO", timeout=5) - if re.match("imap-login.*ok.", data2.lower()): + ac1._evlogger.get_matching("DC_EVENT_(IMAP|SMTP)_CONNECTED") + if evt_name == "DC_EVENT_IMAP_CONNECTED": imap_ok = True - if re.match("smtp-login.*ok.", data2.lower()): + if evt_name == "DC_EVENT_SMTP_CONNECTED": smtp_ok = True assert ac1.get_config("mail_pw") diff --git a/src/dc_context.h b/src/dc_context.h index 572859d7..233e805c 100644 --- a/src/dc_context.h +++ b/src/dc_context.h @@ -122,6 +122,7 @@ void dc_log_error (dc_context_t*, int code, const char* msg, void dc_log_error_if (int* condition, dc_context_t*, int code, const char* msg, ...); void dc_log_warning (dc_context_t*, int code, const char* msg, ...); void dc_log_info (dc_context_t*, int code, const char* msg, ...); +void dc_log_event (dc_context_t* context, int event_code, int code, const char* msg, ...); void dc_receive_imf (dc_context_t*, const char* imf_raw_not_terminated, size_t imf_raw_bytes, const char* server_folder, uint32_t server_uid, uint32_t flags); #define DC_BAK_PREFIX "delta-chat" diff --git a/src/dc_imap.c b/src/dc_imap.c index 4f0c0b1f..bc9c4dc1 100644 --- a/src/dc_imap.c +++ b/src/dc_imap.c @@ -1153,7 +1153,8 @@ static int setup_handle_if_needed(dc_imap_t* imap) goto cleanup; } - dc_log_info(imap->context, 0, "IMAP-login as %s ok.", imap->imap_user); + dc_log_event(imap->context, DC_EVENT_IMAP_CONNECTED, 0, + "IMAP-login as %s ok.", imap->imap_user); success = 1; diff --git a/src/dc_log.c b/src/dc_log.c index e5eb1d98..d1e77a83 100644 --- a/src/dc_log.c +++ b/src/dc_log.c @@ -87,13 +87,18 @@ void dc_log_info(dc_context_t* context, int code, const char* msg, ...) va_end(va); } +void dc_log_event(dc_context_t* context, int event_code, int code, const char* msg, ...) +{ + va_list va; + va_start(va, msg); /* va_start() expects the last non-variable argument as the second parameter */ + log_vprintf(context, event_code, code, msg, va); + va_end(va); +} + void dc_log_warning(dc_context_t* context, int code, const char* msg, ...) { - va_list va; - va_start(va, msg); - log_vprintf(context, DC_EVENT_WARNING, code, msg, va); - va_end(va); + dc_log_event(context, DC_EVENT_WARNING, code, msg); } diff --git a/src/dc_smtp.c b/src/dc_smtp.c index 1553fdb9..81c0a492 100644 --- a/src/dc_smtp.c +++ b/src/dc_smtp.c @@ -219,7 +219,8 @@ int dc_smtp_connect(dc_smtp_t* smtp, const dc_loginparam_t* lp) } } - dc_log_info(smtp->context, 0, "SMTP-login as %s ok.", lp->send_user); + dc_log_event(smtp->context, DC_EVENT_SMTP_CONNECTED, 0, + "SMTP-login as %s ok.", lp->send_user); } success = 1; diff --git a/src/deltachat.h b/src/deltachat.h index ccd30986..73e35b76 100644 --- a/src/deltachat.h +++ b/src/deltachat.h @@ -764,6 +764,28 @@ time_t dc_lot_get_timestamp (const dc_lot_t*); #define DC_EVENT_INFO 100 +/** + * Emitted when SMTP connection is established and login was successful. + * + * @param data1 0 + * @param data2 (const char*) Info string in english language. + * Must not be free()'d or modified and is valid only until the callback returns. + * @return 0 + */ +#define DC_EVENT_SMTP_CONNECTED 101 + + +/** + * Emitted when IMAP connection is established and login was successful. + * + * @param data1 0 + * @param data2 (const char*) Info string in english language. + * Must not be free()'d or modified and is valid only until the callback returns. + * @return 0 + */ +#define DC_EVENT_IMAP_CONNECTED 102 + + /** * The library-user should write a warning string to the log. * Passed to the callback given to dc_context_new(). @@ -1031,7 +1053,7 @@ time_t dc_lot_get_timestamp (const dc_lot_t*); */ #define DC_EVENT_DATA1_IS_STRING(e) ((e)==DC_EVENT_HTTP_GET || (e)==DC_EVENT_IMEX_FILE_WRITTEN || (e)==DC_EVENT_FILE_COPIED) -#define DC_EVENT_DATA2_IS_STRING(e) ((e)==DC_EVENT_INFO || (e)==DC_EVENT_WARNING || (e)==DC_EVENT_ERROR) +#define DC_EVENT_DATA2_IS_STRING(e) ((e)==DC_EVENT_INFO || (e) == DC_EVENT_WARNING || (e) == DC_EVENT_ERROR || (e) == DC_EVENT_SMTP_CONNECTED || (e) == DC_EVENT_IMAP_CONNECTED) #define DC_EVENT_RETURNS_INT(e) ((e)==DC_EVENT_IS_OFFLINE) #define DC_EVENT_RETURNS_STRING(e) ((e)==DC_EVENT_GET_QUANTITY_STRING || (e)==DC_EVENT_GET_STRING || (e)==DC_EVENT_HTTP_GET)