⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abook-loadsave.c

📁 一个功能全面的电子邮件客户端
💻 C
字号:
/* TradeClient <http://tradeclient.sourceforge.net> * $Id: abook-loadsave.c,v 1.10 2001/03/20 22:19:33 ttabner Exp $ * * Copyright (C) 1999-2000 Bynari Inc. * Copyright (C) 2001 Project TradeClient * * LGPL * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library * General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#include "puma.h"#ifdef write#undef write#endif#define BEGIN ('\03')#define SEPER	('\04')#define ESEP	('\05')#define ENDS	('\06')int abook_unlink (char *name) {	char *dir=(char *)calloc(1024, sizeof(char));	char *fullpath=(char *)calloc(1024, sizeof(char));	snprintf (dir, 1023, "$(HOME)/.tradeclient/Addressbooks");	env_srch_rep (dir);	snprintf (fullpath, 1024, "%s/%s", dir, name);	if (unlink(fullpath)==-1) {		switch (errno) {			case EACCES:				insert_warning (_("Cannot delete %s: Permission denied\n"), fullpath);				return FALSE;				break;			case ENOENT:				insert_warning (_("Cannot delete %s: File not found\n"), fullpath);				return FALSE;				break;			case ELOOP:				insert_warning (_("Cannot delete %s: Too many symbolic links encountered\n"), fullpath);				return FALSE;				break;		}	}	return TRUE;}void abooks_read () {	DIR *tdir;	struct dirent *de;	struct stat *st=(struct stat *)calloc(1, sizeof(struct stat));	Abook *abook;	char *dir;	char *tmps=(char *)calloc(1024, sizeof(char));	dir=(char *)calloc(1024, sizeof(char));	snprintf (dir, 1023, "$(HOME)/.tradeclient/Addressbooks");	env_srch_rep (dir);	tdir=opendir (dir);	if (tdir==NULL) {		mkdir(dir, S_IREAD | S_IWRITE | S_IEXEC);		tdir=opendir (dir);	}	while ((de=readdir(tdir))) {		if ( (strncmp (de->d_name,".",1)!=0) && (strncmp (de->d_name,"..",1)!=0) ) {			snprintf (tmps, 1023, "%s/%s", dir, de->d_name);			stat (tmps, st);			if (S_ISREG(st->st_mode)) {				abook=abook_new ();				abook->name=strdup (de->d_name);			}		}	}	closedir (tdir);	free (st);	free (dir);	free (tmps);}char *getnext (pfile *pf, char sep) {	int i=0;	char ch=~sep;	char *tmps=(char *)calloc (2048, sizeof(char));	while ( (ch!=sep) && (i<2048) ) {		ch=getch(pf);		tmps[i]=ch;		i++;	}	if (i==0) {		return NULL;	}	if (tmps[i-1]==sep) {		tmps[i-1]='\0';	}	if (tmps[i]==sep) {		tmps[i]='\0';	}	return tmps ;}void abook_load (char *dir, char *name) {	char *tmps=(char *)calloc (10, sizeof(char));	Abook *book=abook_seek_by_name (name);	pfile *pf=new_pfile ();	g_return_if_fail (book != NULL);		snprintf (pf->name, 2047, "%s/%s", dir, name);	pf->fs=fopen (pf->name, "r");	if (pf->fs==NULL) {		goto done;	}	tmps[0]=getch (pf);	tmps[1]=getch (pf);	tmps[2]=getch (pf);	tmps[3]=getch (pf);	if (strncmp(tmps, "BTCL", 4)!=0) {		insert_warning  (_("File %s is not a TradeClient Contacts List\n"), pf->name);		goto done;	}	tmps[0]=getch (pf);	tmps[1]=getch (pf);	tmps[2]=getch (pf);	tmps[3]=getch (pf);	switch (atoi(tmps)) {		case 1:			abook_load_ver_0001 (pf, book);			break;		default: 			ungetch(pf); 			ungetch(pf); 			ungetch(pf); 			ungetch(pf); 			ungetch(pf);			abook_load_ver_0000 (pf, book);			break;	}done:	fclose (pf->fs);	free (tmps);	delete_pfile( pf ) ;}void abook_load_ver_0000 (pfile *pf, Abook *book) {	char *tmps=(char *)calloc (10, sizeof(char));	char *first, *middle, *last, *title, *sex, *nickname, *hphone;     	char *wphone, *mphone, *pager, *fax, *street, *webpage, *notes;	char *email_address=NULL;	Abook_entry *entry;	char ch;	memset (tmps, 0, 9);	while (pf->T_EOF==0) {		ch=getch (pf);		while ( (ch!=BEGIN) ) {			ch=getch (pf);			if ((pf->T_EOF!=0)||(ch==EOF))				goto done;		}		tmps[0]=getch (pf);		tmps[1]=getch (pf);		tmps[2]=getch (pf);		tmps[3]=getch (pf);		first=getnext(pf, SEPER);		middle=getnext(pf, SEPER);		last=getnext(pf, SEPER);		title=getnext(pf, SEPER);		nickname=getnext(pf, SEPER);		sex=getnext(pf, SEPER);		hphone=getnext(pf, SEPER);		wphone=getnext(pf, SEPER);		mphone=getnext(pf, SEPER);		pager=getnext(pf, SEPER);		fax=getnext(pf, SEPER);		street=getnext(pf, SEPER);		webpage=getnext(pf, SEPER);		notes=getnext(pf, SEPER);		entry=abook_entry_new (book);		abook_entry_edit (entry, first, middle, last, title, nickname, sex, hphone,   		                  wphone, mphone, pager, fax, street, webpage, NULL,		                  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,		                  NULL, NULL, NULL, NULL, NULL, notes);		if (first) free (first);		if (middle) free (middle);		if (last) free (last);		if (title) free (title);		if (nickname) free (nickname);		if (sex) free (sex);		if (hphone) free (hphone);		if (wphone) free (wphone);		if (mphone) free (mphone);		if (pager) free (pager);		if (fax) free (fax);		if (street) free (street);		if (webpage) free (webpage);		if (notes) free (notes);		first=NULL;		middle=NULL;		last=NULL;		title=NULL;		nickname=NULL;		sex=NULL;		hphone=NULL;		wphone=NULL;		mphone=NULL;		pager=NULL;		fax=NULL;		street=NULL;		webpage=NULL;		notes=NULL;		ch=getch (pf);		if ((ch!=ENDS)&&(ch!=EOF)) {			ungetch(pf);			while ((ch!=ENDS)&&(ch!=EOF)) {				email_address=getnext (pf, ESEP);				ch=getch (pf);				if ((ch!=ENDS)&&(ch!=EOF)) {					ungetch(pf);					if (email_address!=NULL) {					  abook_entry_add_email (entry, email_address);					  free( email_address ) ;					}				}			}			abook_entry_set_default (entry, email_address);			free( email_address ) ;		}	}done:	free (tmps);}void abook_load_ver_0001 (pfile *pf, Abook *book) {	char *tmps=(char *)calloc (10, sizeof(char));	char *group;	char *first, *middle, *last, *title, *nickname, *sex, *hphone, *wphone, *mphone;  	char *pager, *fax, *street, *webpage, *company, *position, *cstreet, *ccity;	char *cstate, *czip, *ccountry, *coffice, *cphone1, *cphone2, *cfax;	char *cpager, *cwebpage, *cmanager, *cassistant, *def_email, *notes;	char *email_address=NULL;	Abook_entry *entry;	char ch;	memset (tmps, 0, 9);	while (pf->T_EOF==0) {		ch=getch (pf);		while ( (ch!=BEGIN) ) {			ch=getch (pf);			if ((pf->T_EOF!=0)||(ch==EOF))				goto done;		}		tmps[0]=getch (pf);		tmps[1]=getch (pf);		tmps[2]=getch (pf);		tmps[3]=getch (pf);		first=getnext(pf, SEPER);		middle=getnext(pf, SEPER);		last=getnext(pf, SEPER);		title=getnext(pf, SEPER);		nickname=getnext(pf, SEPER);		sex=getnext(pf, SEPER);		hphone=getnext(pf, SEPER);		wphone=getnext(pf, SEPER);		mphone=getnext(pf, SEPER);		pager=getnext(pf, SEPER);		fax=getnext(pf, SEPER);		street=getnext(pf, SEPER);		webpage=getnext(pf, SEPER);		company=getnext(pf, SEPER);		position=getnext(pf, SEPER);		cstreet=getnext(pf, SEPER);		ccity=getnext(pf, SEPER);		cstate=getnext(pf, SEPER);		czip=getnext(pf, SEPER);		ccountry=getnext(pf, SEPER);		coffice=getnext(pf, SEPER);		cphone1=getnext(pf, SEPER);		cphone2=getnext(pf, SEPER);		cfax=getnext(pf, SEPER);		cpager=getnext(pf, SEPER);		cwebpage=getnext(pf, SEPER);		cmanager=getnext(pf, SEPER);		cassistant=getnext(pf, SEPER);		def_email=getnext(pf, SEPER);		notes=getnext(pf, SEPER);		entry=abook_entry_new (book);		abook_entry_edit (entry, first, middle, last, title, nickname, sex, hphone,   		                  wphone, mphone, pager, fax, street, webpage, company,		                  position, cstreet, ccity, cstate, czip, ccountry,		                  coffice, cphone1, cphone2, cfax, cpager, cwebpage,		                  cmanager, cassistant, notes);		if (first) free (first);		if (middle) free (middle);		if (last) free (last);		if (title) free (title);		if (nickname) free (nickname);		if (sex) free (sex);		if (hphone) free (hphone);		if (wphone) free (wphone);		if (mphone) free (mphone);		if (pager) free (pager);		if (fax) free (fax);		if (street) free (street);		if (webpage) free (webpage);		if (company) free (company);		if (position) free (position);		if (cstreet) free (cstreet);		if (ccity) free (ccity);		if (cstate) free (cstate);		if (czip) free (czip);		if (ccountry) free (ccountry);		if (coffice) free (coffice);		if (cphone1) free (cphone1);		if (cphone2) free (cphone2);		if (cfax) free (cfax);		if (cpager) free (cpager);		if (cwebpage) free (cwebpage);		if (cmanager) free (cmanager);		if (cassistant) free (cassistant);		if (def_email) free (def_email);		if (notes) free (notes);		first=NULL;		middle=NULL;		last=NULL;		title=NULL;		nickname=NULL;		sex=NULL;		hphone=NULL;		wphone=NULL;		mphone=NULL;		pager=NULL;		fax=NULL;		street=NULL;		webpage=NULL;		company=NULL;		position=NULL;		cstreet=NULL;		ccity=NULL;		cstate=NULL;		czip=NULL;		ccountry=NULL;		coffice=NULL;		cphone1=NULL;		cphone2=NULL;		cfax=NULL;		cpager=NULL;		cwebpage=NULL;		cmanager=NULL;		cassistant=NULL;		def_email=NULL;		notes=NULL;		ch=getch (pf);		if ((ch!=ENDS)&&(ch!=ESEP)&&(ch!=EOF)) {			ungetch(pf);			while ((ch!=ENDS)&&(ch!=ESEP)&&(ch!=EOF)) {				email_address=getnext (pf, SEPER);				ch=getch (pf);				if ((ch!=ENDS)&&(ch!=ESEP)&&(ch!=EOF)) {					ungetch(pf);					if (email_address!=NULL) {						abook_entry_add_email (entry, email_address);						free( email_address ) ;					}				}			}			abook_entry_set_default (entry, email_address);			free( email_address ) ;		}		ch=getch (pf);		if ((ch!=ENDS)&&(ch!=EOF)) {//			ungetch(pf);			while ((ch!=ENDS)&&(ch!=EOF)) {				ungetch(pf);				group=getnext (pf, SEPER);				if (group!=NULL) {					Abook_group *grp;					grp=abook_group_seek (book, group);					if (!grp) {						grp=abook_group_new (book);						grp->name=strdup (group);						free( group ) ;					}					abook_group_add_member (grp, entry);				}				ch=getch (pf);			}		}	}done:	free (tmps);}void abooks_load_all () {	GList *seek;	Abook *abook;	char *dir;  dir=(char *)calloc(1024, sizeof(char));  snprintf (dir, 1023, "$(HOME)/.tradeclient/Addressbooks");  env_srch_rep (dir);	for (seek=abook_first();seek;seek=seek->next) {		abook=seek->data;		abook_load (dir, abook->name);	}	free (dir);}void abook_save (Abook *abook) {	int fd;	GList *seek, *seek2;	Abook_entry *entry;	char *begin=(char *)calloc (4, sizeof (char)),	     *seper=(char *)calloc (4, sizeof (char)),	     *esep=(char *)calloc (4, sizeof (char)),       *ends=(char *)calloc (4, sizeof (char));	char *snum=NULL;	char *tmps=(char *)calloc (1024, sizeof (char));	char *dir=(char *)calloc (4096, sizeof (char));	g_return_if_fail (abook != NULL);	begin[0]=BEGIN;	seper[0]=SEPER;	esep[0]=ESEP;	ends[0]=ENDS;	snprintf (dir, 1023, "$(HOME)/.tradeclient/Addressbooks");	env_srch_rep (dir);	snprintf (tmps, 1023, "%s/%s", dir, abook->name);	unlink (tmps);	fd=open (tmps, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);	write (fd, "BTCL", 4);	write (fd, "0001", 4);	for (seek=abook->entrys;seek;seek=seek->next) {		entry=seek->data;		snum=disassemble_long (0);		write (fd, begin, 1);		write (fd, snum, 4);		free (snum);		if (entry->first) write (fd, entry->first, strlen (entry->first));		write (fd, seper, 1);		if (entry->middle) write (fd, entry->middle, strlen (entry->middle));		write (fd, seper, 1);		if (entry->last) write (fd, entry->last, strlen (entry->last));		write (fd, seper, 1);		if (entry->title) write (fd, entry->title, strlen (entry->title));		write (fd, seper, 1);		if (entry->nickname) write (fd, entry->nickname, strlen (entry->nickname));		write (fd, seper, 1);		if (entry->sex) write (fd, entry->sex, strlen (entry->sex));		write (fd, seper, 1);		if (entry->hphone) write (fd, entry->hphone, strlen (entry->hphone));		write (fd, seper, 1);		if (entry->wphone) write (fd, entry->wphone, strlen (entry->wphone));		write (fd, seper, 1);		if (entry->mphone) write (fd, entry->mphone, strlen (entry->mphone));		write (fd, seper, 1);		if (entry->pager) write (fd, entry->pager, strlen (entry->pager));		write (fd, seper, 1);		if (entry->fax) write (fd, entry->fax, strlen (entry->fax));		write (fd, seper, 1);		if (entry->street) write (fd, entry->street, strlen (entry->street));		write (fd, seper, 1);		if (entry->webpage) write (fd, entry->webpage, strlen (entry->webpage));		write (fd, seper, 1);		if (entry->company) write (fd, entry->company, strlen (entry->company));		write (fd, seper, 1);		if (entry->position) write (fd, entry->position, strlen (entry->position));		write (fd, seper, 1);		if (entry->cstreet) write (fd, entry->cstreet, strlen (entry->cstreet));		write (fd, seper, 1);		if (entry->ccity) write (fd, entry->ccity, strlen (entry->ccity));		write (fd, seper, 1);		if (entry->cstate) write (fd, entry->cstate, strlen (entry->cstate));		write (fd, seper, 1);		if (entry->czip) write (fd, entry->czip, strlen (entry->czip));		write (fd, seper, 1);		if (entry->ccountry) write (fd, entry->ccountry, strlen (entry->ccountry));		write (fd, seper, 1);		if (entry->coffice) write (fd, entry->coffice, strlen (entry->coffice));		write (fd, seper, 1);		if (entry->cphone1) write (fd, entry->cphone1, strlen (entry->cphone1));		write (fd, seper, 1);		if (entry->cphone2) write (fd, entry->cphone2, strlen (entry->cphone2));		write (fd, seper, 1);		if (entry->cfax) write (fd, entry->cfax, strlen (entry->cfax));		write (fd, seper, 1);		if (entry->cpager) write (fd, entry->cpager, strlen (entry->cpager));		write (fd, seper, 1);		if (entry->cwebpage) write (fd, entry->cwebpage, strlen (entry->cwebpage));		write (fd, seper, 1);		if (entry->cmanager) write (fd, entry->cmanager, strlen (entry->cmanager));		write (fd, seper, 1);		if (entry->cassistant) write (fd, entry->cassistant, strlen (entry->cassistant));		write (fd, seper, 1);		if (entry->def_email) write (fd, entry->def_email, strlen (entry->def_email));		write (fd, seper, 1);		if (entry->notes) write (fd, entry->notes, strlen (entry->notes));		write (fd, seper, 1);		for (seek2=entry->email;seek2;seek2=seek2->next) {			write (fd, (char *)seek2->data, strlen ((char *)seek2->data));			write (fd, seper, 1);		}		if (entry->def_email) 			write (fd, entry->def_email, strlen (entry->def_email));		write (fd, seper, 1);		write (fd, esep, 1);		for (seek2=entry->groups;seek2;seek2=seek2->next) {			Abook_group *group=seek2->data;			if (group);			write (fd, (char *)group->name, strlen ((char *)group->name));			write (fd, seper, 1);		}		write (fd, ends, 1);	}	close (fd);	if (begin) free (begin);	if (seper) free (seper);	if (esep) free (esep);	if (ends) free (ends);	if (tmps) free (tmps);	if (dir) free (dir);}void abooks_save_all () {	GList *seek=abook_first ();	Abook *abook;	for (seek=abook_first();seek;seek=seek->next) {		abook=seek->data;		abook_save (seek->data);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -