File: display.c

package info (click to toggle)
berolist 2.5.9-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 436 kB
  • ctags: 98
  • sloc: ansic: 2,792; sh: 1,507; makefile: 102
file content (187 lines) | stat: -rw-r--r-- 5,821 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* BeroList WWW gateway        */
/* Display/Edit single message */
/* $Id: display.c,v 1.1.1.1 1998/08/21 18:11:02 root Exp $ */

#include "list.h"
#include "tool.h"
#include "cgitool.h"
#include "cgipath.h"
#include <gdbm.h>
#ifdef HAVE_GETHOSTBYNAME
	#include <netdb.h>
#endif
#ifndef HOST
	char *hostname;
	#define HOST hostname
	#define GET_HOST
#endif

void errhead()
{
	html_head("Server configuration error");
	puts("<H1>Server configuration error</H1><HR>");
}

int main(int argc, char *argv[])
{
	char *listname, *configfile, *config, *archivefile;
	char *d_key, *s;
	char *subject,*from,*msgdat,*message;
	GDBM_FILE db;
	datum key,content;
	unsigned int num,i,function=0;
	register int j;

	#ifdef GET_HOST
		#ifdef HAVE_GETHOSTBYNAME
			struct hostent *x;
			hostname=salloc(1024);
			gethostname(hostname,1024);
			x=gethostbyname(hostname);
			if (x) strcpy(hostname,x->h_name); else strcpy(hostname,"localhost");
		#else   /* Try to determine hostname anyway */
			hostname=salloc(1024);
			gethostname(hostname,1024);
			hostname[slen(hostname)+1]='\0';
			hostname[slen(hostname)]='.';
			getdomainname(hostname+slen(hostname),1024-slen(hostname)-1);
		#endif
	#endif
	
	listname=cgi_scan("list");
	if(listname==NULL) {
		errhead();
		puts("The BeroList message list script is not configured correctly.<BR>");
		puts("You have to specify the parameter list=listname.");
		exit(1);
	}
	
	if(scasecmp(cgi_scan("function"),"edit")==0)
		function=1;

	s=cgi_scan("msg");
	if(s==NULL)
		i=0;
	else
		i=atoi(s);
	
	configfile=salloc(slen(LISTDIR)+slen(listname)+9);
	sprintf(configfile,"%s/%s.config",LISTDIR,listname);
	config=readfile(configfile);
	if(config==NULL) {
		errhead();
		printf("The specified list, %s, does not exist.",listname);
		exit(1);
	}

	if(function==1) {
		if(extract(config,"www-password=",'\n')==NULL) {
			errhead();
			puts("This list is not configured for WWW based administration.");
		} else if(scmp(extract(config,"www-password=",'\n'),cgi_scan("pw"))!=0) {
			errhead();
			puts("Wrong password.");
		}
	}
	archivefile=extract(config,"archive=",'\n');
	if(archivefile==NULL) {
		archivefile=salloc(slen(LISTDIR)+slen(listname)+10);
		sprintf(archivefile,"%s/%s.archive",LISTDIR,listname);
	}
	if(scasecmp(archivefile,"none")==0) {
		errhead();
		printf("The specified list, %s, does not archive messages.",listname);
		exit(1);
	} else if(archivefile[0]!='/') {
		archivefile=(char *) realloc(archivefile,slen(LISTDIR)+slen(archivefile)+2);
		sprintf(archivefile,"%s/%s\0",LISTDIR,archivefile);
	}
	db=gdbm_open(archivefile,0,GDBM_READER,00664,0);
	if(db==NULL) {
		errhead();
		printf("Couldn't open archive file for list %s...<BR>Archive empty?",listname);
		exit(1);
	}

	if(function==0)
		html_head("BeroList messages");
	else
		html_head("BeroList message editor");
	d_key=salloc(4);
	strcpy(d_key,"num");
	key.dptr=d_key;
	key.dsize=3;
	content=gdbm_fetch(db,key);
	free(d_key);
	num=atoi(content.dptr);
	if(i<0) i=0;
        if(num<=i) i=num-1;
        s=salloc(12);
        sprintf(s,"s%u",i);
        key.dptr=s;
        key.dsize=slen(s);
        content=gdbm_fetch(db,key);
        subject=salloc(content.dsize+1);
        strncpy(subject,content.dptr,content.dsize);

	sprintf(s,"d%u",i);
	key.dptr=s;
	key.dsize=slen(s);
	content=gdbm_fetch(db,key);
	if(content.dptr==NULL) {
		msgdat=salloc(20);
		sprintf(msgdat,"(date/time unknown)");
	} else {
		msgdat=salloc(content.dsize+1);
		strncpy(msgdat,content.dptr,content.dsize);
	}

        sprintf(s,"f%u",i);
        key.dptr=s;
        key.dsize=slen(s);
        content=gdbm_fetch(db,key);
        from=salloc(content.dsize+1);
        strncpy(from,content.dptr,content.dsize);
        free(s);
	if(function==0)
	        printf("<CENTER><H1>%s</H1>From <A HREF=mailto:%s>%s</A><BR>List <A HREF=mailto:%s@%s>%s</A><BR>%s</CENTER><HR>",subject,email(from),realname(from),listname,HOST,listname);
	else {
		printf("<FORM METHOD=GET ACTION=%s>",EDIT_CGI);
		puts("<INPUT TYPE=HIDDEN NAME=function VALUE=edit>");
		printf("<INPUT TYPE=HIDDEN NAME=list VALUE=%s>",listname);
		printf("<INPUT TYPE=HIDDEN NAME=pw VALUE=%s>",cgi_scan("pw"));
		printf("<INPUT TYPE=HIDDEN NAME=msg VALUE=%u>",i);
		printf("Message <INPUT TYPE=TEXT NAME=subject VALUE=\"%s\">",subject);
		printf("<BR>From <INPUT TYPE=TEXT NAME=from VALUE=\"%s\"><HR><BR><TEXTAREA NAME=text>",from);
	}
        free(from);
	s=salloc(12);
	sprintf(s,"%u",i);
	key.dptr=s;
	key.dsize=slen(s);
	content=gdbm_fetch(db,key);
	message=salloc(content.dsize+1);
	strncpy(message,content.dptr,content.dsize);
	if(function==0) {
		for(j=0;j<=slen(message);j++)
		{
			if(message[j]=='\n')
				puts("<BR>");
			else
				putchar(message[j]);
		}
		puts("<HR>");
		if(i>0)		printf("<A HREF=\"%s?list=%s&msg=%u\">previous message</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",DISPLAY_CGI,listname,i-1);
		printf("<A HREF=%s?list=%s>message list</A>&nbsp;&nbsp;&nbsp;&nbsp;",MESSAGES_CGI,listname);
		if(i<num-1)	printf("<A HREF=\"%s?list=%s&msg=%u\">next message</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",DISPLAY_CGI,listname,i+1);
	} else {
		puts(message);
		puts("</TEXTAREA><INPUT TYPE=SUBMIT VALUE=Apply>");
		puts("<HR>");
		if(i>0)		printf("<A HREF=\"%s?list=%s&function=edit&msg=%u&pw=%s\">previous message</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",DISPLAY_CGI,listname,i-1,cgi_scan("pw"));
		printf("<A HREF=%s?list=%s&function=edit&pw=%s>message list</A>&nbsp;&nbsp;&nbsp;&nbsp;",MESSAGES_CGI,listname,cgi_scan("pw"));
		if(i<num-1)	printf("<A HREF=\"%s?list=%s&msg=%u&function=edit&pw=%s\">next message</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",DISPLAY_CGI,listname,i+1,cgi_scan("pw"));
	}
	printf("<P><HR><ADDRESS><CENTER><A HREF=http://www.croftj.net/~bero/BeroList/>%s</A> - &copy; by <A HREF=mailto:bero@linux.net.eu.org>Bernhard Rosenkr&auml;nzer</A></ADDRESS>",VERSION);
	return 0;
}