/* Grid is modified. Columns are removed and sorted as required. */ static void filterCols(EdiGrid *grid, MprHash *options, MprHash *colOptions) { MprList *gridCols; MprHash *cp; EdiRec *rec; EdiField f; cchar *columnName; char key[8]; int ncols, r, fnum, currentPos, c, index, *desired, *location, pos, t; gridCols = ediGetGridColumns(grid); if (colOptions) { /* Sort grid record columns into the order specified by the column options */ ncols = grid->records[0]->nfields; location = mprAlloc(sizeof(int) * ncols); for (c = 0; c < ncols; c++) { location[c] = c; } ncols = mprGetHashLength(colOptions); desired = mprAlloc(sizeof(int) * ncols); for (c = 0; c < ncols; c++) { cp = mprLookupKey(colOptions, itosbuf(key, sizeof(key), c, 10)); if ((columnName = mprLookupKey(cp, "name")) == 0) { mprError("Cannot locate \"name\" field for column in table"); return; } pos = mprLookupStringItem(gridCols, columnName); if (pos < 0) { mprError("Cannot find column \"%s\", columns: %s", columnName, mprListToString(gridCols, ", ")); } else { desired[c] = pos; location[c] = c; } } for (c = 0; c < ncols; c++) { fnum = c; for (r = 0; r < grid->nrecords; r++) { rec = grid->records[r]; rec->nfields = ncols; fnum = desired[c]; if (fnum < 0) { continue; } currentPos = location[fnum]; f = rec->fields[c]; rec->fields[c] = rec->fields[currentPos]; rec->fields[currentPos] = f; } t = location[c]; location[c] = location[fnum]; location[fnum] = t; } } else { /* If showId is false, remove the "id" column */ if (httpOption(options, "showId", "false", 0) && (index = mprLookupStringItem(gridCols, "id")) >= 0) { for (r = 0; r < grid->nrecords; r++) { rec = grid->records[r]; rec->nfields--; mprMemcpy(rec->fields, sizeof(EdiField) * rec->nfields, &rec->fields[index], sizeof(EdiField) * (rec->nfields - 1)); } } } }
PUBLIC int espEmail(HttpConn *conn, cchar *to, cchar *from, cchar *subject, MprTime date, cchar *mime, cchar *message, MprList *files) { MprList *lines; MprCmd *cmd; cchar *body, *boundary, *contents, *encoded, *file; char *out, *err; ssize length; int i, next, status; if (!from || !*from) { from = "anonymous"; } if (!subject || !*subject) { subject = "Mail message"; } if (!mime || !*mime) { mime = "text/plain"; } if (!date) { date = mprGetTime(); } boundary = sjoin("esp.mail=", mprGetMD5("BOUNDARY"), NULL); lines = mprCreateList(0, 0); mprAddItem(lines, sfmt("To: %s", to)); mprAddItem(lines, sfmt("From: %s", from)); mprAddItem(lines, sfmt("Date: %s", mprFormatLocalTime(0, date))); mprAddItem(lines, sfmt("Subject: %s", subject)); mprAddItem(lines, "MIME-Version: 1.0"); mprAddItem(lines, sfmt("Content-Type: multipart/mixed; boundary=%s", boundary)); mprAddItem(lines, ""); boundary = sjoin("--", boundary, NULL); mprAddItem(lines, boundary); mprAddItem(lines, sfmt("Content-Type: %s", mime)); mprAddItem(lines, ""); mprAddItem(lines, ""); mprAddItem(lines, message); for (ITERATE_ITEMS(files, file, next)) { mprAddItem(lines, boundary); if ((mime = mprLookupMime(NULL, file)) == 0) { mime = "application/octet-stream"; } mprAddItem(lines, "Content-Transfer-Encoding: base64"); mprAddItem(lines, sfmt("Content-Disposition: inline; filename=\"%s\"", mprGetPathBase(file))); mprAddItem(lines, sfmt("Content-Type: %s; name=\"%s\"", mime, mprGetPathBase(file))); mprAddItem(lines, ""); contents = mprReadPathContents(file, &length); encoded = mprEncode64Block(contents, length); for (i = 0; i < length; i += 76) { mprAddItem(lines, snclone(&encoded[i], i + 76)); } } mprAddItem(lines, sfmt("%s--", boundary)); body = mprListToString(lines, "\n"); httpTraceContent(conn, "esp.email", "context", body, slen(body), 0); cmd = mprCreateCmd(conn->dispatcher); if (mprRunCmd(cmd, "sendmail -t", NULL, body, &out, &err, -1, 0) < 0) { mprDestroyCmd(cmd); return MPR_ERR_CANT_OPEN; } if (mprWaitForCmd(cmd, ME_ESP_EMAIL_TIMEOUT) < 0) { httpTrace(conn, "esp.email.error", "error", "msg=\"Timeout waiting for command to complete\", timeout=%d, command=\"%s\"", ME_ESP_EMAIL_TIMEOUT, cmd->argv[0]); mprDestroyCmd(cmd); return MPR_ERR_CANT_COMPLETE; } if ((status = mprGetCmdExitStatus(cmd)) != 0) { httpTrace(conn, "esp.email.error", "error", "msg=\"Sendmail failed\", status=%d, error=\"%s\"", status, err); mprDestroyCmd(cmd); return MPR_ERR_CANT_WRITE; } mprDestroyCmd(cmd); return 0; }