All pastes #740984 Raw Edit

Untitled

public text v1 · immutable
·published 2007-10-18 11:36 UTC
rendered paste body
	printf("Create profile with %s %s %s %s\n", username, password, domain, server);
	profname = g_strdup_printf("%s@%s", username, domain);
	
	workstation = "localhost";

	//FIXME: USE g_build_filename
	profpath = g_strdup_printf("%s/%s", getenv("HOME"), DEFAULT_PROF_PATH);
	if (!g_file_test (profpath, G_FILE_TEST_EXISTS)) {
		/* Create MAPI Profile */
		//FIXME: Get the PATH from Makefile
		if (CreateProfileStore (profpath, "/usr/local/samba/share/setup") != MAPI_E_SUCCESS) {
			g_warning ("Profile Database creation failed\n");
			return NULL;
		}
	}

	if (MAPIInitialize(profpath) != MAPI_E_SUCCESS){
		g_free(profpath);
		status = GetLastError();
		if (status == MAPI_E_SESSION_LIMIT){
			puts("[Openchange_plugin] openchange-lwmapi.c - Still connected");
			mapi_errstr("MAPIInitialize", GetLastError());

			
		}
		else 
			return NULL;
	}
	//g_free(profpath);

	
	printf("[Openchange_plugin] Profile creation %s in %s",profname, profpath);
	while(CreateProfile(profname, username, password, 0) == -1){
		retval = GetLastError();
		if(retval ==  MAPI_E_NO_ACCESS){
			printf("[Openchange_plugin] The profile alderly exist !. Deleting it and will recreate");
			
			if (DeleteProfile(profname) == -1) {
				retval = GetLastError();
				mapi_errstr("[Openchange_plugin] openchange_profile.c : DeleteProfile() ", retval);
				return NULL;
			}
		}
	}
	
	mapi_profile_add_string_attr(profname, "binding", server);
	mapi_profile_add_string_attr(profname, "workstation", workstation);
	mapi_profile_add_string_attr(profname, "domain", domain);
	
	/* This is only convenient here and should be replaced at some point */
	mapi_profile_add_string_attr(profname, "codepage", "0x4e4");
	mapi_profile_add_string_attr(profname, "language", "0x40c");
	mapi_profile_add_string_attr(profname, "method", "0x409");
	
	
	/* Login now */
	printf("Logging into the server\n");
	if (MapiLogonProvider(&session, profname, NULL, PROVIDER_ID_NSPI) == -1){
		retval = GetLastError();
		mapi_errstr("[Openchange_plugin] Error ", retval);
		if (retval == MAPI_E_NETWORK_ERROR){
			printf("Network error\n");
			return NULL;
		}
		if (retval == MAPI_E_LOGON_FAILED){
			printf("LOGIN Failed\n");
			return NULL;
		}
		printf("Generic error\n");
		return NULL;
	}


	
	printf("Login succeeded: Yeh\n");
	
	printf("[Openchange_plugin] Ambigous name and process filling");
	if (ProcessNetworkProfile(session, username, NULL, NULL) == -1){
		retval = GetLastError();
		mapi_errstr("[Openchange_plugin] openchange_profile.c : ProcessNetworkProfile", retval);
		if (retval != MAPI_E_SUCCESS && retval != 0x1){
			mapi_errstr("[Openchange_plugin] openchange_profile.c : Error ProcessNetworkProfile() ", retval);
			if (retval == MAPI_E_NOT_FOUND){
				printf("Bad user\n");
			}
			if (DeleteProfile(profname) == -1){
				retval = GetLastError();
				mapi_errstr("[Openchange_plugin] openchange_profile.c : DeleteProfile() ", retval);
			}
			return NULL;
		}
	}
	
	
	puts("[Openchange_plugin] Set profile by default");
	if ((retval = SetDefaultProfile(profname)) != MAPI_E_SUCCESS){
		mapi_errstr("[Openchange_plugin] openchange_profile.c : SetDefaultProfile() ", GetLastError());
		return 0;
	}

	MAPIUninitialize ();

	if (MAPIInitialize(profpath) != MAPI_E_SUCCESS){
		g_free(profpath);
		status = GetLastError();
		if (status == MAPI_E_SESSION_LIMIT){
			puts("[Openchange_plugin] openchange-lwmapi.c - Still connected");
			mapi_errstr("MAPIInitialize", GetLastError());

			
		}
		else 
			return NULL;
	}

	if (MapiLogonEx(&session, profname, NULL) == -1){
		retval = GetLastError();
		mapi_errstr("[Openchange_plugin] Error ", retval);
		if (retval == MAPI_E_NETWORK_ERROR){
			printf("Network error\n");
			return NULL;
		}
		if (retval == MAPI_E_LOGON_FAILED){
			printf("LOGIN Failed\n");
			return NULL;
		}
		printf("Generic error\n");
		return NULL;
	}
	
	fetch_data_from_server ();
}

gboolean
fetch_data_from_server ()
{
	TALLOC_CTX *mem_ctx;
	mapi_object_t obj_store;
	enum MAPISTATUS			retval;
	mapi_id_t			id_mailbox;
	struct SPropTagArray		*SPropTagArray;
	struct SPropValue		*lpProps = NULL;
	uint32_t			cValues;
	const char			*mailbox_name;
	char				*utf8_mailbox_name;
	GList *folders = NULL;
	ExchangeMAPIFolder *folder;

	printf("----------------s -----------------s -----------\n");
	mem_ctx = talloc_init("Evolution");
	mapi_object_init(&obj_store);

	/* Retrieve the mailbox folder name */
	SPropTagArray = set_SPropTagArray(mem_ctx, 0x1, PR_DISPLAY_NAME);
	retval = GetProps(&obj_store, SPropTagArray, &lpProps, &cValues);
	MAPIFreeBuffer(SPropTagArray);
	if (retval != MAPI_E_SUCCESS) 
		return FALSE;

	if (lpProps[0].value.lpszA) {
		mailbox_name = lpProps[0].value.lpszA;
	} else {
		return FALSE;
	}	

	/* Prepare the directory listing */
	retval = GetDefaultFolder(&obj_store, &id_mailbox, olFolderTopInformationStore);
	if (retval != MAPI_E_SUCCESS) {
		mapi_errstr("[Openchange_plugin] Error ", retval);
		return FALSE;
	}
	utf8_mailbox_name = utf8tolinux(mem_ctx, mailbox_name);

	/* FIXME: May have to get the child folders count? Do we need/use it? */
	folder = exchange_mapi_folder_new (utf8_mailbox_name, NULL, id_mailbox, IPF_NOTE, 0, 0); 

	folders = g_list_append (folders, folder);
	get_child_folders(mem_ctx, &obj_store, utf8_mailbox_name, id_mailbox, 0, folders);

	MAPIFreeBuffer(utf8_mailbox_name);
}