Browse Source

Refactor IMAP SORT command support to use the extension API.

dvh-chacham15-master
Pitiphong Phongpattranont 9 years ago
parent
commit
7b2f3b6c20
16 changed files with 788 additions and 418 deletions
  1. +20
    -0
      build-mac/libetpan.xcodeproj/project.pbxproj
  2. +4
    -2
      src/low-level/imap/Makefile.am
  3. +0
    -102
      src/low-level/imap/mailimap.c
  4. +0
    -49
      src/low-level/imap/mailimap.h
  5. +3
    -1
      src/low-level/imap/mailimap_extension.c
  6. +2
    -1
      src/low-level/imap/mailimap_extension_types.h
  7. +0
    -8
      src/low-level/imap/mailimap_parser.c
  8. +1
    -118
      src/low-level/imap/mailimap_sender.c
  9. +4
    -7
      src/low-level/imap/mailimap_sender.h
  10. +421
    -0
      src/low-level/imap/mailimap_sort.c
  11. +100
    -0
      src/low-level/imap/mailimap_sort.h
  12. +122
    -0
      src/low-level/imap/mailimap_sort_types.c
  13. +111
    -0
      src/low-level/imap/mailimap_sort_types.h
  14. +0
    -31
      src/low-level/imap/mailimap_types.c
  15. +0
    -31
      src/low-level/imap/mailimap_types.h
  16. +0
    -68
      src/low-level/imap/mailimap_types_helper.c

+ 20
- 0
build-mac/libetpan.xcodeproj/project.pbxproj View File

