mirror of
https://github.com/deltachat/deltachat-core.git
synced 2025-10-05 10:39:27 +02:00
Handle login parameters.
This commit is contained in:
parent
fe6c2298a1
commit
98b975086b
8 changed files with 259 additions and 50 deletions
|
@ -26,6 +26,7 @@
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "mrmailbox.h"
|
#include "mrmailbox.h"
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
#include "mrmailbox.h"
|
#include "mrmailbox.h"
|
||||||
#include "mrchat.h"
|
#include "mrchat.h"
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
#include <libetpan/libetpan.h>
|
#include <libetpan/libetpan.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
@ -33,17 +34,17 @@
|
||||||
#include "mrimap.h"
|
#include "mrimap.h"
|
||||||
|
|
||||||
|
|
||||||
static void check_error(int r, const char * msg)
|
static bool is_error(int r, const char* msg)
|
||||||
{
|
{
|
||||||
if (r == MAILIMAP_NO_ERROR)
|
if(r == MAILIMAP_NO_ERROR)
|
||||||
return;
|
return false;
|
||||||
if (r == MAILIMAP_NO_ERROR_AUTHENTICATED)
|
if(r == MAILIMAP_NO_ERROR_AUTHENTICATED)
|
||||||
return;
|
return false;
|
||||||
if (r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED)
|
if(r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED)
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
printf("%s\n", msg);
|
printf("ERROR: %s\n", msg);
|
||||||
exit(EXIT_FAILURE);
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char * get_msg_att_msg_content(struct mailimap_msg_att * msg_att, size_t * p_msg_size)
|
static char * get_msg_att_msg_content(struct mailimap_msg_att * msg_att, size_t * p_msg_size)
|
||||||
|
@ -107,7 +108,7 @@ static void fetch_msg(struct mailimap * imap, uint32_t uid)
|
||||||
clist * fetch_result;
|
clist * fetch_result;
|
||||||
struct stat stat_info;
|
struct stat stat_info;
|
||||||
|
|
||||||
snprintf(filename, sizeof(filename), "download/%u.eml", (unsigned int) uid);
|
snprintf(filename, sizeof(filename), "/home/bpetersen/temp/%u.eml", (unsigned int) uid);
|
||||||
r = stat(filename, &stat_info);
|
r = stat(filename, &stat_info);
|
||||||
if (r == 0) {
|
if (r == 0) {
|
||||||
// already cached
|
// already cached
|
||||||
|
@ -122,7 +123,9 @@ static void fetch_msg(struct mailimap * imap, uint32_t uid)
|
||||||
mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
|
mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
|
||||||
|
|
||||||
r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
|
r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
|
||||||
check_error(r, "could not fetch");
|
if( is_error(r, "could not fetch") ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
printf("fetch %u\n", (unsigned int) uid);
|
printf("fetch %u\n", (unsigned int) uid);
|
||||||
|
|
||||||
msg_content = get_msg_content(fetch_result, &msg_len);
|
msg_content = get_msg_content(fetch_result, &msg_len);
|
||||||
|
@ -188,7 +191,9 @@ static void fetch_messages(struct mailimap * imap)
|
||||||
mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
|
mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
|
||||||
|
|
||||||
r = mailimap_fetch(imap, set, fetch_type, &fetch_result);
|
r = mailimap_fetch(imap, set, fetch_type, &fetch_result);
|
||||||
check_error(r, "could not fetch");
|
if( is_error(r, "could not fetch") ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* for each message */
|
/* for each message */
|
||||||
for(cur = clist_begin(fetch_result) ; cur != NULL ; cur = clist_next(cur)) {
|
for(cur = clist_begin(fetch_result) ; cur != NULL ; cur = clist_next(cur)) {
|
||||||
|
@ -206,37 +211,6 @@ static void fetch_messages(struct mailimap * imap)
|
||||||
mailimap_fetch_list_free(fetch_result);
|
mailimap_fetch_list_free(fetch_result);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int local_main(const char* gmailaddr_, const char* password_)
|
|
||||||
{
|
|
||||||
struct mailimap * imap;
|
|
||||||
int r;
|
|
||||||
|
|
||||||
/*
|
|
||||||
./imap-sample mygmailaccount@gmail.com mygmailpassword
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
mkdir("download", 0700);
|
|
||||||
|
|
||||||
imap = mailimap_new(0, NULL);
|
|
||||||
r = mailimap_ssl_connect(imap, "imap.gmail.com", 993);
|
|
||||||
fprintf(stderr, "connect: %i\n", r);
|
|
||||||
check_error(r, "could not connect to server");
|
|
||||||
|
|
||||||
r = mailimap_login(imap, gmailaddr_, password_);
|
|
||||||
check_error(r, "could not login");
|
|
||||||
|
|
||||||
r = mailimap_select(imap, "INBOX");
|
|
||||||
check_error(r, "could not select INBOX");
|
|
||||||
|
|
||||||
fetch_messages(imap);
|
|
||||||
|
|
||||||
mailimap_logout(imap);
|
|
||||||
mailimap_free(imap);
|
|
||||||
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
MrImap::MrImap(MrMailbox* mailbox)
|
MrImap::MrImap(MrMailbox* mailbox)
|
||||||
{
|
{
|
||||||
|
@ -249,9 +223,37 @@ MrImap::~MrImap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MrImap::Connect()
|
bool MrImap::Connect(const MrLoginParam* param)
|
||||||
{
|
{
|
||||||
local_main("", "");
|
struct mailimap * imap;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
if( param->m_mail_server == NULL || param->m_mail_user == NULL || param->m_mail_pw == NULL ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
imap = mailimap_new(0, NULL);
|
||||||
|
r = mailimap_ssl_connect(imap, param->m_mail_server, param->m_mail_port);
|
||||||
|
if( is_error(r, "could not connect to server") ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
r = mailimap_login(imap, param->m_mail_user, param->m_mail_pw);
|
||||||
|
if( is_error(r, "could not login") ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
r = mailimap_select(imap, "INBOX");
|
||||||
|
if( is_error(r, "could not select INBOX") ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch_messages(imap);
|
||||||
|
|
||||||
|
mailimap_logout(imap);
|
||||||
|
mailimap_free(imap);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
#define __MRIMAP_H__
|
#define __MRIMAP_H__
|
||||||
|
|
||||||
|
|
||||||
|
#include "mrloginparam.h"
|
||||||
class MrMailbox;
|
class MrMailbox;
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,7 +39,7 @@ class MrImap
|
||||||
public:
|
public:
|
||||||
MrImap (MrMailbox* mailbox);
|
MrImap (MrMailbox* mailbox);
|
||||||
~MrImap ();
|
~MrImap ();
|
||||||
void Connect ();
|
bool Connect (const MrLoginParam*);
|
||||||
void Disconnect ();
|
void Disconnect ();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
124
src/mrloginparam.cpp
Normal file
124
src/mrloginparam.cpp
Normal file
|
@ -0,0 +1,124 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
*
|
||||||
|
* Messenger Backend
|
||||||
|
* Copyright (C) 2016 Björn Petersen Software Design and Development
|
||||||
|
* Contact: r10s@b44t.com, http://b44t.com
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify it under
|
||||||
|
* the terms of the GNU General Public License as published by the Free Software
|
||||||
|
* Foundation, either version 3 of the License, or (at your option) any later
|
||||||
|
* version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
* details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along with
|
||||||
|
* this program. If not, see http://www.gnu.org/licenses/ .
|
||||||
|
*
|
||||||
|
*******************************************************************************
|
||||||
|
*
|
||||||
|
* File: mrloginparam.h
|
||||||
|
* Authors: Björn Petersen
|
||||||
|
* Purpose: Handle IMAP parameters, see header for details.
|
||||||
|
*
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "mrmailbox.h"
|
||||||
|
#include "mrloginparam.h"
|
||||||
|
|
||||||
|
|
||||||
|
MrLoginParam::MrLoginParam(MrMailbox* mailbox)
|
||||||
|
{
|
||||||
|
m_mailbox = mailbox;
|
||||||
|
|
||||||
|
// init pointers (this cannot be done by Clear() as this function checks against NULL pointers)
|
||||||
|
m_email = NULL;
|
||||||
|
m_mail_server = NULL;
|
||||||
|
m_mail_user = NULL;
|
||||||
|
m_mail_pw = NULL;
|
||||||
|
m_mail_port = 0;
|
||||||
|
m_send_server = NULL;
|
||||||
|
m_send_user = NULL;
|
||||||
|
m_send_pw = NULL;
|
||||||
|
m_send_port = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
MrLoginParam::~MrLoginParam()
|
||||||
|
{
|
||||||
|
Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MrLoginParam::Clear()
|
||||||
|
{
|
||||||
|
#define FREE_(a) if((a)) { free((a)); (a) = NULL; }
|
||||||
|
FREE_(m_email)
|
||||||
|
FREE_(m_mail_server)
|
||||||
|
FREE_(m_mail_user)
|
||||||
|
FREE_(m_mail_pw)
|
||||||
|
m_mail_port = 0;
|
||||||
|
FREE_(m_send_server)
|
||||||
|
FREE_(m_send_user)
|
||||||
|
FREE_(m_send_pw)
|
||||||
|
m_send_port = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MrLoginParam::ReadFromSql()
|
||||||
|
{
|
||||||
|
Clear();
|
||||||
|
m_email = m_mailbox->GetConfig ("email", NULL);
|
||||||
|
m_mail_server = m_mailbox->GetConfig ("mail_server", NULL);
|
||||||
|
m_mail_port = m_mailbox->GetConfigInt("mail_port", 0);
|
||||||
|
m_mail_user = m_mailbox->GetConfig ("mail_user", NULL);
|
||||||
|
m_mail_pw = m_mailbox->GetConfig ("mail_pw", NULL);
|
||||||
|
m_send_server = m_mailbox->GetConfig ("send_server", NULL);
|
||||||
|
m_send_port = m_mailbox->GetConfigInt("send_port", 0);
|
||||||
|
m_send_user = m_mailbox->GetConfig ("send_user", NULL);
|
||||||
|
m_send_pw = m_mailbox->GetConfig ("send_pw", NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MrLoginParam::Complete()
|
||||||
|
{
|
||||||
|
if( m_email == NULL ) {
|
||||||
|
return; // nothing we can do
|
||||||
|
}
|
||||||
|
|
||||||
|
char* adr_server = strstr(m_email, "@");
|
||||||
|
if( adr_server == NULL ) {
|
||||||
|
return; // no "@" found in address, normally, this should not happen
|
||||||
|
}
|
||||||
|
adr_server++;
|
||||||
|
|
||||||
|
// set servers, ports etc. for well-known and frequently used services
|
||||||
|
if( strcmp(adr_server, "gmail.com")==0
|
||||||
|
|| strcmp(adr_server, "googlemail.com")==0 )
|
||||||
|
{
|
||||||
|
// GOOGLE
|
||||||
|
if( m_mail_server == NULL ) { m_mail_server = strdup("imap.gmail.com"); }
|
||||||
|
if( m_mail_port == 0 ) { m_mail_port = 993; } // IMAPS
|
||||||
|
if( m_mail_user == NULL ) { m_mail_user = strdup(m_email); }
|
||||||
|
|
||||||
|
if( m_send_server == NULL ) { m_send_server = strdup("smtp.gmail.com"); }
|
||||||
|
if( m_send_port == 0 ) { m_send_port = 465; } // SSMTP - difference between 465 and 587: http://stackoverflow.com/questions/15796530/what-is-the-difference-between-ports-465-and-587
|
||||||
|
if( m_send_user == NULL ) { m_send_user = strdup(m_email); }
|
||||||
|
if( m_send_pw == NULL && m_mail_pw ) { m_send_pw = strdup(m_mail_pw); }
|
||||||
|
}
|
||||||
|
|
||||||
|
// generic approach
|
||||||
|
if( m_mail_port == 0 ) { m_mail_port = 993; }
|
||||||
|
if( m_mail_user == NULL ) { m_mail_user = strdup(m_email); }
|
||||||
|
if( m_send_port == 0 ) { m_send_port = 465; }
|
||||||
|
if( m_send_user == NULL ) { m_send_user = strdup(m_email); }
|
||||||
|
if( m_send_pw == NULL && m_mail_pw ) { m_send_pw = strdup(m_mail_pw); }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
65
src/mrloginparam.h
Normal file
65
src/mrloginparam.h
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
*
|
||||||
|
* Messenger Backend
|
||||||
|
* Copyright (C) 2016 Björn Petersen Software Design and Development
|
||||||
|
* Contact: r10s@b44t.com, http://b44t.com
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify it under
|
||||||
|
* the terms of the GNU General Public License as published by the Free Software
|
||||||
|
* Foundation, either version 3 of the License, or (at your option) any later
|
||||||
|
* version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
* details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along with
|
||||||
|
* this program. If not, see http://www.gnu.org/licenses/ .
|
||||||
|
*
|
||||||
|
*******************************************************************************
|
||||||
|
*
|
||||||
|
* File: mrloginparam.h
|
||||||
|
* Authors: Björn Petersen
|
||||||
|
* Purpose: Handle IMAP parameters
|
||||||
|
*
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef __MRLOGINPARAM_H__
|
||||||
|
#define __MRLOGINPARAM_H__
|
||||||
|
|
||||||
|
|
||||||
|
class MrMailbox;
|
||||||
|
|
||||||
|
|
||||||
|
class MrLoginParam
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MrLoginParam (MrMailbox* mailbox);
|
||||||
|
~MrLoginParam ();
|
||||||
|
|
||||||
|
void Clear ();
|
||||||
|
void ReadFromSql ();
|
||||||
|
void Complete ();
|
||||||
|
|
||||||
|
// IMAP/POP3
|
||||||
|
char* m_email;
|
||||||
|
char* m_mail_server;
|
||||||
|
char* m_mail_user;
|
||||||
|
char* m_mail_pw;
|
||||||
|
uint16_t m_mail_port;
|
||||||
|
|
||||||
|
// SMTP
|
||||||
|
char* m_send_server;
|
||||||
|
char* m_send_user;
|
||||||
|
char* m_send_pw;
|
||||||
|
uint16_t m_send_port;
|
||||||
|
|
||||||
|
private:
|
||||||
|
MrMailbox* m_mailbox;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif // __MRLOGINPARAM_H__
|
||||||
|
|
|
@ -26,9 +26,8 @@
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <libetpan/libetpan.h>
|
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#include "mrmailbox.h"
|
#include "mrmailbox.h"
|
||||||
|
|
||||||
|
@ -95,7 +94,12 @@ void MrMailbox::Close()
|
||||||
|
|
||||||
void MrMailbox::Connect()
|
void MrMailbox::Connect()
|
||||||
{
|
{
|
||||||
m_imap.Connect();
|
MrLoginParam param(this);
|
||||||
|
|
||||||
|
param.ReadFromSql();
|
||||||
|
param.Complete();
|
||||||
|
|
||||||
|
m_imap.Connect(¶m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -216,3 +220,13 @@ char* MrMailbox::GetConfig(const char* key, const char* def) // the returned str
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int32_t MrMailbox::GetConfigInt(const char* key, int32_t def)
|
||||||
|
{
|
||||||
|
char* str = GetConfig(key, NULL);
|
||||||
|
if( str == NULL ) {
|
||||||
|
return def;
|
||||||
|
}
|
||||||
|
return atol(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@
|
||||||
#define __MRMAILBOX_H__
|
#define __MRMAILBOX_H__
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h> // eg. for size_t
|
#include <libetpan/libetpan.h> // defines uint16_t etc.
|
||||||
#include "mrsqlite3.h"
|
#include "mrsqlite3.h"
|
||||||
#include "mrchat.h"
|
#include "mrchat.h"
|
||||||
#include "mrcontact.h"
|
#include "mrcontact.h"
|
||||||
|
@ -73,6 +73,7 @@ public:
|
||||||
// handle configurations
|
// handle configurations
|
||||||
bool SetConfig (const char* key, const char* value);
|
bool SetConfig (const char* key, const char* value);
|
||||||
char* GetConfig (const char* key, const char* def); // the returned string must be free()'d, returns NULL on errors
|
char* GetConfig (const char* key, const char* def); // the returned string must be free()'d, returns NULL on errors
|
||||||
|
int32_t GetConfigInt (const char* key, int32_t def);
|
||||||
|
|
||||||
// misc
|
// misc
|
||||||
char* GetDbFile () { return m_sql.GetDbFile(); } // the returned string must be free()'d, returns NULL on errors or if no database is open
|
char* GetDbFile () { return m_sql.GetDbFile(); } // the returned string must be free()'d, returns NULL on errors or if no database is open
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue