/* Utils.cc ProcesaDate modified by Robin Whittle 24 July 2001 See "RW". */ #include "Utils.h" #include "Language.h" extern Language *L; extern int errno; void initStr (char *thestr) { thestr [0] = '\0'; } void initStr (char *thestr, const char *consstr) { strcpy (thestr, consstr); } int TranslateCmd (char *stcmd) { if (IsEmpty(stcmd)) return CMD_HOME; char *uplinea; uplinea = xstrdup (stcmd); xucase (uplinea); if (IsEmpty(uplinea) == true) {delete [] uplinea; return CMD_HOME;} if (strcmp (uplinea, "CHECK_COOKIE") == 0) {delete [] uplinea; return CMD_CHECK_COOKIE;} if (strcmp (uplinea, "CONF_ACT") == 0) {delete [] uplinea; return CMD_CONF_ACT;} if (strcmp (uplinea, "HOME") == 0) {delete [] uplinea; return CMD_HOME;} if (strcmp (uplinea, "LOGIN") == 0) {delete [] uplinea; return CMD_LOGIN;} if (strcmp (uplinea, "LOGOUT") == 0) {delete [] uplinea; return CMD_LOGOUT;} if (strcmp (uplinea, "HELP") == 0) {delete [] uplinea; return CMD_HELP;} if (strcmp (uplinea, "MG_DUMPFULLMSG") == 0) {delete [] uplinea; return CMD_MG_DUMPFULLMSG;} if (strcmp (uplinea, "MG_DUMPFULLHEADER") == 0) {delete [] uplinea; return CMD_MG_DUMPFULLHEADER;} if (strcmp (uplinea, "MG_DISPLAY") == 0) {delete [] uplinea; return CMD_MG_DISPLAY;} if (strcmp (uplinea, "MG_COPY") == 0) {delete [] uplinea; return CMD_MG_COPY;} if (strcmp (uplinea, "MG_MOVE") == 0) {delete [] uplinea; return CMD_MG_MOVE;} if (strcmp (uplinea, "MG_DELDISPLAY") == 0) {delete [] uplinea; return CMD_MG_DELDISPLAY;} if (strcmp (uplinea, "MG_UNDELDISPLAY")== 0) {delete [] uplinea; return CMD_MG_UNDELDISPLAY;} if (strcmp (uplinea, "MB_INDEX") == 0) {delete [] uplinea; return CMD_MB_INDEX;} if (strcmp (uplinea, "MB_SHOW") == 0) {delete [] uplinea; return CMD_MB_SHOW;} if (strcmp (uplinea, "MB_CHANGE") == 0) {delete [] uplinea; return CMD_MB_CHANGE;} if (strcmp (uplinea, "MB_CREATE") == 0) {delete [] uplinea; return CMD_MB_CREATE;} if (strcmp (uplinea, "MB_DELETE") == 0) {delete [] uplinea; return CMD_MB_DELETE;} if (strcmp (uplinea, "MB_RENAME") == 0) {delete [] uplinea; return CMD_MB_RENAME;} if (strcmp (uplinea, "MB_EXPUNGE") == 0) {delete [] uplinea; return CMD_MB_EXPUNGE;} if (strcmp (uplinea, "MB_DUMP") == 0) {delete [] uplinea; return CMD_MB_DUMP;} if (strcmp (uplinea, "MB_SEARCH") == 0) {delete [] uplinea; return CMD_MB_SEARCH;} if (strcmp (uplinea, "FG_DELETE") == 0) {delete [] uplinea; return CMD_FG_DELETE;} if (strcmp (uplinea, "FG_UNDELETE") == 0) {delete [] uplinea; return CMD_FG_UNDELETE;} if (strcmp (uplinea, "FG_ANSWERED") == 0) {delete [] uplinea; return CMD_FG_ANSWERED;} if (strcmp (uplinea, "FG_UNANSWERED") == 0) {delete [] uplinea; return CMD_FG_UNANSWERED;} if (strcmp (uplinea, "FG_FLAGGED") == 0) {delete [] uplinea; return CMD_FG_FLAGGED;} if (strcmp (uplinea, "FG_UNFLAGGED") == 0) {delete [] uplinea; return CMD_FG_UNFLAGGED;} if (strcmp (uplinea, "FG_SEEN") == 0) {delete [] uplinea; return CMD_FG_SEEN;} if (strcmp (uplinea, "FG_UNSEEN") == 0) {delete [] uplinea; return CMD_FG_UNSEEN;} if (strcmp (uplinea, "CM") == 0) {delete [] uplinea; return CMD_CM;} if (strcmp (uplinea, "CM_SEND") == 0) {delete [] uplinea; return CMD_CM_SEND;} if (strcmp (uplinea, "CM_CANCEL") == 0) {delete [] uplinea; return CMD_CM_CANCEL;} if (strcmp (uplinea, "CM_REPLY") == 0) {delete [] uplinea; return CMD_CM_REPLY;} if (strcmp (uplinea, "CM_FORWARD") == 0) {delete [] uplinea; return CMD_CM_FORWARD;} if (strcmp (uplinea, "CM_CLEANALL") == 0) {delete [] uplinea; return CMD_CM_CLEANALL;} if (strcmp (uplinea, "CM_SAVE") == 0) {delete [] uplinea; return CMD_CM_SAVE;} if (strcmp (uplinea, "OP") == 0) {delete [] uplinea; return CMD_OP;} if (strcmp (uplinea, "OP_CANCEL") == 0) {delete [] uplinea; return CMD_OP_CANCEL;} if (strcmp (uplinea, "OP_SAVE") == 0) {delete [] uplinea; return CMD_OP_SAVE;} if (strcmp (uplinea, "AT_SHOW") == 0) {delete [] uplinea; return CMD_AT_SHOW;} if (strcmp (uplinea, "AT_DUMP") == 0) {delete [] uplinea; return CMD_AT_DUMP;} if (strcmp (uplinea, "AT_SAVE") == 0) {delete [] uplinea; return CMD_AT_SAVE;} if (strcmp (uplinea, "AT_ERASE") == 0) {delete [] uplinea; return CMD_AT_ERASE;} if (strcmp (uplinea, "AB") == 0) {delete [] uplinea; return CMD_AB;} if (strcmp (uplinea, "AB_SHOWALL") == 0) {delete [] uplinea; return CMD_AB_SHOWALL;} if (strcmp (uplinea, "AB_SAVEENTRY") == 0) {delete [] uplinea; return CMD_AB_SAVEENTRY;} if (strcmp (uplinea, "AB_EDITENTRY") == 0) {delete [] uplinea; return CMD_AB_EDITENTRY;} if (strcmp (uplinea, "AB_DELEENTRIES") == 0) {delete [] uplinea; return CMD_AB_DELEENTRIES;} if (strcmp (uplinea, "AB_NEWENTRY") == 0) {delete [] uplinea; return CMD_AB_NEWENTRY;} if (strcmp (uplinea, "AB_SORT") == 0) {delete [] uplinea; return CMD_AB_SORT;} if (strcmp (uplinea, "AB_FROM_CM") == 0) {delete [] uplinea; return CMD_AB_FROM_CM;} if (strcmp (uplinea, "AB_TAKEADDRESS") == 0) {delete [] uplinea; return CMD_AB_TAKEADDRESS;} if (strcmp (uplinea, "AB_ADDFIELDS") == 0) {delete [] uplinea; return CMD_AB_ADDFIELDS;} if (strcmp (uplinea, "AB_DUMP") == 0) {delete [] uplinea; return CMD_AB_DUMP;} if (strcmp (uplinea, "NOOP") == 0) {delete [] uplinea; return CMD_NOOP;} if (strcmp (uplinea, "DEBUG") == 0) {delete [] uplinea; return CMD_DEBUG;} if (strcmp (uplinea, "MB_SORT") == 0) {delete [] uplinea; return CMD_MB_SORT;} if (strcmp (uplinea, "MG_FG_DELETE") == 0) {delete [] uplinea; return CMD_MG_FG_DELETE;} if (strcmp (uplinea, "MG_FG_UNDELETE") == 0) {delete [] uplinea; return CMD_MG_FG_UNDELETE;} if (strcmp (uplinea, "MG_FG_ANSWERED") == 0) {delete [] uplinea; return CMD_MG_FG_ANSWERED;} if (strcmp (uplinea, "MG_FG_UNANSWERED")== 0) {delete [] uplinea; return CMD_MG_FG_UNANSWERED;} if (strcmp (uplinea, "MG_FG_FLAGGED") == 0) {delete [] uplinea; return CMD_MG_FG_FLAGGED;} if (strcmp (uplinea, "MG_FG_UNFLAGGED") == 0) {delete [] uplinea; return CMD_MG_FG_UNFLAGGED;} if (strcmp (uplinea, "MG_FG_SEEN") == 0) {delete [] uplinea; return CMD_MG_FG_SEEN;} if (strcmp (uplinea, "MG_FG_UNSEEN") == 0) {delete [] uplinea; return CMD_MG_FG_UNSEEN;} if (strcmp (uplinea, "MG_FG_SEARCHED") == 0) {delete [] uplinea; return CMD_MG_FG_SEARCHED;} if (strcmp (uplinea, "MG_FG_UNSEARCHED")== 0) {delete [] uplinea; return CMD_MG_FG_UNSEARCHED;} return CMD_INVALID; } int TranslateHelpCmd (char *sthelp) { char *uplinea; uplinea = xstrdup (sthelp); xucase (uplinea); if (strcmp (uplinea, "") == 0) {delete [] uplinea; return CMD_HELP_INDEXMAILBOX;} if (strcmp (uplinea, "LOGIN") == 0) {delete [] uplinea; return CMD_HELP_LOGIN;} if (strcmp (uplinea, "MB_INDEX") == 0) {delete [] uplinea; return CMD_HELP_INDEXMAILBOX;} if (strcmp (uplinea, "MG_DISPLAY") == 0) {delete [] uplinea; return CMD_HELP_DISPLAYMSG;} if (strcmp (uplinea, "MB_SHOW") == 0) {delete [] uplinea; return CMD_HELP_SHOWMAILBOXES;} if (strcmp (uplinea, "CM") == 0) {delete [] uplinea; return CMD_HELP_COMPOSEMSG;} if (strcmp (uplinea, "OP") == 0) {delete [] uplinea; return CMD_HELP_SHOWOPTIONS;} if (strcmp (uplinea, "AB") == 0) {delete [] uplinea; return CMD_HELP_ADDBOOK;} if (strcmp (uplinea, "AB_EDITENTRY") == 0) {delete [] uplinea; return CMD_HELP_AB_EDITENTRY;} if (strcmp (uplinea, "AB_NEWENTRY") == 0) {delete [] uplinea; return CMD_HELP_AB_NEWENTRY;} if (strcmp (uplinea, "AT_SHOW") == 0) {delete [] uplinea; return CMD_HELP_SHOWATTACHS;} if (strcmp (uplinea, "ABOUT") == 0) {delete [] uplinea; return CMD_HELP_ABOUT;} return CMD_HELP_INDEXMAILBOX; } void SaveFileDebug (char *fn, char *pc, int len) { FILE *fw; fw = fopen (fn, "w"); for (int i=0; i 3) && (L <= 6)) {j = (long) ((long)count / (long)1024); if (j == 0) j = 1; strncpy (stres, xltoa(j), 50); strcat(stres, "K");} else if ((L > 6) && (L <= 9)) {j = (long) ((long)count / (long)1048576); if (j == 0) j = 1; strncpy (stres, xltoa(j), 50); strcat(stres, "M");} else {strncpy (stres, stnum, 50); strcat(stres, "b");} if (strlen (stres) == 2) {initStr (stres2, " "); strcat (stres2, stres); return xstrdup(stres2);} else if (strlen (stres) == 3) {initStr (stres2, " "); strcat (stres2, stres); return xstrdup(stres2);} return xstrdup(stres); } char *FormatCountBytes1Dec (long count) { char stnum[30], stres[50]; long L; float f; strncpy (stnum, xltoa (count), 30); L = strlen (stnum); if (L <= 3) {initStr (stres, "<1.0 Kb");} else if ((L > 3) && (L <= 6)) {f = (float)count / (float)1024.0; if (f == 0.0) f = 1.0; sprintf (stres, "%.1f", f); strcat(stres, " Kb");} else if ((L > 6) && (L <= 9)) {f = (float)count / (float)1048576.0; if (f == 0.0) f = 1.0; sprintf (stres, "%.1f", f); strcat(stres, " Mb");} else {strncpy (stres, stnum, 50); strcat(stres, " b");} return xstrdup(stres); } void Redirect (char *newurl) { printf ("Status: 302 Moved\n"); printf ("Location: %s\n", newurl); printf ("Content-Type: text/html\n\n"); } void ErrorPage (char *error) { printf ("Content-type: text/html\n\n"); printf ("%s\n\n", HTML_DOCTYPE); printf ("\n"); printf ("%s", L->get(L_ERROR)); printf ("\n"); printf ("\n", DEFIMG_BG, TXTCOLOR, BGCOLOR, BLUE, BLUE2, RED); printf ("