@ -8,6 +8,12 @@
/* Begin PBXBuildFile section */
365DFFD215D1C93100F2DD85 /* xgmmsgid.c in Sources */ = {isa = PBXBuildFile; fileRef = 365DFFD115D1C93100F2DD85 /* xgmmsgid.c */; };
8A75ECDA17040F92007F9972 /* mailimap_sort.c in Sources */ = {isa = PBXBuildFile; fileRef = 8A75ECD917040F91007F9972 /* mailimap_sort.c */; };
8A75ECDB17040F92007F9972 /* mailimap_sort.c in Sources */ = {isa = PBXBuildFile; fileRef = 8A75ECD917040F91007F9972 /* mailimap_sort.c */; };
8A75ECDC17040F92007F9972 /* mailimap_sort.c in Sources */ = {isa = PBXBuildFile; fileRef = 8A75ECD917040F91007F9972 /* mailimap_sort.c */; };
8A75ECE6170414BA007F9972 /* mailimap_sort_types.c in Sources */ = {isa = PBXBuildFile; fileRef = 8A75ECE5170414B8007F9972 /* mailimap_sort_types.c */; };
8A75ECE7170414BA007F9972 /* mailimap_sort_types.c in Sources */ = {isa = PBXBuildFile; fileRef = 8A75ECE5170414B8007F9972 /* mailimap_sort_types.c */; };
8A75ECE8170414BA007F9972 /* mailimap_sort_types.c in Sources */ = {isa = PBXBuildFile; fileRef = 8A75ECE5170414B8007F9972 /* mailimap_sort_types.c */; };
C60E7B9A16C3809400A25BF4 /* enable.c in Sources */ = {isa = PBXBuildFile; fileRef = C60E7B9816C3809400A25BF4 /* enable.c */; };
C60E7B9D16C3809C00A25BF4 /* enable.c in Sources */ = {isa = PBXBuildFile; fileRef = C60E7B9816C3809400A25BF4 /* enable.c */; };
C60E7B9E16C3809D00A25BF4 /* enable.c in Sources */ = {isa = PBXBuildFile; fileRef = C60E7B9816C3809400A25BF4 /* enable.c */; };
@ -557,6 +563,10 @@
/* Begin PBXFileReference section */
365DFFD115D1C93100F2DD85 /* xgmmsgid.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = xgmmsgid.c; sourceTree = "<group>"; };
365DFFD815D1CF1800F2DD85 /* xgmmsgid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xgmmsgid.h; sourceTree = "<group>"; };
8A75ECD917040F91007F9972 /* mailimap_sort.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = mailimap_sort.c; sourceTree = "<group>"; };
8A75ECDD17040FBD007F9972 /* mailimap_sort.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mailimap_sort.h; sourceTree = "<group>"; };
8A75ECE5170414B8007F9972 /* mailimap_sort_types.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = mailimap_sort_types.c; sourceTree = "<group>"; };
8A75ECEA170414E9007F9972 /* mailimap_sort_types.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mailimap_sort_types.h; sourceTree = "<group>"; };
8DC2EF5A0486A6940098B216 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8DC2EF5B0486A6940098B216 /* libetpan.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = libetpan.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C60E7B9816C3809400A25BF4 /* enable.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = enable.c; sourceTree = "<group>"; };
@ -1503,6 +1513,10 @@
C6F61F731701409B0073032E /* xgmthrid.h */,
C6667DED1342ACCD00969A8E /* xlist.c */,
C6667DEE1342ACCD00969A8E /* xlist.h */,
8A75ECD917040F91007F9972 /* mailimap_sort.c */,
8A75ECDD17040FBD007F9972 /* mailimap_sort.h */,
8A75ECE5170414B8007F9972 /* mailimap_sort_types.c */,
8A75ECEA170414E9007F9972 /* mailimap_sort_types.h */,
);
path = imap;
sourceTree = "<group>";
@ -2017,6 +2031,8 @@
C64BB21816E2FC2F000DB34C /* qresync_types.c in Sources */,
C64BB21B16E2FC2F000DB34C /* qresync.c in Sources */,
C6F61F741701409B0073032E /* xgmthrid.c in Sources */,
8A75ECDA17040F92007F9972 /* mailimap_sort.c in Sources */,
8A75ECE6170414BA007F9972 /* mailimap_sort_types.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -2195,6 +2211,8 @@
C64BB21A16E2FC2F000DB34C /* qresync_types.c in Sources */,
C64BB21D16E2FC2F000DB34C /* qresync.c in Sources */,
C6F61F761701409B0073032E /* xgmthrid.c in Sources */,
8A75ECDC17040F92007F9972 /* mailimap_sort.c in Sources */,
8A75ECE8170414BA007F9972 /* mailimap_sort_types.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -2373,6 +2391,8 @@
C64BB21916E2FC2F000DB34C /* qresync_types.c in Sources */,
C64BB21C16E2FC2F000DB34C /* qresync.c in Sources */,
C6F61F751701409B0073032E /* xgmthrid.c in Sources */,
8A75ECDB17040F92007F9972 /* mailimap_sort.c in Sources */,
8A75ECE7170414BA007F9972 /* mailimap_sort_types.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};


+ 4
- 2
src/low-level/imap/Makefile.am View File

@ -45,7 +45,8 @@ etpaninclude_HEADERS = \
xlist.h xgmlabels.h xgmmsgid.h xgmthrid.h \
mailimap_id.h mailimap_id_types.h \
enable.h condstore.h condstore_types.h \
qresync.h qresync_types.h
qresync.h qresync_types.h \
mailimap_sort.h mailimap_sort_types.h
AM_CPPFLAGS = -I$(top_builddir)/include \
-I$(top_srcdir)/src/data-types
@ -94,5 +95,6 @@ libimap_la_SOURCES = \
mailimap_id_parser.h mailimap_id_parser.c \
enable.h enable.c \
condstore.h condstore.c condstore_types.h condstore_types.c condstore_private.h \
qresync.h qresync.c qresync_types.h qresync_types.c qresync_private.h
qresync.h qresync.c qresync_types.h qresync_types.c qresync_private.h \
mailimap_sort.c mailimap_sort_types.c

+ 0
- 102
src/low-level/imap/mailimap.c View File

@ -2063,108 +2063,6 @@ mailimap_uid_search(mailimap * session, const char * charset,
#endif
}
LIBETPAN_EXPORT
int
mailimap_sort(mailimap * session, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey,
clist ** result)
{
struct mailimap_response * response;
int r;
int error_code;
if (session->imap_state != MAILIMAP_STATE_SELECTED)
return MAILIMAP_ERROR_BAD_STATE;
r = mailimap_send_current_tag(session);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_sort_send(session->imap_stream, charset, key, searchkey);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_crlf_send(session->imap_stream);
if (r != MAILIMAP_NO_ERROR)
return r;
if (mailstream_flush(session->imap_stream) == -1)
return MAILIMAP_ERROR_STREAM;
if (mailimap_read_line(session) == NULL)
return MAILIMAP_ERROR_STREAM;
r = mailimap_parse_response(session, &response);
if (r != MAILIMAP_NO_ERROR)
return r;
* result = session->imap_response_info->rsp_search_result;
session->imap_response_info->rsp_search_result = NULL;
error_code = response->rsp_resp_done->rsp_data.rsp_tagged->rsp_cond_state->rsp_type;
mailimap_response_free(response);
switch (error_code) {
case MAILIMAP_RESP_COND_STATE_OK:
return MAILIMAP_NO_ERROR;
default:
return MAILIMAP_ERROR_SEARCH;
}
}
LIBETPAN_EXPORT
int
mailimap_uid_sort(mailimap * session, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey,
clist ** result)
{
struct mailimap_response * response;
int r;
int error_code;
if (session->imap_state != MAILIMAP_STATE_SELECTED)
return MAILIMAP_ERROR_BAD_STATE;
r = mailimap_send_current_tag(session);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_uid_sort_send(session->imap_stream, charset, key, searchkey);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_crlf_send(session->imap_stream);
if (r != MAILIMAP_NO_ERROR)
return r;
if (mailstream_flush(session->imap_stream) == -1)
return MAILIMAP_ERROR_STREAM;
if (mailimap_read_line(session) == NULL)
return MAILIMAP_ERROR_STREAM;
r = mailimap_parse_response(session, &response);
if (r != MAILIMAP_NO_ERROR)
return r;
* result = session->imap_response_info->rsp_search_result;
session->imap_response_info->rsp_search_result = NULL;
error_code = response->rsp_resp_done->rsp_data.rsp_tagged->rsp_cond_state->rsp_type;
mailimap_response_free(response);
switch (error_code) {
case MAILIMAP_RESP_COND_STATE_OK:
return MAILIMAP_NO_ERROR;
default:
return MAILIMAP_ERROR_UID_SEARCH;
}
}
LIBETPAN_EXPORT
void mailimap_search_result_free(clist * search_result)
{


+ 0
- 49
src/low-level/imap/mailimap.h View File

@ -486,55 +486,6 @@ int
mailimap_uid_search(mailimap * session, const char * charset,
struct mailimap_search_key * key, clist ** result);
/*
mailimap_sort()
All mails that match the given criteria will be returned
their numbers sorted by the given sorting criteria in the result list.
@param session IMAP session
@param charset This indicates the charset of the strings that appears
in the searching criteria
@param key This is the searching criteria
@param result The result is a clist of (uint32_t *) and will be
stored in (* result).
@return the return code is one of MAILIMAP_ERROR_XXX or
MAILIMAP_NO_ERROR codes
*/
LIBETPAN_EXPORT
int
mailimap_sort(mailimap * session, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey,
clist ** result);
/*
mailimap_uid_sort()
All mails that match the given criteria will be returned
their unique identifiers sorted by the given sorting criteria in the result list.
@param session IMAP session
@param charset This indicates the charset of the strings that appears
in the searching criteria
@param key This is the sorting criteria
@param searchkey This is the searching criteria
@param result The result is a clist of (uint32_t *) and will be
stored in (* result).
@return the return code is one of MAILIMAP_ERROR_XXX or
MAILIMAP_NO_ERROR codes
*/
LIBETPAN_EXPORT
int
mailimap_uid_sort(mailimap * session, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey,
clist ** result);
/*
mailimap_search_result_free()


+ 3
- 1
src/low-level/imap/mailimap_extension.c View File

@ -53,6 +53,7 @@
#include "enable.h"
#include "condstore.h"
#include "qresync.h"
#include "mailimap_sort.h"
/*
the list of registered extensions (struct mailimap_extension_api *)
@ -75,7 +76,8 @@ static struct mailimap_extension_api * internal_extension_list[] = {
&mailimap_extension_id,
&mailimap_extension_enable,
&mailimap_extension_condstore,
&mailimap_extension_qresync
&mailimap_extension_qresync,
&mailimap_extension_sort
};
LIBETPAN_EXPORT


+ 2
- 1
src/low-level/imap/mailimap_extension_types.h View File

@ -24,7 +24,8 @@ enum {
MAILIMAP_EXTENSION_ID, /* ID */
MAILIMAP_EXTENSION_ENABLE, /* ENABLE */
MAILIMAP_EXTENSION_CONDSTORE, /* CONDSTORE */
MAILIMAP_EXTENSION_QRESYNC /* QRESYNC */
MAILIMAP_EXTENSION_QRESYNC, /* QRESYNC */
MAILIMAP_EXTENSION_SORT /* SORT */
};


