GroupWare API
-------------

Copyright (c) 1999-2008 IceWarp Ltd. All rights reserved.


(*

Overview
--------                  
                                  
IceWarp GroupWare is a server based application which has a unique API for the complete groupware usage.
It is a professionally designed RDBMS bringing all features a powerful groupware server should provide.

All clients and server modules utilize this API (including SyncML, MAPI Connector, WebDAV and others).
You have to be familiar with the database fields which can be explored in the calendar.xml file including
all field type values and comments however you do not need to use any SQL to utilize the system.
                             
There are several methods to connect to the groupware and use the API. You can use the COM interface, SyncML,
WebDAV, BEEP protocol (TCP/IP) or icewarpphp PHP extension.

For examples and usage see the PHP class gw.php.

For RPC the URL is http://server/rpc/gw.html and you must to call the final function reference names
(not the CalendarFunctionCall itself). See more information about RPC at http://www.xmlrpc.com/

The GroupWare API has been designed so all calls are are in only one function which wraps all functions described below.
It is more like an execute method function. The definition of the function:


result CalendarFunctionCall(string funcname, string param1, string param2, string param3, string param4)


Function Parameters
-------------------

funcname - function name from the API
param1..param4 - proper values for the API function <funcname>

There are several types of function parameters depending on the action.

Add:
Parameters contain an URL encoded field value string where each field contains a value in the following format.

Eg:
EvnNote=Note%20Here&EvnClass=T

Non-mandatory parameters specify an edit/update function of an existing record.
All add functions return the item ID of the inserted item or boolean result for updated items if successful.

There is a special way to specify the ItemID parameters. For instance an ID of newly created item.
ItemID format: <CurrentID>;ID=<NewID>&CLASS=<Class>
This syntax is supported in item add and delete functions.

Eg:
;ID=AA-2425-234234.ics


List:
All list functions return an array of URL encoded lines. The first line contains the database field names
and following lines contain the values for the fields (according to their order).

Eg:
EVN_ID&Evn_EditCounter& CRLF
1234567&0&
2345678&1&


Where:
Where allows you to specify additional condition for the SQL statement. Use only simple statements
and try to beware of database specifics.

Eg:
EvnClass='E'

Attributes:
Some list functions allows you to specify attributes. Attributes are used to set special behavior to
the list function. The following syntax and parameters is used:

order=<fields> ["DESC"]
position=<index>
limit=<count>

Each attribute is separated with semi-colon.
Eg:
order=EvnTitle, EvnLocation DESC; limit=1000

Function Reference
------------------

*)

// --------------
// Authentication
// --------------

Function LoginUser(EmailAddress, Password: String; IP: String = ''; Options: String = ''; _P: Pointer = Nil): String;
// Logs in the user with the email address password and IP connected to. If successul the function
// returns the SessionID which has to be used for other functions.

Function AuthenticateChallenge(ccP: Pointer = Nil): String;
// Returns the challenge string for authentication. The P is an internal parameter and will be ignored.

Function Authenticate(EmailAddress, Hash: String; IP: String = ''; Options: String = ''; _P: Pointer = Nil): String;
// Authenticates the user using the Options (Scheme "|" Challenge). Hash is the calcualted digest using the challenge string and the password.
// By default CRAM-MD5 authentication scheme is used. Supported schemes are (PLAIN, MD5, SHA1, CRAM-MD5, DIGEST-MD5, SYNCML-MD5)
// The result is the SessionID if successful.

Function UserLoggedOn(SessionID: String): Boolean;
// Returns true if sessionID exists on the server.

Function LogoutUser(SessionID: String): Boolean;
// Logs out the user and terminates all group sessions. The GroupWare uses persistent sessions
// so if you do not log out the session will remain on the server and time out after some time. During
// this time you can reuse the session anytime. To check if it is still active use the UserLoggedOn.


// -------
// Session
// -------

// Owner

Function GetOwnerInfo(SessionID: String): String;
// Returns the owner's details

Function UpdateOwnerInfo(SessionID: String; Parameters: String): Boolean;
// Set the owner's details.

// Groups

