﻿use contacts,math,phone,time;

function GetName(number)
  names=contacts.findnr(number,12);
  if len(names) = 0 then
    return number;
  end;

  name=contacts.get(names[0]);
  return name['fname']+' '+name['name'];
end;

while true do
  try
    call=phone.new();
    if call['incoming'] then
      start=time.get();
      case phone.state(phone.idle | phone.active)
        in phone.active: phone.state(phone.idle);
        in phone.idle:
          length=time.get()-start;
          print time.str(start,'hh:mm:ss')+' '+GetName(call['number'])+': '+str(math.round(length,1))+' seconds';
      end;
    end;
  catch e by   // m-shell bug?
    print time.str(time.get(),'hh:mm:ss')+' Short call';
  end
end;
