Prev: Module agenda: Agenda Database
Module contacts: Contacts Database
This module allows to read and manipulate the contacts stored on the phone.
In the phone's database, a contact is identified by its id, an integer number.
In m, a contact is represented as an array whose elements are the fields of the contact. Fields are identified by their (array) keys. m recognizes the following keys, with the corresponding data type:
| Key||Meaning |
| adr||Address (street) |
|cell||Cellphone number |
|company||Company name |
|email||e-mail address |
|extadr||Additional address |
|extname||Additional name |
|fax||Fax number |
|fname||First name |
|loc||Locality (city) |
|name||(Family) name |
| Key||Meaning |
| note||Contact note |
|pager||Pager number |
|phone||Voice phone number |
|pict||Picture image data |
|po||Post Office |
|ring||Ringtone file name |
|text||Free text |
|title||Job Title |
|url||Website URL |
|video||Video phone number |
|zip||Post Code |
Key names are not case sensitive.
The order of fields in the array describing a contact is arbitrary. Arrays returned by functions in this module always start with the two fields name and fname, if these fields exist.
Address and phone number fields can have one of the following suffices:
| Suffix||Meaning |
| .home||Home address or phone |
|.work||Work address or phone |
For instance, phone.home refers to the home phone number, phone.work to the work phone number. phone without suffix is unspecified.
Most fields are represented as strings. There are two exceptions:
- birth: The birthday is stored as a number indicating the seconds since year zero. This is the format used by module time.
- pict: The picture is stored as an array containing the image data, typically in JPEG format. Example functions to load or store a the picture of a contact c:
function loadpict(file, c)
s=io.read(f, io.size(f)); // read whole file
c["pict"]=code(s) // string to byte array
function storepict(c, file)
if c["pict"]#null then
s=char(c["pict"]); // byte array to string
Note that the builtin contacts application in the phone may not support all keys, or display some of them in a strange way. Furthermore, not all applications clearly separate home from work data. Hence, the cell phone number of a person is sometimes stored as cell
, sometimes as cell.work
or as cell.home
The functions of this module throw ExcInvalidParam if a contact array has no keys, or ErrBadName if a contact array has a key which is not in the above table.
• function add(contact) → Number
Add a contact to the database, and return its id. The contact must be an array with keys from the above tables.
• function delete(id) → null
Delete the contact with the given id.
Throws ErrNotFound if there is no such contact.
// delete the contact added in the add example|
• function find(text=null, keys=["name","fname"], sort=) → Array
Searches the contact database for entries matching text considering the fields specified in keys, and returns the ids of the matching contacts sorted by the fields specified in sort:
- If text=null, all entries are returned, and keys is ignored.
- If text#null, searches the contact database for all entries matching the words in text when considering the fields defined by keys. Both text and all fields from the database are split into words (sequences of characters or digits) before comparing them. An entry matches if all of the words in text are found in any of the fields considered. Words can also be abbreviated: William matches both W or Will in the search text.
If keys defines a single field, it can be a string, otherwise it must be an array of strings.
- If sort=, the ids are sorted by their ascending numeric value.
- If sort is a string, the ids are sorted by the corresponding field.
- If sort is an array, the ids are sorted by the corresponding fields, from highest to lowest sort order.
if there are more than 32 keys or sort keys specified.
// get the number of contacts in the database|
// print these contacts, sorted by name and first name
for id in contacts.find(null,null,["name", "fname"]) do
print c, c
// Will matches William; so does W
print contacts.find("Will Shakespeare")
print contacts.find("W. Shakespeare")
// get the ids of everybody living or working in London
print contacts.find("London", "loc")
// Stratford-upon-Avon is considered three words,
// so Avon matches
print contacts.find("Avon", "loc")
• function findnr(number, digits=8) → Array
Retrieves the ids of the entries matching the given phone number. Only the last digits digits in number are considered when comparing. The minimum for digits is 7.
This function is much faster than find, and more useful, as it only looks at digits, and the end of the phone numbers.
Throws ExcValueOutOfRange if digits is out of range.
print contacts.findnr("+41(079)7654321", 9)|
• function get(id, keys=null) → Array
Get fields of the contact with id id. If keys=null, returns all fields defined for the contact. If keys#null, returns only the fields specified in keys. keys can be a single string specifying a single field, or an array specifying multiple fields.
If they exist, the fields name and/or fname are at the beginning of the returned array.
Throws ErrNotFound if there is no contact with this id; throws ErrArgument if there are more than 32 keys specified.
→ 1564-04-23 00:00:00
print contacts.get(114, ["name", "fname"])
• function labels(keys=null) → Array
Get labels for the fields. Labels are language dependent. keys is interpreted as follows:
- If keys=null, returns all standard labels.
- If keys is a string, returns the label(s) for the corresponding field(s).
- If keys is an array, returns the labels for the corresponding fields.
if there are more than 32 keys specified.
Suffices (.home, .work) can be used as keys, but not as field suffices: labels() throws ErrBadName in this case.
If they exist, the labels for name and/or fname are at the beginning of the returned array.
The label array has the same keys as a contact.
→ [Last name,First name,Tel. (home),Mobile
(home),Fax (home),E-mail (home),Web addr. (home),
→ Job title
// print a contact with all its labels
for k in keys(c) do
print l[k], "-", c[k]
→ Last name - Shakespeare
// get all work related labels
First name - William
City (home) - Stratford-upon-Avon
City (business) - London
Birthday - 49365849600
→ [Tel. (business),Mobile (business),Fax
(business),E-mail (business),Web addr. (bus.),Street
→ ErrBadName thrown
• function new(time) → Array
Returns the list of contacts modified since the specified point in time. time is the number of seconds since year 0 UTC. See also module time.
// get the entries changed within the last ten minutes|
• function own() → Number
• function own(id) → Number
There is a single contact in the database which can be marked as own contact, indicating the owner of the phone (or any other particular person). Some phones can use this information to quickly send a vCard of the phone owner.
Without an argument, the id of this contact is returned. With an argument, the own contact id is set to id, and the old one is returned.
Returns -1 if no own contact has been set, or it has been deleted.
Throws ErrNotFound if there is no contact with this id.
// if there is no owner, make it the first Shakespeare|
if contacts.own()=-1 then
if len(ids)>0 then
• function set(id, contact) → null
Updates the contact with id id, updating or adding fields in array contact. contact must be an array with keys from the above tables.
Fields already existing in the database are updated, the other fields are added. Fields not in the array are not modified. Fields which are null in the array are removed from the contact.
// Replace all +41 1 numbers by +41 44|
const fields=["phone", "fax", "cell", "pager"]);
for id in contacts.find() do
for i=0 to len(c)-1 do
// field could be null or too short
if c[i]!=null then
if len(n)>=11 then
// replace +411 by +4144
if substr(n,0,4)="+411" then
c[i]="+4144" + substr(n, 4); m=true
// replace +41 1 by +41 44
elsif substr(n,0,5)="+41 1" then
c[i]="+41 44" + substr(n, 5); m=true
if m then
© 2004-2011 airbit AG, CH-8008 Zürich