Function GetGroupList(SessionID: String; EmailAddress: String = ''): String;
// Returns the list of all valid groups for the session. If EmailAddress supplied
// a group of that account will be queried for the list (if trusted user).
//
// GRP_ID - ID of the group
// GRPOWN_ID - ID of the owner
// GrpLink - Link/reference to the real group from this virtual group
// GrpTitle - Description
// GrpShareMode - Share mode for this group

Function GetGlobalGroupList(SessionID: String): String;
// Returns the list of all groups available for the user to subscribe to.
// Subscribing is done by adding a new group with the GrpLink pointing to the original group.

Function OpenGroup(SessionID, GroupID: String): String;
Function GetGroupAccess(SessionID, GroupID: String): String;
// Opens the group from the group list. A special groupid "*" stands for all user's groups.
// In such case GetFolderList and others will work with all the groups available.
// If successful a groupsessionid will be returned.
//
// GetGroupAccess is an alias to OpenGroup

Function CloseGroup(GroupSessionID: String): Boolean;
// Closes an open group

Function GetGroupAccessRights(GroupSessionID: String): String;
// Returns the bit rights for the group session.
//
// Bit0 - Real Owner
// Bit1 - Write
// Bit2 - Owner
// Bit3 - Delete
// Bit4 - Modify
// Bit5 - Read

Function GetGroupAccessRightsList(SessionID: String): String;
// Returns the list of all open groups and their rights (as in GetGroupAccessRights)
//
// Fields: GRP_ID, GrpRights

Function AddGroup(SessionID: String; Parameters: String; GroupID: String = ''): String;
// Only owners can add new groups. Group is a collector for all objects and can can have
// different share modes and properties. By default every user has only one group.
// Shared accounts are used via multiple groups pointing with GrpLink to the original group.

Function DeleteGroup(SessionID: String; GroupID: String): Boolean;
// Delete existing group from the list.

Function GetGroupUsers(GroupSessionID: String): String;
// Returns a complete list of all friends for the group. Only owner of the group can
// call this function. Group users can be email addresses, user groups, and domain groups.

Function AddGroupUser(GroupSessionID: String; Parameters: String; ID: String = ''): String;
// Adds new friends who can manage the group's items.
// Friends can be accounts, domains or user groups.

Function DeleteGroupUsers(GroupSessionID: String; ID: String = ''): Boolean;
// Deletes users from the group.

Function SyncGroupUsersContacts(GroupSessionID: String; Folder: String = ''): Boolean;
// Synchronize all group members with the contacts folder
// Members will be expanded (every member to its final form (domain = members, group = group members, etc.)
// Complete contacts sync will be performed

Function ExportGroup(GroupSessionID: String; UserID: String = ''): String;
// Exports all group data to XML

Function ImportGroup(GroupSessionID: String; Data: String): Boolean;
// Imports all group data from XML

Function ExportData(SessionID: String; List: String): String;
// Export list of users to xml structure

Function ImportData(SessionID: String; Data: String): Boolean;
// Import xml structure created with ExportData function

Function DeleteGroupObjects(GroupSessionID: String): Boolean;
// Deletes all objects from the group.

Function GetGroupOwnerEmail(GroupSessionID: String): String;
// Returns the email address of the group owner

Function DeleteOwner(SessionID: String; EmailAddress: String): Boolean;
// Deletes account from groupware with all its groups and properties.
// Only superuser or administrator can do this and it is used by the account management system.

Function CreateUser(SessionID: String; EmailAddress: String): String;
// Creates a new user in the groupware. The user does not need to exist as email account on the server.
// The result value is the new ownerid. Only superuser can create new users.

Function RenameDomain(SessionID: String; OldDomain: String; NewDomain: String): Boolean;
// Renames a domain and updates all required information in the database so the data
// is still accessible for account from the new domain.

Function GetUserSettings(SessionID: String): String;
Function SetUserSettings(SessionID: String; Settings: String): Boolean;
// Get and set user settings and attributes. See the xml settings structure below.
// SessionID can be blank to query global server settings

(*

<settings>
  <forwarder>
    <forwardto/>
    <null/>
  <forwarder/>

  <responder>
    <mode/>
    <after/>
    <message/>
    <between/>
  </responder>

  <rules>
    <content/>
  </rules>

  <password>
    <newpassword/>
  </password>

  <bwlist>
    <white/>
    <black/>
  </bwlist>

  <update>
    <version/>
    <url/>
  </update>
</settings>

*)


