gopst

package module
v0.0.0-...-9b98d9a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2023 License: GPL-2.0 Imports: 5 Imported by: 0

README

gopst

Golang binding for a libpst2 of a pseudo-forked project of libpst.

Installation

Requirements

libpst2 installation.

Usage

Creating an Pst

Creates a new Pst initialization and passes the path to the .pst file with the parameter.

Has to be deallocated with Destroy method after use.

pst := gopst.NewPst("./fixtures/sample.pst")
if pst.NumError != gopst.NO_ERROR {
    return errors.New(pst.LastError)
}

Export Configuration

Next, it is necessary to create the export configurator, according to which the subsequent Export is created.

This configurator is necessary for how the individual records will be unpacked.

Export configuration structure:

type ExportConf struct {
	Mode                 int
	ModeMH               int // A submode of MODE_SEPARATE
	ModeEX               int // A submode of MODE_SEPARATE
	ModeMSG              int // A submode of MODE_SEPARATE
	ModeThunder          int // A submode of MODE_RECURSE
	OutputMode           int
	ContactMode          int // Not used within the code
	DeletedMode          int // Not used within the code
	OutputTypeMode       int // Default to all. Not used within the code
	ContactModeSpecified int // Not used within the code
	Overwrite            int
	PreferUtf8           int
	SaveRtfBody          int // Unused
	FileNameLen          int // Enough room for MODE_SPEARATE file name
	AcceptableExtensions string
}
Settings options
Mode
  • MODE_NORMAL - Normal mode just creates mbox format files in the current directory. Each file is named the same as the folder's name that it represents.
  • MODE_KMAIL - KMail mode creates a directory structure suitable for being used directly by the KMail application.
  • MODE_RECURSE - Recurse mode creates a directory structure like the PST file. Each directory contains only one file which stores the emails in mboxrd format.
  • MODE_SEPARATE - Separate mode creates the same directory structure as recurse. The emails are stored in separate files, numbering from 1 upward. Attachments belonging to the emails are saved as email_no-filename (e.g. 1-samplefile.doc or 1-Attachment2.zip).
OutputMode
  • OUTPUT_NORMAL - Output Normal just prints the standard information about what is going on.
  • OUTPUT_QUIET - Output Quiet is provided so that only errors are printed.
ContactMode
  • CMODE_VCARD
  • CMODE_LIST
DeletedMode
  • DMODE_EXCLUDE
  • DMODE_INCLUDE
OutputTypeMode
  • OTMODE_EMAIL
  • OTMODE_APPOINTMENT
  • OTMODE_JOURNAL
  • OTMODE_CONTACT
  • OTMODE_ALL
Export Configuration default settings
func ExportConfDefault() ExportConf {
	return ExportConf{
		Mode:                 MODE_NORMAL,
		ModeMH:               0,
		ModeEX:               0,
		ModeMSG:              0,
		ModeThunder:          0,
		OutputMode:           OUTPUT_NORMAL,
		ContactMode:          CMODE_VCARD,
		DeletedMode:          DMODE_INCLUDE,
		OutputTypeMode:       OTMODE_ALL,
		ContactModeSpecified: 0,
		Overwrite:            0,
		PreferUtf8:           1,
		SaveRtfBody:          0,
		FileNameLen:          10,
		AcceptableExtensions: "",
	}
}
Creating and Export

Then we create a new initialization of the Export using the configurator.

Has to be deallocated with Destroy method after use.

export := gopst.NewExport(gopst.ExportConfDefault())
if export == nil {
    return errors.New("NewExport failed")
}

List

The next step is the List method. This method lists content of an pst in form of arrays.

Records must be destroyed by DestroyList call explicitly.

Alternatively, it is possible to destroy individual records using the Destroy function.

records := pst.List()

Extraction to file and renaming

Here comes the time for extraction. To extract, you need to iterate over the individual records and call the RecordToFile function on them individually with the export parameter passed.

Alternatively, Records can be renamed before extraction using the SetRecordRenaming method. The full path with the new name must be passed as a parameter.