+ 0
- 8
src/low-level/imap/mailimap_parser.c View File

@ -6679,14 +6679,6 @@ mailimap_mailbox_data_parse(mailstream * fd, MMAPString * buffer,
if (r == MAILIMAP_NO_ERROR)
type = MAILIMAP_MAILBOX_DATA_SEARCH;
}
if (r == MAILIMAP_ERROR_PARSE) {
r = mailimap_mailbox_data_sort_parse(fd, buffer, &cur_token,
&data_search,
progr_rate, progr_fun);
if (r == MAILIMAP_NO_ERROR)
type = MAILIMAP_MAILBOX_DATA_SEARCH;
}
if (r == MAILIMAP_ERROR_PARSE) {
r = mailimap_mailbox_data_status_parse(fd, buffer, &cur_token,


+ 1
- 118
src/low-level/imap/mailimap_sender.c View File

@ -130,13 +130,6 @@ static int mailimap_password_send(mailstream * fd, const char * pass);
static int mailimap_quoted_char_send(mailstream * fd, char ch);
static int mailimap_search_key_send(mailstream * fd,
struct mailimap_search_key * key);
static int mailimap_sort_key_send(mailstream * fd,
struct mailimap_sort_key * key);
static int
mailimap_section_send(mailstream * fd,
struct mailimap_section * section);
@ -1948,7 +1941,7 @@ mailimap_uid_search_send(mailstream * fd, const char * charset,
*/
static int mailimap_search_key_send(mailstream * fd,
int mailimap_search_key_send(mailstream * fd,
struct mailimap_search_key * key)
{
int r;
@ -2352,116 +2345,6 @@ int mailimap_mod_sequence_value_send(mailstream * fd, uint64_t modseq)
}
int
mailimap_sort_send(mailstream * fd, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey)
{
int r;
r = mailimap_token_send(fd, "SORT");
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_space_send(fd);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_oparenth_send(fd);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_sort_key_send(fd, key);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_cparenth_send(fd);
if (r != MAILIMAP_NO_ERROR)
return r;
if (charset != NULL) {
r = mailimap_space_send(fd);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_astring_send(fd, charset);
if (r != MAILIMAP_NO_ERROR)
return r;
}
r = mailimap_space_send(fd);
if (r != MAILIMAP_NO_ERROR)
return r;
if (searchkey != NULL) {
r = mailimap_search_key_send(fd, searchkey);
if (r != MAILIMAP_NO_ERROR)
return r;
}
return MAILIMAP_NO_ERROR;
}
int
mailimap_uid_sort_send(mailstream * fd, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey)
{
int r;
r = mailimap_token_send(fd, "UID");
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_space_send(fd);
if (r != MAILIMAP_NO_ERROR)
return r;
return mailimap_sort_send(fd, charset, key, searchkey);
}
static int mailimap_sort_key_send(mailstream * fd,
struct mailimap_sort_key * key)
{
int r;
if (key->sortk_is_reverse) {
r = mailimap_token_send(fd, "REVERSE");
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_space_send(fd);
if (r != MAILIMAP_NO_ERROR)
return r;
}
switch (key->sortk_type) {
case MAILIMAP_SORT_KEY_ARRIVAL:
return mailimap_token_send(fd, "ARRIVAL");
case MAILIMAP_SORT_KEY_CC:
return mailimap_token_send(fd, "CC");
case MAILIMAP_SORT_KEY_DATE:
return mailimap_token_send(fd, "DATE");
case MAILIMAP_SORT_KEY_FROM:
return mailimap_token_send(fd, "FROM");
case MAILIMAP_SORT_KEY_SIZE:
return mailimap_token_send(fd, "SIZE");
case MAILIMAP_SORT_KEY_SUBJECT:
return mailimap_token_send(fd, "SUBJECT");
case MAILIMAP_SORT_KEY_TO:
return mailimap_token_send(fd, "TO");
case MAILIMAP_SORT_KEY_MULTIPLE:
r = mailimap_struct_spaced_list_send(fd, key->sortk_multiple,
(mailimap_struct_sender *)
mailimap_sort_key_send);
return MAILIMAP_NO_ERROR;
default:
/* should not happend */
return MAILIMAP_ERROR_INVAL;
}
}
/*
=> section = "[" [section-spec] "]"
*/


+ 4
- 7
src/low-level/imap/mailimap_sender.h View File

@ -112,14 +112,11 @@ mailimap_search_send(mailstream * fd, const char * charset,
int
mailimap_uid_search_send(mailstream * fd, const char * charset,
struct mailimap_search_key * key);
int
mailimap_sort_send(mailstream * fd, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey);
int
mailimap_uid_sort_send(mailstream * fd, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey);
int mailimap_search_key_send(mailstream * fd,
struct mailimap_search_key * key);
int
mailimap_select_send(mailstream * fd, const char * mb, int condstore);


+ 421
- 0
src/low-level/imap/mailimap_sort.c View File

@ -0,0 +1,421 @@
//
// mailimap_sort.c
// libetpan
//
// Created by Pitiphong Phongpattranont on 28/3/56 BE.
//
//
#include "mailimap_sort.h"
#include <stdlib.h>
#include "mailimap.h"
#include "mailimap_extension.h"
#include "mailimap_extension_types.h"
#include "mailimap_sender.h"
#include "mailimap_parser.h"
#include "mailimap_keywords.h"
#include "mailimap_sender.h"
enum {
MAILIMAP_SORT_TYPE_SORT
};
int
mailimap_sort_send(mailstream * fd, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey);
int
mailimap_uid_sort_send(mailstream * fd, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey);
int mailimap_sort_key_send(mailstream * fd,
struct mailimap_sort_key * key);
static int
mailimap_sort_extension_parse(int calling_parser, mailstream * fd,
MMAPString * buffer, size_t * indx,
struct mailimap_extension_data ** result,
size_t progr_rate, progress_function * progr_fun);
static void
mailimap_sort_extension_data_free(struct mailimap_extension_data * ext_data);
LIBETPAN_EXPORT
struct mailimap_extension_api mailimap_extension_sort = {
/* name */ "SORT",
/* extension_id */ MAILIMAP_EXTENSION_SORT,
/* parser */ mailimap_sort_extension_parse,
/* free */ mailimap_sort_extension_data_free
};
LIBETPAN_EXPORT
int
mailimap_sort(mailimap * session, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey,
clist ** result)
{
struct mailimap_response * response;
int r;
int error_code;
if (session->imap_state != MAILIMAP_STATE_SELECTED)
return MAILIMAP_ERROR_BAD_STATE;
r = mailimap_send_current_tag(session);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_sort_send(session->imap_stream, charset, key, searchkey);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_crlf_send(session->imap_stream);
if (r != MAILIMAP_NO_ERROR)
return r;
if (mailstream_flush(session->imap_stream) == -1)
return MAILIMAP_ERROR_STREAM;
if (mailimap_read_line(session) == NULL)
return MAILIMAP_ERROR_STREAM;
r = mailimap_parse_response(session, &response);
if (r != MAILIMAP_NO_ERROR)
return r;
clist * sort_result = NULL;
clistiter * cur = NULL;
for (cur = clist_begin(session->imap_response_info->rsp_extension_list);
cur != NULL; cur = clist_next(cur)) {
struct mailimap_extension_data * ext_data;
ext_data = (struct mailimap_extension_data *) clist_content(cur);
if (ext_data->ext_extension->ext_id == MAILIMAP_EXTENSION_SORT) {
if (sort_result == NULL) {
sort_result = ext_data->ext_data;
ext_data->ext_data = NULL;
ext_data->ext_type = -1;
}
}
}
clist_foreach(session->imap_response_info->rsp_extension_list,
(clist_func) mailimap_extension_data_free, NULL);
clist_free(session->imap_response_info->rsp_extension_list);
session->imap_response_info->rsp_extension_list = NULL;
if (sort_result == NULL) {
return MAILIMAP_ERROR_EXTENSION;
}
error_code = response->rsp_resp_done->rsp_data.rsp_tagged->rsp_cond_state->rsp_type;
switch (error_code) {
case MAILIMAP_RESP_COND_STATE_OK:
break;
default:
mailimap_search_result_free(sort_result);
return MAILIMAP_ERROR_EXTENSION;
}
mailimap_response_free(response);
* result = sort_result;
return MAILIMAP_NO_ERROR;
}
LIBETPAN_EXPORT
int
mailimap_uid_sort(mailimap * session, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey,
clist ** result)
{
struct mailimap_response * response;
int r;
int error_code;
if (session->imap_state != MAILIMAP_STATE_SELECTED)
return MAILIMAP_ERROR_BAD_STATE;
r = mailimap_send_current_tag(session);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_uid_sort_send(session->imap_stream, charset, key, searchkey);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_crlf_send(session->imap_stream);
if (r != MAILIMAP_NO_ERROR)
return r;
if (mailstream_flush(session->imap_stream) == -1)
return MAILIMAP_ERROR_STREAM;
if (mailimap_read_line(session) == NULL)
return MAILIMAP_ERROR_STREAM;
r = mailimap_parse_response(session, &response);
if (r != MAILIMAP_NO_ERROR)
return r;
clistiter * cur = NULL;
clist * sort_result = NULL;
for (cur = clist_begin(session->imap_response_info->rsp_extension_list);
cur != NULL; cur = clist_next(cur)) {
struct mailimap_extension_data * ext_data;
ext_data = (struct mailimap_extension_data *) clist_content(cur);
if (ext_data->ext_extension->ext_id == MAILIMAP_EXTENSION_SORT) {
if (sort_result == NULL) {
sort_result = ext_data->ext_data;
ext_data->ext_data = NULL;
ext_data->ext_type = -1;
}
}
}
clist_foreach(session->imap_response_info->rsp_extension_list,
(clist_func) mailimap_extension_data_free, NULL);
clist_free(session->imap_response_info->rsp_extension_list);
session->imap_response_info->rsp_extension_list = NULL;
if (sort_result == NULL) {
return MAILIMAP_ERROR_EXTENSION;
}
error_code = response->rsp_resp_done->rsp_data.rsp_tagged->rsp_cond_state->rsp_type;
switch (error_code) {
case MAILIMAP_RESP_COND_STATE_OK:
break;
default:
mailimap_search_result_free(sort_result);
return MAILIMAP_ERROR_EXTENSION;
}
mailimap_response_free(response);
* result = sort_result;
return MAILIMAP_NO_ERROR;
}
int
mailimap_sort_send(mailstream * fd, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey)
{
int r;
r = mailimap_token_send(fd, "SORT");
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_space_send(fd);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_oparenth_send(fd);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_sort_key_send(fd, key);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_cparenth_send(fd);
if (r != MAILIMAP_NO_ERROR)
return r;
if (charset != NULL) {
r = mailimap_space_send(fd);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_astring_send(fd, charset);
if (r != MAILIMAP_NO_ERROR)
return r;
}
r = mailimap_space_send(fd);
if (r != MAILIMAP_NO_ERROR)
return r;
if (searchkey != NULL) {
r = mailimap_search_key_send(fd, searchkey);
if (r != MAILIMAP_NO_ERROR)
return r;
}
return MAILIMAP_NO_ERROR;
}
int
mailimap_uid_sort_send(mailstream * fd, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey)
{
int r;
r = mailimap_token_send(fd, "UID");
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_space_send(fd);
if (r != MAILIMAP_NO_ERROR)
return r;
return mailimap_sort_send(fd, charset, key, searchkey);
}
int mailimap_sort_key_send(mailstream * fd,
struct mailimap_sort_key * key)
{
int r;
if (key->sortk_is_reverse) {
r = mailimap_token_send(fd, "REVERSE");
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_space_send(fd);
if (r != MAILIMAP_NO_ERROR)
return r;
}
switch (key->sortk_type) {
case MAILIMAP_SORT_KEY_ARRIVAL:
return mailimap_token_send(fd, "ARRIVAL");
case MAILIMAP_SORT_KEY_CC:
return mailimap_token_send(fd, "CC");
case MAILIMAP_SORT_KEY_DATE:
return mailimap_token_send(fd, "DATE");
case MAILIMAP_SORT_KEY_FROM:
return mailimap_token_send(fd, "FROM");
case MAILIMAP_SORT_KEY_SIZE:
return mailimap_token_send(fd, "SIZE");
case MAILIMAP_SORT_KEY_SUBJECT:
return mailimap_token_send(fd, "SUBJECT");
case MAILIMAP_SORT_KEY_TO:
return mailimap_token_send(fd, "TO");
case MAILIMAP_SORT_KEY_MULTIPLE:
r = mailimap_struct_spaced_list_send(fd, key->sortk_multiple,
(mailimap_struct_sender *)
mailimap_sort_key_send);
return MAILIMAP_NO_ERROR;
default:
/* should not happend */
return MAILIMAP_ERROR_INVAL;
}
}
static int
mailimap_number_list_data_sort_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
clist ** result,
size_t progr_rate,
progress_function * progr_fun)
{
size_t cur_token;
clist * number_list;
int r;
int res;
cur_token = * indx;
r = mailimap_token_case_insensitive_parse(fd, buffer, &cur_token, "SORT");
if (r != MAILIMAP_NO_ERROR) {
res = r;
return r;
}
r = mailimap_space_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
return r;
}
r = mailimap_struct_spaced_list_parse(fd, buffer, &cur_token, &number_list,
(mailimap_struct_parser *)
mailimap_nz_number_alloc_parse,
(mailimap_struct_destructor *)
mailimap_number_alloc_free,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
return r;
}
* result = number_list;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
}
static int
mailimap_sort_extension_parse(int calling_parser, mailstream * fd,
MMAPString * buffer, size_t * indx,
struct mailimap_extension_data ** result,
size_t progr_rate, progress_function * progr_fun)
{
int r;
clist * number_list = NULL;
struct mailimap_extension_data * ext_data;
void * data;
int type;
size_t cur_token;
cur_token = * indx;
switch (calling_parser)
{
case MAILIMAP_EXTENDED_PARSER_MAILBOX_DATA:
r = mailimap_number_list_data_sort_parse(fd, buffer, &cur_token,
&number_list, progr_rate, progr_fun);
if (r == MAILIMAP_NO_ERROR) {
type = MAILIMAP_SORT_TYPE_SORT;
data = number_list;
}
if (r != MAILIMAP_NO_ERROR) {
return r;
}
ext_data = mailimap_extension_data_new(&mailimap_extension_sort,
type, data);
if (ext_data == NULL) {
if (number_list != NULL)
mailimap_mailbox_data_search_free(number_list);
return MAILIMAP_ERROR_MEMORY;
}
* result = ext_data;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
default:
/* return a MAILIMAP_ERROR_PARSE if the extension
doesn't extend calling_parser. */
return MAILIMAP_ERROR_PARSE;
}
}
static void
mailimap_sort_extension_data_free(struct mailimap_extension_data * ext_data)
{
if (ext_data->ext_data != NULL) {
mailimap_mailbox_data_search_free((clist *) ext_data->ext_data);
}
free(ext_data);
}

+ 100
- 0
src/low-level/imap/mailimap_sort.h View File

@ -0,0 +1,100 @@
/*
* libEtPan! -- a mail stuff library
*
* Copyright (C) 2001, 2011 - DINH Viet Hoa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the libEtPan! project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef libetpan_mailimap_sort_h
#define libetpan_mailimap_sort_h
#ifdef __cplusplus
extern "C" {
#endif
#include <libetpan/libetpan-config.h>
#include <libetpan/mailimap_extension.h>
#include <libetpan/mailimap_sort_types.h>
LIBETPAN_EXPORT
extern struct mailimap_extension_api mailimap_extension_sort;
/*
mailimap_sort()
All mails that match the given criteria will be returned
their numbers sorted by the given sorting criteria in the result list.
@param session IMAP session
@param charset This indicates the charset of the strings that appears
in the searching criteria
@param key This is the searching criteria
@param result The result is a clist of (uint32_t *) and will be
stored in (* result).
@return the return code is one of MAILIMAP_ERROR_XXX or
MAILIMAP_NO_ERROR codes
*/
LIBETPAN_EXPORT
int
mailimap_sort(mailimap * session, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey,
clist ** result);
/*
mailimap_uid_sort()
All mails that match the given criteria will be returned
their unique identifiers sorted by the given sorting criteria in the result list.
@param session IMAP session
@param charset This indicates the charset of the strings that appears
in the searching criteria
@param key This is the sorting criteria
@param searchkey This is the searching criteria
@param result The result is a clist of (uint32_t *) and will be
stored in (* result).
@return the return code is one of MAILIMAP_ERROR_XXX or
MAILIMAP_NO_ERROR codes
*/
LIBETPAN_EXPORT
int
mailimap_uid_sort(mailimap * session, const char * charset,
struct mailimap_sort_key * key, struct mailimap_search_key * searchkey,
clist ** result);
#ifdef __cplusplus
}
#endif
#endif

+ 122
- 0
src/low-level/imap/mailimap_sort_types.c View File

@ -0,0 +1,122 @@
//
// mailimap_sort_types.c
// libetpan
//
// Created by Pitiphong Phongpattranont on 28/3/56 BE.
//
//
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "mailimap_sort_types.h"
#include "mmapstring.h"
#include "mail.h"
#include "mailimap_extension.h"
#include "mailimap_sort.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
struct mailimap_sort_key *
mailimap_sort_key_new(int sortk_type,
_Bool is_reverse,
clist * sortk_multiple) {
struct mailimap_sort_key * key;
key = malloc(sizeof(* key));
if (key == NULL)
return NULL;
key->sortk_type = sortk_type;
key->sortk_is_reverse = is_reverse;
if (sortk_type == MAILIMAP_SORT_KEY_MULTIPLE) {
key->sortk_multiple = sortk_multiple;
}
return key;
}
void mailimap_sort_key_free(struct mailimap_sort_key * key) {
if (key->sortk_type == MAILIMAP_SORT_KEY_MULTIPLE) {
clist_foreach(key->sortk_multiple,
(clist_func) mailimap_sort_key_free, NULL);
clist_free(key->sortk_multiple);
}
free(key);
}
struct mailimap_sort_key *
mailimap_sort_key_new_arrival(_Bool is_reverse) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_ARRIVAL, is_reverse, NULL);
}
struct mailimap_sort_key *
mailimap_sort_key_new_cc(_Bool is_reverse) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_CC, is_reverse, NULL);
}
struct mailimap_sort_key *
mailimap_sort_key_new_date(_Bool is_reverse) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_DATE, is_reverse, NULL);
}
struct mailimap_sort_key *
mailimap_sort_key_new_from(_Bool is_reverse) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_FROM, is_reverse, NULL);
}
struct mailimap_sort_key *
mailimap_sort_key_new_size(_Bool is_reverse) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_SIZE, is_reverse, NULL);
}
struct mailimap_sort_key *
mailimap_sort_key_new_subject(_Bool is_reverse) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_SUBJECT, is_reverse, NULL);
}
struct mailimap_sort_key *
mailimap_sort_key_new_to(_Bool is_reverse) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_TO, is_reverse, NULL);
}
struct mailimap_sort_key *
mailimap_sort_key_new_multiple(clist * keys) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_MULTIPLE, false, keys);
}
struct mailimap_sort_key *
mailimap_sort_key_new_multiple_empty(void)
{
clist * list;
list = clist_new();
if (list == NULL)
return NULL;
return mailimap_sort_key_new_multiple(list);
}
int
mailimap_sort_key_multiple_add(struct mailimap_sort_key * keys,
struct mailimap_sort_key * key_item)
{
int r;
r = clist_append(keys->sortk_multiple, key_item);
if (r < 0)
return MAILIMAP_ERROR_MEMORY;
return MAILIMAP_NO_ERROR;
}

