summaryrefslogtreecommitdiff
path: root/rem.lua
blob: 1c0887aa6c712fca3d0945046cde71c705ba17cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
--[[--

    remind(1) compatible formatting.

--]]--

local inspect = require "ext.inspect"

local pcall               = pcall

local io                  = require "io"
local iostdout            = io.stdout

local math                = require "math"
local mathfloor           = math.floor

local os                  = os
local osdate              = os.date
local ostime              = os.time

local string              = require "string"
local stringformat        = string.format
local stringsub           = string.sub
local stringgsub          = string.gsub

local table               = require "table"
local tableconcat         = table.concat

local common              = require "common"
local println             = common.println
local errorln             = common.errorln
local noiseln             = common.noiseln
local debugln             = common.debugln
local trim_whitespace     = common.trim_whitespace
local unescape_string     = common.unescape_string

local seconds_of_time = function (t)
  return t % 60
end

local minutes_of_time = function (t)
  return mathfloor (t / 60) % 60
end

local hours_of_time = function (t)
  return mathfloor (t / (60 * 60)) % 60
end

local days_of_time = function (t)
  return mathfloor (t / (24 * 60 * 60))
end

local get_param = function (params, name)
  local np = #params

  for i = 1, np do
    local param = params [i]
    if param.name == name then return param end
  end

  return nil
end

local get_param_value = function (params, name)
  local param = get_param (params, name)

  if param ~= nil then return param.value end

  return nil
end

local get_param_value_1 = function (params, name)
  local val = get_param_value (params, name)

  if val ~= nil then
    return val [1]
  end

  return nil
end