%s: %s

\n", RED, L->get(L_ERROR), error); printf ("


\n"); printf ("%s\n", L->get (L_COPYRIGHT)); printf ("\n"); printf ("\n"); } void CGIErrorPageConnectionRefused (char *lang) { printf ("Content-type: text/html\n\n"); printf ("%s\n\n", HTML_DOCTYPE); printf ("\n"); printf ("%s", L->get(L_ERROR)); printf ("\n"); printf ("\n", DEFIMG_BG, TXTCOLOR, BGCOLOR, BLUE, BLUE2, RED); printf ("

%s: %s

\n", RED, L->get(L_ERROR), L->get(MSG_CONNREFUSED)); printf ("

%s
", L->get(MSG_SERVERISDOWN)); printf ("%s", L->get(MSG_PERHAPSTIMEOUT)); printf ("

%s", PATH_POSTMAN, lang, L->get(MSG_CANRECONNECTHERE)); printf ("


\n"); printf ("%s\n", L->get (L_COPYRIGHT)); printf ("\n"); printf ("\n"); exit (0); } void PrintHelpPage (char *topic) { int helpcmd; helpcmd = TranslateHelpCmd (topic); switch (helpcmd) { case CMD_HELP_LOGIN: Redirect(L->getPage(CMD_HELP_LOGIN)); break; case CMD_HELP_INDEXMAILBOX: Redirect(L->getPage(CMD_HELP_INDEXMAILBOX)); break; case CMD_HELP_DISPLAYMSG: Redirect(L->getPage(CMD_HELP_DISPLAYMSG)); break; case CMD_HELP_SHOWMAILBOXES: Redirect(L->getPage(CMD_HELP_SHOWMAILBOXES)); break; case CMD_HELP_COMPOSEMSG: Redirect(L->getPage(CMD_HELP_COMPOSEMSG)); break; case CMD_HELP_SHOWOPTIONS: Redirect(L->getPage(CMD_HELP_SHOWOPTIONS)); break; case CMD_HELP_ADDBOOK: Redirect(L->getPage(CMD_HELP_ADDBOOK)); break; case CMD_HELP_SHOWATTACHS: Redirect(L->getPage(CMD_HELP_SHOWATTACHS)); break; case CMD_HELP_ABOUT: Redirect(L->getPage(CMD_HELP_ABOUT)); break; case CMD_HELP_AB_NEWENTRY: Redirect(L->getPage(CMD_HELP_AB_NEWENTRY)); break; case CMD_HELP_AB_EDITENTRY: Redirect(L->getPage(CMD_HELP_AB_EDITENTRY)); break; default: Redirect(L->getPage(CMD_HELP_LOGIN)); break; } } //COPIA UN FICHERO, SI EL NUEVO EXISTE NO LO HACE Y DEVUELVE FALSE bool CopyFile (char *oldfile, char *newfile) { if (FileExists (newfile) == true) return false; char buffer[CMAXBUFFER]; FILE *FR, *FW; int sz, leido, res1, res2; sz = FileSize (oldfile); FR = fopen (oldfile, "r"); FW = fopen (newfile, "w"); if ((FR == NULL) || (FW == NULL)) return false; int totalleido = 0, porleer; bool PRIMER = false; while (!feof(FR) && (PRIMER == false)) { if (totalleido + CMAXBUFFER <= sz) porleer = CMAXBUFFER; else {porleer = sz - totalleido; PRIMER = true;} leido = fread (buffer, 1, porleer, FR); totalleido += leido; fwrite (buffer, 1, (int)leido, FW); } res1 = fclose (FR); res2 = fclose (FW); if ((res1 == EOF) || (res2 == EOF)) return false; return true; } //COPIA UN FICHERO, SI EL NUEVO EXISTE LO SOBRESCRIBE bool CopyFileOverWrite (char *oldfile, char *newfile) { if (FileExists (newfile) == true) DeleteFile (newfile); char buffer[CMAXBUFFER]; FILE *FR, *FW; int sz, leido, res1, res2; sz = FileSize (oldfile); FR = fopen (oldfile, "r"); FW = fopen (newfile, "w"); if ((FR == NULL) || (FW == NULL)) return false; int totalleido = 0, porleer; bool PRIMER = false; while (!feof(FR) && (PRIMER == false)) { if (totalleido + CMAXBUFFER <= sz) porleer = CMAXBUFFER; else {porleer = sz - totalleido; PRIMER = true;} leido = fread (buffer, 1, porleer, FR); totalleido += leido; fwrite (buffer, 1, (int)leido, FW); } res1 = fclose (FR); res2 = fclose (FW); if ((res1 == EOF) || (res2 == EOF)) return false; return true; } char *replace (char *src, char *why, char *forwhy) { if (src == NULL) return ""; XString JS, JS1, JS2; JS = XString (src); JS1 = XString (why); JS2 = XString (forwhy); JS.replace (JS1, JS2); return xstrdup ((char *)JS.cstr()); } char *HTML2text (char *ast) { if (ast == NULL) return ""; long L, LENORIG, LENNEW, SIZENEW; const int INCMEM = 1001; char c, *newpc, letra[2]; letra[1] = '\0'; LENNEW = 0; SIZENEW = strlen(ast) + INCMEM; newpc = (char *) malloc ((uint)SIZENEW); newpc[0] = '\0'; LENORIG = strlen (ast); for (L=0; L < LENORIG; ++L) { c = ast[L]; switch (c) { case '\010': //\n _J break; case '\013': //\r _M LENNEW = LENNEW + strlen (HTML_CR); if (LENNEW >= SIZENEW) {SIZENEW = SIZENEW + INCMEM; newpc = (char *)realloc (newpc, (uint)SIZENEW);} strcat (newpc, HTML_CR); break; case '>': LENNEW = LENNEW + strlen (HTML_MAYOR); if (LENNEW >= SIZENEW) {SIZENEW = SIZENEW + INCMEM; newpc = (char *)realloc (newpc, (uint)SIZENEW);} strcat (newpc, HTML_MAYOR); break; case '<': LENNEW = LENNEW + strlen (HTML_MENOR); if (LENNEW >= SIZENEW) {SIZENEW = SIZENEW + INCMEM; newpc = (char *)realloc (newpc, (uint)SIZENEW);} strcat (newpc, HTML_MENOR); break; case '"': LENNEW = LENNEW + strlen (HTML_DCOMILLAS); if (LENNEW >= SIZENEW) {SIZENEW = SIZENEW + INCMEM; newpc = (char *)realloc (newpc, (uint)SIZENEW);} strcat (newpc, HTML_DCOMILLAS); break; default: LENNEW = LENNEW + 1; if (LENNEW >= SIZENEW) {SIZENEW = SIZENEW + INCMEM; newpc = (char *)realloc (newpc, (uint)SIZENEW);} letra[0] = c; strcat (newpc, letra); break; } } return newpc; } bool IsEmpty (char *pc) { if (pc == NULL) return true; if (strcmp (pc, "") == 0) return true; for (xuint i=0; i= 0) && (ESPACIO == true)) { if (linea [i] != SP) {ESPACIO = false; linea[i + 1] = '\0';} --i; } } void trim (char *linea) { Ltrim (linea); Rtrim (linea); } char *ForceSpace (char *value) { if (IsEmpty(value)) return HTML_SP; else return value; } char *Cut (char *pc, int maxlong) { char *px; px = xstrdup (pc); if ((xuint)maxlong <= strlen(px)) { px[maxlong] = '\0'; } return px; } char *CutSign (char *pc, int maxlong, char *signal) { char *px; px = xstrdup (pc); if ((xuint)maxlong <= strlen(px)) { px[maxlong - strlen(signal)] = '\0'; strcat (px, signal); } return px; } //Coge un intervalo de lineas en el fichero entre x e y char *getLineFile (char *flname, int x, int y) { #define CMAXY 100000 TBuffer linea; FILE *fr; int cont, maxy; cont = 1; XString XS; XS = XString (""); fr = fopen (flname, "r"); if (fr == NULL) return ""; maxy = y; if (maxy == -1) maxy = CMAXY; while ((fgets (linea, CMAXBUFFER, fr) != NULL) && (cont <= maxy)) { if ((cont >= x) && (cont <= maxy)) { if (maxy != CMAXY) QuitaRetornoCarroDeLinea (linea); XS += linea; } ++cont; } fclose (fr); return xstrdup((char *)XS.cstr()); } void CreaFileSavedMsg (char *fn) { FILE *FW; FW = fopen (fn, "w"); fprintf (FW, "0\n"); fprintf (FW, "\n"); fprintf (FW, "\n"); fprintf (FW, "\n"); fprintf (FW, "\n"); fprintf (FW, "\n"); fclose (FW); } char *MakeTmpName (char *fntmp) { if (fntmp == NULL) { return xstrdup (tempnam(TMP, NULL)); } else { strncpy (fntmp, tempnam(TMP, NULL), CMAXBUFFER); return NULL; } } //Return true if the line will be cutted bool ConcatLineToLineOfFile (char *fn, int numlinea, char *whatadd, char *sep, int MAX) { XString XS; int cont; bool res; FILE *FR, *FW; THBuffer linea; TBuffer fntmp; MakeTmpName (fntmp); FR = fopen (fn, "r"); FW = fopen (fntmp, "w"); res = false; cont = 0; while (fgets (linea, CMAXHBUFFER, FR) != NULL) { ++cont; QuitaRetornoCarroDeLinea (linea); if (cont == numlinea) { if (!IsEmpty(linea)) { XS = XString (linea); XS += sep; XS += whatadd; } else XS = XString (whatadd); if (XS.length() > MAX) {res = true; XS.Cut (MAX);} fprintf (FW, "%s\n", XS.cstr()); } else { fprintf (FW, "%s\n", linea); } } fclose (FW); fclose (FR); CopyFileOverWrite (fntmp, fn); DeleteFile (fntmp); return res; } int CountChar (char *string, char tocount) { if (string == NULL) return 0; int cont = 0; for (xuint i=0; i < strlen(string); ++i) { if (string[i] == tocount) ++cont; } return cont; } pid_t WritePidFile (void) { pid_t thepid; TSBuffer fn; FILE *FW; thepid = getpid (); sprintf (fn, "%s/%s.pid", PIDDIR, PROGNAME); FW = fopen (fn, "w"); fprintf (FW, "%d\n", thepid); fclose (FW); return thepid; } void DeletePidFile (void) { TSBuffer fn; sprintf (fn, "%s/%s.pid", PIDDIR, PROGNAME); DeleteFile (fn); } void DelAllFilesFromDir (char *thedir) { DIR *dirp; struct dirent *dp; TBuffer abuf; dirp = opendir(thedir); while ((dp = readdir(dirp)) != NULL) { if ((strcmp ("..", dp->d_name) != 0) && (strcmp (".", dp->d_name) != 0)) { sprintf (abuf, "%s/%s", thedir, dp->d_name); DeleteFile (abuf); } } (void) closedir(dirp); } char *getMailcapTipoFromFilename (const char *fn, const char *fnmailcap) { if (fn == NULL) return ""; LINEA linea, exts, tipo, miext; FILE *FR; FR = fopen (fnmailcap, "r"); if (FR == NULL) { LOG ("Error: Can not open mailcap file:'%s'", fnmailcap); return ""; } initStr(miext, "."); strcat (miext, getExtensionFromFile (fn)); strcat (miext, "."); while (fgets (linea, MAXLENGTHLINE, FR) != NULL) { sscanf (linea, "%s %s", exts, tipo); if (NCstrstr (exts, miext) > 0) { fclose (FR); return strdup(tipo); } } fclose (FR); return ""; } void PrintHomePage (char *lang) { if (strcmp (lang, "spa") == 0) DumpHTMLPage (HOMEPAGE_SPA); else if (strcmp (lang, "val") == 0) DumpHTMLPage (HOMEPAGE_VAL); else DumpHTMLPage (HOMEPAGE_ENG); } void DumpHTMLPage (char *fn) { if (fn == NULL) return; LINEA linea; char c, CHARCOMMAND = '^'; printf ("Content-type: text/html\n\n"); FILE *FR; FR = fopen (fn, "r"); if (FR == NULL) return; while (fgets (linea, MAXLENGTHLINE, FR) != NULL) { if (strcmp (linea, "") != 0) { c = linea[0]; if (c == CHARCOMMAND) { TSBuffer charcommand, command, afn; sscanf (linea, "%s %s %s", charcommand, command, afn); if (strcmp (command, "INC") == 0) { FILE *f; TBuffer abuf; if (FileExists (afn) == true) { f = fopen (afn, "r"); if (f != NULL) { while (fgets (abuf, CMAXBUFFER, f) != NULL) { printf ("%s", abuf); } fclose (f); } } } } else { printf (linea); } } } fclose (FR); } long min (long i,long j) { return ((i < j) ? i : j); } void TICK (char *label) { static struct tms T0 = {0,0,0}; struct tms T1; times (&T1); if (label != NULL) {LOG ("%s TIME WASTED: User=%ld System=%ld", label, T1.tms_utime - T0.tms_utime, T1.tms_stime - T0.tms_stime);} T0 = T1; } void PChar2SL (char *chunk, StringList *SL) { int cont, i; char c, letra[2]; XString Ad; letra[1] = '\0'; SL->Clear(); Ad = XString(""); cont = -1; for (i = 0; i < (xsint)strlen(chunk); ++i) { ++cont; c = chunk[i]; letra[0] = c; switch (c) { case '\t': case ' ': { if (cont == 0) { Ad = XString(SL->getString(SL->Count() - 1)); Ad.Cut (Ad.length() - 2); SL->removeElementAt (SL->Count() - 1); } else { Ad += " "; if (i == (xsint)strlen(chunk) - 1) { Ad += "\r\n"; SL->Add(Ad); } } break; } case '\r': break; case '\n': cont = -1; Ad += "\r\n"; SL->Add(Ad); Ad = XString(""); break; default: Ad += letra; if (i == (xsint)strlen(chunk) - 1) { Ad += "\r\n"; SL->Add(Ad); } break; } } int pos = -1; for (i = 0; i < SL->Count(); ++i) { Ad = XString(SL->getString(i)).toUpperCase(); if (strstr(Ad.cstr(), "CONTENT-TRANSFER-ENCODING:") != NULL) pos = i; } if (pos > -1) SL->removeElementAt (pos); } void ResaltaURLsEnText (char *txt, XSocket *aXS, int MaxLongLine) { const unsigned int MAXPALABRA = 1000; int INCR, sizeorig, contlonglinea; int pos_initxt; char c, letra[2], palabra[MAXPALABRA]; TBuffer abuf; sizeorig = strlen (txt); INCR = (int)((float)sizeorig * (float)(5.0 / 100.0)); if (INCR <= 1000) INCR = 1000; letra[1] = '\0'; palabra[0] = '\0'; contlonglinea = 0; for (pos_initxt = 0; pos_initxt < sizeorig; ++pos_initxt) { ++contlonglinea; if (IsMultiplo (contlonglinea, MaxLongLine) == true) {contlonglinea=0; aXS->Write ("\n", 1);} c = txt[pos_initxt]; switch (c) { case '\r': case '\n': contlonglinea = 0; case ' ' : case '\t': case '(' : case ')' : case '<' : case '>' : case '\"': { if (strlen(palabra) > 0) { if ((strncasecmp (palabra, "http://", 7) == 0) || (strncasecmp (palabra, "ftp://", 6) == 0) || (strncasecmp (palabra, "https://", 8) == 0) || (strncasecmp (palabra, "mailto:", 7) == 0) || (strncasecmp (palabra, "news://", 7) == 0)) { sprintf (abuf, "%s", palabra, palabra); } else strncpy (abuf, palabra, CMAXBUFFER); aXS->Write (abuf, (uint)strlen(abuf)); } if (c == '<') { aXS->Write (HTML_MENOR, (uint)strlen(HTML_MENOR)); } else if (c == '>') { aXS->Write (HTML_MAYOR, (uint)strlen(HTML_MAYOR)); } else { letra[0] = c; aXS->Write (letra, (uint)strlen(letra)); } palabra[0] = '\0'; break; } default: { if (strlen(palabra) >= MAXPALABRA - 1) { if ((strncasecmp (palabra, "http://", 7) == 0) || (strncasecmp (palabra, "ftp://", 6) == 0) || (strncasecmp (palabra, "https://", 8) == 0) || (strncasecmp (palabra, "mailto:", 7) == 0) || (strncasecmp (palabra, "news://", 7) == 0)) { sprintf (abuf, "%s", palabra, palabra); } else strncpy (abuf, palabra, CMAXBUFFER); aXS->Write (abuf, (uint)strlen(abuf)); palabra[0] = '\0'; } letra[0] = c; strcat (palabra, letra); break; } } } if (strlen(palabra) > 0) { if ((strncasecmp (palabra, "http://", 7) == 0) || (strncasecmp (palabra, "ftp://", 6) == 0) || (strncasecmp (palabra, "https://", 8) == 0) || (strncasecmp (palabra, "mailto:", 7) == 0) || (strncasecmp (palabra, "news://", 7) == 0)) { sprintf (abuf, "%s", palabra, palabra); } else strncpy (abuf, palabra, CMAXBUFFER); aXS->Write (abuf, (uint)strlen(abuf)); } } //Devuelve true si dividendo es un multiplo de divisor bool IsMultiplo (int dividendo, int divisor) { if (dividendo % divisor == 0.0) return true; else return false; } //Una implementation, ma o menos, de sscanf para que separe los tokens con quoting "" void xscanf (const char *linea, char *c0, int *int1, int *int2, int *int3, char *c1, char *c2, char *c3, char *c4, char *c5, char *c6, char *c7) { #define MODO_COM 0 #define MODO_ESP 1 TBuffer palabra; char c, letra[2]; int L, i, MODO; StringList *SL; MODO = MODO_ESP; letra[1] = '\0'; initStr (palabra); SL = new StringList (); L = strlen (linea); for (i=0; iAdd(palabra); initStr (palabra); } MODO = MODO_ESP; } else { MODO = MODO_COM; } break; } case ' ': { if (MODO == MODO_ESP) { if (strcmp (palabra, "") != 0) { SL->Add(palabra); initStr (palabra); } } else {strcat (palabra, letra);} break; } default: { strcat (palabra, letra); } } } if (strcmp (palabra, "") != 0) {SL->Add(palabra);} for (i = 0; i < SL->Count(); ++i) { switch (i) { case 0: strncpy (c0, (char *)SL->getString(i).cstr(), CSMALLBUFFER); break; case 1: *int1 = xatoidef ((char *)SL->getString(i).cstr(), 0); break; case 2: *int2 = xatoidef ((char *)SL->getString(i).cstr(), 0); break; case 3: *int3 = xatoidef ((char *)SL->getString(i).cstr(), 0); break; case 4: if (c1 != NULL) {strncpy (c1, (char *)SL->getString(i).cstr(), CSMALLBUFFER);} break; case 5: if (c2 != NULL) {strncpy (c2, (char *)SL->getString(i).cstr(), CSMALLBUFFER);} break; case 6: if (c3 != NULL) {strncpy (c3, (char *)SL->getString(i).cstr(), CSMALLBUFFER);} break; case 7: if (c4 != NULL) {strncpy (c4, (char *)SL->getString(i).cstr(), CSMALLBUFFER);} break; case 8: if (c5 != NULL) {strncpy (c5, (char *)SL->getString(i).cstr(), CSMALLBUFFER);} break; case 9: if (c6 != NULL) {strncpy (c6, (char *)SL->getString(i).cstr(), CSMALLBUFFER);} break; case 10: if (c7 != NULL) {strncpy (c7, (char *)SL->getString(i).cstr(), CSMALLBUFFER);} break; } } delete SL; } // Changes by Robin Whittle 24 July 2001: // // 1 - Have a two digit year if the message // is not this year. // // 2 - Always have an hour:minute // // Ideally, we would correct for the // local timezone (with summertime) and // the timezones in each message. Leave // that for another day, since it involves // wrapping days, months and years. Best // to do that earlier, by working on the // raw data in the MESSAGECACHE data. //ENTRADA: FECHA=' 8-Sep-2000 08:51:13 +0100' //SALIDA : char *ProcesaDate (char *tmp) { char c, letra[2]; int L, i, cont, nowyear, dateyear; TSBuffer nuevafecha, dia, mes, anyo, resto; time_t now; struct tm *xtm; now = time (NULL); xtm = localtime (&now); nowyear = xtm->tm_year + 1900; letra[1] = '\0'; L = strlen (tmp); cont = 0; initStr(nuevafecha); initStr (dia), initStr (mes); initStr (anyo); initStr(resto); // The current year is figured out and vars // initialised. // // Step through the input string, which was // created by c-client mail.c function // mail_date() with a structure such as: for (i = 0; i < L; ++i) { c = tmp[i]; letra[0] = c; // Count the hyphens in " 8-Sep-2000" if (c == '-') {++cont;} // Skip any spaces, but not if it was // in tmp[0]. else if ((c == ' ') && (i > 0)) {++cont;} // For the first space, insert two   // This used to be one   but with proportional // spaced fonts, I find two spaces is about the // same width as a printable character space. else if ((c == ' ') && (i == 0)) { strcat (dia, HTML_HSP); strcat (dia, HTML_HSP); } else { switch (cont) { // Collect the day, month and year // in separate strings. case 0: strcat (dia, letra); break; case 1: strcat (mes, letra); break; case 2: strcat (anyo, letra); break; // Collect everything else here: // 08:51:13+0100 default: strcat (resto, letra); break; } } } // Utils.cc xatoidef() is described as: // CONVIERTE UN PCHAR A INT Y SI ES INVALIDO DEVUELVE EL DEFAULT // // Converts string numeric to an Int, with a default if input is invalid? dateyear = xatoidef ((const char *)anyo, 2000); // Output the day = "8". strcat (nuevafecha, dia); // Output a "-". strcat (nuevafecha, "-"); // Output month. strcat (nuevafecha, mes); // If the message is not from this year, output // the year preceded by "-". if (nowyear != dateyear) { strcat (nuevafecha, "-"); strcat (nuevafecha, anyo); } // Now we want to output the first 5 chars // of everything else, which is typically // the time, but first output two spaces. strcat (nuevafecha, HTML_HSP); strcat (nuevafecha, HTML_HSP); strncat (nuevafecha, resto, 5); // To give a bit of white space at the end // of this busy date section, and so to make // it easier to read the From section, add // four non-breaking spaces. strcat (nuevafecha, HTML_HSP); strcat (nuevafecha, HTML_HSP); strcat (nuevafecha, HTML_HSP); strcat (nuevafecha, HTML_HSP); return xstrdup (nuevafecha); } // End of changes. int GetBufferFromFile (char *afile, char *abuf, int maxlength) { TBuffer linea; int cont = 0; FILE *fr; initStr (abuf); fr = fopen (afile, "r"); if (fr == NULL) {return 0;} while (fgets (linea, CMAXBUFFER, fr) != NULL) { cont = cont + strlen (linea); if (cont < maxlength) {strcat (abuf, linea);} else {fclose (fr); return cont - strlen (linea);} } fclose (fr); return cont; } char *bool2char (bool b) { if (b == true) return "1"; else return "0"; } bool char2bool (char *c) { if (c == NULL) return false; if (strcmp (c, "0") == 0) return false; else return true; } char *DisplayFlags (const char *flags, bool clickable, const char *molde, int show_way) { XString XS; char c; TBuffer abuf; //show_way = 0: All flags //show_way = 1: Active flags //show_way = 2: Inactive flags XS = XString (HTML_HSP); XS += "\n"; for (xuint i=0; i < strlen (flags); ++i) { c = flags[i]; if (i == POS_FLAG_DELETED) { if ((c == FDELETED) && ((show_way == 0) || (show_way == 1))) { if (clickable) { sprintf (abuf, "get(L_FLAG_DELETED); XS += ")\">"; if (clickable) XS += "\n"; } else { if ((c == FEMPTY) && (clickable) && ((show_way == 0) || (show_way == 2))) { sprintf (abuf, "get(L_FLAG_UNDELETED); XS += ")\">"; XS += "\n"; } } } else if (i == POS_FLAG_SEARCHED) { if ((c == FSEARCHED) && ((show_way == 0) || (show_way == 1))) { if (clickable) { sprintf (abuf, "get(L_FLAG_SEARCHED); XS += ")\">"; if (clickable) XS += "\n"; } else { if ((c == FEMPTY) && (clickable) && ((show_way == 0) || (show_way == 2))) { sprintf (abuf, "get(L_FLAG_UNSEARCHED); XS += ")\">"; XS += "\n"; } } } else if (i == POS_FLAG_ANSWERED) { if ((c == FANSWERED) && ((show_way == 0) || (show_way == 1))) { if (clickable) { sprintf (abuf, "get(L_FLAG_ANSWERED); XS += ")\">"; if (clickable) XS += "\n"; } else { if ((c == FEMPTY) && (clickable) && ((show_way == 0) || (show_way == 2))) { sprintf (abuf, "get(L_FLAG_UNANSWERED); XS += ")\">"; XS += "\n"; } } } else if (i == POS_FLAG_IMPORTANT) { if ((c == FFLAGGED) && ((show_way == 0) || (show_way == 1))) { if (clickable) { sprintf (abuf, "get(L_FLAG_FLAGGED); XS += ")\">"; if (clickable) XS += "\n"; } else { if ((c == FEMPTY) && (clickable) && ((show_way == 0) || (show_way == 2))) { sprintf (abuf, "get(L_FLAG_UNFLAGGED); XS += ")\">"; XS += "\n"; } } } else if ((i == POS_FLAG_NEW) && (clickable == false)) { if (((c == FNEW) || (c == FUNSEEN)) && ((show_way == 0) || (show_way == 1))) { if (clickable) { sprintf (abuf, "get(L_FLAG_UNSEEN); XS += ")\">"; if (clickable) XS += "\n"; } else { if ((c == FEMPTY) && (clickable) && ((show_way == 0) || (show_way == 2))) { sprintf (abuf, "get(L_FLAG_SEEN); XS += ")\">"; XS += "\n"; } } } } return (xstrdup((char *)XS.cstr())); } int getNumChilds (void) { int ires; FILE *FR; char abuf[500 + 1]; strcpy (abuf, ""); FR = popen(CMDNUMCHILDS, "r"); if (FR != NULL) { //Only must get one line. while (fgets (abuf, 500, FR) != NULL) {/*NOTHING HERE*/}; pclose (FR); FR = fopen (TMPSERVERCOUNT, "r"); if (FR == NULL) {LOG ("Error opening '%s'", TMPSERVERCOUNT); return -1;} fgets (abuf, 500, FR); fclose (FR); QuitaRetornoCarroDeLinea(abuf); trim(abuf); ires = xatoidef (abuf, -1); } else { ires = -1; LOG ("Error haciendo popen!\n"); } return ires; } int SeparaTokensEnStringList (const char *pc, const char sep, StringList *SL) { XString XS; char c; int counter = 0; SL->Clear(); XS = ""; for (int i = 0; (unsigned int)i < strlen(pc); ++i) { c = pc[i]; if (c == sep) { if (XS.length() > 0) {++counter; SL->Add ((char *)XS.cstr());} XS = ""; } else { XS += c; } } if (XS.length() > 0) {++counter; SL->Add ((char *)XS.cstr());} return counter; } int CountTokensEnString (const char *pc, const char sep) { XString XS; char c; int counter = 0; for (int i = 0; (unsigned int)i < strlen(pc); ++i) { c = pc[i]; if (c == sep) { if (XS.length() > 0) {++counter;} XS = ""; } else { XS += c; } } if (XS.length() > 0) {++counter;} return counter; }