/* Copyright 2001-2005 The Apache Software Foundation or its licensors, as
 * applicable.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef LIBMBOX_H
#define LIBMBOX_H

#define CORE_PRIVATE
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_main.h"
#include "http_protocol.h"
#include "http_request.h"
#include "util_script.h"

#include <stdio.h>
#include <ctype.h>

#include "apr.h"
#include "apr_pools.h"
#include "apr_strings.h"
#include "apr_date.h"
#include "apr_buckets.h"
#include "apr_dbm.h"
#include "apr_mmap.h"
#include "apr_xlate.h"

#ifndef NO_MBOX_SEARCH
#include "lcn_init.h"
#include "lcn_document.h"
#include "lcn_pools.h"
#include "lcn_index_writer.h"
#include "lcn_query_parser.h"
#include "lcn_searcher.h"
#endif

#define MBOX_SORT_DATE   0
#define MBOX_SORT_AUTHOR 1
#define MBOX_SORT_THREAD 2
#define MBOX_SORT_REVERSE_DATE 3

#define MBOX_CACHE_VERSION 0x2

#ifdef __cplusplus
extern "C" {
#endif

/*
 * MBOX_LIST is a generic linked list node.
 * key is the value associated with the node
 * value is the value associated with the key
 * next is the next item in the list
 */
typedef struct mbox_list_struct MBOX_LIST;

struct mbox_list_struct{
    MBOX_LIST * next;
    apr_time_t key;
    void * value;
};

/*
 * All possible Content-Transfer-Encodings.
 */
typedef enum {
    CTE_NONE = 0,
    CTE_7BIT = 1,
    CTE_8BIT = 2,
    CTE_UUENCODE = 3,
    CTE_BINARY = 4,
    CTE_QP = 5,
    CTE_BASE64 = 6,
} mbox_cte_e;

mbox_cte_e mbox_parse_cte_header(char* src);

/* The following is based on Jamie Zawinski's description of the Netscape 3.x
 * threading algorithm at <http://www.jwz.org/doc/threading.html>.
 */

/* As JWZ says, this shouldn't be char*, but something that could be
 * derived from an MD5 hash.  That can come later.
 */
typedef char* ID;

typedef struct mbox_mime_message
{
    char *body;
    apr_size_t body_len;
    char *boundary;

    char *content_type;
    char *content_encoding;
    char *content_disposition;
    char *content_name;
    mbox_cte_e cte;

    struct mbox_mime_message **sub;
    unsigned int sub_count;
} mbox_mime_message_t;

/* Typedefs for Message and Container */
typedef struct Message_Struct Message;
typedef struct Container_Struct Container;

/* The basic information about a message.  */
struct Message_Struct
{
    ID msgID;

    char *from;
    char *str_from;

    char *subject;

    apr_time_t date;
    char *str_date;
    char *rfc822_date;

    char *content_type;
    char *boundary;
    mbox_cte_e cte;

    apr_table_t *references;
    char *raw_ref;

    apr_off_t msg_start;
    apr_off_t body_start;
    apr_off_t body_end;

    char *raw_msg;
    char *raw_body;

    mbox_mime_message_t *mime_msg;
};

/* The threading information about a message. */
struct Container_Struct
{
    Message* message;
    Container* parent;  /* Only one parent */
    Container* child;   /* Many children */
    Container* next;    /* Many siblings */
};

/**
 * Supported Mailing List Managers
 *
 * Changes how directions are given to subscribe.
 */
typedef enum mbox_mlist_manager_e {
    MANAGER_EZMLM = 0,
} mbox_mlist_manager_e;

typedef struct mbox_cache_info {
    int version;
    apr_time_t mtime;
    mbox_mlist_manager_e type;
    const char* list;
    const char* domain;
    apr_dbm_t* db;
    apr_pool_t* pool;
} mbox_cache_info;

typedef struct mbox_search_doc_t {
    const char* msgid;
    const char* list;
    const char* domain;
    const char* from;
    const char* subject;
    char date[APR_RFC822_DATE_LEN+1];
    float score;
} mbox_search_doc_t;