// Holidays

Function GetHolidays(SessionID: String): String;
// Returns all system holidays containing the selected flag (holidays selected by the user)

Function GetIntervalHolidays(SessionID: String; Interval: String): String;
// GetAllIntervalEvents returns subscribed holidays with the EvnClass=H automatically
//
// GetIntervalHolidays is obsolete but still working.
// Returns holiday values and attributes for the given interval

Function AddHoliday(SessionID: String; Parameters: String): Boolean;
// Adds a new holiday

Function DeleteHolidays(SessionID: String; ID: String = ''): Boolean;
// Deletes all holidays selected flag or only the specified HolidayID

// ------
// Folder
// ------

Function GetFolderList(GroupSessionID: String): String;
// Returns the list of folders
//
// FDR_ID - ID of the folder
// FDR_PARENT - ID of the parent folder
// FdrType - Type of the folder
//
// If the groupsessionid is a link to "*" group then all folders in all groups will be returned.

Function AddFolder(GroupSessionID: String; Folder: String; Type_: String): Boolean;
Function DeleteFolder(GroupSessionID: String; Folder: String): Boolean;
Function RenameFolder(GroupSessionID: String; OldFolder, NewFolder: String): Boolean;
// Manages folders

Function OpenFolder(GroupSessionID: String; Folder: String; FailIfNotExists: Boolean = False): String;
Function CloseFolder(GroupSessionID: String): Boolean;
// Get access to folder items and close access. The function uses a special dynamic
// folder/group mode. You do not have to specify the folder in the Where clausule when using this method.
// Use OpenFolder and the resulting FolderSessionID to access groups.
//
// Eg.:
// $sessid = LoginUser()
// $groupsessid = OpenGroup($sessid, "*");
// $foldersessid = OpenFolder($groupsessid, "Contacts")
// $list = GetEventList($foldersessid)
// CloseFolder($foldersessid)


Function GetFolderRightList(GroupSessionID: String; Folder: String): String;
Function AddFolderRight(GroupSessionID: String; Folder: String; Email: String; Right: Longint): Boolean;
Function DeleteFolderRight(GroupSessionID: String; Folder: String; Email: String): Boolean;
// Folder right management. The same right values as for GetGroupAccessRights
// Folder rights are automatically hiearchic and are used when called OpenFolder function.


// --------
// Calendar
// --------

Function GetEventList(GroupSessionID: String; Where: String = ''; OtherSelect: String = ''; Attributes: String = ''): String;
Function GetEventCount(GroupSessionID: String; Where: String = ''; Attributes: String = ''): Cardinal;
Function GetAllIntervalEvents(GroupSessionID: String; Interval: String; Where: String = ''; OtherSelect: String = ''): String;
// Returns a list of all Events for the group or the number of records
//
// Where specifies other SQL conditions.
//
// OtherSelect specifies what fields you want to return besides EVN_ID.
// Eg: EvnClass,EnvNote
//
// GetAllIntervalEvents returns events occuring in the given interval. The format of the interval is:
// JulianDateStart "-" JulianDateEnd

Function GetEventDetailList(GroupSessionID: String; Where: String = ''; OtherSelect: String = ''; Attributes: String = ''): String;
Function GetEventDetailCount(GroupSessionID: String; Where: String = ''; Attributes: String = ''): Cardinal;
// Returns the same output as GetEventList plus some other detailed information about
// reminders and recurrences/

Function GetEventInfo(GroupSessionID: String; EventID: String): String;
Function AddEventInfo(GroupSessionID: String; Parameters: String; EventID: String = ''; RCRID: String = ''; _Notif: String = ''): String;
Function DeleteEvent(GroupSessionID: String; EventID: String; KeepTables: Boolean = False; _Notif: String = ''): Boolean;
// Manages calendar objects
//
// Special handling for item completion is used. For proper functionality and integrated functions
// you should call AddEventInfo once more after editing has been completed with blank Parameters and
// specified EventID. This notifies the server the item is finished and can process subsequent tasks
// with it.
//
// KeepTables - Does not delete the extension tables