+ 111
- 0
src/low-level/imap/mailimap_sort_types.h View File

@ -0,0 +1,111 @@
/*
* libEtPan! -- a mail stuff library
*
* Copyright (C) 2001, 2011 - DINH Viet Hoa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the libEtPan! project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef UIDPLUS_TYPES_H
#define UIDPLUS_TYPES_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef WIN32
#include <inttypes.h>
#endif
#include "mailimap_types.h"
/* this is the condition of the SORT operation */
enum {
MAILIMAP_SORT_KEY_ARRIVAL,
MAILIMAP_SORT_KEY_CC,
MAILIMAP_SORT_KEY_DATE,
MAILIMAP_SORT_KEY_FROM,
MAILIMAP_SORT_KEY_SIZE,
MAILIMAP_SORT_KEY_SUBJECT,
MAILIMAP_SORT_KEY_TO,
MAILIMAP_SORT_KEY_MULTIPLE
};
struct mailimap_sort_key {
int sortk_type;
_Bool sortk_is_reverse;
clist * sortk_multiple; /* list of (struct mailimap_sort_key *) */
};
LIBETPAN_EXPORT
struct mailimap_sort_key *
mailimap_sort_key_new(int sortk_type,
_Bool is_reverse,
clist * sortk_multiple);
LIBETPAN_EXPORT
void mailimap_sort_key_free(struct mailimap_sort_key * key);
struct mailimap_sort_key *
mailimap_sort_key_new_arrival(_Bool is_reverse);
struct mailimap_sort_key *
mailimap_sort_key_new_cc(_Bool is_reverse);
struct mailimap_sort_key *
mailimap_sort_key_new_date(_Bool is_reverse);
struct mailimap_sort_key *
mailimap_sort_key_new_from(_Bool is_reverse);
struct mailimap_sort_key *
mailimap_sort_key_new_size(_Bool is_reverse);
struct mailimap_sort_key *
mailimap_sort_key_new_subject(_Bool is_reverse);
struct mailimap_sort_key *
mailimap_sort_key_new_to(_Bool is_reverse);
struct mailimap_sort_key *
mailimap_sort_key_new_multiple(clist * keys);
struct mailimap_sort_key *
mailimap_sort_key_new_multiple_empty(void);
int
mailimap_sort_key_multiple_add(struct mailimap_sort_key * keys,
struct mailimap_sort_key * key_item);
#ifdef __cplusplus
}
#endif
#endif