typedef struct mbox_search_query_t {
    const char* msgid;
    const char* list;
    const char* domain;
    const char* from;
    const char* subject;
    const char* terms;
} mbox_search_query_t;

typedef struct mbox_searcher_t {
    void* reader;
} mbox_searcher_t;

typedef struct mbox_indexer_t {
#ifndef NO_MBOX_SEARCH
    apr_pool_t* pool;
    apr_pool_t* tpool;
    lcn_analyzer_t *analyzer;
    lcn_index_writer_t* writer;
    lcn_searcher_t* searcher;
    const char* path;
#endif
} mbox_indexer_t;

/** mbox_thread **/
Container* calculate_threads(apr_pool_t *p, MBOX_LIST *l);

/** mbox_cache **/
APR_DECLARE(apr_status_t)
mbox_cache_get(mbox_cache_info** mli,
                     const char* path, apr_pool_t* p);

APR_DECLARE(apr_status_t)
mbox_cache_get_count(mbox_cache_info* mli, int *count, char* path);

APR_DECLARE(void)
mbox_cache_close(mbox_cache_info* mli);

APR_DECLARE(apr_status_t)
mbox_cache_update(mbox_cache_info** mli,
                        const char* path, apr_pool_t* p,
                        char* list, char* domain);

APR_DECLARE(apr_status_t)
mbox_cache_set_count(mbox_cache_info* mli, int count, char* path);

APR_DECLARE(apr_status_t)
mbox_cache_touch(mbox_cache_info* mli);

/** mbox_sort **/
void *mbox_sort_linked_list(void *p, unsigned index,
     int (*compare)(void *, void *, void *), void *pointer,
     unsigned long *pcount);

/** mbox_parse **/
/* Sorts a list of MBOX_LIST items by the specified order. */
MBOX_LIST* mbox_sort_list(MBOX_LIST* l, int sortFlags);

/* Generates the DBM file. */
apr_status_t mbox_generate_index(request_rec *r, apr_file_t * f,
                            mbox_indexer_t *indexer, const char* list,
                            const char* domain);

/* Returns a list of Messages */
MBOX_LIST* mbox_load_index(request_rec *r, apr_file_t *f, int *count);

/* Returns a single message based on message ID */
Message* mbox_fetch_index(request_rec *r, apr_file_t * f, const char * msgID);

/* Get the total message count for a file. */
int mbox_msg_count(request_rec *r, char* path);
char* mbox_get_list_post(request_rec *r, char* path);

/** mbox_search **/
apr_status_t mbox_indexer_init(mbox_indexer_t** ctx, const char* path,
                               apr_pool_t* pool);

apr_status_t mbox_indexer_optimize(mbox_indexer_t* ctx);

apr_status_t mbox_indexer_close(mbox_indexer_t* ctx);

apr_status_t mbox_indexer_add(mbox_indexer_t* ctx, mbox_search_doc_t* doc);

typedef int (query_callback_fn_t)(void* baton, int position,
                                  mbox_search_doc_t* rdoc);

apr_status_t mbox_search_init(mbox_searcher_t** ctx_, const char* path,
                              apr_pool_t* pool);

apr_status_t mbox_search_query_do(mbox_searcher_t* ctx,
                                  mbox_search_query_t* qt,
                                  query_callback_fn_t* cb, void* baton);

apr_status_t mbox_search_close(mbox_searcher_t* ctx);

/** mbox_mime */
mbox_mime_message_t *mbox_mime_decode_multipart(apr_pool_t *p, char *body,
						char *ct, mbox_cte_e cte,
						char *boundary);

/** mbox_cte **/
const char *mbox_cte_to_char(mbox_cte_e cte);
apr_size_t mbox_cte_decode_qp(char* p);
apr_size_t mbox_cte_decode_b64(char *src);
apr_size_t mbox_cte_escape_html(apr_pool_t *p, const char *s,
				apr_size_t len, char **body);
char *mbox_cte_decode_rfc2047(apr_pool_t *p, char *src);
char *mbox_cte_decode_header(apr_pool_t *p, char *src);

#ifdef __cplusplus
}
#endif

#endif /* LIBMBOX_H */