for i, record := range records {
    newName := fmt.Sprintf("output_%d.eml", i)
    record.SetRecordRenaming(pathToExtract + newName)
    record.RecordToFile(export)
}

Errors in records

Next, it is a good idea to iterate through the records to see if any of them have errors.

This can be done, for example, with the following code:

for i, record := range records {
	if record.Err != NO_ERROR {
		fmt.Printf("Record %s has error %d\n", record.Name, record.Err)
	}
}

Destroy

Destroy Pst

First, the Pst must be destroyed using the Destroy function.

if err := pst.Destroy(); err != nil {
    return err
}
Destroy List

It is also necessary to destroy the list of records using the DestroyList function.

if err := gopst.DestroyList(records); err != nil {
    return err
}

Alternatively, it is possible to destroy individual records using the Destroy function.

if err := record.Destroy(); err != nil{
	return err
}
Destroy Export

The last thing to do is to destroy the export also using the Destroy function.

if err := export.Destroy(); err != nil {
    return err
}

Errors

Different types of errors are defined by constants.

const (
    NO_ERROR = iota
    ERROR_NOT_UNIQUE_MSG_STORE
    ERROR_ROOT_NOT_FOUND
    ERROR_OPEN
    ERROR_INDEX_LOAD
    ERROR_UNKNOWN_RECORD
)

/*
Record to file errors.
*/
const (
	PST_MESSAGE_ERROR_FILE_ERROR = iota + 1
	PST_MESSAGE_ERROR_UNSUPPORTED_PARAM
)

Example

import (
	"errors"
	"fmt"

	"github.com/SpongeData-cz/gopst"
)

func example() error {
	path := "./fixtures/"

	// Creates a new Pst
	pst := gopst.NewPst(path + "sample.pst")
	if pst.NumError != gopst.NO_ERROR {
		return fmt.Errorf("NewPst failed with error %d", pst.NumError)
	}

	// Creates a new Export
	export := gopst.NewExport(gopst.ExportConfDefault())
	if export == nil {
		pst.Destroy()
		return errors.New("NewExport failed")
	}

	// Make slice of Records
	records := pst.List()

	for i, record := range records {
		// Optional Records rename
		record.SetRecordRenaming(path + fmt.Sprintf("out/output_%d.eml", i))

		// Record extraction
		record.RecordToFile(export)
	}

	// Inspection of per-Record errors
	for _, record := range records {
		if record.Err != gopst.NO_ERROR {
			fmt.Printf("WARNING %s, ERROR WITH NUMBER: %d\n", record.Name, record.Err)
		}
	}

	// Correct Pst removal
	if err := pst.Destroy(); err != nil {
		export.Destroy()
		gopst.DestroyList(records)
		return err
	}

	// Correct Records removal
	if err := gopst.DestroyList(records); err != nil {
		export.Destroy()
		return err
	}

	// Correct Export removal
	if err := export.Destroy(); err != nil {
		return err
	}

	return nil
}

Documentation

Index

Constants

View Source
const (
	NO_ERROR = iota
	ERROR_NOT_UNIQUE_MSG_STORE
	ERROR_ROOT_NOT_FOUND
	ERROR_OPEN
	ERROR_INDEX_LOAD
	ERROR_UNKNOWN_RECORD
)

Errors.

View Source
const (
	PST_MESSAGE_ERROR_FILE_ERROR = iota + 1
	PST_MESSAGE_ERROR_UNSUPPORTED_PARAM
)

Record to file errors.

View Source
const (
	/*
		Normal mode just creates mbox format files in the current directory. Each file is named
		the same as the folder's name that it represents.
	*/
	MODE_NORMAL = iota
	/*
		KMail mode creates a directory structure suitable for being used directly
		by the KMail application.
	*/
	MODE_KMAIL
	/*
		Recurse mode creates a directory structure like the PST file. Each directory
		contains only one file which stores the emails in mboxrd format.
	*/
	MODE_RECURSE
	/*
		Separate mode creates the same directory structure as recurse. The emails are stored in
		separate files, numbering from 1 upward. Attachments belonging to the emails are
		saved as email_no-filename (e.g. 1-samplefile.doc or 1-Attachment2.zip).
		Unsupported parameter in pst_message_to_file().
	*/
	MODE_SEPARATE
)