+ 0
- 31
src/low-level/imap/mailimap_types.c View File

@ -2846,37 +2846,6 @@ void mailimap_search_key_free(struct mailimap_search_key * key)
}
struct mailimap_sort_key *
mailimap_sort_key_new(int sortk_type,
_Bool is_reverse,
clist * sortk_multiple) {
struct mailimap_sort_key * key;
key = malloc(sizeof(* key));
if (key == NULL)
return NULL;
key->sortk_type = sortk_type;
key->sortk_is_reverse = is_reverse;
if (sortk_type == MAILIMAP_SORT_KEY_MULTIPLE) {
key->sortk_multiple = sortk_multiple;
}
return key;
}
void mailimap_sort_key_free(struct mailimap_sort_key * key) {
if (key->sortk_type == MAILIMAP_SORT_KEY_MULTIPLE) {
clist_foreach(key->sortk_multiple,
(clist_func) mailimap_sort_key_free, NULL);
clist_free(key->sortk_multiple);
}
free(key);
}


+ 0
- 31
src/low-level/imap/mailimap_types.h View File

@ -2996,37 +2996,6 @@ mailimap_search_key_new(int sk_type,
LIBETPAN_EXPORT
void mailimap_search_key_free(struct mailimap_search_key * key);
/* this is the condition of the SORT operation */
enum {
MAILIMAP_SORT_KEY_ARRIVAL,
MAILIMAP_SORT_KEY_CC,
MAILIMAP_SORT_KEY_DATE,
MAILIMAP_SORT_KEY_FROM,
MAILIMAP_SORT_KEY_SIZE,
MAILIMAP_SORT_KEY_SUBJECT,
MAILIMAP_SORT_KEY_TO,
MAILIMAP_SORT_KEY_MULTIPLE
};
struct mailimap_sort_key {
int sortk_type;
_Bool sortk_is_reverse;
clist * sortk_multiple; /* list of (struct mailimap_sort_key *) */
};
LIBETPAN_EXPORT
struct mailimap_sort_key *
mailimap_sort_key_new(int sortk_type,
_Bool is_reverse,
clist * sortk_multiple);
LIBETPAN_EXPORT
void mailimap_sort_key_free(struct mailimap_sort_key * key);
/*
mailimap_status_att_list is a list of mailbox STATUS request type


+ 0
- 68
src/low-level/imap/mailimap_types_helper.c View File

@ -994,74 +994,6 @@ mailimap_search_key_multiple_add(struct mailimap_search_key * keys,
}
struct mailimap_sort_key *
mailimap_sort_key_new_arrival(_Bool is_reverse) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_ARRIVAL, is_reverse, NULL);
}
struct mailimap_sort_key *
mailimap_sort_key_new_cc(_Bool is_reverse) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_CC, is_reverse, NULL);
}
struct mailimap_sort_key *
mailimap_sort_key_new_date(_Bool is_reverse) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_DATE, is_reverse, NULL);
}
struct mailimap_sort_key *
mailimap_sort_key_new_from(_Bool is_reverse) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_FROM, is_reverse, NULL);
}
struct mailimap_sort_key *
mailimap_sort_key_new_size(_Bool is_reverse) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_SIZE, is_reverse, NULL);
}
struct mailimap_sort_key *
mailimap_sort_key_new_subject(_Bool is_reverse) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_SUBJECT, is_reverse, NULL);
}
struct mailimap_sort_key *
mailimap_sort_key_new_to(_Bool is_reverse) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_TO, is_reverse, NULL);
}
struct mailimap_sort_key *
mailimap_sort_key_new_multiple(clist * keys) {
return mailimap_sort_key_new(MAILIMAP_SORT_KEY_MULTIPLE, false, keys);
}
struct mailimap_sort_key *
mailimap_sort_key_new_multiple_empty(void)
{
clist * list;
list = clist_new();
if (list == NULL)
return NULL;
return mailimap_sort_key_new_multiple(list);
}
int
mailimap_sort_key_multiple_add(struct mailimap_sort_key * keys,
struct mailimap_sort_key * key_item)
{
int r;
r = clist_append(keys->sortk_multiple, key_item);
if (r < 0)
return MAILIMAP_ERROR_MEMORY;
return MAILIMAP_NO_ERROR;
}
/* CAPABILITY */
/* no args */


Loading…
Cancel
Save