annotate client.c @ 0:04124a4423d4 noffle

[svn] Initial revision
author enz
date Tue, 04 Jan 2000 11:35:42 +0000
parents
children 6e822f4b19f9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
1 /*
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
2 client.c
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
3
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
4 $Id: client.c 3 2000-01-04 11:35:42Z enz $
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
5 */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
6
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
7 #include "client.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
8
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
9 #include <arpa/inet.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
10 #include <ctype.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
11 #include <netdb.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
12 #include <netinet/in.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
13 #include <signal.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
14 #include <stdarg.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
15 #include <sys/socket.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
16 #include <unistd.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
17 #include "config.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
18 #include "content.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
19 #include "dynamicstring.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
20 #include "group.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
21 #include "log.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
22 #include "over.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
23 #include "protocol.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
24 #include "pseudo.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
25 #include "request.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
26 #include "util.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
27
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
28 struct
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
29 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
30 FILE* in; /* Receiving socket from server */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
31 FILE* out; /* Sending socket to server */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
32 Str lastCmd; /* Last command line */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
33 Str lastStat; /* Response from server to last command */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
34 Str grp; /* Selected group */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
35 int rmtFirst; /* First article of current group at server */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
36 int rmtLast; /* Last article of current group at server */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
37 Bool auth; /* Authetication already done? */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
38 Str serv; /* Remote server name */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
39 } client = { NULL, NULL, "", "", "", 1, 0, FALSE, "" };
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
40
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
41 static void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
42 logBreakDown( void )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
43 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
44 Log_err( "Connection to remote server lost "
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
45 "(article numbers could be inconsistent)" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
46 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
47
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
48 static Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
49 getLn( Str line )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
50 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
51 Bool r;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
52
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
53 r = Prt_getLn( line, client.in );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
54 if ( ! r )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
55 logBreakDown();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
56 return r;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
57 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
58
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
59 static Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
60 getTxtLn( Str line, Bool *err )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
61 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
62 Bool r;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
63
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
64 r = Prt_getTxtLn( line, err, client.in );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
65 if ( *err )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
66 logBreakDown();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
67 return r;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
68 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
69
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
70 static void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
71 putTxtBuf( const char *buf )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
72 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
73 Prt_putTxtBuf( buf, client.out );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
74 fflush( client.out );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
75 Log_dbg( "[S FLUSH]" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
76 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
77
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
78 static void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
79 putEndOfTxt( void )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
80 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
81 Prt_putEndOfTxt( client.out );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
82 fflush( client.out );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
83 Log_dbg( "[S FLUSH]" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
84 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
85
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
86 static Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
87 putCmd( const char *fmt, ... )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
88 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
89 Bool err;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
90 unsigned int n;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
91 Str line;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
92 va_list ap;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
93
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
94 va_start( ap, fmt );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
95 vsnprintf( line, MAXCHAR, fmt, ap );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
96 va_end( ap );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
97 strcpy( client.lastCmd, line );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
98 Log_dbg( "[S] %s", line );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
99 n = fprintf( client.out, "%s\r\n", line );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
100 fflush( client.out );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
101 Log_dbg( "[S FLUSH]" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
102 err = ( n != strlen( line ) + 2 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
103 if ( err )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
104 logBreakDown();;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
105 return ! err;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
106 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
107
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
108 static Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
109 putCmdNoFlush( const char *fmt, ... )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
110 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
111 Bool err;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
112 unsigned int n;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
113 Str line;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
114 va_list ap;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
115
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
116 va_start( ap, fmt );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
117 vsnprintf( line, MAXCHAR, fmt, ap );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
118 va_end( ap );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
119 strcpy( client.lastCmd, line );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
120 Log_dbg( "[S] %s", line );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
121 n = fprintf( client.out, "%s\r\n", line );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
122 err = ( n != strlen( line ) + 2 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
123 if ( err )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
124 logBreakDown();;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
125 return ! err;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
126 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
127
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
128 static int getStat( void );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
129
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
130 static Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
131 performAuth( void )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
132 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
133 int stat;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
134 Str user, pass;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
135
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
136 Cfg_authInfo( client.serv, user, pass );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
137 if ( strcmp( user, "" ) == 0 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
138 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
139 Log_err( "No username for authentication set" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
140 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
141 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
142 putCmd( "AUTHINFO USER %s", user );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
143 stat = getStat();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
144 if ( stat == STAT_AUTH_ACCEPTED )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
145 return TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
146 else if ( stat != STAT_MORE_AUTH_REQUIRED )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
147 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
148 Log_err( "Username rejected. Server stat: %s", client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
149 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
150 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
151 if ( strcmp( pass, "" ) == 0 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
152 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
153 Log_err( "No password for authentication set" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
154 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
155 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
156 putCmd( "AUTHINFO PASS %s", pass );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
157 stat = getStat();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
158 if ( stat != STAT_AUTH_ACCEPTED )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
159 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
160 Log_err( "Password rejected. Server status: %s", client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
161 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
162 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
163 return TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
164 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
165
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
166 static int
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
167 getStat( void )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
168 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
169 int result;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
170 Str lastCmd;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
171
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
172 if ( ! getLn( client.lastStat ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
173 result = STAT_PROGRAM_FAULT;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
174 else if ( sscanf( client.lastStat, "%d", &result ) != 1 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
175 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
176 Log_err( "Invalid server status: %s", client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
177 result = STAT_PROGRAM_FAULT;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
178 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
179 if ( result == STAT_AUTH_REQUIRED && ! client.auth )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
180 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
181 client.auth = TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
182 strcpy( lastCmd, client.lastCmd );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
183 if ( performAuth() )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
184 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
185 putCmd( lastCmd );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
186 return getStat();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
187 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
188 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
189 return result;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
190 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
191
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
192 static void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
193 connectAlarm( int sig )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
194 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
195 return;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
196 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
197
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
198 static sig_t
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
199 installSignalHandler( int sig, sig_t handler )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
200 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
201 struct sigaction act, oldAct;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
202
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
203 act.sa_handler = handler;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
204 sigemptyset( &act.sa_mask );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
205 act.sa_flags = 0;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
206 if ( sig == SIGALRM )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
207 act.sa_flags |= SA_INTERRUPT;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
208 else
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
209 act.sa_flags |= SA_RESTART;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
210 if ( sigaction( sig, &act, &oldAct ) < 0 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
211 return SIG_ERR;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
212 return oldAct.sa_handler;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
213 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
214
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
215 static Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
216 connectWithTimeout( int sock, const struct sockaddr *servAddr,
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
217 socklen_t addrLen )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
218 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
219 sig_t oldHandler;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
220 int r, to;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
221
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
222 oldHandler = installSignalHandler( SIGALRM, connectAlarm );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
223 if ( oldHandler == SIG_ERR )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
224 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
225 Log_err( "client.c:connectWithTimeout: signal failed." );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
226 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
227 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
228 to = Cfg_connectTimeout();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
229 if ( alarm( to ) != 0 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
230 Log_err( "client.c:connectWithTimeout: Alarm was already set." );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
231 r = connect( sock, servAddr, addrLen );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
232 alarm( 0 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
233 installSignalHandler( SIGALRM, oldHandler );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
234 return ( r >= 0 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
235 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
236
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
237 Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
238 Client_connect( const char *serv )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
239 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
240 unsigned short int port;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
241 int sock, i;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
242 unsigned int stat;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
243 struct hostent *hp;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
244 char *pStart, *pColon;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
245 Str host, s;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
246 struct sockaddr_in sIn;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
247
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
248 Utl_cpyStr( s, serv );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
249 pStart = Utl_stripWhiteSpace( s );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
250 pColon = strstr( pStart, ":" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
251 if ( pColon == NULL )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
252 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
253 strcpy( host, pStart );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
254 port = 119;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
255 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
256 else
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
257 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
258 *pColon = '\0';
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
259 strcpy( host, pStart );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
260 if ( sscanf( pColon + 1, "%hi", &port ) != 1 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
261 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
262 Log_err( "Syntax error in server name: '%s'", serv );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
263 return FALSE;;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
264 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
265 if ( port <= 0 || port > 65535 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
266 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
267 Log_err( "Invalid port number %hi. Must be in [1, 65535]", port );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
268 return FALSE;;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
269 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
270 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
271 memset( (void *)&sIn, 0, sizeof( sIn ) );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
272 hp = gethostbyname( host );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
273 if ( hp )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
274 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
275 for ( i = 0; (hp->h_addr_list)[ i ]; ++i )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
276 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
277 sIn.sin_family = hp->h_addrtype;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
278 sIn.sin_port = htons( port );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
279 sIn.sin_addr = *( (struct in_addr *)hp->h_addr_list[ i ] );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
280 sock = socket( AF_INET, SOCK_STREAM, 0 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
281 if ( sock < 0 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
282 break;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
283 if ( ! connectWithTimeout( sock, (struct sockaddr *)&sIn,
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
284 sizeof( sIn ) ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
285 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
286 close( sock );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
287 break;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
288 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
289 if ( ! ( client.out = fdopen( sock, "w" ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
290 || ! ( client.in = fdopen( dup( sock ), "r" ) ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
291 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
292 close( sock );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
293 break;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
294 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
295 stat = getStat();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
296 switch( stat ) {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
297 case STAT_READY_POST_ALLOW:
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
298 case STAT_READY_NO_POST_ALLOW:
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
299 Log_inf( "Connected to %s:%d",
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
300 inet_ntoa( sIn.sin_addr ), port );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
301 Utl_cpyStr( client.serv, serv );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
302 return TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
303 default:
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
304 Log_err( "Bad server stat %d", stat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
305 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
306 shutdown( fileno( client.out ), 0 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
307 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
308 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
309 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
310 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
311
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
312 static void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
313 processGrps( void )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
314 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
315 char postAllow;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
316 Bool err;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
317 int first, last;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
318 Str grp, line, file;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
319
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
320 while ( getTxtLn( line, &err ) && ! err )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
321 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
322 if ( sscanf( line, "%s %i %i %c",
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
323 grp, &last, &first, &postAllow ) != 4 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
324 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
325 Log_err( "Unknown reply to LIST or NEWGROUPS: %s", line );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
326 continue;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
327 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
328 if ( ! Grp_exists( grp ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
329 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
330 Log_inf( "Registering new group '%s'", grp );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
331 Grp_create( grp );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
332 /* Start local numbering with remote first number to avoid
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
333 new numbering at the readers if noffle is re-installed */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
334 if ( first != 0 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
335 Grp_setFirstLast( grp, first, first - 1 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
336 else
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
337 Grp_setFirstLast( grp, 1, 0 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
338 Grp_setRmtNext( grp, first );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
339 Grp_setServ( grp, client.serv );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
340 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
341 else
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
342 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
343 if ( Cfg_servIsPreferential( client.serv, Grp_serv( grp ) ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
344 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
345 Log_inf( "Changing server for '%s': '%s'->'%s'",
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
346 grp, Grp_serv( grp ), client.serv );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
347 Grp_setServ( grp, client.serv );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
348 Grp_setRmtNext( grp, first );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
349 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
350 else
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
351 Log_dbg( "Group %s is already fetched from %s",
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
352 grp, Grp_serv( grp ) );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
353
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
354 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
355 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
356 if ( ! err )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
357 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
358 snprintf( file, MAXCHAR, "%s/groupinfo.lastupdate", Cfg_spoolDir() );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
359 Utl_stamp( file );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
360 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
361 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
362
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
363 void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
364 Client_disconnect( void )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
365 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
366 if ( putCmd( "QUIT" ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
367 getStat();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
368 fclose( client.in );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
369 fclose( client.out );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
370 client.in = client.out = NULL;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
371 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
372
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
373 Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
374 Client_getGrps( void )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
375 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
376 if ( ! putCmd( "LIST ACTIVE" ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
377 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
378 if ( getStat() != STAT_GRPS_FOLLOW )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
379 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
380 Log_err( "LIST ACTIVE command failed: %s", client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
381 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
382 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
383 processGrps();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
384 return TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
385 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
386
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
387 Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
388 Client_getDsc( void )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
389 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
390 Bool err;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
391 Str name, line, dsc;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
392
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
393 Log_inf( "Querying group descriptions" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
394 if ( ! putCmd( "LIST NEWSGROUPS" ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
395 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
396 if ( getStat() != STAT_GRPS_FOLLOW )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
397 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
398 Log_err( "LIST NEWSGROUPS failed: %s", client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
399 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
400 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
401 while ( getTxtLn( line, &err ) && ! err )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
402 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
403 if ( sscanf( line, "%s", name ) != 1 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
404 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
405 Log_err( "Unknown reply to LIST NEWSGROUPS: %s", line );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
406 continue;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
407 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
408 strcpy( dsc, Utl_restOfLn( line, 1 ) );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
409 if ( Grp_exists( name ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
410 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
411 Log_dbg( "Description of %s: %s", name, dsc );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
412 Grp_setDsc( name, dsc );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
413 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
414 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
415 return TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
416 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
417
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
418 Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
419 Client_getCreationTimes( void )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
420 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
421 Bool err;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
422 Str name, line;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
423 time_t t;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
424
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
425 Log_inf( "Querying group creation times" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
426 if ( ! putCmd( "LIST ACTIVE.TIMES" ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
427 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
428 if ( getStat() != STAT_GRPS_FOLLOW )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
429 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
430 Log_err( "LIST ACTIVE.TIMES failes: %s", client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
431 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
432 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
433 while ( getTxtLn( line, &err ) && ! err )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
434 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
435 if ( sscanf( line, "%s %ld", name, &t ) != 2 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
436 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
437 Log_err( "Unknown reply to LIST ACTIVE.TIMES: %s", line );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
438 continue;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
439 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
440 if ( Grp_exists( name ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
441 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
442 Log_inf( "Creation time of %s: %ld", name, t );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
443 Grp_setCreated( name, t );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
444 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
445 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
446 return TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
447 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
448
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
449 Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
450 Client_getNewgrps( const time_t *lastTime )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
451 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
452 Str s;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
453 const char *p;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
454
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
455 ASSERT( *lastTime > 0 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
456 strftime( s, MAXCHAR, "%Y%m%d %H%M00", gmtime( lastTime ) );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
457 /*
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
458 Do not use century for working with old server software until 2000.
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
459 According to newest IETF draft, this is still valid after 2000.
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
460 (directly using %y in fmt string causes a Y2K compiler warning)
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
461 */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
462 p = s + 2;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
463 if ( ! putCmd( "NEWGROUPS %s", p ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
464 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
465 if ( getStat() != STAT_NEW_GRP_FOLLOW )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
466 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
467 Log_err( "NEWGROUPS command failed: %s", client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
468 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
469 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
470 processGrps();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
471 return TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
472 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
473
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
474 static const char *
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
475 readField( Str result, const char *p )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
476 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
477 size_t len;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
478 char *r;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
479
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
480 if ( ! p )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
481 return NULL;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
482 r = result;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
483 *r = '\0';
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
484 len = 0;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
485 while ( *p != '\t' && *p != '\n' )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
486 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
487 if ( ! *p )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
488 return p;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
489 *(r++) = *(p++);
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
490 ++len;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
491 if ( len >= MAXCHAR - 1 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
492 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
493 *r = '\0';
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
494 Log_err( "Field in overview too long: %s", r );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
495 return ++p;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
496 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
497 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
498 *r = '\0';
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
499 return ++p;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
500 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
501
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
502 static Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
503 parseOvLn( Str line, int *numb, Str subj, Str from,
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
504 Str date, Str msgId, Str ref, size_t *bytes, size_t *lines )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
505 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
506 const char *p;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
507 Str t;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
508
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
509 p = readField( t, line );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
510 if ( sscanf( t, "%i", numb ) != 1 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
511 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
512 p = readField( subj, p );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
513 p = readField( from, p );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
514 p = readField( date, p );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
515 p = readField( msgId, p );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
516 p = readField( ref, p );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
517 p = readField( t, p );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
518 *bytes = 0;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
519 *lines = 0;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
520 if ( sscanf( t, "%d", bytes ) != 1 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
521 return TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
522 p = readField( t, p );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
523 if ( sscanf( t, "%d", lines ) != 1 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
524 return TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
525 return TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
526 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
527
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
528 static const char*
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
529 nextXref( const char *pXref, Str grp, int *numb )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
530 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
531 Str s;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
532 const char *pColon, *src;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
533 char *dst;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
534
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
535 src = pXref;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
536 while ( *src && isspace( *src ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
537 ++src;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
538 dst = s;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
539 while ( *src && ! isspace( *src ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
540 *(dst++) = *(src++);
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
541 *dst = '\0';
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
542 if ( strlen( s ) == 0 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
543 return NULL;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
544 pColon = strstr( s, ":" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
545 if ( ! pColon || sscanf( pColon + 1, "%i", numb ) != 1 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
546 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
547 Log_err( "Corrupt Xref at position '%s'", pXref );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
548 return NULL;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
549 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
550 Utl_cpyStrN( grp, s, pColon - s );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
551 Log_dbg( "client.c: nextXref: grp '%s' numb %lu", grp, numb );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
552 return src;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
553 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
554
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
555 static Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
556 needsMark( const char *ref )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
557 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
558 Bool done = FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
559 char *p;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
560 Str msgId;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
561 int stat, len;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
562 time_t lastAccess, nowTime;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
563 double limit;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
564
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
565 nowTime = time( NULL );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
566 limit = Cfg_threadFollowTime() * 24. * 3600.;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
567 while ( ! done )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
568 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
569 p = msgId;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
570 while ( *ref != '<' )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
571 if ( *(ref++) == '\0' )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
572 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
573 len = 0;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
574 while ( *ref != '>' )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
575 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
576 if ( *ref == '\0' || ++len >= MAXCHAR - 1 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
577 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
578 *(p++) = *(ref++);
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
579 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
580 *(p++) = '>';
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
581 *p = '\0';
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
582 if ( Db_contains( msgId ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
583 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
584 stat = Db_stat( msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
585 lastAccess = Db_lastAccess( msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
586 if ( ( stat & DB_INTERESTING )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
587 && difftime( nowTime, lastAccess ) <= limit )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
588 return TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
589 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
590 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
591 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
592 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
593
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
594 static void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
595 prepareEntry( Over *ov )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
596 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
597 Str g, t;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
598 const char *msgId, *p, *xref;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
599 int n;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
600
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
601 msgId = Ov_msgId( ov );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
602 if ( Pseudo_isGeneralInfo( msgId ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
603 Log_dbg( "Skipping general info '%s'", msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
604 else if ( Db_contains( msgId ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
605 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
606 xref = Db_xref( msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
607 Log_dbg( "Entry '%s' already in db with Xref '%s'", msgId, xref );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
608 p = nextXref( xref, g, &n );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
609 if ( p == NULL )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
610 Log_err( "Overview with no group in Xref '%s'", msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
611 else
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
612 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
613 if ( Cfg_servIsPreferential( client.serv, Grp_serv( g ) ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
614 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
615 Log_dbg( "Changing first server for '%s' from '%s' to '%s'",
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
616 msgId, Grp_serv( g ), client.serv );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
617 snprintf( t, MAXCHAR, "%s:%i %s",
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
618 client.grp, Ov_numb( ov ), xref );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
619 Db_setXref( msgId, t );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
620 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
621 else
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
622 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
623 Log_dbg( "Adding '%s' to Xref of '%s'", g, msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
624 snprintf( t, MAXCHAR, "%s %s:%i",
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
625 xref, client.grp, Ov_numb( ov ) );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
626 Db_setXref( msgId, t );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
627 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
628 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
629 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
630 else
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
631 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
632 Log_dbg( "Preparing '%s' in database", msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
633 Db_prepareEntry( ov, client.grp, Ov_numb( ov ) );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
634 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
635 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
636
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
637 Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
638 Client_getOver( int rmtFirst, int rmtLast, FetchMode mode )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
639 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
640 Bool err;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
641 size_t bytes, lines;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
642 int rmtNumb, oldLast, cntMarked;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
643 Over *ov;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
644 Str line, subj, from, date, msgId, ref;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
645
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
646 ASSERT( strcmp( client.grp, "" ) != 0 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
647 if ( ! putCmd( "XOVER %lu-%lu", rmtFirst, rmtLast ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
648 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
649 if ( getStat() != STAT_OVERS_FOLLOW )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
650 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
651 Log_err( "XOVER command failed: %s", client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
652 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
653 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
654 Log_dbg( "Requesting overview for remote %lu-%lu", rmtFirst, rmtLast );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
655 oldLast = Cont_last();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
656 cntMarked = 0;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
657 while ( getTxtLn( line, &err ) && ! err )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
658 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
659 if ( ! parseOvLn( line, &rmtNumb, subj, from, date, msgId, ref,
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
660 &bytes, &lines ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
661 Log_err( "Bad overview line: %s", line );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
662 else
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
663 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
664 ov = new_Over( subj, from, date, msgId, ref, bytes, lines );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
665 Cont_app( ov );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
666 prepareEntry( ov );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
667 if ( mode == FULL || ( mode == THREAD && needsMark( ref ) ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
668 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
669 Req_add( client.serv, msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
670 ++cntMarked;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
671 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
672 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
673 Grp_setRmtNext( client.grp, rmtNumb + 1 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
674 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
675 if ( oldLast != Cont_last() )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
676 Log_inf( "Added %s %lu-%lu", client.grp, oldLast + 1, Cont_last() );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
677 Log_inf( "%u articles marked for download in %s", cntMarked, client.grp );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
678 return err;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
679 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
680
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
681 static void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
682 retrievingFailed( const char* msgId, const char *reason )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
683 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
684 int stat;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
685
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
686 Log_err( "Retrieving of %s failed: %s", msgId, reason );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
687 stat = Db_stat( msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
688 Pseudo_retrievingFailed( msgId, reason );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
689 Db_setStat( msgId, stat | DB_RETRIEVING_FAILED );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
690 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
691
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
692 static Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
693 retrieveAndStoreArt( const char *msgId )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
694 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
695 Bool err;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
696 DynStr *s = NULL;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
697 Str line;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
698
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
699 Log_inf( "Retrieving %s", msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
700 s = new_DynStr( 5000 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
701 while ( getTxtLn( line, &err ) && ! err )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
702 DynStr_appLn( s, line );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
703 if ( ! err )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
704 Db_storeArt( msgId, DynStr_str( s ) );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
705 else
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
706 retrievingFailed( msgId, "Connection broke down" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
707 del_DynStr( s );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
708 return ! err;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
709 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
710
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
711 void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
712 Client_retrieveArt( const char *msgId )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
713 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
714 if ( ! Db_contains( msgId ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
715 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
716 Log_err( "Article '%s' not prepared in database. Skipping.", msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
717 return;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
718 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
719 if ( ! ( Db_stat( msgId ) & DB_NOT_DOWNLOADED ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
720 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
721 Log_inf( "Article '%s' already retrieved. Skipping.", msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
722 return;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
723 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
724 if ( ! putCmd( "ARTICLE %s", msgId ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
725 retrievingFailed( msgId, "Connection broke down" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
726 else if ( getStat() != STAT_ART_FOLLOWS )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
727 retrievingFailed( msgId, client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
728 else
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
729 retrieveAndStoreArt( msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
730 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
731
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
732 void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
733 Client_retrieveArtList( const char *list )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
734 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
735 Str msgId;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
736 DynStr *s;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
737 const char *p;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
738
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
739 Log_inf( "Retrieving article list" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
740 s = new_DynStr( strlen( list ) );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
741 p = list;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
742 while ( ( p = Utl_getLn( msgId, p ) ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
743 if ( ! Db_contains( msgId ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
744 Log_err( "Skipping retrieving of %s (not prepared in database)",
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
745 msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
746 else if ( ! ( Db_stat( msgId ) & DB_NOT_DOWNLOADED ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
747 Log_inf( "Skipping %s (already retrieved)", msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
748 else if ( ! putCmdNoFlush( "ARTICLE %s", msgId ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
749 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
750 retrievingFailed( msgId, "Connection broke down" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
751 del_DynStr( s );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
752 return;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
753 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
754 else
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
755 DynStr_appLn( s, msgId );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
756 fflush( client.out );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
757 Log_dbg( "[S FLUSH]" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
758 p = DynStr_str( s );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
759 while ( ( p = Utl_getLn( msgId, p ) ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
760 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
761 if ( getStat() != STAT_ART_FOLLOWS )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
762 retrievingFailed( msgId, client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
763 else if ( ! retrieveAndStoreArt( msgId ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
764 break;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
765 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
766 del_DynStr( s );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
767 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
768
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
769 Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
770 Client_changeToGrp( const char* name )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
771 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
772 unsigned int stat;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
773 int estimatedNumb, first, last;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
774
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
775 if ( ! Grp_exists( name ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
776 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
777 if ( ! putCmd( "GROUP %s", name ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
778 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
779 if ( getStat() != STAT_GRP_SELECTED )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
780 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
781 if ( sscanf( client.lastStat, "%u %i %i %i",
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
782 &stat, &estimatedNumb, &first, &last ) != 4 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
783 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
784 Log_err( "Bad server response to GROUP: %s", client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
785 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
786 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
787 Utl_cpyStr( client.grp, name );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
788 client.rmtFirst = first;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
789 client.rmtLast = last;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
790 return TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
791 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
792
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
793 void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
794 Client_rmtFirstLast( int *first, int *last )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
795 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
796 *first = client.rmtFirst;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
797 *last = client.rmtLast;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
798 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
799
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
800 Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
801 Client_postArt( const char *msgId, const char *artTxt,
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
802 Str errStr )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
803 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
804 if ( ! putCmd( "POST" ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
805 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
806 if ( getStat() != STAT_SEND_ART )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
807 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
808 Log_err( "Posting of %s not allowed: %s", msgId, client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
809 strcpy( errStr, client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
810 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
811 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
812 putTxtBuf( artTxt );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
813 putEndOfTxt();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
814 if ( getStat() != STAT_POST_OK )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
815 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
816 Log_err( "Posting of %s failed: %s", msgId, client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
817 strcpy( errStr, client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
818 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
819 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
820 Log_inf( "Posted %s (Status: %s)", msgId, client.lastStat );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
821 return TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
822 }