Commit 100fe99c 100fe99c3cc661d2a2915f0125e1496f2c944a7c by Jakob Kaivo

Added locking function and interface

Added mbox_header_line
Added mbox_body_lines
1 parent 3dea9405
1 /* copyright and license info go here */ 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
2 17
3 #include "mailbox.h" 18 #include "mailbox.h"
4 #include "unixmbox.h" 19 #include "unixmbox.h"
...@@ -40,6 +55,7 @@ mbox_open (const char *name) ...@@ -40,6 +55,7 @@ mbox_open (const char *name)
40 mbox->_expunge = _mbox_dummy1; 55 mbox->_expunge = _mbox_dummy1;
41 mbox->_add_message = _mbox_dummy3; 56 mbox->_add_message = _mbox_dummy3;
42 mbox->_is_deleted = _mbox_dummy2; 57 mbox->_is_deleted = _mbox_dummy2;
58 mbox->_lock = _mbox_dummy2;
43 mbox->_get_body = _mbox_dummy4; 59 mbox->_get_body = _mbox_dummy4;
44 mbox->_get_header = _mbox_dummy4; 60 mbox->_get_header = _mbox_dummy4;
45 61
...@@ -68,6 +84,97 @@ mbox_open (const char *name) ...@@ -68,6 +84,97 @@ mbox_open (const char *name)
68 } 84 }
69 85
70 /* 86 /*
87 * Gets the contents of a header field
88 */
89 char *
90 mbox_header_line (mailbox *mbox, int num, const char *header)
91 {
92 char *full, *tmp, *line;
93 int i = 0, j=0, try = 1, len;
94 int lh = strlen (header);
95 full = mbox_get_header (mbox, num);
96 if (full == NULL)
97 return NULL;
98
99 len = strlen (full);
100
101 /* First get the appropriate line at the beginning */
102 for (i=0; i < len-(lh+2); i++)
103 {
104 if (try == 1)
105 {
106 if (!strncasecmp(&full[i], header, lh) && full[i+lh] == ':')
107 {
108 full[len-i] = '\0';
109 tmp = strdup (&full[i+lh+2]);
110 i = len;
111 }
112 else
113 try = 0;
114 }
115 else if (full[i] == '\n')
116 try = 1;
117 else
118 try = 0;
119 }
120
121 /* Now trim the fat */
122 len = strlen (tmp);
123 for (i = 0; i < len; i++)
124 {
125 if (tmp[i] == '\n' && i < (len - 1) && (tmp[i+1] == ' ' || tmp[i+1] == '\t'))
126 {
127 if (tmp[i+1] == '\t')
128 tmp[i + 1] = ' ';
129 for (j = i; j < len; j++)
130 tmp[j] = tmp[j+1];
131 }
132 else if (tmp[i] == '\n')
133 {
134 tmp[i] = '\0';
135 i = len;
136 }
137 }
138 line = strdup (tmp);
139 free (tmp);
140 free (full);
141 return line;
142 }
143
144 /*
145 * Gets first LINES lines from message body
146 */
147 char *
148 mbox_body_lines (mailbox *mbox, int num, int lines)
149 {
150 char *full, *buf = NULL;
151 int i=0, line = 0, len;
152 if (lines < 1)
153 return strdup ("");
154 full = mbox_get_body (mbox, num);
155 if (full == NULL)
156 return NULL;
157 len = strlen (full);
158 for (i=0; i < len && line < lines; i++)
159 {
160 if (full[i] == '\n')
161 {
162 line++;
163 if (line >= lines)
164 {
165 full[i+1] = '\0';
166 buf = strdup (full);
167 i = len;
168 }
169 }
170 }
171 if (buf == NULL)
172 buf = strdup (full);
173 free (full);
174 return buf;
175 }
176
177 /*
71 * Bogus functions for unimplemented functions that return int 178 * Bogus functions for unimplemented functions that return int
72 */ 179 */
73 int 180 int
...@@ -97,3 +204,4 @@ _mbox_dummy4 (mailbox * mbox, int num) ...@@ -97,3 +204,4 @@ _mbox_dummy4 (mailbox * mbox, int num)
97 errno = ENOSYS; 204 errno = ENOSYS;
98 return NULL; 205 return NULL;
99 } 206 }
207
......
1 /* copyright and license info go here */ 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
2 17
3 #ifndef _MAILBOX_H 18 #ifndef _MAILBOX_H
4 #define _MAILBOX_H 1 19 #define _MAILBOX_H 1
...@@ -20,6 +35,12 @@ ...@@ -20,6 +35,12 @@
20 #define mbox_add_message(m,s) m->_add_message(m,s) 35 #define mbox_add_message(m,s) m->_add_message(m,s)
21 #define mbox_get_body(m,n) m->_get_body(m,n) 36 #define mbox_get_body(m,n) m->_get_body(m,n)
22 #define mbox_get_header(m,n) m->_get_header(m,n) 37 #define mbox_get_header(m,n) m->_get_header(m,n)
38 #define mbox_lock(m,n) m->_lock(m,n)
39
40 /* Lock settings */
41 #define MO_ULOCK 0
42 #define MO_RLOCK 1
43 #define MO_WLOCK 2
23 44
24 typedef struct _mailbox 45 typedef struct _mailbox
25 { 46 {
...@@ -37,6 +58,7 @@ typedef struct _mailbox ...@@ -37,6 +58,7 @@ typedef struct _mailbox
37 int (*_expunge) __P ((struct _mailbox *)); 58 int (*_expunge) __P ((struct _mailbox *));
38 int (*_add_message) __P ((struct _mailbox *, char *)); 59 int (*_add_message) __P ((struct _mailbox *, char *));
39 int (*_is_deleted) __P ((struct _mailbox *, int)); 60 int (*_is_deleted) __P ((struct _mailbox *, int));
61 int (*_lock) __P((struct _mailbox *, int));
40 char *(*_get_body) __P ((struct _mailbox *, int)); 62 char *(*_get_body) __P ((struct _mailbox *, int));
41 char *(*_get_header) __P ((struct _mailbox *, int)); 63 char *(*_get_header) __P ((struct _mailbox *, int));
42 } 64 }
...@@ -44,4 +66,9 @@ mailbox; ...@@ -44,4 +66,9 @@ mailbox;
44 66
45 mailbox *mbox_open __P ((const char *name)); 67 mailbox *mbox_open __P ((const char *name));
46 68
69 char *mbox_header_line __P ((mailbox *mbox, int num, const char *header));
70 char *mbox_body_lines __P ((mailbox *mbox, int num, int lines));
71
47 #endif 72 #endif
73
74
......
1 /* copyright and license info go here */ 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
2 17
3 #include "unixmbox.h" 18 #include "unixmbox.h"
4 19
5 /* 20 /*
6 * Opens and locks a mailbox 21 * Opens a mailbox
7 */ 22 */
8 int 23 int
9 unixmbox_open (mailbox * mbox) 24 unixmbox_open (mailbox * mbox)
...@@ -22,7 +37,6 @@ unixmbox_open (mailbox * mbox) ...@@ -22,7 +37,6 @@ unixmbox_open (mailbox * mbox)
22 37
23 mbox->_data = data; 38 mbox->_data = data;
24 data->file = fopen (mbox->name, "r+"); 39 data->file = fopen (mbox->name, "r+");
25 /* FIXME: do a good, NFS safe, file lock here, with error handling */
26 if (data->file == NULL) 40 if (data->file == NULL)
27 { 41 {
28 /* errno is set by fopen() */ 42 /* errno is set by fopen() */
...@@ -39,6 +53,17 @@ unixmbox_open (mailbox * mbox) ...@@ -39,6 +53,17 @@ unixmbox_open (mailbox * mbox)
39 return -1; 53 return -1;
40 } 54 }
41 55
56 fgets (buf, 80, data->file);
57 if (strncmp (buf, "From ", 5))
58 {
59 /* This is NOT an mbox file */
60 unixmbox_close (mbox);
61 errno = 0;
62 return -1;
63 }
64 else
65 rewind (data->file);
66
42 while (fgets (buf, 80, data->file)) 67 while (fgets (buf, 80, data->file))
43 { 68 {
44 if (!strncmp (buf, "From ", 5)) 69 if (!strncmp (buf, "From ", 5))
...@@ -88,6 +113,7 @@ unixmbox_open (mailbox * mbox) ...@@ -88,6 +113,7 @@ unixmbox_open (mailbox * mbox)
88 mbox->_expunge = unixmbox_expunge; 113 mbox->_expunge = unixmbox_expunge;
89 mbox->_add_message = unixmbox_add_message; 114 mbox->_add_message = unixmbox_add_message;
90 mbox->_is_deleted = unixmbox_is_deleted; 115 mbox->_is_deleted = unixmbox_is_deleted;
116 mbox->_lock = unixmbox_lock;
91 mbox->_get_body = unixmbox_get_body; 117 mbox->_get_body = unixmbox_get_body;
92 mbox->_get_header = unixmbox_get_header; 118 mbox->_get_header = unixmbox_get_header;
93 119
...@@ -101,7 +127,7 @@ int ...@@ -101,7 +127,7 @@ int
101 unixmbox_close (mailbox * mbox) 127 unixmbox_close (mailbox * mbox)
102 { 128 {
103 unixmbox_data *data = mbox->_data; 129 unixmbox_data *data = mbox->_data;
104 /* FIXME: good file unlocking, to go along with the locking above */ 130 unixmbox_lock (mbox, MO_ULOCK);
105 fclose (data->file); 131 fclose (data->file);
106 free (data->messages); 132 free (data->messages);
107 free (mbox->sizes); 133 free (mbox->sizes);
...@@ -266,3 +292,17 @@ unixmbox_get_header (mailbox * mbox, int num) ...@@ -266,3 +292,17 @@ unixmbox_get_header (mailbox * mbox, int num)
266 fread (buf, size, sizeof (char), data->file); 292 fread (buf, size, sizeof (char), data->file);
267 return buf; 293 return buf;
268 } 294 }
295
296 /*
297 * Sets lock mode for a mailbox
298 * FIXME: this doesn't do anything, really
299 * Get locking code from Procmail and/or Exim
300 */
301 int
302 unixmbox_lock (mailbox *mbox, int mode)
303 {
304 unixmbox_data *data = mbox->_data;
305 data->lockmode = mode;
306 return 0;
307 }
308
......
1 /* copyright and license info go here */ 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
2 17
3 #ifndef _UNIXMBOX_H 18 #ifndef _UNIXMBOX_H
4 #define _UNIXMBOX_H 1 19 #define _UNIXMBOX_H 1
...@@ -40,6 +55,7 @@ typedef struct _unixmbox_data ...@@ -40,6 +55,7 @@ typedef struct _unixmbox_data
40 { 55 {
41 unixmbox_message *messages; 56 unixmbox_message *messages;
42 FILE *file; 57 FILE *file;
58 int lockmode;
43 } 59 }
44 unixmbox_data; 60 unixmbox_data;
45 61
...@@ -49,6 +65,7 @@ int unixmbox_delete (mailbox *mbox, int num); ...@@ -49,6 +65,7 @@ int unixmbox_delete (mailbox *mbox, int num);
49 int unixmbox_undelete (mailbox *mbox, int num); 65 int unixmbox_undelete (mailbox *mbox, int num);
50 int unixmbox_expunge (mailbox *mbox); 66 int unixmbox_expunge (mailbox *mbox);
51 int unixmbox_is_deleted (mailbox *mbox, int num); 67 int unixmbox_is_deleted (mailbox *mbox, int num);
68 int unixmbox_lock (mailbox *mbox, int mode);
52 int unixmbox_add_message (mailbox *mbox, char *message); 69 int unixmbox_add_message (mailbox *mbox, char *message);
53 char *unixmbox_get_body (mailbox *mbox, int num); 70 char *unixmbox_get_body (mailbox *mbox, int num);
54 char *unixmbox_get_header (mailbox *mbox, int num); 71 char *unixmbox_get_header (mailbox *mbox, int num);
......