local rem_of_vcalendars do

  local f_sec    = 1
  local f_min    = 60 * f_sec
  local f_hour   = 60 * f_min
  local f_day    = 24 * f_hour
  local l_month  = { [00] =  0
                   , [01] = 31
                   , [02] = 30
                   , [03] = 31
                   , [04] = 30
                   , [05] = 31
                   , [06] = 30
                   , [07] = 31
                   , [08] = 31
                   , [09] = 30
                   , [10] = 31
                   , [11] = 30
                   , [12] = 31
                   }

  local time_of_month = function (m, y)
    local r = 0

    for i = 0, m - 1 do
      local f = l_month [i] 

      if i == 2 and y % 4 == 0 and y % 100 ~= 0 then
        f = f + 1
      end

      r = r + f * f_day
    end

    return r
  end

  local time_of_year = function (y)
    if y % 4 == 0 and y % 100 ~= 0 then
    end
  end

  --[[--

     Date format “%Y%M%D”

  --]]--

  local parse_date_value_DATE = function (st, tzid)
    local Y = tonumber (stringsub (st, 1, 4))
    local M = tonumber (stringsub (st, 5, 6))
    local D = tonumber (stringsub (st, 7, 8))

    return ostime { year = Y, month = M, day = D }
  end

  --[[--

      Date format 1 date with local time: “%Y%M%DT%h%m%s”
      Date format 2 date with local time: “%Y%M%DT%h%m%sZ%z”
      Date format 3 date with local time: “%Y%M%DT%h%m%s” with TZID parm

  --]]--
  local parse_date_value_DATE_TIME = function (st, tzid)
    if #st < 15 then
      error (stringformat ("date-time specification [%s] is rubbish", st))
    end

    local Y = tonumber (stringsub (st, 1, 4))
    local M = tonumber (stringsub (st, 5, 6))
    local D = tonumber (stringsub (st, 7, 8))
    assert (stringsub (st, 9, 9) == "T")
    local h = tonumber (stringsub (st, 10, 11))
    local m = tonumber (stringsub (st, 12, 13))
    local s = tonumber (stringsub (st, 14, 15))

    local t = { year  = Y
              , month = M
              , day   = D
              , hour  = h
              , min   = m
              , sec   = s
              , isdst = false
              }

    if #st == 15 then
      --- format 3 (with time zone reference)
      if tzid then
        error (stringformat ("timezone specification (TZID) for dates \z
                              not implemented, sorry"))
      end

      --- format 1 (local)
      return ostime (t), true
    end

    --- format 2 (UTC)
    assert (stringsub (st, 16, 16) == "Z",
            stringformat ("rubbish date-time specification %s", st))

    return ostime (t), false
  end

  --[[--

    if params lack a VALUE entry, DATE-TIME is assumed
  --]]--

  local parse_date_value = function (val)
    local params = val.params

    local ptzid = get_param_value_1 (params, "TZID") 
    local pdate = get_param_value_1 (params, "VALUE") 

    if pdate == nil or pdate == "DATE-TIME" then
      return parse_date_value_DATE_TIME (val.value, ptzid)
    end

    if pdate == "DATE" then
      return parse_date_value_DATE (val.value, ptzid), false
    end

    return nil
  end

  local rem_date = function (d, loctime)
    if loctime == true then
      return osdate ("%b %d %Y AT %H:%M", d)
    end

    return osdate ("!%b %d %Y AT %H:%M", d)
  end

  local rem_duration = function (dt)
    --[[-- remind(1): Note that duration is specified in hours and minutes. --]]--

    local s
    local ret

    if dt == 0 then return nil end

    ret = ""

    s = dt % 60
    ret = ret .. stringformat ("%0.2d", s)

    dt = mathfloor (dt / 60)
    if dt == 0 then return ret end

    return stringformat ("%0.2d:%s", dt, ret)
  end

  local rem_date_duration = function (t0, te, loctime)
    local dt = te - t0
    local s_t0 = rem_date (t0, loctime)

    if dt == 0 then return s_t0 end

    local s_dt = rem_duration (dt)

    return stringformat ("%s DURATION %s", s_t0, s_dt)
  end

  local date_of_interval = function (dtstart, dtend)
    local t0, loctime = parse_date_value (dtstart)
    local te, _ignore = parse_date_value (dtend)

    if t0 ~= nil then
      if te ~= nil then return rem_date_duration (t0, te, loctime) end
      return rem_date (t0, loctime)
    end

    return nil
  end

  local string_of_textdata = function (val)
    local sane = unescape_string (val.value)

    return trim_whitespace (sane)
  end

  local msg_of_string = function (s)
    return stringgsub (s, "\n", "^M")
  end

  local rem_of_vevent = function (ev)
    local dtstart, dtend
    local date, msg, msg_desc
    local vals  = ev.value
    local nvals = #vals

    for i = 1, nvals do
      local val = vals [i]

      if val.kind == "other" then
        local valname = val.name

        if     valname == "summary"     then msg      = string_of_textdata (val)
        elseif valname == "description" then msg_desc = string_of_textdata (val)
        elseif valname == "dtstart"     then dtstart = val
        elseif valname == "dtend"       then dtend = val
        else
          debugln ("rem_of_vevent: ignoring value [%s]", valname)
        end
      else
      end
    end

    date = date_of_interval (dtstart, dtend)

    if date == nil then
      error ("could not derive date from event values")
    end

    if msg == nil then
      error ("could not derive message text from event values")
    end

    if msg_desc ~= nil then
      msg = stringformat ("%s (%s)", msg, msg_desc)
    end

    return stringformat ("REM %s MSG %s", date, msg_of_string (msg))
  end

  local rem_of_vcalendar = function (vcal)
    local acc   = { }
    local vals  = vcal.value
    local nvals = #vals

    for i = 1, nvals do
      local val = vals [i]

      if val.kind ~= "scope" or val.name ~= "VEVENT" then
        noiseln ("rem_of_vcalendar: ignoring value %d of type [%s/%s]",
                 i, val.kind, val.name)
      else
        local ok, res = pcall (rem_of_vevent, val)
        if ok then
          acc [#acc + 1] = res
        else
          errorln ("rem_of_vcalendar: vevent %d contains invalid entry: %s",
                   i, tostring (res))
        end
      end
    end

    return tableconcat (acc, "\n")
  end

  rem_of_vcalendars = function (vcals)
    local value = vcals.value
    local nvcal = #value
    local acc = { }

    for i = 1, nvcal do
      local vcal = value [i]
      if vcal.kind ~= "scope" or vcal.name ~= "VCALENDAR" then
        noiseln ("rem_of_vcalendars: ignoring object %d of type [%s/%s]",
                 i, vcal.kind, vcal.name)
      else
        acc [#acc + 1] = rem_of_vcalendar (vcal)
      end
    end

    return tableconcat (acc, "\n")
  end
end --- [rem_of_vcalendars]

-------------------------------------------------------------------------------
--- API
-------------------------------------------------------------------------------

local output_calendar = function (cal, fh)
  fh = fh or iostdout
  local ok, str = pcall (rem_of_vcalendars, cal)
  if ok then
    fh:write (str)
  end
end

return { output_calendar = output_calendar
       }