EXPORT

View Source
const (
	/*
		Output Normal just prints the standard information about what is going on.
	*/
	OUTPUT_NORMAL = iota
	/*
		Output Quiet is provided so that only errors are printed.
	*/
	OUTPUT_QUIET
)
View Source
const (
	CMODE_VCARD = iota
	CMODE_LIST
)

Output mode for contacts.

View Source
const (
	DMODE_EXCLUDE = iota
	DMODE_INCLUDE
)

Output mode for deleted items.

View Source
const (
	OTMODE_EMAIL = 1 << iota
	OTMODE_APPOINTMENT
	OTMODE_JOURNAL
	OTMODE_CONTACT
	OTMODE_ALL = 0xff
)

Output type mode flags.

View Source
const POINTER_SIZE = bits.UintSize / 8

Size of pointer in bytes.

Variables

This section is empty.

Functions

func DestroyList

func DestroyList(records []*Record) error

Destroys a slice of Records.

Parameters:

  • records - slice of Records to be destroyed.

Returns:

  • error if any of the Record has already been destroyed.

Types

type Export

type Export struct {
	// contains filtered or unexported fields
}

func NewExport

func NewExport(conf ExportConf) *Export

Creates a new instance of the Export structure. Has to be deallocated with Destroy method after use.

Parameters:

  • conf - Export Configuration.

Returns:

  • Newly created Export structure.

func (*Export) Destroy

func (ego *Export) Destroy() error

Destroys Export.

Returns:

  • Error, if Export has been already destroyed.

type ExportConf

type ExportConf struct {
	Mode                 int
	ModeMH               int // A submode of MODE_SEPARATE
	ModeEX               int // A submode of MODE_SEPARATE
	ModeMSG              int // A submode of MODE_SEPARATE
	ModeThunder          int // A submode of MODE_RECURSE
	OutputMode           int
	ContactMode          int // Not used within the code
	DeletedMode          int // Not used within the code
	OutputTypeMode       int // Default to all. Not used within the code
	ContactModeSpecified int // Not used within the code
	Overwrite            int
	PreferUtf8           int
	SaveRtfBody          int // Unused
	FileNameLen          int // Enough room for MODE_SPEARATE file name
	AcceptableExtensions string
}

func ExportConfDefault

func ExportConfDefault() ExportConf

Default parameters of ExportConf.

Returns:

  • Newly created ExportConf structure with default parameters.

type Pst

type Pst struct {
	Capacity uint
	Used     uint

	LastError string
	NumError  int
	// contains filtered or unexported fields
}

PST

func NewPst

func NewPst(path string) *Pst

Creates a new Pst. Has to be deallocated with Destroy method after use.

Parameters:

  • path - path to the existing Pst.

Returns:

  • Pointer to a new instance of Pst.

func (*Pst) Destroy

func (ego *Pst) Destroy() error

Destroys Pst.

Returns:

  • Error, if Pst has been already destroyed.

func (*Pst) List

func (ego *Pst) List() []*Record

Lists content of an Pst in form of arrays.

Records must be destroyed by DestroyList() call explicitly. Alternatively, it is possible to destroy individual Records using the Destroy() function.

Returns:

  • Slice of Records.

type Record

type Record struct {
	TypeOfRecord     uint8
	LogicalPath      string
	Name             string
	Renaming         string
	ExtraMimeHeaders string
	Err              int
	// contains filtered or unexported fields
}

RECORD

func (*Record) Destroy

func (ego *Record) Destroy() error

Destroys individual Record.

Returns:

  • Error, if Record has been already destroyed.

func (*Record) GetDir

func (ego *Record) GetDir() bool

Predicate - is directory?

func (*Record) RecordToFile

func (ego *Record) RecordToFile(export *Export) int

Writes individual records to a file.

Parameters:

  • export - Pst export.

Return:

  • 1, if successfully written, otherwise 0.

func (*Record) SetRecordRenaming

func (ego *Record) SetRecordRenaming(renaming string)

Sets renaming for the Record.

Parameters:

  • renaming - Path with a new name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL