serv_func.c

00001 /*
00002  * $Id: serv_func.c 5147 2007-05-08 15:36:22Z ajc $
00003  */
00010 #include "webcit.h"
00011 #include "webserver.h"
00012 
00013 struct serv_info serv_info; 
00020 void get_serv_info(char *browser_host, char *user_agent)
00021 {
00022         char buf[SIZ];
00023         int a;
00024 
00026         serv_printf("IDEN %d|%d|%d|%s|%s",
00027                 DEVELOPER_ID,
00028                 CLIENT_ID,
00029                 CLIENT_VERSION,
00030                 user_agent,
00031                 browser_host
00032         );
00033         serv_getln(buf, sizeof buf);
00034 
00036         serv_puts("MSGP text/html|text/plain");
00037         serv_getln(buf, sizeof buf);
00038 
00039 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
00040 
00045         serv_puts("ICAL sgi|1");
00046         serv_getln(buf, sizeof buf);
00047 #endif
00048 
00050         serv_puts("INFO");
00051         serv_getln(buf, sizeof buf);
00052         if (buf[0] != '1')
00053                 return;
00054 
00055         a = 0;
00056         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00057                 switch (a) {
00058                 case 0:
00059                         serv_info.serv_pid = atoi(buf);
00060                         WC->ctdl_pid = serv_info.serv_pid;
00061                         break;
00062                 case 1:
00063                         safestrncpy(serv_info.serv_nodename, buf, sizeof serv_info.serv_nodename);
00064                         break;
00065                 case 2:
00066                         safestrncpy(serv_info.serv_humannode, buf, sizeof serv_info.serv_humannode);
00067                         break;
00068                 case 3:
00069                         safestrncpy(serv_info.serv_fqdn, buf, sizeof serv_info.serv_fqdn);
00070                         break;
00071                 case 4:
00072                         safestrncpy(serv_info.serv_software, buf, sizeof serv_info.serv_software);
00073                         break;
00074                 case 5:
00075                         serv_info.serv_rev_level = atoi(buf);
00076                         break;
00077                 case 6:
00078                         safestrncpy(serv_info.serv_bbs_city, buf, sizeof serv_info.serv_bbs_city);
00079                         break;
00080                 case 7:
00081                         safestrncpy(serv_info.serv_sysadm, buf, sizeof serv_info.serv_sysadm);
00082                         break;
00083                 case 9:
00084                         safestrncpy(serv_info.serv_moreprompt, buf, sizeof serv_info.serv_moreprompt);
00085                         break;
00086                 case 14:
00087                         serv_info.serv_supports_ldap = atoi(buf);
00088                         break;
00089                 case 15:
00090                         serv_info.serv_newuser_disabled = atoi(buf);
00091                         break;
00092                 case 16:
00093                         safestrncpy(serv_info.serv_default_cal_zone, buf, sizeof serv_info.serv_default_cal_zone);
00094                         break;
00095                 }
00096                 ++a;
00097         }
00098 }
00099 
00100 
00101 
00106 void fmout(char *align)
00107 {
00108         int intext = 0;
00109         int bq = 0;
00110         char buf[SIZ];
00111 
00112         wprintf("<div align=%s>\n", align);
00113         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00114 
00115                 if ((intext == 1) && (isspace(buf[0]))) {
00116                         wprintf("<br />");
00117                 }
00118                 intext = 1;
00119 
00125                 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
00126                         wprintf("<BLOCKQUOTE>");
00127                         bq = 1;
00128                 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
00129                         wprintf("</BLOCKQUOTE>");
00130                         bq = 0;
00131                 }
00132                 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
00133                         strcpy(buf, &buf[2]);
00134                 }
00136                 url(buf);
00137 
00138                 escputs(buf);
00139                 wprintf("\n");
00140         }
00141         if (bq == 1) {
00142                 wprintf("</I>");
00143         }
00144         wprintf("</div><br />\n");
00145 }
00146 
00147 
00148 
00149 
00155 void pullquote_fmout(void) {
00156         int intext = 0;
00157         int bq = 0;
00158         char buf[SIZ];
00159 
00160         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00161 
00162                 if ((intext == 1) && (isspace(buf[0]))) {
00163                         wprintf("<br />");
00164                 }
00165                 intext = 1;
00166 
00172                 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
00173                         wprintf("<BLOCKQUOTE>");
00174                         bq = 1;
00175                 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
00176                         wprintf("</BLOCKQUOTE>");
00177                         bq = 0;
00178                 }
00179                 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
00180                         strcpy(buf, &buf[2]);
00181                 }
00182 
00183                 msgescputs(buf);
00184         }
00185         if (bq == 1) {
00186                 wprintf("</I>");
00187         }
00188 }
00189 
00190 
00191 
00192 
00198 void text_to_server(char *ptr)
00199 {
00200         char buf[256];
00201         int ch, a, pos;
00202 
00203         pos = 0;
00204         buf[0] = 0;
00205 
00206         while (ptr[pos] != 0) {
00207                 ch = ptr[pos++];
00208                 if (ch == 10) {
00209                         while ( (isspace(buf[strlen(buf) - 1]))
00210                           && (strlen(buf) > 1) )
00211                                 buf[strlen(buf) - 1] = 0;
00212                         serv_puts(buf);
00213                         buf[0] = 0;
00214                         if (ptr[pos] != 0) strcat(buf, " ");
00215                 } else {
00216                         a = strlen(buf);
00217                         buf[a + 1] = 0;
00218                         buf[a] = ch;
00219                         if ((ch == 32) && (strlen(buf) > 200)) {
00220                                 buf[a] = 0;
00221                                 serv_puts(buf);
00222                                 buf[0] = 0;
00223                         }
00224                         if (strlen(buf) > 250) {
00225                                 serv_puts(buf);
00226                                 buf[0] = 0;
00227                         }
00228                 }
00229         }
00230         serv_puts(buf);
00231 }
00232 
00233 
00240 void text_to_server_qp(char *ptr)
00241 {
00242         unsigned char ch, buf[256];
00243         int pos;
00244         int output_len = 0;
00245 
00246         pos = 0;
00247         buf[0] = 0;
00248         output_len = 0;
00249 
00250         while (ptr[pos] != 0) {
00251                 ch = (unsigned char)(ptr[pos++]);
00252 
00253                 if (ch == 13) {
00254                         /* ignore carriage returns */
00255                 }
00256                 else if (ch == 10) {
00257                         /* hard line break */
00258                         if (output_len > 0) {
00259                                 if (isspace(buf[output_len-1])) {
00260                                         sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
00261                                         output_len += 2;
00262                                 }
00263                         }
00264                         buf[output_len++] = 0;
00265                         serv_puts((char *)buf);
00266                         output_len = 0;
00267                 }
00268                 else if (ch == 9) {
00269                         buf[output_len++] = ch;
00270                 }
00271                 else if ( (ch >= 32) && (ch <= 60) ) {
00272                         buf[output_len++] = ch;
00273                 }
00274                 else if ( (ch >= 62) && (ch <= 126) ) {
00275                         buf[output_len++] = ch;
00276                 }
00277                 else {
00278                         sprintf((char *)&buf[output_len], "=%02X", ch);
00279                         output_len += 3;
00280                 }
00281                 
00282                 if (output_len > 72) {
00283                         /* soft line break */
00284                         if (isspace(buf[output_len-1])) {
00285                                 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
00286                                 output_len += 2;
00287                         }
00288                         buf[output_len++] = '=';
00289                         buf[output_len++] = 0;
00290                         serv_puts((char *)buf);
00291                         output_len = 0;
00292                 }
00293         }
00294 
00295         /* end of data - transmit anything that's left */
00296         if (output_len > 0) {
00297                 if (isspace(buf[output_len-1])) {
00298                         sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
00299                         output_len += 2;
00300                 }
00301                 buf[output_len++] = 0;
00302                 serv_puts((char *)buf);
00303                 output_len = 0;
00304         }
00305 }
00306 
00307 
00308 
00309 
00314 void server_to_text()
00315 {
00316         char buf[SIZ];
00317 
00318         int count = 0;
00319 
00320         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00321                 if ((buf[0] == 32) && (count > 0)) {
00322                         wprintf("\n");
00323                 }
00324                 wprintf("%s", buf);
00325                 ++count;
00326         }
00327 }
00328 
00329 
00330 
00337 void read_server_binary(char *buffer, size_t total_len) {
00338         char buf[SIZ];
00339         size_t bytes = 0;
00340         size_t thisblock = 0;
00341 
00342         memset(buffer, 0, total_len);
00343         while (bytes < total_len) {
00344                 thisblock = 4095;
00345                 if ((total_len - bytes) < thisblock) {
00346                         thisblock = total_len - bytes;
00347                         if (thisblock == 0) return;
00348                 }
00349                 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
00350                 serv_getln(buf, sizeof buf);
00351                 if (buf[0] == '6') {
00352                         thisblock = (size_t)atoi(&buf[4]);
00353                         if (!WC->connected) return;
00354                         serv_read(&buffer[bytes], thisblock);
00355                         bytes += thisblock;
00356                 }
00357                 else {
00358                         lprintf(3, "Error: %s\n", &buf[4]);
00359                         return;
00360                 }
00361         }
00362 }
00363 
00364 
00370 char *read_server_text(void) {
00371         char *text = NULL;
00372         size_t bytes_allocated = 0;
00373         size_t bytes_read = 0;
00374         int linelen;
00375         char buf[SIZ];
00376 
00377         text = malloc(SIZ);
00378         if (text == NULL) {
00379                 return(NULL);
00380         }
00381         text[0] = 0;
00382         bytes_allocated = SIZ;
00383 
00384         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00385                 linelen = strlen(buf);
00386                 buf[linelen] = '\n';
00387                 buf[linelen+1] = 0;
00388                 ++linelen;
00389 
00390                 if ((bytes_read + linelen) >= (bytes_allocated - 2)) {
00391                         bytes_allocated = 2 * bytes_allocated;
00392                         text = realloc(text, bytes_allocated);
00393                 }
00394 
00395                 strcpy(&text[bytes_read], buf);
00396                 bytes_read += linelen;
00397         }
00398 
00399         return(text);
00400 }
00401 
00402 
00403 

Generated on Wed Jun 20 23:13:10 2007 for webcit by  doxygen 1.5.2