#include #include #include #include #include #include #include "tlmpmail.h" #include "tlmpmail.m" PUBLIC NICK::NICK() { selected = false; } /* Edit an addressbook entry */ PUBLIC int NICK::edit(NICKS &nicks) { int ret = -1; DIALOG dia; dia.settype (DIATYPE_POPUP); dia.newf_str (MSG_U(F_NICKNAME,"Nick name"),nick); dia.last_noempty(); mime_translate (name.get(),name); dia.newf_str (MSG_U(F_FULLNAME,"Full name"),name); dia.newf_str (MSG_R(F_FCC),fcc,40); dia.newf_str (MSG_U(F_COMMENT,"Comment"),comment,60); for (int i=0; i<3; i++) addrs.add (new SSTRING); for (int i=0; iget()); }else{ fputs ("(",fout); for (int i=0; i0 ? "," : "",addrs.getitem(i)->get()); } fputs (")\t",fout); } fprintf (fout,"%s\t\n",fcc.get()); if (!comment.is_empty()){ fprintf (fout," %s\n",comment.get()); } } PUBLIC void NICK::copyaddr (EMAILADDRS &emails) { for (int j=0; j1 || adr->strchr('<')!=NULL){ emails.set ("",adr->get()); }else{ SSTRING tmpname; mime_translate (name.get(),tmpname); emails.set (tmpname.get(),adr->get()); } } } static const char *addrbook_getstrif(SSTRINGS &tb, int no) { const char *ret = ""; SSTRING *s = tb.getitem(no); if (s != NULL) ret = s->get(); return ret; } static void addrbook_split (const char *line, SSTRINGS &tb) { char tmp[strlen(line)+1]; strcpy (tmp,line); char *start = tmp; char *pt = tmp; while (*pt != '\0'){ if (*pt == '\t'){ *pt = '\0'; tb.add (new SSTRING (start)); start = pt+1; } pt++; } if (start != pt) tb.add (new SSTRING (start)); } PUBLIC NICKS::NICKS() { SSTRING lastline; NICK *last; NICKS *tba; glocal.last = NULL; glocal.tba = this; SSTRING path; addrbook_getpath(path); (path.get(),false); int len = strlen(line); char tmp[len+1]; strcpy (tmp,line); if (len > 0 && tmp[len-1] == '\n') tmp[len-1] = '\0'; SSTRINGS tb; if (tmp[0] > ' ' && tmp[0] != '#'){ addrbook_split (tmp,tb); NICK *a = new NICK; glocal.tba->add (a); glocal.last = a; glocal.lastline.setfrom(tmp); }else if (glocal.last != NULL && tmp[0] == ' '){ SSTRING nl; nl.setfromf ("%s%s",glocal.lastline.get(),str_skip(tmp)); addrbook_split (nl.get(),tb); } glocal.last->nick.setfrom (addrbook_getstrif(tb,0)); glocal.last->name.setfrom (addrbook_getstrif(tb,1)); const char *tmpa = addrbook_getstrif(tb,2); glocal.last->addrs.remove_all(); if (tmpa[0] == '('){ // This is a list tmpa++; int len = strlen(tmpa); char tmpaa[len+1]; strcpy (tmpaa,tmpa); if (len > 0 && tmpaa[len-1] == ')') tmpaa[len-1] = '\0'; str_splitline (tmpaa,',',glocal.last->addrs); }else{ glocal.last->addrs.add (new SSTRING(tmpa)); } glocal.last->fcc.setfrom (addrbook_getstrif(tb,3)); glocal.last->comment.setfrom (addrbook_getstrif(tb,4)); return 0; } PUBLIC NICK *NICKS::getitem (int no) const { return (NICK*)ARRAY::getitem(no); } PUBLIC NICK *NICKS::getitem (const char *nick) const { NICK *ret = NULL; for (int i=0; inick.cmp(nick)==0){ ret = n; break; } } return ret; } PUBLIC NICK *NICKS::locateaddr (const char *addr) const { NICK *ret = NULL; for (int i=0; iaddrs.lookup(addr)!=-1){ ret = n; break; } } return ret; } PUBLIC int NICKS::write () { int ret = -1; SSTRING path; addrbook_getpath(path); FILE *fout = xconf_fopen (path.get(),"w"); if (fout != NULL){ for (int i=0; iwrite(fout); } ret = fclose (fout); } return ret; } /* Return the path of the main address book */ void addrbook_getpath (SSTRING &path) { const char *home = getenv ("HOME"); if (home != NULL) path.setfromf("%s/.addressbook",home); } /* Pick interactivly some addresses in the addresses book. Use prefix to filter the one presented. */ int addrbook_pick(EMAILADDRS &addrs, const char *prefix) { int ret; EMAILADDRS *addrs; NICKS tba; NICKS sorted; bool filter; const char *prefix; glocal.filter = prefix[0] != '\0'; glocal.prefix = prefix; glocal.ret = -1; glocal.addrs = &addrs; glocal.sorted.neverdelete(); (MSG_U(T_ADDRBOOK,"Address book") ,MSG_U(I_ADDRBOOK ,"Pick an address\n" "Use shift key to pick more than one\n" "Use third button to edit an entry") ,help_nil); newf_head (MSG_U(H_ADDRBOOK,"Nick name\tFull name\tEmail\tFcc\tComment")); settype (DIATYPE_POPUP); sortable(); sortpolicy("aaaaa"); addwhat (MSG_U(I_ADDNICK,"a new entry")); if (glocal.prefix[0] != '\0'){ setbutinfo (MENU_USR1,MSG_U(B_FILTERON,"Filter On/Off") ,MSG_R(B_FILTERON)); } glocal.sorted.remove_all(); int len = strlen(glocal.prefix); for (int i=0; inick.ncmp(glocal.prefix,len)==0){ glocal.sorted.add (a); } } const char *fbold = guiid_setfont (10,GFONT_ID_DEFAULT,GFONT_STYLE_DEFAULT,GFONT_WEIGHT_BOLD,false); const char *dcb = guiid_setdc (fbold,NULL,NULL); const char *dc = guiid_setdc (NULL,NULL,NULL); for (int i=0; iaddrs.getnb(); j++){ const char *adr = a->addrs.getitem(j)->get(); adrs.appendf ("%s%s",j==0 ? "" : ",",adr); } SSTRING name; mime_translate (a->name.get(),name); const char *adr = adrs.get(); if (strlen(adr)>20){ tmp.setfromf ("%s\t%-20.20s...\t%s\t%s",name.get() ,adr ,a->fcc.get(),a->comment.get()); }else{ tmp.setfromf ("%s\t%s\t%s\t%s",name.get(),adr ,a->fcc.get(),a->comment.get()); } setnextdcs (a->selected ? dcb : dc); new_menuitem (a->nick.get(),tmp.get()); } NICK *a = glocal.sorted.getitem(no); if (uistate.rightb){ int ok = a->edit(glocal.tba); if (ok >= 0){ if (ok == 1){ glocal.tba.remove_del(a); } glocal.tba.write(); } }else{ a->selected = !a->selected; if (!uistate.shiftkey){ endedit(); for (int i=0; iselected){ a->copyaddr (*glocal.addrs); } } glocal.ret = 0; } } NICK *a = new NICK; int ok = a->edit(glocal.tba); if (ok == 0){ glocal.tba.add (a); glocal.tba.write(); }else{ delete a; } if (code == MENU_USR1){ glocal.filter = !glocal.filter; } return glocal.ret; } int addrbook_pick(EMAILADDRS &addrs) { return addrbook_pick (addrs,""); } /* Find the folder carbon copy associated with an address if it is in the address book. Return -1 if none found or defined. */ int addrbook_getfcc(const char *addr, SSTRING &fcc) { int ret = -1; NICKS nicks; for (int i=0; iaddrs.getitem(0)->cmp(addr)==0){ fcc.setfrom (n->fcc); ret = fcc.is_filled() ? 0 : -1; break; } } return ret; } /* Complete a nick name from the address book. Return -1 if none found or select. If several nick name match the prefix, a popup is presented */ int addrbook_complete (const char *nick, EMAILADDRS &emails) { int ret = 0; NICKS nicks; int found = 0; NICK *one = NULL; int len = strlen(nick); for (int i=0; inick.ncmp(nick,len)==0){ found++; one = n; } } if (found == 1){ one->copyaddr (emails); ret = 1; }else if (found > 1){ addrbook_pick (emails,nick); ret = 1; } return ret; } void addrbook_add (MAIL_MESSAGE &msg) { EMAILADDRS addrs; glocal.addrs.append (msg.from); glocal.addrs.append (msg.to); glocal.addrs.append (msg.cc); ("","",help_nil); newf_head (MSG_U(H_PICKADDR,"Address\tName")); sortable(); sortpolicy("aa"); settype (DIATYPE_POPUP); for (int i=0; iget() ,glocal.addrs.names.getitem(i)->get()); } NICKS tba; NICK *a = new NICK; a->name.setfrom (glocal.addrs.names.getitem(no)); a->addrs.add (new SSTRING(glocal.addrs.addrs.getitem(no))); int ok = a->edit(tba); if (ok == 0){ tba.add (a); tba.write(); }else{ delete a; } }