Sort index by date

SVN revision: 119
This commit is contained in:
Stefan Ritt 2002-06-14 06:08:19 +00:00
parent 73de6dbe40
commit 10dc35e116

205
elogd.c
View File

@ -6,6 +6,9 @@
Contents: Web server program for Electronic Logbook ELOG Contents: Web server program for Electronic Logbook ELOG
$Log$ $Log$
Revision 2.15 2002/06/14 06:08:19 midas
Sort index by date
Revision 2.14 2002/06/13 12:46:09 midas Revision 2.14 2002/06/13 12:46:09 midas
Fixed problem wiht 'reply to:' in subject Fixed problem wiht 'reply to:' in subject
@ -1347,12 +1350,23 @@ INT ss_file_find(char *path, char *pattern, char **plist)
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
int eli_compare(const EL_INDEX *e1, const EL_INDEX *e2)
{
if (e1->file_time < e2->file_time)
return -1;
if (e1->file_time > e2->file_time)
return 1;
return 0;
}
/*------------------------------------------------------------------*/
int el_build_index(LOGBOOK *lbs) int el_build_index(LOGBOOK *lbs)
/* scan all ??????a.log files and build an index table in eli[] */ /* scan all ??????a.log files and build an index table in eli[] */
{ {
char *file_list, str[256], dir[256], file_name[256], *buffer, *p; char *file_list, str[256], date[256], dir[256], file_name[256], *buffer, *p;
int index, n, length; int index, n, length;
int fh; int i, fh;
time_t ltime; time_t ltime;
struct tm tms; struct tm tms;
@ -1423,11 +1437,20 @@ struct tm tms;
strcpy(str, file_list+index*MAX_PATH_LENGTH); strcpy(str, file_list+index*MAX_PATH_LENGTH);
strcpy(lbs->el_index[lbs->n_el_index].file_name, str); strcpy(lbs->el_index[lbs->n_el_index].file_name, str);
el_decode(p, "Date: ", date);
memset(&tms, 0, sizeof(struct tm)); memset(&tms, 0, sizeof(struct tm));
tms.tm_year = (str[0]-'0')*10 + (str[1]-'0');
tms.tm_mon = (str[2]-'0')*10 + (str[3]-'0') -1; for (i=0 ; i<12 ; i++)
tms.tm_mday = (str[4]-'0')*10 + (str[5]-'0'); if (strncmp(date+4, mname[i], 3) == 0)
tms.tm_hour = 0; break;
tms.tm_mon = i;
tms.tm_mday = atoi(date+8);
tms.tm_hour = atoi(date+11);
tms.tm_min = atoi(date+14);
tms.tm_sec = atoi(date+17);
tms.tm_year = atoi(date+20)-1900;
if (tms.tm_year < 90) if (tms.tm_year < 90)
tms.tm_year += 100; tms.tm_year += 100;
@ -1461,6 +1484,18 @@ struct tm tms;
free(file_list); free(file_list);
/* sort entries according to date */
qsort(lbs->el_index, lbs->n_el_index, sizeof(EL_INDEX), eli_compare);
if (verbose)
{
printf("After sort:\n");
for (i=0 ; i<lbs->n_el_index ; i++)
printf(" ID %d in %s, offset %d\n",
lbs->el_index[i].message_id, lbs->el_index[i].file_name,
lbs->el_index[i].offset);
}
return EL_SUCCESS; return EL_SUCCESS;
} }
@ -1712,6 +1747,7 @@ char message[TEXT_SIZE+1000], attachment_all[64*MAX_ATTACHMENTS];
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
int el_submit(LOGBOOK *lbs, int message_id, int el_submit(LOGBOOK *lbs, int message_id,
char *date,
char attr_name[MAX_N_ATTR][NAME_LENGTH], char attr_name[MAX_N_ATTR][NAME_LENGTH],
char attr_value[MAX_N_ATTR][NAME_LENGTH], char attr_value[MAX_N_ATTR][NAME_LENGTH],
int n_attr, char *text, int n_attr, char *text,
@ -1730,7 +1766,7 @@ int el_submit(LOGBOOK *lbs, int message_id,
LOGBOOK lbs Logbook structure LOGBOOK lbs Logbook structure
int message_id Message id for existing message, 0 for int message_id Message id for existing message, 0 for
new message new message
char *date Message date
char attr_name[][] Name of attributes char attr_name[][] Name of attributes
char attr_value[][] Value of attributes char attr_value[][] Value of attributes
int n_attr Number of attributes int n_attr Number of attributes
@ -1752,11 +1788,11 @@ int el_submit(LOGBOOK *lbs, int message_id,
\********************************************************************/ \********************************************************************/
{ {
INT n, i, j, size, fh, index, tail_size, orig_size, delta, reply_id; INT n, i, j, size, fh, index, tail_size, orig_size, delta, reply_id;
struct tm *tms; struct tm tms;
char file_name[256], afile_name[MAX_ATTACHMENTS][256], dir[256], str[256], char file_name[256], afile_name[MAX_ATTACHMENTS][256], dir[256], str[256],
date[80], attachment_all[64*MAX_ATTACHMENTS], attachment_all[64*MAX_ATTACHMENTS],
rep1[256], rep2[256]; rep1[256], rep2[256];
time_t now; time_t now, ltime;
char message[TEXT_SIZE+100], *p; char message[TEXT_SIZE+100], *p;
BOOL bedit; BOOL bedit;
@ -1795,15 +1831,15 @@ BOOL bedit;
strcpy(dir, lbs->data_dir); strcpy(dir, lbs->data_dir);
time(&now); time(&now);
tms = localtime(&now); memcpy(&tms, localtime(&now), sizeof(struct tm));
strcpy(str, p); strcpy(str, p);
sprintf(afile_name[index], "%02d%02d%02d_%02d%02d%02d_%s", sprintf(afile_name[index], "%02d%02d%02d_%02d%02d%02d_%s",
tms->tm_year % 100, tms->tm_mon+1, tms->tm_mday, tms.tm_year % 100, tms.tm_mon+1, tms.tm_mday,
tms->tm_hour, tms->tm_min, tms->tm_sec, str); tms.tm_hour, tms.tm_min, tms.tm_sec, str);
sprintf(file_name, "%s%02d%02d%02d_%02d%02d%02d_%s", dir, sprintf(file_name, "%s%02d%02d%02d_%02d%02d%02d_%s", dir,
tms->tm_year % 100, tms->tm_mon+1, tms->tm_mday, tms.tm_year % 100, tms.tm_mon+1, tms.tm_mday,
tms->tm_hour, tms->tm_min, tms->tm_sec, str); tms.tm_hour, tms.tm_min, tms.tm_sec, str);
/* save attachment */ /* save attachment */
fh = open(file_name, O_CREAT | O_RDWR | O_BINARY, 0644); fh = open(file_name, O_CREAT | O_RDWR | O_BINARY, 0644);
@ -1854,7 +1890,7 @@ BOOL bedit;
/* file might have been edited, rebuild index */ /* file might have been edited, rebuild index */
free(lbs->el_index); free(lbs->el_index);
el_build_index(lbs); el_build_index(lbs);
return el_submit(lbs, message_id, attr_name, attr_value, n_attr, text, return el_submit(lbs, message_id, date, attr_name, attr_value, n_attr, text,
in_reply_to, reply_to, encoding, afilename, buffer, buffer_size); in_reply_to, reply_to, encoding, afilename, buffer, buffer_size);
} }
@ -1874,9 +1910,12 @@ BOOL bedit;
message[size] = 0; message[size] = 0;
el_decode(message, "Date: ", date); if (equal_ustring(date, "<keep>"))
el_decode(message, "Reply to: ", rep1); el_decode(message, "Date: ", date);
el_decode(message, "In reply to: ", rep2); if (equal_ustring(reply_to, "<keep>"))
el_decode(message, "Reply to: ", reply_to);
if (equal_ustring(in_reply_to, "<keep>"))
el_decode(message, "In reply to: ", in_reply_to);
el_decode(message, "Attachment: ", attachment_all); el_decode(message, "Attachment: ", attachment_all);
/* buffer tail of logfile */ /* buffer tail of logfile */
@ -1901,32 +1940,57 @@ BOOL bedit;
else else
{ {
/* create new message */ /* create new message */
time(&now); if (!date[0])
tms = localtime(&now); {
time(&now);
strcpy(date, ctime(&now));
date[24] = 0;
}
sprintf(file_name, "%02d%02d%02da.log", for (i=0 ; i<12 ; i++)
tms->tm_year % 100, tms->tm_mon+1, tms->tm_mday); if (strncmp(date+4, mname[i], 3) == 0)
break;
memset(&tms, 0, sizeof(struct tm));
tms.tm_mon = i;
tms.tm_mday = atoi(date+8);
tms.tm_hour = atoi(date+11);
tms.tm_min = atoi(date+14);
tms.tm_sec = atoi(date+17);
tms.tm_year = atoi(date+20)-1900;
if (tms.tm_year < 90)
tms.tm_year += 100;
ltime = mktime(&tms);
sprintf(file_name, "%c%c%02d%c%ca.log",
date[22], date[23], i+1, date[8], date[9]);
sprintf(str, "%s%s", dir, file_name); sprintf(str, "%s%s", dir, file_name);
fh = open(str, O_CREAT | O_RDWR | O_BINARY, 0644); fh = open(str, O_CREAT | O_RDWR | O_BINARY, 0644);
if (fh < 0) if (fh < 0)
return -1; return -1;
strcpy(date, ctime(&now));
date[24] = 0;
lseek(fh, 0, SEEK_END); lseek(fh, 0, SEEK_END);
/* new message id is old plus one */ /* new message id is old plus one */
message_id = el_search_message(lbs, EL_LAST, 0)+1; message_id = 1;
for (i= 0 ; i<lbs->n_el_index ; i++)
if (lbs->el_index[i].message_id >= message_id)
message_id = lbs->el_index[i].message_id + 1;
/* enter message in index */ /* enter message in index */
lbs->n_el_index++; lbs->n_el_index++;
lbs->el_index = realloc(lbs->el_index, sizeof(EL_INDEX)*lbs->n_el_index); lbs->el_index = realloc(lbs->el_index, sizeof(EL_INDEX)*lbs->n_el_index);
lbs->el_index[lbs->n_el_index-1].message_id = message_id; lbs->el_index[lbs->n_el_index-1].message_id = message_id;
strcpy(lbs->el_index[lbs->n_el_index-1].file_name, file_name); strcpy(lbs->el_index[lbs->n_el_index-1].file_name, file_name);
lbs->el_index[lbs->n_el_index-1].file_time = now; lbs->el_index[lbs->n_el_index-1].file_time = ltime;
lbs->el_index[lbs->n_el_index-1].offset = TELL(fh); lbs->el_index[lbs->n_el_index-1].offset = TELL(fh);
/* if index not ordered, sort it */
i = lbs->n_el_index;
if (i > 1 && lbs->el_index[i-1].file_time < lbs->el_index[i-2].file_time)
qsort(lbs->el_index, i, sizeof(EL_INDEX), eli_compare);
} }
/* compose message */ /* compose message */
@ -1934,21 +1998,11 @@ BOOL bedit;
sprintf(message, "$@MID@$: %d\n", message_id); sprintf(message, "$@MID@$: %d\n", message_id);
sprintf(message+strlen(message), "Date: %s\n", date); sprintf(message+strlen(message), "Date: %s\n", date);
if (!equal_ustring(reply_to, "<keep>")) if (reply_to[0])
{ sprintf(message+strlen(message), "Reply to: %s\n", reply_to);
if (reply_to[0])
sprintf(message+strlen(message), "Reply to: %s\n", reply_to);
}
else if (rep1[0])
sprintf(message+strlen(message), "Reply to: %s\n", rep1);
if (!equal_ustring(in_reply_to, "<keep>")) if (in_reply_to[0])
{ sprintf(message+strlen(message), "In reply to: %s\n", in_reply_to);
if (in_reply_to[0])
sprintf(message+strlen(message), "In reply to: %s\n", in_reply_to);
}
else if (rep2[0])
sprintf(message+strlen(message), "In reply to: %s\n", rep2);
for (i=0 ; i<n_attr ; i++) for (i=0 ; i<n_attr ; i++)
sprintf(message+strlen(message), "%s: %s\n", attr_name[i], attr_value[i]); sprintf(message+strlen(message), "%s: %s\n", attr_name[i], attr_value[i]);
@ -2063,7 +2117,7 @@ BOOL bedit;
memset(att, 0, sizeof(att)); memset(att, 0, sizeof(att));
/* write modified message */ /* write modified message */
el_submit(lbs, reply_id, attr_list, attr, n_attr, el_submit(lbs, reply_id, date, attr_list, attr, n_attr,
message, in_reply_to, reply_to, enc, att, NULL, NULL); message, in_reply_to, reply_to, enc, att, NULL, NULL);
} }
@ -2118,7 +2172,7 @@ int size, n_attr, status;
memset(att, 0, sizeof(att)); memset(att, 0, sizeof(att));
/* write modified message */ /* write modified message */
el_submit(lbs, message_id, attr_list, attr, n_attr, el_submit(lbs, message_id, date, attr_list, attr, n_attr,
message, in_reply_to, reply_to, enc, att, NULL, NULL); message, in_reply_to, reply_to, enc, att, NULL, NULL);
} }
@ -2290,16 +2344,17 @@ char message[TEXT_SIZE+1000], attachment_all[64*MAX_ATTACHMENTS];
if (reply_to[0]) if (reply_to[0])
{ {
p = reply_to; p = reply_to;
do if (isdigit(*p))
{ do
if (atoi(p)) {
el_delete_message(lbs, atoi(p), TRUE, NULL, FALSE); if (atoi(p))
el_delete_message(lbs, atoi(p), TRUE, NULL, FALSE);
while (*p && isdigit(*p)) while (*p && isdigit(*p))
p++; p++;
while (*p && (*p == ',' || *p == ' ')) while (*p && (*p == ',' || *p == ' '))
p++; p++;
} while(*p); } while(*p);
} }
/* delete backward references */ /* delete backward references */
@ -2909,6 +2964,7 @@ int i;
if (!printable && getcfg(logbook, "logbook tabs", str) && atoi(str) == 1) if (!printable && getcfg(logbook, "logbook tabs", str) && atoi(str) == 1)
{ {
/*
if (!getcfg("global", "tab cellpadding", str)) if (!getcfg("global", "tab cellpadding", str))
strcpy(str, "5"); strcpy(str, "5");
rsprintf("<tr><td><table width=100%% border=0 cellpadding=%s cellspacing=0 bgcolor=#FFFFFF><tr>\n", str); rsprintf("<tr><td><table width=100%% border=0 cellpadding=%s cellspacing=0 bgcolor=#FFFFFF><tr>\n", str);
@ -2938,6 +2994,31 @@ int i;
} }
rsprintf("<td width=100%% bgcolor=#FFFFFF>&nbsp;</td>\n"); rsprintf("<td width=100%% bgcolor=#FFFFFF>&nbsp;</td>\n");
rsprintf("</tr></table></td></tr>\n\n"); rsprintf("</tr></table></td></tr>\n\n");
*/
rsprintf("<tr><td bgcolor=#FFFFFF>\n");
if (getcfg("global", "main tab", str))
rsprintf("<font style=\"background-color:#E0E0E0\">&nbsp;<a href=\"../\">%s</a>&nbsp;</font>&nbsp\n", str);
for (i=0 ; ; i++)
{
if (!enumgrp(i, str))
break;
if (equal_ustring(str, "global"))
continue;
strcpy(ref, str);
url_encode(ref);
if (equal_ustring(str, logbook))
rsprintf("<font style=\"color:%s;background-color:%s\">&nbsp;%s&nbsp;</font>&nbsp;\n", gt("Title fontcolor"), gt("Title BGColor"), str);
else
rsprintf("<font style=\"background-color:#E0E0E0\">&nbsp;<a href=\"../%s/\">%s</a>&nbsp;</font>&nbsp\n", ref, str);
}
rsprintf("</td></tr>\n\n");
} }
/*---- title row ----*/ /*---- title row ----*/
@ -5692,9 +5773,10 @@ skip:
void submit_elog(LOGBOOK *lbs) void submit_elog(LOGBOOK *lbs)
{ {
char str[256], mail_to[256], mail_from[256], file_name[256], error[1000], char str[256], mail_to[256], mail_from[256], file_name[256], error[1000], date[80],
mail_text[TEXT_SIZE+1000], mail_list[MAX_N_LIST][NAME_LENGTH], list[10000], smtp_host[256], mail_text[TEXT_SIZE+1000], mail_list[MAX_N_LIST][NAME_LENGTH], list[10000], smtp_host[256],
subject[256], attrib[MAX_N_ATTR][NAME_LENGTH], subst_str[256], in_reply_to[80]; subject[256], attrib[MAX_N_ATTR][NAME_LENGTH], subst_str[256], in_reply_to[80],
reply_to[256];
char *buffer[MAX_ATTACHMENTS], mail_param[1000]; char *buffer[MAX_ATTACHMENTS], mail_param[1000];
char att_file[MAX_ATTACHMENTS][256]; char att_file[MAX_ATTACHMENTS][256];
char slist[MAX_N_ATTR+10][NAME_LENGTH], svalue[MAX_N_ATTR+10][NAME_LENGTH]; char slist[MAX_N_ATTR+10][NAME_LENGTH], svalue[MAX_N_ATTR+10][NAME_LENGTH];
@ -5804,7 +5886,9 @@ int i, j, n, missing, first, index, n_attr, n_mail, suppress, message_id;
} }
message_id = 0; message_id = 0;
reply_to[0] = 0;
in_reply_to[0] = 0; in_reply_to[0] = 0;
date[0] = 0;
if (*getparam("edit") && if (*getparam("edit") &&
*getparam("resubmit") && *getparam("resubmit") &&
@ -5813,6 +5897,9 @@ int i, j, n, missing, first, index, n_attr, n_mail, suppress, message_id;
/* delete old message */ /* delete old message */
el_delete_message(lbs, atoi(getparam("orig")), FALSE, att_file, TRUE); el_delete_message(lbs, atoi(getparam("orig")), FALSE, att_file, TRUE);
unsetparam("orig"); unsetparam("orig");
strcpy(reply_to, "<keep>");
strcpy(in_reply_to, "<keep>");
date[0] = 0;
} }
else else
{ {
@ -5820,13 +5907,15 @@ int i, j, n, missing, first, index, n_attr, n_mail, suppress, message_id;
{ {
message_id = atoi(getparam("orig")); message_id = atoi(getparam("orig"));
strcpy(in_reply_to, "<keep>"); strcpy(in_reply_to, "<keep>");
strcpy(reply_to, "<keep>");
strcpy(date, "<keep>");
} }
else else
strcpy(in_reply_to, getparam("orig")); strcpy(in_reply_to, getparam("orig"));
} }
message_id = el_submit(lbs, message_id, attr_list, attrib, n_attr, getparam("text"), message_id = el_submit(lbs, message_id, date, attr_list, attrib, n_attr, getparam("text"),
in_reply_to, "<keep>", *getparam("html") ? "HTML" : "plain", in_reply_to, reply_to, *getparam("html") ? "HTML" : "plain",
att_file, att_file,
_attachment_buffer, _attachment_buffer,
_attachment_size); _attachment_size);
@ -6051,7 +6140,7 @@ LOGBOOK *lbs_dest;
/* submit in destination logbook without links */ /* submit in destination logbook without links */
message_id = el_submit(lbs_dest, 0, attr_list, attrib, n_attr, text, message_id = el_submit(lbs_dest, 0, date, attr_list, attrib, n_attr, text,
"", "", encoding, "", "", encoding,
attachment, attachment,
_attachment_buffer, _attachment_buffer,