aboutsummaryrefslogtreecommitdiff
path: root/src/Groups.c
blob: e1c833b1efef19e05811f5818332ee5e921da0a0 (plain)
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
 /*@@
   @file      Groups.c
   @date      Wed Sep 14 23:47:43 2000
   @author    Gabrielle Allen
   @desc
              Pages about groups
   @enddesc
   @version   $Id$
 @@*/

#include <stdio.h>
#include <stdlib.h>

#include "cctk.h"
#include "util_String.h"

#include "CactusConnect/HTTPD/src/http_Request.h"
#include "CactusConnect/HTTPD/src/http_Content.h"

static const char *rcsid = "$Header$";

CCTK_FILEVERSION(CactusConnect_HTTPDExtra_Groups_c)


/********************************************************************
 ********************* Local Routine Prototypes *********************
 ********************************************************************/

int HTTPUTILS_RegisterPages(void);
static int MessagesPage(const cGH *cctkGH, httpRequest *request, void *data);

/********************************************************************
 *********************     External Routines   **********************
 ********************************************************************/

 /*@@
   @routine    HTTPUTILS_RegisterPages
   @date       Wed Sep 14 11:29:43 2000
   @author     Gabrielle Allen
   @desc
               Httpd utils registration routine.
   @enddesc
@@*/
int HTTPUTILS_RegisterPages (void)
{
  /* Register the message board page. */
  HTTP_RegisterPage ("/Messages", MessagesPage, NULL);

  HTTP_ContentLink ("/Messages/index.html", "Message Board",
                    "Collaborative simulation notepad",
                    HTTP_QUICKLINK);

  return (0);
}

/********************************************************************
 *********************     Local Routines   *************************
 ********************************************************************/

 /*@@
   @routine    MessagesPage
   @date       Sat Sep 16
   @author     Gabrielle Allen
   @desc
               Message board for simulation.
   @enddesc
@@*/
static int MessagesPage (const cGH *GH, httpRequest *request, void *data)
{
  int retval;
  size_t message_board_len;
  char message[1024], currtime[64], currdate[64];
  const char *const_string, *name, *memo;
  static char *message_board = NULL;


  /* avoid compiler warning about unused parameter */
  data = data;

  if (request->n_arguments > 0)
  {
    name = HTTP_ArgumentValue (request, "name");
    memo = HTTP_ArgumentValue (request, "memo");

    if (name && *name && memo && *memo)
    {
      /* concatenate new message, labeled with current date/time, to the
         message board */
      Util_CurrentTime (sizeof (currtime), currtime);
      Util_CurrentDate (sizeof (currdate), currdate);

      if (message_board)
      {
        message_board_len = strlen (message_board);
        message_board = (char *) realloc (message_board,
                                          message_board_len +
                                          strlen (currtime) + strlen (currdate)+
                                          strlen (name) + strlen (memo) + 60);
      }
      else
      {
        message_board_len = 0;
        message_board = (char *) malloc (strlen (currtime) + strlen (currdate) +
                                         strlen (name) + strlen (memo) + 60);
      }
      if (message_board)
      {
        sprintf (message_board + message_board_len,
                 "<P><STRONG>%s</STRONG> %s %s<BR>\n\n<I>%s</I></P>",
                 name, currtime, currdate, memo);
      }
    }

    /* Now redirect the browser to the normal message board page */
    if (request->http_major_version < 1 ||
        (request->http_major_version == 1 && request->http_minor_version < 1))
    {
      /* Older browsers don't understand 303 */
      const_string = "HTTP/1.0 302 Found\r\n"
                     "Location: /Messages/index.html\r\n\r\n";
    }
    else
    {
      const_string = "HTTP/1.0 303 See Other\r\n"
                     "Location: /Messages/index.html\r\n\r\n";
    }

    HTTP_Write (request, const_string, strlen (const_string));

    return (0);
  }

  /* Status message */
  const_string = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
  HTTP_Write (request, const_string, strlen (const_string));

  /* Start the page */
  const_string = "<HTML><HEAD><TITLE>CACTUS Messages</TITLE>\n";
  HTTP_Write (request, const_string, strlen (const_string));

  /* Write out the header part */
  HTTP_ContentHeader (GH, 0, sizeof (message), message, NULL);
  HTTP_Write (request, message, strlen (message));

  const_string = "<CENTER><H1>Message Board</H1></CENTER>\n"
                 "<P>This page can be used to post messages during a "
                 "simulation. At the moment the messages will disappear "
                 "when the simulation finishes, but soon there will be an "
                 "option to save them to a file.</P>\n"
                 "<CENTER><TABLE><FORM ACTION=\"/Messages/\">\n"
                 "<TR><TD>Name:</TD>"
                 "<TD><INPUT TYPE=TEXT SIZE=40 MAXLENGTH=100 NAME=\"name\" "
                 "VALUE=\"\"></TD></TR>\n"
                 "<TR><TD VALIGN=TOP>Message:</TD><TD>"
                 "<TEXTAREA NAME=\"memo\" ROWS=10 COLS=40></TEXTAREA>\n"
                 "</TD></TR></TABLE>"
                 "<INPUT TYPE=SUBMIT VALUE=\"Submit Message\"></FORM>\n"
                 "<TABLE WIDTH=80%%><TR><TD><H2>Messages:</H2></TD></TR>"
                 "<TR><TD><TABLE WIDTH=\"100%%\" CELLPADDING=5 CELLSPACING=5>"
                 "<TR><TD BGCOLOR=\"#E9F4D3\">";
  HTTP_Write (request, const_string, strlen (const_string));

  const_string = message_board ? message_board :
                 "No messages yet ... use the form above to add one";
  HTTP_Write (request, const_string, strlen (const_string));

  const_string = "</TD></TR></TABLE></TD></TR></TABLE></CENTER>";
  HTTP_Write (request, const_string, strlen (const_string));

  /* Write out the footer part. */
  HTTP_ContentFooter (GH, 0, sizeof (message), message);
  retval = HTTP_Write (request, message, strlen (message));

  return (retval);
}