Diffstat (limited to 'libetpan/src/driver/implementation/db/dbstorage.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libetpan/src/driver/implementation/db/dbstorage.c | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/libetpan/src/driver/implementation/db/dbstorage.c b/libetpan/src/driver/implementation/db/dbstorage.c new file mode 100644 index 0000000..c4be63c --- a/dev/null +++ b/libetpan/src/driver/implementation/db/dbstorage.c | |||
@@ -0,0 +1,144 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include "dbstorage.h" | ||
37 | #include "mailstorage.h" | ||
38 | |||
39 | #include "mail.h" | ||
40 | #include "mailmessage.h" | ||
41 | #include "dbdriver.h" | ||
42 | #include "maildriver.h" | ||
43 | |||
44 | #include <stdlib.h> | ||
45 | #include <string.h> | ||
46 | |||
47 | /* db storage */ | ||
48 | |||
49 | static int db_mailstorage_connect(struct mailstorage * storage); | ||
50 | static int | ||
51 | db_mailstorage_get_folder_session(struct mailstorage * storage, | ||
52 | char * pathname, mailsession ** result); | ||
53 | static void db_mailstorage_uninitialize(struct mailstorage * storage); | ||
54 | |||
55 | static mailstorage_driver db_mailstorage_driver = { | ||
56 | .sto_name = "db", | ||
57 | .sto_connect = db_mailstorage_connect, | ||
58 | .sto_get_folder_session = db_mailstorage_get_folder_session, | ||
59 | .sto_uninitialize = db_mailstorage_uninitialize, | ||
60 | }; | ||
61 | |||
62 | int db_mailstorage_init(struct mailstorage * storage, | ||
63 | char * db_pathname) | ||
64 | { | ||
65 | struct db_mailstorage * db_storage; | ||
66 | |||
67 | db_storage = malloc(sizeof(* db_storage)); | ||
68 | if (db_storage == NULL) | ||
69 | goto err; | ||
70 | |||
71 | db_storage->db_pathname = strdup(db_pathname); | ||
72 | if (db_storage->db_pathname == NULL) | ||
73 | goto free; | ||
74 | |||
75 | storage->sto_data = db_storage; | ||
76 | storage->sto_driver = &db_mailstorage_driver; | ||
77 | |||
78 | return MAIL_NO_ERROR; | ||
79 | |||
80 | free: | ||
81 | free(db_storage); | ||
82 | err: | ||
83 | return MAIL_ERROR_MEMORY; | ||
84 | } | ||
85 | |||
86 | static void db_mailstorage_uninitialize(struct mailstorage * storage) | ||
87 | { | ||
88 | struct db_mailstorage * db_storage; | ||
89 | |||
90 | db_storage = storage->sto_data; | ||
91 | free(db_storage->db_pathname); | ||
92 | free(db_storage); | ||
93 | |||
94 | storage->sto_data = NULL; | ||
95 | } | ||
96 | |||
97 | static int db_mailstorage_connect(struct mailstorage * storage) | ||
98 | { | ||
99 | struct db_mailstorage * db_storage; | ||
100 | mailsession_driver * driver; | ||
101 | int r; | ||
102 | int res; | ||
103 | mailsession * session; | ||
104 | |||
105 | db_storage = storage->sto_data; | ||
106 | |||
107 | driver = db_session_driver; | ||
108 | |||
109 | session = mailsession_new(driver); | ||
110 | if (session == NULL) { | ||
111 | res = MAIL_ERROR_MEMORY; | ||
112 | goto err; | ||
113 | } | ||
114 | |||
115 | r = mailsession_connect_path(session, db_storage->db_pathname); | ||
116 | switch (r) { | ||
117 | case MAIL_NO_ERROR_NON_AUTHENTICATED: | ||
118 | case MAIL_NO_ERROR_AUTHENTICATED: | ||
119 | case MAIL_NO_ERROR: | ||
120 | break; | ||
121 | default: | ||
122 | res = r; | ||
123 | goto free; | ||
124 | } | ||
125 | |||
126 | storage->sto_session = session; | ||
127 | |||
128 | return MAIL_NO_ERROR; | ||
129 | |||
130 | free: | ||
131 | mailsession_free(session); | ||
132 | err: | ||
133 | return res; | ||
134 | } | ||
135 | |||
136 | static int | ||
137 | db_mailstorage_get_folder_session(struct mailstorage * storage, | ||
138 | char * pathname, mailsession ** result) | ||
139 | { | ||
140 | * result = storage->sto_session; | ||
141 | |||
142 | return MAIL_NO_ERROR; | ||
143 | } | ||
144 | |||