/** * get the name of the file going to be attached from an absolute path **/ dstrbuf * mimeFilename(const char *in_name) { char *nameptr=NULL; dstrbuf *ret = DSB_NEW; nameptr = strrchr(in_name, '/'); if (nameptr) { dsbCopy(ret, ++nameptr); } else { dsbCopy(ret, in_name); } return ret; }
/** * takes a string that is a supposed file path, and * checks for certian wildcards ( like ~ for home directory ) * and resolves to an actual absolute path. **/ dstrbuf * expandPath(const char *path) { struct passwd *pw = NULL; dstrbuf *tmp = DSB_NEW; dstrbuf *ret = DSB_NEW; dsbCopy(tmp, path); if (tmp->len > 0 && tmp->str[0] == '&') { dsbCopy(ret, EMAIL_DIR); } else if (tmp->len > 0 && tmp->str[0] == '~') { if (tmp->str[1] == '/') { pw = getpwuid(getuid()); } else { int pos = strfind(tmp->str, '/'); if (pos >= 0) { char *p = substr(tmp->str, 1, pos-1); if (p) { pw = getpwnam(p); xfree(p); } } if (!pw) { pw = getpwuid(getuid()); } } if (pw) { dsbCopy(ret, pw->pw_dir); } } if (ret->len > 0) { int pos = strfind(tmp->str, '/'); if (pos > 0) { char *p = substr(tmp->str, pos, tmp->len); if (p) { dsbCat(ret, p); xfree(p); } } } else { dsbCopy(ret, path); } dsbDestroy(tmp); return ret; }
/** * Get the first element from the Mopts.to list of emails * and return it without the name or formating. just the * email address itself. **/ dstrbuf * getFirstEmail(void) { char *tmp=NULL; dstrbuf *buf = DSB_NEW; struct addr *a = (struct addr *)dlGetTop(Mopts.to); assert(a != NULL); /* If we haven't found a <, consider the e-mail unformatted. */ tmp = strchr(a->email, '<'); if (!tmp) { tmp = a->email; } else { /* strchr only brings us to the '<', Get past it */ ++tmp; } dsbCopy(buf, tmp); tmp = strchr(buf->str, '>'); if (tmp) { *tmp = '\0'; } return buf; }
/** * Simple interface to copy over buffer into error string */ static void smtpSetErr(const char *buf) { if (!errorstr) { errorstr = DSB_NEW; } dsbClear(errorstr); dsbCopy(errorstr, buf); }
dstrbuf * mimeFiletype(const char *filename) { bool found=false; int i=0, veclen=0; dstrbuf *type=NULL; dstrbuf *buf=DSB_NEW; dvector vec=NULL; const char *ext=NULL; dstrbuf *filen=NULL; FILE *file = fopen(MAGIC_FILE, "r"); if (!file) { goto exit; } filen = mimeFilename(filename); ext = strrchr(filen->str, '.'); /* If we don't know the extension, we don't know what type * of file it's going to be. Therefore, skip all of this. */ if (!ext) { goto exit; } /* Get past . in extension name. */ ext++; while (!feof(file)) { dsbReadline(buf, file); if (buf->str[0] == '#' || buf->str[0] == '\n') { continue; } chomp(buf->str); /* If we still have an allocated type, free it */ if (type) { dsbDestroy(type); } type = getMimeType(buf->str); if (type->len == 0) { continue; } vec = explode(buf->str, " \t"); veclen = dvLength(vec); /* Start i at 1 since the first element in the * vector is the mime type. The exts are after that. */ for (i=1; i < veclen; i++) { if (strcmp((char *)vec[i], ext) == 0) { found = true; break; } } dvDestroy(vec); if (found) { /* Found it! */ break; } } exit: dsbDestroy(filen); dsbDestroy(buf); if (file) { fclose(file); } if (!type || type->len == 0) { type = DSB_NEW; dsbCopy(type, "application/unknown"); } return type; }