]> code.citadel.org Git - citadel.git/blob - webcit/messages.c
Brought over message reading functions from old WebCit
[citadel.git] / webcit / messages.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <ctype.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <time.h>
8 #include "webcit.h"
9 #include "child.h"
10
11 char reply_to[512];
12 long msgarr[1024];
13
14 /*
15  * Look for URL's embedded in a buffer and make them linkable.  We use a
16  * target window in order to keep the BBS session in its own window.
17  */
18 void url(buf)
19 char buf[]; {
20
21         int pos;
22         int start,end;
23         char ench;
24         char urlbuf[256];
25         char outbuf[256];
26
27         start = (-1);
28         end = strlen(buf);
29         ench = 0;
30
31         for (pos=0; pos<strlen(buf); ++pos) {
32                 if (!strncasecmp(&buf[pos],"http://",7)) start = pos;
33                 if (!strncasecmp(&buf[pos],"ftp://",6)) start = pos;
34                 }
35
36         if (start<0) return;
37
38         if ((start>0)&&(buf[start-1]=='<')) ench = '>';
39         if ((start>0)&&(buf[start-1]=='[')) ench = ']';
40         if ((start>0)&&(buf[start-1]=='(')) ench = ')';
41         if ((start>0)&&(buf[start-1]=='{')) ench = '}';
42
43         for (pos=strlen(buf); pos>start; --pos) {
44                 if ((buf[pos]==' ')||(buf[pos]==ench)) end = pos;
45                 }
46
47         strncpy(urlbuf,&buf[start],end-start);
48         urlbuf[end-start] = 0;
49
50
51         strncpy(outbuf,buf,start);
52         sprintf(&outbuf[start],"%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c", 
53                 LB,QU,urlbuf,QU,QU,TARGET,QU,RB,urlbuf,LB,RB);
54         strcat(outbuf,&buf[end]);
55         strcpy(buf,outbuf);
56         }
57
58
59 void read_message(msgnum, oper)
60 long msgnum;
61 char *oper; {
62         char buf[256];
63         char m_subject[256];
64         char from[256];
65         long now;
66         struct tm *tm;
67         int format_type = 0;
68         int nhdr = 0;
69         int bq = 0;
70
71         sprintf(buf,"MSG0 %ld",msgnum);
72         serv_puts(buf);
73         serv_gets(buf);
74         if (buf[0]!='1') {
75                 wprintf("<STRONG>ERROR:</STRONG> %s<BR>\n",&buf[4]);
76                 return;
77                 }
78
79         wprintf("<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=0 BGCOLOR=000077><TR><TD>\n");
80         wprintf("<FONT COLOR=\"FFFF00\"><B> ");
81         strcpy(m_subject,"");
82
83         strcpy(reply_to,"nobody...xxxxx");
84         while(serv_gets(buf), strncasecmp(buf,"text",4)) {
85                 if (!strncasecmp(buf,"nhdr=yes",8)) nhdr=1;
86                 if (nhdr==1) buf[0]='_';
87                 if (!strncasecmp(buf,"type=",5))
88                         format_type=atoi(&buf[5]);
89                 if (!strncasecmp(buf,"from=",5)) {
90                         wprintf("from %s ",&buf[5]);
91                         strcpy(from,&buf[5]);
92                         }
93                 if (!strncasecmp(buf,"path=",5))
94                         strcpy(reply_to,&buf[5]);
95                 if (!strncasecmp(buf,"subj=",5))
96                         strcpy(m_subject,&buf[5]);
97                 if ((!strncasecmp(buf,"hnod=",5)) 
98                    && (strcasecmp(&buf[5],serv_info.serv_humannode)))
99                         wprintf("(%s) ",&buf[5]);
100                 if ((!strncasecmp(buf,"room=",5))
101                    && (strcasecmp(&buf[5],wc_roomname)))
102                         wprintf("in %s> ",&buf[5]);
103
104                 if (!strncasecmp(buf,"node=",5)) {
105                         if ( (room_flags&QR_NETWORK)
106                            || ((strcasecmp(&buf[5],serv_info.serv_nodename)
107                            &&(strcasecmp(&buf[5],serv_info.serv_fqdn)))))
108                                 {
109                                 wprintf("@%s ",&buf[5]);
110                                 }
111                         if ((!strcasecmp(&buf[5],serv_info.serv_nodename))
112                            ||(!strcasecmp(&buf[5],serv_info.serv_fqdn)))
113                                 {
114                                 strcpy(reply_to,from);
115                                 }
116                         else if (haschar(&buf[5],'.')==0) {
117                                 sprintf(reply_to,"%s @ %s",from,&buf[5]);
118                                 }
119                         }
120
121                 if (!strncasecmp(buf,"rcpt=",5))
122                         wprintf("to %s ",&buf[5]);
123                 if (!strncasecmp(buf,"time=",5)) {
124                         now=atol(&buf[5]);
125                         tm=(struct tm *)localtime(&now);
126                         strcpy(buf,(char *)asctime(tm)); buf[strlen(buf)-1]=0;
127                         strcpy(&buf[16],&buf[19]);
128                         wprintf("%s ",&buf[4]);
129                         }
130                 }
131
132         if (nhdr==1) wprintf("****");
133         wprintf("</B></FONT></TD>");
134         
135         if (is_room_aide) {
136                 wprintf("<TD ALIGN=RIGHT NOWRAP><FONT COLOR=\"FFFF00\"><B>");
137
138                 wprintf("<A HREF=\"/confirm_move_msg");
139                 wprintf("&msgid=%ld", msgnum);
140                 wprintf("&referer=%s\">Move</A>", oper);
141
142                 wprintf("&nbsp;&nbsp;");
143
144                 wprintf("<A HREF=\"/confirm_delete_msg");
145                 wprintf("&msgid=%ld", msgnum);
146                 wprintf("&referer=%s\">Del</A>", oper);
147
148                 wprintf("</B></FONT></TD>");
149                 }
150         
151         wprintf("</TR></TABLE>\n");
152
153         if (strlen(m_subject)>0) {
154                 wprintf("Subject: %s<BR>\n",m_subject);
155                 }
156
157         if (format_type == 0) {
158                 fmout(NULL);
159                 }
160         else {
161                 while(serv_gets(buf), strcmp(buf,"000")) {
162                         while ((strlen(buf)>0)&&(isspace(buf[strlen(buf)-1])))
163                                 buf[strlen(buf)-1] = 0;
164                         if ((bq==0)&&
165 ((!strncmp(buf,">",1))||(!strncmp(buf," >",2))||(!strncmp(buf," :-)",4)))) {
166                                 wprintf("<FONT COLOR=\"000044\"><I>");
167                                 bq = 1;
168                                 }
169                         else if ((bq==1)&&
170 (strncmp(buf,">",1))&&(strncmp(buf," >",2))&&(strncmp(buf," :-)",4))) {
171                                 wprintf("</FONT></I>");
172                                 bq = 0;
173                                 }
174                         wprintf("<TT>");
175                         url(buf);
176                         escputs(buf);
177                         wprintf("</TT><BR>\n");
178                         }
179                 }
180         }
181
182
183
184 /* 
185  * load message pointers from the server
186  */
187 int load_msg_ptrs(servcmd)
188 char *servcmd; {
189         char buf[256];
190         int nummsgs;
191
192         nummsgs = 0;
193         serv_puts(servcmd);
194         serv_gets(buf);
195         if (buf[0]!='1') {
196                 wprintf("<EM>%s</EM><BR>\n",&buf[4]);
197                 return(nummsgs);
198                 }
199         while (serv_gets(buf), strcmp(buf,"000")) {
200                 msgarr[nummsgs] = atol(buf);
201                 ++nummsgs;
202                 }
203         return(nummsgs);
204         }
205
206
207 /*
208  * command loop for reading messages
209  */
210 void readloop(char *oper) {
211         char cmd[256];
212         int a;
213         int nummsgs;
214
215         printf("HTTP/1.0 200 OK\n");
216         output_headers();
217         wprintf("<HTML><HEAD><TITLE>Messages</TITLE>\n");
218         wprintf("</HEAD><BODY BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
219
220         wprintf("<CENTER><B>%s - ",wc_roomname);
221         if (!strcmp(oper,"readnew")) {
222                 strcpy(cmd,"MSGS NEW");
223                 wprintf("new messages");
224                 }
225         else if (!strcmp(oper,"readold")) {
226                 strcpy(cmd,"MSGS OLD");
227                 wprintf("old messages");
228                 }
229         else {
230                 strcpy(cmd,"MSGS ALL");
231                 wprintf("all messages");
232                 }
233         wprintf("</B></CENTER><BR>\n");
234
235         nummsgs = load_msg_ptrs(cmd);
236         if (nummsgs == 0) {
237                 if (!strcmp(oper,"readnew")) {
238                         wprintf("<EM>No new messages in this room.</EM>\n");
239                         }
240                 else if (!strcmp(oper,"readold")) {
241                         wprintf("<EM>No old messages in this room.</EM>\n");
242                         }
243                 else {
244                         wprintf("<EM>This room is empty.</EM>\n");
245                         }
246                 goto DONE;
247                 }
248
249         for (a=0; a<nummsgs; ++a) {
250                 read_message(msgarr[a], oper);
251                 }
252
253 DONE:   wprintf("</BODY></HTML>\n");
254         wDumpContent();
255         }
256
257