Function GetvCalendar(GroupSessionID: String; EventID: String; Format: String = ''): String;
Function AddvCalendar(GroupSessionID: String; Item: String; EventID: String = ''; Params: String = ''): String;
// vCalendar of the calendar object (vEvent, vToDo, vJournal, vNote)
//
// Format specifies additional format parameters.
// Supported format parameters SIF, VERIFY, VERSION, FILTER can be delimited by ";".
//
// SIF     - Data format is Funambol SIF
// VERIFY  - Used to verify if the inserted item is already on ths server. In such case the item will
//           not be added and the id will be returned. A special compare and search method is used. Use only for slow synchronization.
// VERSION - Specifies the versit version (1.0, 2.0, 2.1, 3.0)
// FILTER  - List of versit attributes which are not supported, use "&" as attribute delimiter (VALARM)
// NOTIF   - Notification parameters sent to the notification server

Function ProcessvCalendar(GroupSessionID: String; EventID: String; Item: String; Action: String): String;
// Processes iTIP / iMIP requests and handles all possible actions
// Supports method creation: REQUEST, PUBLISH, CANCEL, REPLY
// Supports method replies: CANCEL, REPLY

Function GetEventReminders(GroupSessionID: String; EventID: String): String;
Function AddEventReminder(GroupSessionID: String; EventID: String; Parameters: String; ItemID: String = ''): String;
Function DeleteEventReminders(GroupSessionID: String; EventID: String; ID: String = ''): Boolean;
// Manages calendar reminders

Function GetEventRecurrence(GroupSessionID: String; EventID: String): String;
Function AddEventRecurrence(GroupSessionID: String; EventID: String; Parameters: String; ItemID: String = ''): String;
Function DeleteEventRecurrence(GroupSessionID: String; EventID: String): Boolean;
// Manages calendar recurrences

Function GetEventException(GroupSessionID: String; RecurrenceID: String): String;
Function AddEventException(GroupSessionID: String; RecurrenceID: String; Parameters: String; ItemID: String = ''): String;
Function DeleteEventException(GroupSessionID: String; ItemID: String): Boolean;
// Manages calendar recurrence exceptions

Function GetEventContacts(GroupSessionID: String; EventID: String): String;
Function AddEventContact(GroupSessionID: String; EventID: String; Parameters: String; ItemID: String = ''): String;
Function DeleteEventContacts(GroupSessionID: String; EventID: String; ID: String = ''): Boolean;
// Manages calendar attendees

Function GetEventNoteText(GroupSessionID: String; EventID: String): String;
Function SetEventNoteText(GroupSessionID: String; EventID: String; Text: String): Boolean;
// Manages note object's infinite text content

Function GetVFreeBusy(Email: String; Interval: String; Where: String = ''): String;
// Returns the VFreeBusy versit object for the given interval.
//
// GetVFreeBusy is a public and anonymous function and is used via HTTP:
// http://server/freebusy/?john@doe.com

Function GetPublicVCalendar(Email: String; Interval: String; Where: String = ''; Format: String = ''): String;
// Returns iCalendar for all events in the given Interval
// Format is the same type as in GetvCalendar function
//
// The result is "Internet Calendar" or .ics content used by many applications:
// http://server/calendar/?john@doe.com

Function ConvertVersit(SessionID, Data, Format: String): String;
// Converts a versit object to other format and vice-versa
// Supported formats: XML, ''



// --------
// Contacts
// --------

Function GetContactList(GroupSessionID: String; Where: String = ''; OtherSelect: String = ''; Attributes: String = ''): String;
Function GetContactCount(GroupSessionID: String; Where: String = ''; Attributes: String = ''): Cardinal;
// Returns a list of all contacts in the group or the number of records

Function GetContactDetailList(GroupSessionID: String; Where: String = ''; OtherSelect: String = ''; Attributes: String = ''): String;
Function GetContactDetailCount(GroupSessionID: String; Where: String = ''; Attributes: String = ''): Cardinal;
// Returns the same output as GetContactList plus some other detailed information about locations

