/** * Takes a string and breaks it up based on a specific character * and returns it in a dvector */ dvector explode(const char *str, const char *delim) { bool found=false; const char *ptr=str, *dtmp=NULL; dstrbuf *buf = dsbNew(100); dvector vec = dvCreate(5, __strexplodeDestr); while (*ptr != '\0') { dtmp = delim; while (*dtmp != '\0') { if (*ptr == *dtmp) { if (buf->len > 0) { dvAddItem(&vec, xstrdup(buf->str)); dsbClear(buf); } found = true; break; } dtmp++; } if (!found) { dsbCatChar(buf, *ptr); } else { found = false; } ptr++; } /* Add the last element if there was one. */ if (buf->len > 0) { dvAddItem(&vec, xstrdup(buf->str)); } dsbDestroy(buf); return vec; }
int copyUpTo(dstrbuf *buf, int stop, FILE *in) { int ch; while ((ch = fgetc(in)) != EOF) { if (ch == '\\') { ch = fgetc(in); if (ch == '\r') { ch = fgetc(in); } if (ch == '\n') { continue; } } if (ch == '\r') { ch = fgetc(in); if (ch == '\n') { return ch; } } if (ch == stop) { return ch; } dsbCatChar(buf, ch); } return ch; }
/** * Makes a message type specifically for gpg encryption and * signing. Specific MIME message descriptions are needed * when signing/encrypting a file before it is actuall signed * or encrypted. This function takes care of that. **/ static int makeGpgMessage(dstrbuf *in, dstrbuf *out, const char *border) { char *ptr=NULL; dstrbuf *qp=NULL; assert(in != NULL); assert(out != NULL); assert(border != NULL); if (Mopts.attach) { dsbPrintf(out, "Content-Type: multipart/mixed; " "boundary=\"%s\"\r\n\r\n", border); dsbPrintf(out, "\r\n--%s\r\n", border); } if (Mopts.html) { dsbPrintf(out, "Content-Type: text/html\r\n"); } else { dsbPrintf(out, "Content-Type: text/plain\r\n"); } dsbPrintf(out, "Content-Transfer-Encoding: quoted-printable\r\n\r\n"); qp = mimeQpEncodeString((u_char *)in->str, true); /* Fix single dot on it's on line so we don't terminate the message prematurely. */ dstrbuf *formatted = DSB_NEW; char previous='\0'; for (ptr = qp->str; ptr && *ptr != '\0'; previous=*ptr, ptr++) { dsbCatChar(formatted, *ptr); /* If we have a dot starting on a newline. */ if ((previous == '\n' || previous == '\r') && *ptr == '.') { dsbCatChar(formatted, '.'); } } dsbDestroy(qp); dsbnCat(out, formatted->str, formatted->len); dsbDestroy(formatted); if (Mopts.attach) { attachFiles(border, out); dsbPrintf(out, "\r\n--%s--\r\n", border); } return 0; }
dstrbuf * randomString(size_t size) { size_t i; long randval; struct timeval mill; dstrbuf *ret = DSB_NEW; gettimeofday(&mill, NULL); srand((getuid() + getpid()) + (mill.tv_usec << 16)); for (i=0; i < size; i++) { randval = rand() / (RAND_MAX / SIZEOF_LETTERS); dsbCatChar(ret, letters[randval]); } return ret; }
/* * Reads a line from a FILE * * Params * dsb - The dstrbuf to store the data into * file - a FILE to read the data from * * Returns * number of bytes read. */ size_t dsbReadline(dstrbuf *dsb, FILE *file) { int ch=0; size_t totalLen=0; assert(dsb != NULL); dsbClear(dsb); while ((ch = fgetc(file)) != EOF) { totalLen++; dsbCatChar(dsb, (char)ch); if (ch == '\n') { break; } } return totalLen; }
/** * Makes a standard plain text message while taking into * account the MIME message types and boundary's needed * if and when a file is attached. **/ static int makeMessage(dstrbuf *in, dstrbuf *out, const char *border, CharSetType charset) { char *ptr=NULL; dstrbuf *enc=NULL; if (Mopts.attach) { dsbPrintf(out, "--%s\r\n", border); if (charset == IS_UTF8 || charset == IS_PARTIAL_UTF8) { if (Mopts.html) { dsbPrintf(out, "Content-Type: text/html; charset=utf-8\r\n"); } else { dsbPrintf(out, "Content-Type: text/plain; charset=utf-8\r\n"); } if (IS_PARTIAL_UTF8) { dsbPrintf(out, "Content-Transfer-Encoding: quoted-printable\r\n"); enc = mimeQpEncodeString((u_char *)in->str, true); } else { dsbPrintf(out, "Content-Transfer-Encoding: base64\r\n"); enc = mimeB64EncodeString((u_char *)in->str, in->len, true); } dsbPrintf(out, "Content-Disposition: inline\r\n\r\n"); } else if (Mopts.html) { dsbPrintf(out, "Content-Type: text/html\r\n\r\n"); enc = DSB_NEW; dsbCat(enc, in->str); } else { dsbPrintf(out, "Content-Type: text/plain\r\n\r\n"); enc = DSB_NEW; dsbCat(enc, in->str); } } else { if (charset == IS_UTF8) { enc = mimeB64EncodeString((u_char *)in->str, in->len, true); } else if (charset == IS_PARTIAL_UTF8) { enc = mimeQpEncodeString((u_char *)in->str, true); } else { enc = DSB_NEW; dsbCat(enc, in->str); } } /* Fix single dot on it's on line so we don't terminate the message prematurely. */ dstrbuf *formatted = DSB_NEW; char previous='\0'; for (ptr = enc->str; ptr && *ptr != '\0'; previous=*ptr, ptr++) { dsbCatChar(formatted, *ptr); /* If we have a dot starting on a newline. */ if ((previous == '\n' || previous == '\r') && *ptr == '.') { dsbCatChar(formatted, '.'); } } dsbDestroy(enc); dsbPrintf(out, "%s\r\n", formatted->str); if (Mopts.attach) { if (attachFiles(border, out) == ERROR) { return ERROR; } dsbPrintf(out, "\r\n\r\n--%s--\r\n", border); } dsbDestroy(formatted); return 0; }