mirror of
https://github.com/chatmail/core.git
synced 2026-05-04 05:46:29 +03:00
reduce code duplication (round 1)
This commit is contained in:
387
src/dc_oauth2.rs
387
src/dc_oauth2.rs
@@ -1,334 +1,19 @@
|
||||
use c2rust_bitfields::BitfieldStruct;
|
||||
use libc;
|
||||
|
||||
use crate::dc_contact::*;
|
||||
use crate::dc_context::dc_context_t;
|
||||
use crate::dc_imap::dc_imap_t;
|
||||
use crate::dc_jsmn::*;
|
||||
use crate::dc_log::*;
|
||||
use crate::dc_lot::dc_lot_t;
|
||||
use crate::dc_smtp::dc_smtp_t;
|
||||
use crate::dc_sqlite3::dc_sqlite3_t;
|
||||
use crate::dc_sqlite3::*;
|
||||
use crate::dc_strencode::*;
|
||||
use crate::dc_tools::*;
|
||||
use crate::types::*;
|
||||
use crate::x::*;
|
||||
|
||||
extern "C" {
|
||||
pub type mailstream_cancel;
|
||||
pub type sqlite3;
|
||||
#[no_mangle]
|
||||
fn calloc(_: libc::c_ulong, _: libc::c_ulong) -> *mut libc::c_void;
|
||||
#[no_mangle]
|
||||
fn free(_: *mut libc::c_void);
|
||||
#[no_mangle]
|
||||
fn atol(_: *const libc::c_char) -> libc::c_long;
|
||||
#[no_mangle]
|
||||
fn strchr(_: *const libc::c_char, _: libc::c_int) -> *mut libc::c_char;
|
||||
#[no_mangle]
|
||||
fn strcmp(_: *const libc::c_char, _: *const libc::c_char) -> libc::c_int;
|
||||
#[no_mangle]
|
||||
fn strlen(_: *const libc::c_char) -> libc::c_ulong;
|
||||
#[no_mangle]
|
||||
fn strncmp(_: *const libc::c_char, _: *const libc::c_char, _: libc::c_ulong) -> libc::c_int;
|
||||
#[no_mangle]
|
||||
fn strdup(_: *const libc::c_char) -> *mut libc::c_char;
|
||||
#[no_mangle]
|
||||
fn strndup(_: *const libc::c_char, _: libc::c_ulong) -> *mut libc::c_char;
|
||||
#[no_mangle]
|
||||
fn strcasecmp(_: *const libc::c_char, _: *const libc::c_char) -> libc::c_int;
|
||||
#[no_mangle]
|
||||
fn time(_: *mut time_t) -> time_t;
|
||||
#[no_mangle]
|
||||
fn pthread_mutex_lock(_: *mut pthread_mutex_t) -> libc::c_int;
|
||||
#[no_mangle]
|
||||
fn pthread_mutex_unlock(_: *mut pthread_mutex_t) -> libc::c_int;
|
||||
#[no_mangle]
|
||||
fn dc_urlencode(_: *const libc::c_char) -> *mut libc::c_char;
|
||||
#[no_mangle]
|
||||
fn dc_str_replace(
|
||||
haystack: *mut *mut libc::c_char,
|
||||
needle: *const libc::c_char,
|
||||
replacement: *const libc::c_char,
|
||||
) -> libc::c_int;
|
||||
/* string tools */
|
||||
#[no_mangle]
|
||||
fn dc_strdup(_: *const libc::c_char) -> *mut libc::c_char;
|
||||
/* handle configurations, private */
|
||||
#[no_mangle]
|
||||
fn dc_sqlite3_set_config(
|
||||
_: *mut dc_sqlite3_t,
|
||||
key: *const libc::c_char,
|
||||
value: *const libc::c_char,
|
||||
) -> libc::c_int;
|
||||
#[no_mangle]
|
||||
fn dc_addr_normalize(addr: *const libc::c_char) -> *mut libc::c_char;
|
||||
#[no_mangle]
|
||||
fn dc_sqlite3_set_config_int64(
|
||||
_: *mut dc_sqlite3_t,
|
||||
key: *const libc::c_char,
|
||||
value: int64_t,
|
||||
) -> libc::c_int;
|
||||
#[no_mangle]
|
||||
fn dc_sqlite3_get_config(
|
||||
_: *mut dc_sqlite3_t,
|
||||
key: *const libc::c_char,
|
||||
def: *const libc::c_char,
|
||||
) -> *mut libc::c_char;
|
||||
#[no_mangle]
|
||||
fn dc_sqlite3_get_config_int64(
|
||||
_: *mut dc_sqlite3_t,
|
||||
key: *const libc::c_char,
|
||||
def: int64_t,
|
||||
) -> int64_t;
|
||||
#[no_mangle]
|
||||
fn dc_log_warning(_: *mut dc_context_t, data1: libc::c_int, msg: *const libc::c_char, _: ...);
|
||||
#[no_mangle]
|
||||
fn dc_log_info(_: *mut dc_context_t, data1: libc::c_int, msg: *const libc::c_char, _: ...);
|
||||
/* *
|
||||
* Run JSON parser. It parses a JSON data string into and array of tokens, each describing
|
||||
* a single JSON object.
|
||||
*/
|
||||
#[no_mangle]
|
||||
fn jsmn_parse(
|
||||
parser: *mut jsmn_parser,
|
||||
js: *const libc::c_char,
|
||||
len: size_t,
|
||||
tokens: *mut jsmntok_t,
|
||||
num_tokens: libc::c_uint,
|
||||
) -> libc::c_int;
|
||||
/* *
|
||||
* Create JSON parser over an array of tokens
|
||||
*/
|
||||
#[no_mangle]
|
||||
fn jsmn_init(parser: *mut jsmn_parser);
|
||||
}
|
||||
|
||||
/* define DC_USE_RPGP to enable use of rPGP instead of netpgp where available;
|
||||
preferrably, this should be done in the project configuration currently */
|
||||
//#define DC_USE_RPGP 1
|
||||
/* Includes that are used frequently. This file may also be used to create predefined headers. */
|
||||
|
||||
/* *
|
||||
* Callback function that should be given to dc_context_new().
|
||||
*
|
||||
* @memberof dc_context_t
|
||||
* @param context The context object as returned by dc_context_new().
|
||||
* @param event one of the @ref DC_EVENT constants
|
||||
* @param data1 depends on the event parameter
|
||||
* @param data2 depends on the event parameter
|
||||
* @return return 0 unless stated otherwise in the event parameter documentation
|
||||
*/
|
||||
pub type dc_callback_t = Option<
|
||||
unsafe extern "C" fn(
|
||||
_: *mut dc_context_t,
|
||||
_: libc::c_int,
|
||||
_: uintptr_t,
|
||||
_: uintptr_t,
|
||||
) -> uintptr_t,
|
||||
>;
|
||||
/* *
|
||||
* @mainpage Getting started
|
||||
*
|
||||
* This document describes how to handle the Delta Chat core library.
|
||||
* For general information about Delta Chat itself,
|
||||
* see <https://delta.chat> and <https://github.com/deltachat>.
|
||||
*
|
||||
* Let's start.
|
||||
*
|
||||
* First of all, you have to **define an event-handler-function**
|
||||
* that is called by the library on specific events
|
||||
* (eg. when the configuration is done or when fresh messages arrive).
|
||||
* With this function you can create a Delta Chat context then:
|
||||
*
|
||||
* ~~~
|
||||
* #include <deltachat.h>
|
||||
*
|
||||
* uintptr_t event_handler_func(dc_context_t* context, int event,
|
||||
* uintptr_t data1, uintptr_t data2)
|
||||
* {
|
||||
* return 0; // for unhandled events, it is always safe to return 0
|
||||
* }
|
||||
*
|
||||
* dc_context_t* context = dc_context_new(event_handler_func, NULL, NULL);
|
||||
* ~~~
|
||||
*
|
||||
* After that, you should make sure,
|
||||
* sending and receiving jobs are processed as needed.
|
||||
* For this purpose, you have to **create two threads:**
|
||||
*
|
||||
* ~~~
|
||||
* #include <pthread.h>
|
||||
*
|
||||
* void* imap_thread_func(void* context)
|
||||
* {
|
||||
* while (true) {
|
||||
* dc_perform_imap_jobs(context);
|
||||
* dc_perform_imap_fetch(context);
|
||||
* dc_perform_imap_idle(context);
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* void* smtp_thread_func(void* context)
|
||||
* {
|
||||
* while (true) {
|
||||
* dc_perform_smtp_jobs(context);
|
||||
* dc_perform_smtp_idle(context);
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* static pthread_t imap_thread, smtp_thread;
|
||||
* pthread_create(&imap_thread, NULL, imap_thread_func, context);
|
||||
* pthread_create(&smtp_thread, NULL, smtp_thread_func, context);
|
||||
* ~~~
|
||||
*
|
||||
* The example above uses "pthreads",
|
||||
* however, you can also use anything else for thread handling.
|
||||
* NB: The deltachat-core library itself does not create any threads on its own,
|
||||
* however, functions, unless stated otherwise, are thread-safe.
|
||||
*
|
||||
* After that you can **define and open a database.**
|
||||
* The database is a normal sqlite-file and is created as needed:
|
||||
*
|
||||
* ~~~
|
||||
* dc_open(context, "example.db", NULL);
|
||||
* ~~~
|
||||
*
|
||||
* Now you can **configure the context:**
|
||||
*
|
||||
* ~~~
|
||||
* // use some real test credentials here
|
||||
* dc_set_config(context, "addr", "alice@example.org");
|
||||
* dc_set_config(context, "mail_pw", "***");
|
||||
* dc_configure(context);
|
||||
* ~~~
|
||||
*
|
||||
* dc_configure() returns immediately, the configuration itself may take a while
|
||||
* and is done by a job in the imap-thread you've defined above.
|
||||
* Once done, the #DC_EVENT_CONFIGURE_PROGRESS reports success
|
||||
* to the event_handler_func() that is also defined above.
|
||||
*
|
||||
* The configuration result is saved in the database,
|
||||
* on subsequent starts it is not needed to call dc_configure()
|
||||
* (you can check this using dc_is_configured()).
|
||||
*
|
||||
* Now you can **send the first message:**
|
||||
*
|
||||
* ~~~
|
||||
* // use a real testing address here
|
||||
* uint32_t contact_id = dc_create_contact(context, NULL, "bob@example.org");
|
||||
* uint32_t chat_id = dc_create_chat_by_contact_id(context, contact_id);
|
||||
*
|
||||
* dc_send_text_msg(context, chat_id, "Hi, here is my first message!");
|
||||
* ~~~
|
||||
*
|
||||
* dc_send_text_msg() returns immediately;
|
||||
* the sending itself is done by a job in the smtp-thread you've defined above.
|
||||
* If you check the testing address (bob)
|
||||
* and you should have received a normal email.
|
||||
* Answer this email in any email program with "Got it!"
|
||||
* and the imap-thread you've create above will **receive the message**.
|
||||
*
|
||||
* You can then **list all messages** of a chat as follow:
|
||||
*
|
||||
* ~~~
|
||||
* dc_array_t* msglist = dc_get_chat_msgs(context, chat_id, 0, 0);
|
||||
* for (int i = 0; i < dc_array_get_cnt(msglist); i++)
|
||||
* {
|
||||
* uint32_t msg_id = dc_array_get_id(msglist, i);
|
||||
* dc_msg_t* msg = dc_get_msg(context, msg_id);
|
||||
* char* text = dc_msg_get_text(msg);
|
||||
*
|
||||
* printf("Message %i: %s\n", i+1, text);
|
||||
*
|
||||
* free(text);
|
||||
* dc_msg_unref(msg);
|
||||
* }
|
||||
* dc_array_unref(msglist);
|
||||
* ~~~
|
||||
*
|
||||
* This will output the following two lines:
|
||||
*
|
||||
* ~~~
|
||||
* Message 1: Hi, here is my first message!
|
||||
* Message 2: Got it!
|
||||
* ~~~
|
||||
*
|
||||
*
|
||||
* ## Class reference
|
||||
*
|
||||
* For a class reference, see the "Classes" link atop.
|
||||
*
|
||||
*
|
||||
* ## Further hints
|
||||
*
|
||||
* Here are some additional, unsorted hints that may be useful.
|
||||
*
|
||||
* - For `get`-functions, you have to unref the return value in some way.
|
||||
*
|
||||
* - Strings in function arguments or return values are usually UTF-8 encoded.
|
||||
*
|
||||
* - The issue-tracker for the core library is here:
|
||||
* <https://github.com/deltachat/deltachat-core/issues>
|
||||
*
|
||||
* The following points are important mainly
|
||||
* for the authors of the library itself:
|
||||
*
|
||||
* - For indentation, use tabs.
|
||||
* Alignments that are not placed at the beginning of a line
|
||||
* should be done with spaces.
|
||||
*
|
||||
* - For padding between functions,
|
||||
* classes etc. use 2 empty lines
|
||||
*
|
||||
* - Source files are encoded as UTF-8 with Unix line endings
|
||||
* (a simple `LF`, `0x0A` or `\n`)
|
||||
*
|
||||
* If you need further assistance,
|
||||
* please do not hesitate to contact us
|
||||
* through the channels shown at https://delta.chat/en/contribute
|
||||
*
|
||||
* Please keep in mind, that your derived work
|
||||
* must respect the Mozilla Public License 2.0 of libdeltachat
|
||||
* and the respective licenses of the libraries libdeltachat links with.
|
||||
*
|
||||
* See you.
|
||||
*/
|
||||
|
||||
pub type dc_receive_imf_t = Option<
|
||||
unsafe extern "C" fn(
|
||||
_: *mut dc_imap_t,
|
||||
_: *const libc::c_char,
|
||||
_: size_t,
|
||||
_: *const libc::c_char,
|
||||
_: uint32_t,
|
||||
_: uint32_t,
|
||||
) -> (),
|
||||
>;
|
||||
|
||||
pub type dc_precheck_imf_t = Option<
|
||||
unsafe extern "C" fn(
|
||||
_: *mut dc_imap_t,
|
||||
_: *const libc::c_char,
|
||||
_: *const libc::c_char,
|
||||
_: uint32_t,
|
||||
) -> libc::c_int,
|
||||
>;
|
||||
pub type dc_set_config_t = Option<
|
||||
unsafe extern "C" fn(_: *mut dc_imap_t, _: *const libc::c_char, _: *const libc::c_char) -> (),
|
||||
>;
|
||||
pub type dc_get_config_t = Option<
|
||||
unsafe extern "C" fn(
|
||||
_: *mut dc_imap_t,
|
||||
_: *const libc::c_char,
|
||||
_: *const libc::c_char,
|
||||
) -> *mut libc::c_char,
|
||||
>;
|
||||
/* ** library-private **********************************************************/
|
||||
|
||||
/* *
|
||||
* Library-internal.
|
||||
*/
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(C)]
|
||||
pub struct _dc_sqlite3 {
|
||||
pub cobj: *mut sqlite3,
|
||||
pub context: *mut dc_context_t,
|
||||
}
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(C)]
|
||||
pub struct oauth2_t {
|
||||
@@ -338,65 +23,7 @@ pub struct oauth2_t {
|
||||
pub refresh_token: *mut libc::c_char,
|
||||
pub get_userinfo: *mut libc::c_char,
|
||||
}
|
||||
/* *
|
||||
* JSON token description.
|
||||
* type type (object, array, string etc.)
|
||||
* start start position in JSON data string
|
||||
* end end position in JSON data string
|
||||
*/
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(C)]
|
||||
pub struct jsmntok_t {
|
||||
pub type_0: jsmntype_t,
|
||||
pub start: libc::c_int,
|
||||
pub end: libc::c_int,
|
||||
pub size: libc::c_int,
|
||||
}
|
||||
/*
|
||||
Copyright (c) 2010 Serge A. Zaitsev
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
/* *
|
||||
* JSON type identifier. Basic types are:
|
||||
* o Object
|
||||
* o Array
|
||||
* o String
|
||||
* o Other primitive: number, boolean (true/false) or null
|
||||
*/
|
||||
pub type jsmntype_t = libc::c_uint;
|
||||
pub const JSMN_PRIMITIVE: jsmntype_t = 4;
|
||||
pub const JSMN_STRING: jsmntype_t = 3;
|
||||
pub const JSMN_ARRAY: jsmntype_t = 2;
|
||||
pub const JSMN_OBJECT: jsmntype_t = 1;
|
||||
pub const JSMN_UNDEFINED: jsmntype_t = 0;
|
||||
/* *
|
||||
* JSON parser. Contains an array of token blocks available. Also stores
|
||||
* the string being parsed now and current position in that string
|
||||
*/
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(C)]
|
||||
pub struct jsmn_parser {
|
||||
pub pos: libc::c_uint,
|
||||
pub toknext: libc::c_uint,
|
||||
pub toksuper: libc::c_int,
|
||||
}
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn dc_get_oauth2_url(
|
||||
mut context: *mut dc_context_t,
|
||||
|
||||
Reference in New Issue
Block a user