Function GetContactInfo(GroupSessionID: String; ContactID: String): String;
Function AddContactInfo(GroupSessionID: String; Parameters: String; ContactID: String = ''; _Notif: String = ''): String;
Function DeleteContact(GroupSessionID: String; ContactID: String; KeepTables: Boolean = False; _Notif: String = ''): Boolean;
// Manages contact objects
//
// Special handling for item completion is used. For proper functionality and integrated functions
// you should call AddContactInfo once more after editing has been completed with blank Parameters and
// specified ContactID. This notifies the server the item is finished and can process subsequent tasks
// with it.
//
// KeepTables - Does not delete the extension tables


Function GetvCard(GroupSessionID: String; ContactID: String; Format: String = ''): String;
Function AddvCard(GroupSessionID: String; Item: String; ContactID: String = ''; Params: String = ''): String;
// vCard of the contact object

Function GetContactLocations(GroupSessionID: String; ContactID: String; Where: String = ''): String;
Function AddContactLocation(GroupSessionID: String; ContactID: String; Parameters: String; ItemID: String = ''): String;
Function DeleteContactLocations(GroupSessionID: String; ContactID: String; LocationID: String = ''): Boolean;
// Manages contact locations

Function GetContactLocationPhones(GroupSessionID: String; LocationID: String): String;
Function AddContactLocationPhone(GroupSessionID: String; LocationID: String; Parameters: String; ItemID: String = ''): String;
Function DeleteContactLocationPhones(GroupSessionID: String; LocationID: String; PhoneID: String = ''): Boolean;
// Manages contact location phones

Function GetContactCertificate(GroupSessionID: String; ContactID: String): String;
Function SetContactCertificate(GroupSessionID: String; ContactID: String; Cert: String): Boolean;
// Manages contact certificates


// ----------
// Attachment
// ----------

Function GetAttachmentList(GroupSessionID: String; ObjectID: String; AttName: String = ''): String;
// Returns list of all attachments for the object. Any object can have an infinite number of
// attachments including Notes, Contacts, Tasks, Journals etc.
//
// AttName - ID of the attachment
// AttDesc - Description or filename
// AttType - F,I,U (F - File, I - Link [index of another groupware object in the format of Class:GroupIndex:ObjectIndex], U - URL)
// AttSize - Size of the attachment
// AttTime - Unix time of the last update

Function GetAttachment(GroupSessionID: String; ObjectID: String; Name: String): String;
// Returns attachment for the object in binary

Function AddAttachment(GroupSessionID: String; ObjectID: String; Parameters: String; Value: String): Boolean;
// Adds a new attachment with data and value in binary format

Function DeleteAttachments(GroupSessionID: String; ObjectID: String; ID: String = ''): Boolean;
// Deletes attachments. If ID is blank all attachments will be deleted.


// -----
// Other
// -----

Function GetLastError(SessionID: String): String;
// Returns the error of the last failed function. Call this function only after
// the call to the previous function failed. Do not call this function to validate
// the success of previous calls.
//
// E_LICENSE = 1;     // Server does not have a license
// E_RIGHTS = 2;      // User does not have rights to perform this action
// E_ACCESS = 3;      // User does not have access permissions to performs this action
// E_DB = 4;          // No database record found
// E_PARAMETERS = 5;  // Incorrect parameters
// E_GROUP = 6;       // Group not found

Function SetSessionExpiration(SessionID: String; Minutes: Longint): Boolean;
// Set non default session expiration in minutes

Function MigrateData(SessionID: String; SourceDSN, DestinationDSN: String): Boolean;
// Migrates all tables to new database. Only superuser or administrator can do this.

Function GetServerVersion: String;
// Returns the server version

Function GetServerTimeZone: String;
// Returns the server timezone

Function IsAddressBookEmail(OwnerEmail: String; Email: String; Separate: Boolean): Boolean;
// Returns true if email is found in the server's address book

Function GetOwnerList(SessionID: String): String;
// Returns all groupware owner/users and their IDs

Function GetSessionList(SessionID: String): String;
// Returns all current sessions on the server

Function DialSIPNumber(SessionID: String; Number: String): Boolean;
// Initiates call dialer on the SIP server which dials the number and then
// transfers the call to your SIP client. Useful for groupware contact phone
// dialing directly from groupware application


