comparison outgoing.c @ 0:04124a4423d4 noffle

[svn] Initial revision
author enz
date Tue, 04 Jan 2000 11:35:42 +0000
parents
children 526a4c34ee2e
comparison
equal deleted inserted replaced
-1:000000000000 0:04124a4423d4
1 /*
2 outgoing.c
3
4 $Id: outgoing.c 3 2000-01-04 11:35:42Z enz $
5 */
6
7 #include "outgoing.h"
8
9 #include <dirent.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <time.h>
15 #include <unistd.h>
16 #include "config.h"
17 #include "log.h"
18 #include "util.h"
19
20 struct Outgoing
21 {
22 DIR *dir;
23 Str serv;
24 } outgoing = { NULL, "" };
25
26 static void
27 fileOutgoing( Str file, const char *serv, const char *msgId )
28 {
29 snprintf( file, MAXCHAR, "%s/outgoing/%s/%s",
30 Cfg_spoolDir(), serv, msgId );
31 }
32
33 static void
34 createDir( const char *serv )
35 {
36 Str dir;
37 int r;
38
39 snprintf( dir, MAXCHAR, "%s/outgoing/%s", Cfg_spoolDir(), serv );
40 r = mkdir( dir, 0755 );
41 if ( r != 0 )
42 Log_dbg( "mkdir: %s", strerror( errno ) );
43 }
44
45 Bool
46 Out_add( const char *serv, const Str msgId, const DynStr *artTxt )
47 {
48 Str file;
49 FILE *f;
50
51 fileOutgoing( file, serv, msgId );
52 if ( ! ( f = fopen( file, "w" ) ) )
53 {
54 createDir( serv );
55 if ( ! ( f = fopen( file, "w" ) ) )
56 {
57 Log_err( "Cannot open %s", file );
58 return FALSE;
59 }
60 }
61 fprintf( f, "%s", DynStr_str( artTxt ) );
62 fclose( f );
63 return TRUE;
64 }
65
66 Bool
67 Out_first( const char *serv, Str msgId, DynStr *artTxt )
68 {
69 Str file;
70
71 snprintf( file, MAXCHAR, "%s/outgoing/%s", Cfg_spoolDir(), serv );
72 if ( ! ( outgoing.dir = opendir( file ) ) )
73 {
74 Log_dbg( "Cannot open %s", file );
75 return FALSE;
76 }
77 Utl_cpyStr( outgoing.serv, serv );
78 Out_next( NULL, NULL ); /* "." */
79 Out_next( NULL, NULL ); /* ".." */
80 return Out_next( msgId, artTxt );
81 }
82
83 Bool
84 Out_next( Str msgId, DynStr *artTxt )
85 {
86 struct dirent *d;
87 FILE *f;
88 Str file, line;
89
90 ASSERT( outgoing.dir );
91 if ( ! ( d = readdir( outgoing.dir ) ) )
92 {
93 closedir( outgoing.dir );
94 outgoing.dir = NULL;
95 return FALSE;
96 }
97 if ( artTxt == NULL )
98 return ( d->d_name != NULL );
99 fileOutgoing( file, outgoing.serv, d->d_name );
100 if ( ! ( f = fopen( file, "r" ) ) )
101 {
102 Log_err( "Cannot open %s for read", file );
103 return FALSE;
104 }
105 DynStr_clear( artTxt );
106 while ( fgets( line, MAXCHAR, f ) )
107 DynStr_app( artTxt, line );
108 Utl_cpyStr( msgId, d->d_name );
109 fclose( f );
110 return TRUE;
111 }
112
113 void
114 Out_remove( const char *serv, Str msgId )
115 {
116 Str file;
117
118 fileOutgoing( file, serv, msgId );
119 if ( unlink( file ) != 0 )
120 Log_err( "Cannot remove %s", file );
121 }