1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-06 12:00:11 +02:00

move connect/disconnect thread creation to cli program

This commit is contained in:
B. Petersen 2018-05-31 22:07:49 +02:00
parent d4731eca19
commit e94296f7f2
3 changed files with 84 additions and 30 deletions

View file

@ -32,6 +32,7 @@ your library */
#include "../src/mrpgp.h"
/*
* Reset database tables. This function is called from Core cmdline.
*
@ -451,8 +452,8 @@ char* mrmailbox_cmdline(mrmailbox_t* mailbox, const char* cmdline)
"set <configuration-key> [<value>]\n"
"get <configuration-key>\n"
"configure\n"
"idle\n"
"interruptidle\n"
"connect\n"
"disconnect\n"
"poll\n"
"help imex (Import/Export)\n"
"==============================Chat commands==\n"
@ -680,14 +681,6 @@ char* mrmailbox_cmdline(mrmailbox_t* mailbox, const char* cmdline)
{
ret = mrmailbox_configure(mailbox)? COMMAND_SUCCEEDED : COMMAND_FAILED;
}
else if( strcmp(cmd, "idle")==0 )
{
ret = mrmailbox_idle(mailbox)? COMMAND_SUCCEEDED : COMMAND_FAILED;
}
else if( strcmp(cmd, "interruptidle")==0 )
{
ret = mrmailbox_interrupt_idle(mailbox)? COMMAND_SUCCEEDED : COMMAND_FAILED;
}
else if( strcmp(cmd, "poll")==0 )
{
ret = mrmailbox_poll(mailbox)? COMMAND_SUCCEEDED : COMMAND_FAILED;

View file

@ -35,28 +35,15 @@ all further options can be set using the set-command (type ? for help). */
#include "stress.h"
#define ANSI_RED "\e[31m"
#define ANSI_YELLOW "\e[33m"
#define ANSI_NORMAL "\e[0m"
static char* read_cmd(void)
{
printf("> ");
static char cmdbuffer[1024];
fgets(cmdbuffer, 1000, stdin);
while( strlen(cmdbuffer)>0
&& (cmdbuffer[strlen(cmdbuffer)-1]=='\n' || cmdbuffer[strlen(cmdbuffer)-1]==' ') )
{
cmdbuffer[strlen(cmdbuffer)-1] = '\0';
}
return cmdbuffer;
}
/*******************************************************************************
* Event Handler
******************************************************************************/
static int s_do_log_info = 1;
#define ANSI_RED "\e[31m"
#define ANSI_YELLOW "\e[33m"
#define ANSI_NORMAL "\e[0m"
static uintptr_t receive_event(mrmailbox_t* mailbox, int event, uintptr_t data1, uintptr_t data2)
@ -132,6 +119,65 @@ static uintptr_t receive_event(mrmailbox_t* mailbox, int event, uintptr_t data1,
}
/*******************************************************************************
* The idle thread - wait for push messages
******************************************************************************/
pthread_t idle_thread;
int idle_thread_started = 0;
static void* idle_thread_entry_point(void* entry_arg)
{
mrmailbox_t* mailbox = (mrmailbox_t*)entry_arg;
mrmailbox_idle(mailbox); // this may take hours ...
return NULL;
}
static void idle_connect(mrmailbox_t* mailbox)
{
if( !idle_thread_started )
{
idle_thread_started = 1;
pthread_create(&idle_thread, NULL, idle_thread_entry_point, mailbox);
}
}
static void idle_disconnect(mrmailbox_t* mailbox)
{
if( idle_thread_started )
{
mrmailbox_interrupt_idle(mailbox);
pthread_join(idle_thread, NULL);
idle_thread_started = 0;
}
}
/*******************************************************************************
* The main loop
******************************************************************************/
static char* read_cmd(void)
{
printf("> ");
static char cmdbuffer[1024];
fgets(cmdbuffer, 1000, stdin);
while( strlen(cmdbuffer)>0
&& (cmdbuffer[strlen(cmdbuffer)-1]=='\n' || cmdbuffer[strlen(cmdbuffer)-1]==' ') )
{
cmdbuffer[strlen(cmdbuffer)-1] = '\0';
}
return cmdbuffer;
}
int main(int argc, char ** argv)
{
char* cmd = NULL;
@ -166,13 +212,22 @@ int main(int argc, char ** argv)
char* arg1 = strchr(cmd, ' ');
if( arg1 ) { *arg1 = 0; arg1++; }
if( strcmp(cmd, "clear")==0 )
if( strcmp(cmd, "connect")==0 )
{
idle_connect(mailbox);
}
else if( strcmp(cmd, "disconnect")==0 )
{
idle_disconnect(mailbox);
}
else if( strcmp(cmd, "clear")==0 )
{
printf("\n\n\n\n"); /* insert some blank lines to visualize the break in the buffer */
printf("\e[1;1H\e[2J"); /* should work on ANSI terminals and on Windows 10. If not, well, then not. */
}
else if( strcmp(cmd, "getqr")==0 || strcmp(cmd, "getbadqr")==0 )
{
idle_connect(mailbox);
char* qrstr = mrmailbox_get_securejoin_qr(mailbox, arg1? atoi(arg1) : 0);
if( qrstr && qrstr[0] ) {
if( strcmp(cmd, "getbadqr")==0 && strlen(qrstr)>40 ) {
@ -204,6 +259,7 @@ int main(int argc, char ** argv)
}
free(cmd);
idle_disconnect(mailbox);
mrmailbox_close(mailbox);
mrmailbox_unref(mailbox);
mailbox = NULL;

View file

@ -893,6 +893,10 @@ void mrimap_watch_n_wait(mrimap_t* ths)
time_t last_fullread_time = 0;
if( ths->m_watch_thread_running ) {
goto exit_;
}
ths->m_watch_thread_running = 1;
mrmailbox_log_info(ths->m_mailbox, 0, "IMAP-watch-thread started.");
@ -1090,6 +1094,7 @@ exit_:
UNBLOCK_IDLE
ths->m_watch_thread_running = 0;
ths->m_watch_do_exit = 0;
}