#include #include #include "tlmpmail.h" /* Copy until a marker is found or end of string We assume s is pointing on the opening marker. */ static const char *folder_copyto ( const char *s, SSTRING &val, const char endchar, const char endchar2) { s = str_skip(s+1); const char *end = s; while (*end != '\0' && *end != endchar && *end != endchar2) end++; val.setfrom(s,(int)(end - s)); val.strip_end (); return end; } /* Parse a From or Reply-To line and split the name from the email address. We must support the format name and email(name) */ PUBLIC void EMAILADDRS::parse (const char *s) { while (*s != '\0'){ s = str_skip(s); SSTRING *name = new SSTRING; SSTRING *addr = new SSTRING; names.add (name); addrs.add (addr); if (s[0] == '"'){ s = str_extract (s,*name); name->strip_end (); }else if (s[0] != '<'){ char tmp[strlen(s)+1]; char *pt = tmp; while (*s != '\0' && *s != '<' && *s != '(' && *s != ',') *pt++ = *s++; *pt = '\0'; strip_end (tmp); name->setfrom (tmp); }else{ name->setempty(); } s = str_skip(s); if (s[0] == '<'){ s = folder_copyto (s,*addr,'>',','); if (*s == '>') s++; }else if (s[0] == '('){ addr->setfrom(*name); s = folder_copyto (s,*name,')',','); if (*s == ')') s++; }else{ addr->setfrom(name->get()); } s = str_skip(s); if (*s == ',') s = str_skip(s+1); } } PUBLIC void EMAILADDRS::append (const EMAILADDRS &tb) { names.append (tb.names); addrs.append (tb.addrs); } /* Append some addresses to the table, but avoid duplicates */ PUBLIC void EMAILADDRS::append_nodup (const EMAILADDRS &tb) { for (int i=0; iget(); if (addrs.lookup(addr)==-1){ set (tb.names.getitem(i)->get(),addr); } } } PUBLIC const char *EMAILADDRS::getfirstname() const { const char *ret = ""; SSTRING *s = names.getitem(0); if (s != NULL) ret = s->get(); return ret; } PUBLIC const char *EMAILADDRS::getfirstaddr() const { const char *ret = ""; SSTRING *s = addrs.getitem(0); if (s != NULL) ret = s->get(); return ret; } PUBLIC bool EMAILADDRS::is_empty() const { return addrs.getnb()==0; } PUBLIC bool EMAILADDRS::is_filled() const { return !is_empty(); } PUBLIC int EMAILADDRS::getnb() const { return names.getnb(); } PUBLIC void EMAILADDRS::set (const char *name, const char *addr) { names.add (new SSTRING(name)); addrs.add (new SSTRING(addr)); } PRIVATE void EMAILADDRS::format_append (int i, SSTRING &s) const { if (i >=0 && i < size()){ const char *name = names.getitem(i)->get(); const char *addr = addrs.getitem(i)->get(); const char *quote = ""; if (strchr(name,',')!=NULL) quote = "\""; if (strcmp(name,addr)==0){ s.appendf ("%s%s%s",quote,name,quote); }else{ s.appendf ("%s%s%s <%s>",quote,name,quote,addr); } } } PUBLIC void EMAILADDRS::format (SSTRING &s) const { s.setempty(); for (int i=0; i=0 && i < size()){ const char *name = names.getitem(i)->get(); const char *addr = addrs.getitem(i)->get(); to.set (name,addr); } } PUBLIC void EMAILADDRS::remove_all () { names.remove_all(); addrs.remove_all(); } PUBLIC void EMAILADDRS::get (int i, SSTRING &name, SSTRING &addr) { if (i >=0 && i < size()){ name = names.getitem(i)->get(); addr = addrs.getitem(i)->get(); } }