Delta Chat Core C-Library
mrloginparam.c
1 /*******************************************************************************
2  *
3  * Delta Chat Core
4  * Copyright (C) 2017 Björn Petersen
5  * Contact: r10s@b44t.com, http://b44t.com
6  *
7  * This program is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later
10  * version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program. If not, see http://www.gnu.org/licenses/ .
19  *
20  ******************************************************************************/
21 
22 
23 #include "mrmailbox_internal.h"
24 #include "mrloginparam.h"
25 
26 
27 /*******************************************************************************
28  * Main interface
29  ******************************************************************************/
30 
31 
32 mrloginparam_t* mrloginparam_new()
33 {
34  mrloginparam_t* ths = NULL;
35 
36  if( (ths=calloc(1, sizeof(mrloginparam_t)))==NULL ) {
37  exit(22); /* cannot allocate little memory, unrecoverable error */
38  }
39 
40  return ths;
41 }
42 
43 
44 void mrloginparam_unref(mrloginparam_t* ths)
45 {
46  if( ths==NULL ) {
47  return;
48  }
49 
50  mrloginparam_empty(ths);
51  free(ths);
52 }
53 
54 
55 void mrloginparam_empty(mrloginparam_t* ths)
56 {
57  if( ths == NULL ) {
58  return; /* ok, but nothing to do */
59  }
60 
61  free(ths->m_addr); ths->m_addr = NULL;
62  free(ths->m_mail_server); ths->m_mail_server = NULL;
63  ths->m_mail_port = 0;
64  free(ths->m_mail_user); ths->m_mail_user = NULL;
65  free(ths->m_mail_pw); ths->m_mail_pw = NULL;
66  free(ths->m_send_server); ths->m_send_server = NULL;
67  ths->m_send_port = 0;
68  free(ths->m_send_user); ths->m_send_user = NULL;
69  free(ths->m_send_pw); ths->m_send_pw = NULL;
70  ths->m_server_flags= 0;
71 }
72 
73 
74 void mrloginparam_read__(mrloginparam_t* ths, mrsqlite3_t* sql, const char* prefix)
75 {
76  char* key = NULL;
77  #define MR_PREFIX(a) sqlite3_free(key); key=sqlite3_mprintf("%s%s", prefix, (a));
78 
79  mrloginparam_empty(ths);
80 
81  MR_PREFIX("addr"); ths->m_addr = mrsqlite3_get_config__ (sql, key, NULL);
82 
83  MR_PREFIX("mail_server"); ths->m_mail_server = mrsqlite3_get_config__ (sql, key, NULL);
84  MR_PREFIX("mail_port"); ths->m_mail_port = mrsqlite3_get_config_int__(sql, key, 0);
85  MR_PREFIX("mail_user"); ths->m_mail_user = mrsqlite3_get_config__ (sql, key, NULL);
86  MR_PREFIX("mail_pw"); ths->m_mail_pw = mrsqlite3_get_config__ (sql, key, NULL);
87 
88  MR_PREFIX("send_server"); ths->m_send_server = mrsqlite3_get_config__ (sql, key, NULL);
89  MR_PREFIX("send_port"); ths->m_send_port = mrsqlite3_get_config_int__(sql, key, 0);
90  MR_PREFIX("send_user"); ths->m_send_user = mrsqlite3_get_config__ (sql, key, NULL);
91  MR_PREFIX("send_pw"); ths->m_send_pw = mrsqlite3_get_config__ (sql, key, NULL);
92 
93  MR_PREFIX("server_flags");ths->m_server_flags= mrsqlite3_get_config_int__(sql, key, 0);
94 
95  sqlite3_free(key);
96 }
97 
98 
99 void mrloginparam_write__(const mrloginparam_t* ths, mrsqlite3_t* sql, const char* prefix)
100 {
101  char* key = NULL;
102 
103  MR_PREFIX("addr"); mrsqlite3_set_config__ (sql, key, ths->m_addr);
104 
105  MR_PREFIX("mail_server"); mrsqlite3_set_config__ (sql, key, ths->m_mail_server);
106  MR_PREFIX("mail_port"); mrsqlite3_set_config_int__(sql, key, ths->m_mail_port);
107  MR_PREFIX("mail_user"); mrsqlite3_set_config__ (sql, key, ths->m_mail_user);
108  MR_PREFIX("mail_pw"); mrsqlite3_set_config__ (sql, key, ths->m_mail_pw);
109 
110  MR_PREFIX("send_server"); mrsqlite3_set_config__ (sql, key, ths->m_send_server);
111  MR_PREFIX("send_port"); mrsqlite3_set_config_int__(sql, key, ths->m_send_port);
112  MR_PREFIX("send_user"); mrsqlite3_set_config__ (sql, key, ths->m_send_user);
113  MR_PREFIX("send_pw"); mrsqlite3_set_config__ (sql, key, ths->m_send_pw);
114 
115  MR_PREFIX("server_flags"); mrsqlite3_set_config_int__(sql, key, ths->m_server_flags);
116 
117  sqlite3_free(key);
118 }
119 
120 
121 static char* get_readable_flags(int flags)
122 {
123  mrstrbuilder_t strbuilder;
124  mrstrbuilder_init(&strbuilder);
125  #define CAT_FLAG(f, s) if( (1<<bit)==(f) ) { mrstrbuilder_cat(&strbuilder, (s)); flag_added = 1; }
126 
127  for( int bit = 0; bit <= 30; bit++ )
128  {
129  if( flags&(1<<bit) )
130  {
131  int flag_added = 0;
132 
133  CAT_FLAG(MR_AUTH_XOAUTH2, "XOAUTH2 ");
134  CAT_FLAG(MR_AUTH_NORMAL, "AUTH_NORMAL ");
135 
136  CAT_FLAG(MR_IMAP_SOCKET_STARTTLS, "IMAP_STARTTLS ");
137  CAT_FLAG(MR_IMAP_SOCKET_SSL, "IMAP_SSL ");
138  CAT_FLAG(MR_IMAP_SOCKET_PLAIN, "IMAP_PLAIN ");
139 
140  CAT_FLAG(MR_SMTP_SOCKET_STARTTLS, "SMTP_STARTTLS ");
141  CAT_FLAG(MR_SMTP_SOCKET_SSL, "SMTP_SSL ");
142  CAT_FLAG(MR_SMTP_SOCKET_PLAIN, "SMTP_PLAIN ");
143 
144  CAT_FLAG(MR_NO_EXTRA_IMAP_UPLOAD, "NO_EXTRA_IMAP_UPLOAD ");
145  CAT_FLAG(MR_NO_MOVE_TO_CHATS, "NO_MOVE_TO_CHATS ");
146 
147  if( !flag_added ) {
148  char* temp = mr_mprintf("0x%x ", 1<<bit); mrstrbuilder_cat(&strbuilder, temp); free(temp);
149  }
150  }
151  }
152 
153  if( strbuilder.m_buf[0]==0 ) { mrstrbuilder_cat(&strbuilder, "0"); }
154  mr_trim(strbuilder.m_buf);
155  return strbuilder.m_buf;
156 }
157 
158 
159 char* mrloginparam_get_readable(const mrloginparam_t* ths)
160 {
161  const char* unset = "0";
162  const char* pw = "***";
163 
164  if( ths==NULL ) {
165  return safe_strdup(NULL);
166  }
167 
168  char* flags_readable = get_readable_flags(ths->m_server_flags);
169 
170  char* ret = mr_mprintf("%s %s:%s:%s:%i %s:%s:%s:%i %s",
171  ths->m_addr? ths->m_addr : unset,
172 
173  ths->m_mail_user? ths->m_mail_user : unset,
174  ths->m_mail_pw? pw : unset,
175  ths->m_mail_server? ths->m_mail_server : unset,
176  ths->m_mail_port,
177 
178  ths->m_send_user? ths->m_send_user : unset,
179  ths->m_send_pw? pw : unset,
180  ths->m_send_server? ths->m_send_server : unset,
181  ths->m_send_port,
182 
183  flags_readable);
184 
185  free(flags_readable);
186  return ret;
187 }
188