Mercurial > noffle
comparison src/server.c @ 259:b660fadc1814 noffle
[svn] * src/server.c,src/util.c,src/util.h: Recognise GMT/UTC on NNTP NEWGROUPS.
Do small reorg of some of the timezone sensitive code, and introduce
use of timegm(). An implementation is provided for systems without
timegm().
author | bears |
---|---|
date | Wed, 26 Jun 2002 14:30:26 +0100 |
parents | d70e9dd6b308 |
children | 94b7962a0fbe |
comparison
equal
deleted
inserted
replaced
258:ebd9c98bbc7f | 259:b660fadc1814 |
---|---|
1 /* | 1 /* |
2 server.c | 2 server.c |
3 | 3 |
4 $Id: server.c 372 2002-03-13 11:12:23Z mirkol $ | 4 $Id: server.c 391 2002-06-26 13:30:26Z bears $ |
5 */ | 5 */ |
6 | 6 |
7 #if HAVE_CONFIG_H | 7 #if HAVE_CONFIG_H |
8 #include <config.h> | 8 #include <config.h> |
9 #endif | 9 #endif |
813 doList( char *line, const Cmd *cmd ) | 813 doList( char *line, const Cmd *cmd ) |
814 { | 814 { |
815 Str s, arg; | 815 Str s, arg; |
816 const char *pat; | 816 const char *pat; |
817 | 817 |
818 if ( sscanf( line, "%s", s ) != 1 ) | 818 if ( sscanf( line, MAXCHAR_FMT, s ) != 1 ) |
819 doListActive( "*" ); | 819 doListActive( "*" ); |
820 else | 820 else |
821 { | 821 { |
822 Utl_toLower( s ); | 822 Utl_toLower( s ); |
823 strcpy( arg, Utl_restOfLn( line, 1 ) ); | 823 strcpy( arg, Utl_restOfLn( line, 1 ) ); |
872 | 872 |
873 putStat( STAT_READY_POST_ALLOW, "Ok" ); | 873 putStat( STAT_READY_POST_ALLOW, "Ok" ); |
874 return TRUE; | 874 return TRUE; |
875 } | 875 } |
876 | 876 |
877 /* Can return -1, if date is outside the range of time_t. */ | 877 /* |
878 * Given a time specification (local time or GMT), return a time_t. | |
879 */ | |
878 static time_t | 880 static time_t |
879 getTimeInSeconds( int year, int mon, int day, int hour, int min, int sec ) | 881 getTimeInSeconds( int year, int mon, int day, |
882 int hour, int min, int sec, | |
883 Bool timeIsGMT ) | |
880 { | 884 { |
881 struct tm t; | 885 struct tm t; |
882 time_t result; | 886 time_t result; |
883 | 887 |
884 ASSERT( year >= 1900 ); | 888 ASSERT( year >= 1900 ); |
897 t.tm_mon = mon - 1; | 901 t.tm_mon = mon - 1; |
898 t.tm_mday = day; | 902 t.tm_mday = day; |
899 t.tm_hour = hour; | 903 t.tm_hour = hour; |
900 t.tm_min = min; | 904 t.tm_min = min; |
901 t.tm_sec = sec; | 905 t.tm_sec = sec; |
902 result = mktime( &t ); | 906 t.tm_isdst = -1; |
907 result = timeIsGMT ? Utl_mktimeGMT( &t ) : mktime( &t ); | |
903 return result; | 908 return result; |
904 } | 909 } |
905 | 910 |
906 | 911 |
907 static Bool | 912 static Bool |
908 doNewgrps( char *arg, const Cmd *cmd ) | 913 doNewgrps( char *arg, const Cmd *cmd ) |
909 { | 914 { |
910 time_t t, now, lastUpdate, nextCentBegin; | 915 time_t t, now, lastUpdate; |
911 int year, mon, day, hour, min, sec, cent, len; | 916 int year, mon, day, hour, min, sec, len, fields, nowYear; |
912 const char *g; | 917 const char *g; |
913 Str date, timeofday, file; | 918 Str date, timeofday, file, utc; |
914 | 919 Bool timeIsGMT = FALSE; |
915 if ( sscanf( arg, "%s %s", date, timeofday ) != 2 ) | 920 struct tm *tm; |
921 | |
922 fields = sscanf( arg, MAXCHAR_FMT " " MAXCHAR_FMT " " MAXCHAR_FMT, | |
923 date, timeofday, utc ); | |
924 if ( fields == 3 ) | |
925 { | |
926 Utl_toLower( utc ); | |
927 if ( strcmp( utc, "gmt" ) != 0 && strcmp( utc, "utc" ) != 0 ) | |
928 { | |
929 putSyntax( cmd ); | |
930 return TRUE; | |
931 } | |
932 timeIsGMT = TRUE; | |
933 } | |
934 else if ( fields != 2 ) | |
916 { | 935 { |
917 putSyntax( cmd ); | 936 putSyntax( cmd ); |
918 return TRUE; | 937 return TRUE; |
919 } | 938 } |
920 len = strlen( date ); | 939 len = strlen( date ); |
924 if ( sscanf( date, "%2d%2d%2d", &year, &mon, &day ) != 3 ) | 943 if ( sscanf( date, "%2d%2d%2d", &year, &mon, &day ) != 3 ) |
925 { | 944 { |
926 putSyntax( cmd ); | 945 putSyntax( cmd ); |
927 return TRUE; | 946 return TRUE; |
928 } | 947 } |
929 now = time( NULL ); | 948 /* |
930 cent = 1900; | 949 * As per current IETF draft, year is this century if <= now, |
931 nextCentBegin = getTimeInSeconds( cent + 100, 1, 1, 0, 0, 0 ); | 950 * else last century. |
932 while ( nextCentBegin != (time_t)-1 && now != (time_t)-1 | 951 */ |
933 && now > nextCentBegin ) | 952 now = time( NULL ); |
934 { | 953 tm = timeIsGMT ? gmtime( &now ) : localtime( &now ); |
935 cent += 100; | 954 nowYear = tm->tm_year + 1900; |
936 nextCentBegin = getTimeInSeconds( cent + 100, 1, 1, 0, 0, 0 ); | 955 year += ( nowYear / 100 ) * 100; |
937 } | 956 if ( year % 100 > nowYear % 100 ) |
938 year += cent; | 957 year -= 100; |
939 break; | 958 break; |
940 case 8: | 959 case 8: |
941 if ( sscanf( date, "%4d%2d%2d", &year, &mon, &day ) != 3 ) | 960 if ( sscanf( date, "%4d%2d%2d", &year, &mon, &day ) != 3 ) |
942 { | 961 { |
943 putSyntax( cmd ); | 962 putSyntax( cmd ); |
959 { | 978 { |
960 putSyntax( cmd ); | 979 putSyntax( cmd ); |
961 return TRUE; | 980 return TRUE; |
962 } | 981 } |
963 snprintf( file, MAXCHAR, "%s/groupinfo.lastupdate", Cfg_spoolDir() ); | 982 snprintf( file, MAXCHAR, "%s/groupinfo.lastupdate", Cfg_spoolDir() ); |
964 t = getTimeInSeconds( year, mon, day, hour, min, sec ); | 983 t = getTimeInSeconds( year, mon, day, hour, min, sec, timeIsGMT ); |
965 | 984 |
966 if ( ! Utl_getStamp( &lastUpdate, file ) ) | 985 if ( ! Utl_getStamp( &lastUpdate, file ) ) |
967 { | 986 { |
968 /* Can't get stamp. Put out error message. */ | 987 /* Can't get stamp. Put out error message. */ |
969 putStat( STAT_PROGRAM_FAULT, "Server error reading %s", file ); | 988 putStat( STAT_PROGRAM_FAULT, "Server error reading %s", file ); |
970 return TRUE; | 989 return TRUE; |
971 } | 990 } |
972 | 991 |
973 putStat( STAT_NEW_GRP_FOLLOW, "New groups since %s", arg ); | 992 /* Show timestamp back as news format time for confirmation. */ |
993 Utl_newsDate( t, timeofday ); | |
994 | |
995 putStat( STAT_NEW_GRP_FOLLOW, "New groups since %s", timeofday ); | |
974 | 996 |
975 if ( t == (time_t)-1 || t <= lastUpdate ) | 997 if ( t == (time_t)-1 || t <= lastUpdate ) |
976 { | 998 { |
977 if ( Grp_firstGrp( &g ) ) | 999 if ( Grp_firstGrp( &g ) ) |
978 do | 1000 do |
1181 { | 1203 { |
1182 enum XhdrType what; | 1204 enum XhdrType what; |
1183 const char *p; | 1205 const char *p; |
1184 Str whatStr; | 1206 Str whatStr; |
1185 | 1207 |
1186 if ( sscanf( arg, "%s", whatStr ) != 1 ) | 1208 if ( sscanf( arg, MAXCHAR_FMT, whatStr ) != 1 ) |
1187 { | 1209 { |
1188 putSyntax( cmd ); | 1210 putSyntax( cmd ); |
1189 return TRUE; | 1211 return TRUE; |
1190 } | 1212 } |
1191 what = whatXhdrField( whatStr ); | 1213 what = whatXhdrField( whatStr ); |
1246 doXpat( char *arg, const Cmd *cmd ) | 1268 doXpat( char *arg, const Cmd *cmd ) |
1247 { | 1269 { |
1248 enum XhdrType what; | 1270 enum XhdrType what; |
1249 Str whatStr, articles, pat; | 1271 Str whatStr, articles, pat; |
1250 | 1272 |
1251 if ( sscanf( arg, "%s %s %s", whatStr, articles, pat ) != 3 ) | 1273 if ( sscanf( arg, MAXCHAR_FMT " " MAXCHAR_FMT " " MAXCHAR_FMT, |
1274 whatStr, articles, pat ) != 3 ) | |
1252 { | 1275 { |
1253 putSyntax( cmd ); | 1276 putSyntax( cmd ); |
1254 return TRUE; | 1277 return TRUE; |
1255 } | 1278 } |
1256 what = whatXhdrField( whatStr ); | 1279 what = whatXhdrField( whatStr ); |
1391 unsigned int i, n; | 1414 unsigned int i, n; |
1392 Cmd *c; | 1415 Cmd *c; |
1393 Str s, arg; | 1416 Str s, arg; |
1394 Bool ret; | 1417 Bool ret; |
1395 | 1418 |
1396 if ( sscanf( line, "%s", s ) == 1 ) | 1419 if ( sscanf( line, MAXCHAR_FMT, s ) == 1 ) |
1397 { | 1420 { |
1398 Utl_toLower( s ); | 1421 Utl_toLower( s ); |
1399 strcpy( arg, Utl_restOfLn( line, 1 ) ); | 1422 strcpy( arg, Utl_restOfLn( line, 1 ) ); |
1400 n = sizeof( commands ) / sizeof( commands[ 0 ] ); | 1423 n = sizeof( commands ) / sizeof( commands[ 0 ] ); |
1401 for ( i = 0, c = commands; i < n; ++i, ++c ) | 1424 for ( i = 0, c = commands; i < n; ++i, ++c ) |