This file is indexed.

/usr/lib/ruby/1.8/mhc-palm.rb is in mhc-utils 0.25.1+20090531-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
### mhc-palm.rb
##
## Author:  Yoshinari Nomura <nom@quickhack.net>
##
## Created: 1999/09/01
## Revised: 2009-01-07 00:15:05
##

require 'mhc-date'
begin
  require 'mhc_pilib'
rescue LoadError
#  STDERR .print "Warning: require 'mhc_pilib' was failed."
end

################################################################
##
## fundamental class 1/4
##
## Pilot    -- connection management.
##
class Pilot
  attr :sd

  def initialize(port = '/dev/pilot')
    @sd = PiLib .openSock(port)
  end

  def listen
    @sd = PiLib .listenSock(@sd)
    return nil if @sd .nil?
    return self
  end

  def close 
    PiLib .closeSock(@sd)
    return self
  end

  ## Add an entry into the HotSync log on the Pilot. 
  ## \n is OK, as usual. You may invoke this command once or more before
  ## calling EndOfSync (sockClose), but it is not required.
  def add_synclog(string)
    PiLib .dlp_AddSyncLogEntry(@sd, string)
    return self
  end

  ## reset lastSyncPC in the UserInfo to 0 
  def reset_lastsync_pc
    PiLib .dlp_ResetLastSyncPC(@sd)
    return self
  end

  def get_time
    return PiLib .dlp_GetSysDateTime(@sd)
  end

  def set_time(time)
    PiLib .dlp_SetSysDateTime(@sd, time)
    return self
  end
end

################################################################
##
## fundamental classe 2/4
##
## PilotDB
##
class PilotDB
  def initialize(pi, dbname)
    @sd = pi .sd
    @db = PiLib .dlp_OpenDB(@sd, dbname)
    @recClass = PilotRecord
  end

  def close
    PiLib .dlp_CloseDB(@sd, @db)
  end

  def record_by_index(i)
    ary = PiLib .dlp_ReadRecordByIndex(@sd, @db, i)
    return nil if ary .nil?
    return @recClass .new(*ary)
  end

  def record_by_id(id)
    ary = PiLib .dlp_ReadRecordById(@sd, @db, id)
    return nil if ary .nil?
    return @recClass .new(*ary)
  end

  def each_record
    i = 0
    while (rec = record_by_index(i)) != nil
      yield rec
      i += 1
    end
  end

  def write_record(rec)
    
    rec_array = rec .to_a
    new_id = PiLib .dlp_WriteRecord(@sd, @db, rec_array)
    return new_id
  end

  def delete_by_id(id)
    PiLib .dlp_DeleteRecord(@sd, @db, false, id)
    return self
  end

  def delete_all
    PiLib .dlp_DeleteRecord(@sd, @db, true, 0)
    return self
  end

  ## Deletes all records in the opened database which are marked as archived
  ## or deleted.
  def cleanup_record
    PiLib .dlp_CleanUpDatabase(@sd, @db)
    return self
  end

  ## For record databases, reset all dirty flags. For both record and
  ## resource databases, set the last sync time to now.
  def reset_sync_flags
    PiLib .dlp_ResetSyncFlags(@sd, @db)
    return self
  end

  def get_app_info()
    return PiLib .dlp_ReadAppBlock(@sd, @db)
  end
end

################################################################
##
## fundamental class 3/4
##
## PilotRecord -- PilotDB record
##
class PilotRecord
  def initialize(id = 0, attr = 0, category = 0, data = '')
    @id, @attr, @category, @data = id, attr, category, data
    unpack
    check
  end

  attr :id
  def set_id(id)
    raise "Integer required." if !(id .is_a?(Integer))
    @id = id
    return self
  end

  def attribute_deleted?       ; return  (@attr & 0x80 != 0) ;end
  def attribute_dirty?         ; return  (@attr & 0x40 != 0) ;end
  def attribute_busy?          ; return  (@attr & 0x20 != 0) ;end
  def attribute_secret?        ; return  (@attr & 0x10 != 0) ;end
  def attribute_archived?      ; return  (@attr & 0x08 != 0) ;end

  def set_attribute_deleted    ; @attr |= 0x80; return self  ;end
  def set_attribute_dirty      ; @attr |= 0x40; return self  ;end
  def set_attribute_busy       ; @attr |= 0x20; return self  ;end
  def set_attribute_secret     ; @attr |= 0x10; return self  ;end
  def set_attribute_archived   ; @attr |= 0x08; return self  ;end

  def reset_attribute_deleted  ; @attr &= ~0x80; return self ;end
  def reset_attribute_dirty    ; @attr &= ~0x40; return self ;end
  def reset_attribute_busy     ; @attr &= ~0x20; return self ;end
  def reset_attribute_secret   ; @attr &= ~0x10; return self ;end
  def reset_attribute_archived ; @attr &= ~0x08; return self ;end

  def set_attribute(attr)
    raise "Integer required." if !(attr .is_a?(Integer))
    @attr = attr
    return self
  end

  def attribute
    return @attr
  end

  def attribute_string
    attr_str = []
    attr_str << 'Deleted'  if (@attr & 0x80 != 0)
    attr_str << 'Dirty'    if (@attr & 0x40 != 0)
    attr_str << 'Busy'     if (@attr & 0x20 != 0)
    attr_str << 'Secret'   if (@attr & 0x10 != 0)
    attr_str << 'Archived' if (@attr & 0x08 != 0)
    return attr_str .join(' ')
  end

  attr :category
  def set_category(category)
    raise "Integer required." if !(category .is_a?(Integer))
    @category = category
    return self
  end

  def data
    pack
    return @data
  end

  def set_data(data)
    raise "String required." if !(data .is_a?(String))
    @data = data
    return self
  end

  def to_a
    check
    pack
    return [@id, @attr, @category, @data]
  end

  private
  def pack;    end
  def unpack;  end

  def check
    raise "Id must be Integer."         if !(@id .is_a?(Integer))
    raise "Attribute must be Integer."  if !(@attr .is_a?(Integer))
    raise "Category must be Integer."   if !(@category .is_a?(Integer))
    raise "Data must be String."        if !(@data .is_a?(String))
    return self
  end
end


################################################################
##
## fundamental class 4/4
##
## PilotFile  -- File DB
##
class PilotFile
  ## these methods are defined in pilib.c
  ## get_app_info
  ## read_record
  ## close

  def initialize(filename, recClass = PilotRecord)
    @recClass = recClass
  end

  def each_record
    i = 0
    while (rec = record_by_index(i)) != nil
      yield rec
      i += 1
    end
  end

  def record_by_index(i)
    ary = read_record(i)
    return nil if ary .nil?
    return @recClass .new(*ary)
  end
end
    
################################################################
##
## classes for Memo
##
class PilotMemoDB < PilotDB
  def initialize(pi, dbname)
    super
    @recClass = PilotMemoRecord
  end
end

class PilotMemoRecord < PilotRecord
  undef set_data
  
  def set_memo_data(string)
    @memo_data = string
    return self
  end

  def memo_data
    return @memo_data
  end

  def unpack
    @memo_data = @data .sub(/\0$/, '')
    return self
  end

  def pack
    @data = Kconv::tosjis(@memo_data || '') + "\0"
    return self
  end
end

################################################################
##
## classes for Datebook
##

class PilotApptDB < PilotDB
  def initialize(pi, dbname)
    super
    @recClass = PilotApptRecord
  end
end


class PilotApptRecord < PilotRecord
  UNIT_TYPE   = %w(minute hour day)
  ORDER_TYPE  = %w(1st 2nd 3rd 4th Last)
  WEEK_TYPE   = %w(Sun Mon Tue Wed Thu Fri Sat)
  MONTH_TYPE  = %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
  REPEAT_TYPE = %w(None Daily Weekly MonthlyByDay MonthlyByDate Yearly)

  undef set_data

  def repeat?    ; return @repeatType != 0                ;end
  def event?     ; return @event                          ;end
  def alarm?     ; return @alarm                          ;end
  def category?  ; return @category == 0 ? false : true   ;end
  def forever?   ; return @repeatForever                  ;end

  def repeatType ; return REPEAT_TYPE[@repeatType]        ;end
  def repeatDay
    return "#{ORDER_TYPE[@repeatDay / 7]} #{WEEK_TYPE[@repeatDay % 7]}"
  end

  def alarm
    if alarm?
      return @advance .to_s  + ' ' + UNIT_TYPE[@advanceUnits]
    else
      return ''
    end
  end

  def repeatDays
    ary  = []
    for i in (0 .. 6)
      ary << WEEK_TYPE[i] if @repeatDays[i]
    end
    return ary .join(' ')
  end

  ## b 	@event                 	»þ´Ö»ØÄ꤬¤Ê¤¤¥¤¥Ù¥ó¥È¤«¤É¤¦¤«
  ## t 	@beg	           	³«»ÏÆüÉÕ¡¢»þ´Ö¡£
  ## 		           	(repeat ¤Î¾ì¹ç¤Ï¡¢duration ¤Î³«»Ï¤Ç¤â¤¢¤ë)
  ## t 	@fin                   	½ª¤ï¤ê¤Î»þ´Ö (date Éôʬ¤Ï beg ¤ÈƱ¤¸¤Ë¤¹¤ë)
  ## 			   	event == 1 ¤Î¤È¤­¤Ï¡¢time Éôʬ¤ÏÁ´Éô 0
  ## b 	@alarm                	1 or 0
  ## i 	@advance		0-99
  ## i 	@advanceUnits		units = ['minute', 'hour', 'day'];

  ## i     @repeatType           	None,Daily Weekly MonthlyByDay,
  ##				MonthlyByDate,Yearly
  ## 				byday -> cond (@repeatDay ¤ò¿®ÍÑ)
  ## 				bydate -> num @beg .day ¤ò¿®ÍÑ¡£
  ## b     @repeatForever		repeatEnd ¤ò¿®ÍѤ·¤Æ¤¤¤¤¤«¤É¤¦¤«¡£
  ## 				repeatEnd ¤Ï¿®ÍѤ·¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
  ## t     @repeatEnd		Duration end (date Éôʬ¤À¤±)
  ## i     @repeatFrequency		int
  ## i     @repeatDay		o = repeatDay /7, w = repeatDay % 7
  ## 				5th ¤¬¤Ê¤¤¤Î¤Ï¤Ê¤¼?
  ## b[7]  @repeatDays		Sun, Mon, Tue, 1 or 0
  ## i     @repeatWeekstart		¤¤¤Ä¤â 0

  ## i     @exceptions, 	0?
  ## t[x]  @exception		[]
  ## s     @description		NULL  or Subject:
  ## s     @note		NULL  or ËÜʸ


  ################################################################
  ## ¥¹¥±¥¸¥å¡¼¥ë¥¿¥¤¥×¤Ë¤è¤é¤Ê¤¤
  ##

  ## set alarm in second.
  def set_alarm(alarm)
    raise "Type error: requires Integer\n" if alarm && !alarm .is_a?(Integer)

    if alarm
      @alarm = true

      if alarm % 86400 == 0 && alarm <= 86400 * 99
	@alarmUnit = 2 ## day
	@advance   = alarm / 86400
      elsif alarm % 3600 == 0 && alarm <= 3600 * 99
	@alarmUnit = 1 ## hour
	@advance   = alarm / 3600
      elsif alarm % 60 == 0 && alarm <= 60 * 99
	@alarmUnit = 0 ## minute
	@advance   = alarm / 60
      else
	raise "Could not convert alarm."
      end
    else
      @alarm        = false
      @advance      = 0
      @advanceUnits = 0
    end
  end

  def set_time(b = nil, e = nil)
    raise "Type error: requires MhcTime\n" if b && !b .is_a?(MhcTime)
    raise "Type error: requires MhcTime\n" if e && !e .is_a?(MhcTime)
    if b
      @event = false
      e = b if !e ## ½ªÎ»»þ´Ö¤ò»ØÄꤷ¤Æ¤¤¤Ê¤«¤Ã¤¿¤é¡¢³«»Ï¤ÈƱ¤¸¤Ë
      @beg = replace_time(@beg, b .hh, b .mm)
      @fin = replace_time(@fin, e .hh, e .mm)
    else
      @event = true
      @beg = replace_time(@beg, 0, 0)
      @fin = replace_time(@fin, 0, 0)
    end
  end

  def add_exception(date)
    ## repeatType = None ¤Î¤È¤­¤Ï¡¢exception ¤òÀßÄꤷ¤Æ¤â°ÕÌ£¤¬¤Ê¤¤?
    raise "Type error: requires MhcDate\n" if !date .is_a?(MhcDate)
    @exception << date .to_t
    @exceptions = @exception .length
    return self
  end

  def set_note(txt)
    raise "Type error: requires String\n" if !txt .is_a?(String)
    @note = Kconv::tosjis(txt)
    return self
  end

  def set_description(txt)  ## subject
    raise "Type error: requires String\n" if !txt .is_a?(String)
    @description = Kconv::tosjis(txt)
    return self
  end

  ################################################################
  ## ¥¹¥±¥¸¥å¡¼¥ë¥¿¥¤¥×ÊÌ
  ##

  ## ÉáÄ̤Π¥ê¥Ô¡¼¥È¤·¤Ê¤¤¤ä¤Ä
  ##
  def set_nonrepeat_date(date)
    raise "Type error: requires MhcDate\n" if !date .is_a?(MhcDate)
    @repeatType = 0
    @beg = replace_date(@beg, date .y, date .m, date .d)
    @fin = replace_date(@fin, date .y, date .m, date .d)
    return self
  end

  ## repeatType -> Daily
  ##
  ## x-sc-day ¤ò sort ¤·¤Æ¡¢ 1Æü´Ö³Ö¤Ëʤó¤Ç¤¤¤ë &&
  ## x-sc-cond ¤Ï¶õ
  ##
  ## n ÆüËè
  ## duration_b - duration_e ¤Þ¤Ç
  def set_daily(beg, fin, freq)
    set_duration(beg, fin) ## beg, fin ¤Î·¿¥Á¥§¥Ã¥¯¤â¤¹¤ë
    set_frequency(freq)    ## freq     ¤Î·¿¥Á¥§¥Ã¥¯¤â¤¹¤ë
    ## xxx: Àè¤ËÁ´Éô¥Á¥§¥Ã¥¯¤·¤Æ¤«¤é¤Ç¤Ê¤¤¤È rollback ¤Ç¤­¤Ê¤¤¡£¡£
    @repeatType = 1
    return self
  end

  ## repeatType -> Weekly
  ##
  ## x-sc-day ¤Ï¶õ && 
  ## x-sc-cond ¤Ï wek ¤À¤±(Ê£¿ô²Ä)
  ##
  ## Sun Mon Tue
  ## duration_b - duration_e ¤Þ¤Ç
  ##
  ## weeks = [false, true, true, false, false, false, false]
  ##     -> Mon, Tue
  ##
  def set_weekly(beg, fin, freq, weeks)
    set_duration(beg, fin) ## beg, fin ¤Î·¿¥Á¥§¥Ã¥¯¤â¤¹¤ë
    set_frequency(freq)    ## freq     ¤Î·¿¥Á¥§¥Ã¥¯¤â¤¹¤ë
    w = []

    if weeks .is_a?(Array) && weeks .length == 7
      weeks .each{|bool|
	if !(bool == true || bool == false)
	  raise "Type error: weeks must be bool[7]" 
	end
	w << bool
      }
    else
      raise "Type error: weeks must be bool[7]" 
    end

    @repeatType      = 2
    @repeatDays      = w
    @repeatWeekstart = 0
    return self
  end
    
  ## repatType -> Monthly
  ##
  ## x-sc-day ¤Ï¶õ &&
  ## ((x-sc-cond ¤Ï ord ¤¬ 1¸Ä && ord != 5th && wek ¤¬°ì¸Ä¤À¤±) ||
  ##  x-sc-cond ¤Ï num ¤¬ 1¸Ä¤À¤±)
  ##
  ## 1st Sun  -- by day
  ## 01       -- by date
  ## duration_b - duration_e ¤Þ¤Ç
  ##
  ## ord  .. 0 - 4 ¤ÎÀ°¿ô 1st, 2nd, 3rd, 4th, Last ¤ËÂбþ
  ## wek  .. 0 - 6 ¤ÎÀ°¿ô Sun, ... ,Sat            ¤ËÂбþ
  ##
  def set_monthly_by_day(beg, fin, freq, ord, wek)
    set_duration(beg, fin) ## beg, fin ¤Î·¿¥Á¥§¥Ã¥¯¤â¤¹¤ë
    set_frequency(freq)    ## freq     ¤Î·¿¥Á¥§¥Ã¥¯¤â¤¹¤ë

    ## beg ¤ÎÆüÉÕ¤¬ ord, week ¤òËþ¤¿¤·¤Æ¤¤¤ë¤«¤Î¥Á¥§¥Ã¥¯¤¬É¬Í×
    msg = "Type/Range error: (0< ord <4, 0< wek <6) required. (#{ord}, #{wek})"
    raise msg if !(ord .is_a?(Integer) && 0 <= ord && ord <= 4)
    raise msg if !(wek .is_a?(Integer) && 0 <= wek && wek <= 6)
    raise msg if !((ord == 4 && beg .o_last?) || (ord < 4  && beg .o == ord))

    @repeatDay  = ord * 7 + wek
    @repeatType = 3
    return self
  end

  def set_monthly_by_date(beg, fin, freq)
    set_duration(beg, fin) ## beg, fin ¤Î·¿¥Á¥§¥Ã¥¯¤â¤¹¤ë
    set_frequency(freq)    ## freq     ¤Î·¿¥Á¥§¥Ã¥¯¤â¤¹¤ë

    ## beg ¤Î ÆüÉÕ¤ÎÉôʬ¤Î ¡ÖÆü¡× ¤¬¤½¤Î¤Þ¤Þ»È¤ï¤ì¤ë
    @repeatType = 4
    return self
  end

  ## repeatType -> yearly
  ##
  ## x-sc-day ¤Ï¶õ &&
  ## x-sc-cond ¤Ï mon ¤¬ 1¸Ä¤È num ¤¬1¸Ä¤À¤±
  ##
  ## Jan 01
  ## duration_b - duration_e ¤Þ¤Ç
  def set_yearly(beg, fin, freq)
    set_duration(beg, fin) ## beg, fin ¤Î·¿¥Á¥§¥Ã¥¯¤â¤¹¤ë
    set_frequency(freq)    ## freq     ¤Î·¿¥Á¥§¥Ã¥¯¤â¤¹¤ë

    ## beg ¤Î ÆüÉÕ¤ÎÉôʬ¤Î ¡Ö·î¡¦Æü¡× ¤¬¤½¤Î¤Þ¤Þ»È¤ï¤ì¤ë
    @repeatType = 5
    return self
  end

  ################################################################
  ## mhc ¤¬»È¤¦ MhcScheduleItem ¤Ø¤ÎÊÑ´¹

  $last_mid_rand = 'AAAA'
  $last_mid_time = nil
  $last_mid_counter = 0

  def create_message_id(domain = 'from.your.palm')
    mid_time = Time .now .strftime("%Y%m%d%H%M%S")
    mid_user = Process .uid .to_s

    if $last_mid_time && mid_time == $last_mid_time
      $last_mid_counter += 1
      $last_mid_rand .succ!
      mid_rand = $last_mid_rand
    else
      $last_mid_rand = 'AAAA'
      mid_rand = $last_mid_rand
      $last_mid_counter = 0
    end

    mid_rand += '-' + $$ .to_s
    $last_mid_time = mid_time
    return '<' + mid_time + mid_rand + '.' + mid_user + '@' + domain + '>'
  end

  def to_xsc
    xsc = {};
    xsc["Record-Id"]  = create_message_id(@id .to_s) # xxx
    xsc["Pilot-Attr"] = attribute_string
    xsc["Pilot-Id"]   = @id
    xsc["Subject"]    = Kconv::tojis(Kconv::toeuc(@description) .sub(/\[[^\]]*\]\s*$/, ''))
    xsc["Location"]   = Kconv::tojis($1) if Kconv::toeuc(@description) =~ /\[([^\]]+)\]\s*$/
    xsc["Note"]       = Kconv::tojis(@note)
    xsc["Category"]   = @category  if category?
    xsc["Alarm"]      = alarm
    xsc["Day"]        = @exception .collect{|t| '!' + t .to_xscday} .join(' ')
    xsc["Day"]       += ' ' + @beg .to_xscday if !repeat?
    if !event?
      if @beg .to_xsctime == @fin .to_xsctime
        xsc["Time"]  = ' ' + @beg .to_xsctime
      else
        xsc["Time"]  = ' ' + @beg .to_xsctime + '-' + @fin .to_xsctime
      end
    end

    if repeat?
      if @repeatFrequency > 1
	STDERR .print "#{@beg} : #{Kconv::tojis(@description)} "
	STDERR .print "unsupported. ignored..\n"
	return nil 
      end

      if !forever?
	b, e = @beg .to_xscday, @repeatEnd .to_xscday
	xsc["Duration"] = b + '-' + e
	b_date, e_date = MhcDate .new(b), MhcDate .new(e)
      end

      case repeatType
      when 'Daily'
	if !forever? && (e_date - b_date < 7)
	  for d in b_date .. e_date
	    xsc["Day"] += ' ' + d .to_s
	  end
	else
	  xsc["Cond"] = 'Sun Mon Tue Wed Thu Fri Sat' # xxx
	end
      when 'Weekly'
 	xsc["Cond"] = repeatDays
      when 'MonthlyByDay'
 	xsc["Cond"] = repeatDay
      when 'MonthlyByDate'
 	xsc["Cond"] = @beg .day .to_s
      when 'Yearly'
 	xsc["Cond"] = MONTH_TYPE[@beg .mon - 1] + ' ' + @beg .day .to_s
      end
    end

    str = ''
    note = ''
    xsc .each{|key, val|
      if key == 'Note'
	note = val
      else
	str += "X-SC-#{key}: #{val}\n"
      end
    }
    x = MhcScheduleItem .new(str, false)

    note_hdr, note_desc, datebk3_icon = conv_note(note)
    x .set_non_xsc_header(note_hdr)
    x .set_description(note_desc)
    x .add_category(datebk3_icon) if datebk3_icon

    x .set_pilot_id([@id])
    return x
  end

  ################################################################
  ## private

  ## Palm ¤Î¥Î¡¼¥È -> mhc ¤Î body ¤È X-SC-* °Ê³°¤Î¥Ø¥Ã¥ÀÉôʬ¤ËÊÑ´¹
  def conv_note(string)
    if string =~ /\A(\#\#@@@.@@@)\n([^\z]*)\z/n
      datebk3_icon, string = $1, $2
    end

    part1_is_header = true

    part1, part2 = string .split("\n\n", 2)

    if !(part1 =~ /^[ \t]+/ or part1 =~ /^[A-Za-z0-9_-]+:/)
      part1_is_header = false
    end

    part1 .to_s .split("\n") .each{|line|
      if !(line =~ /^[ \t]+/ or line =~ /^[A-Za-z0-9_-]+:/)
	part1_is_header = false
      end
    }

    if part1_is_header
      header, body = part1, part2
    else
      header, body = nil, string
    end

    return header, body, datebk3_icon
  end
    
  ## Time ¥¯¥é¥¹¥¤¥ó¥¹¥¿¥ó¥¹¤Î »þ´ÖÉôʬ¤À¤±¤òÃÖ¤­´¹¤¨¤ë
  def replace_time(time, hour, min)
    return Time .local(*time .to_a .indexes(5, 4, 3) + [hour, min])
  end

  ## Time ¥¯¥é¥¹¥¤¥ó¥¹¥¿¥ó¥¹¤Î ÆüÉÕÉôʬ¤À¤±¤òÃÖ¤­´¹¤¨¤ë
  def replace_date(time, y, m, d)
    return Time .local(y, m, d, *time .to_a .indexes(2, 1))
  end

  ## 
  def set_frequency(freq)
    raise "Type error: freq must be Integer\n"  if !freq .is_a?(Integer)
    @repeatFrequency = freq
  end

  ## ·«¤êÊÖ¤·¤Î duration Éôʬ¤òÀßÄꤹ¤ë e == nil ¤Ï forever
  def set_duration(beg, fin)
    raise "Type error: begin must be MhcDate\n"  if !beg .is_a?(MhcDate)
    raise "Type error: end   must be MhcDate\n"  if fin && !fin .is_a?(MhcDate)

    @beg = replace_date(@beg, beg .y, beg .m, beg .d)
    ## @fin ¤ÎÆüÉÕÉôʬ¤Ï¡¢¾ï¤Ë @beg ¤ÈƱ¤¸¤Ë¤Ê¤ë
    ## duration end ¤Ï @repeatEnd ¤ÇÀßÄê
    @fin = replace_date(@fin, beg .y, beg .m, beg .d)

    if fin
      @repeatForever = false
      @repeatEnd = Time .local(fin .y, fin .m, fin .d)
    else
      @repeatForever = true
      @repeatEnd = Time .local(1970, 1, 2)
    end
  end

  def unpack
    if @data != ''
      @event, @beg, @fin, @alarm, @advance, @advanceUnits, 
	@repeatType, @repeatForever, @repeatEnd, @repeatFrequency, 
	@repeatDay,  @repeatDays, @repeatWeekstart, @exceptions, 
	@exception,  @description, @note = PiLib .unpack_Appointment(@data)

    else
      @event           = true
      @beg             = Time .local(1970, 1, 2)
      @fin             = Time .local(1970, 1, 2)
      @alarm           = false
      @advance         = 0
      @advanceUnits    = 0
      @repeatType      = 0
      @repeatForever   = false
      @repeatEnd       = Time .local(1970, 1, 2)
      @repeatFrequency = 0
      @repeatDay       = 0
      @repeatDays      = [false] * 7
      @repeatWeekstart = 0
      @exceptions      = 0
      @exception       = []
      @description     = 'No Subject'
      @note            = ''
    end
    return self
  end

  def pack
    ary = [@event, @beg, @fin, @alarm, @advance,
      @advanceUnits, @repeatType, @repeatForever,
      @repeatEnd, @repeatFrequency, 
      @repeatDay,  @repeatDays, @repeatWeekstart,
      @exceptions, @exception,  @description, @note]
    @data = PiLib .pack_Appointment(ary)
    return self
  end

end

class Time
  def to_xscday
    return format("%04d%02d%02d", year, mon, day)
 end
  def to_xsctime
    return format("%02d:%02d", hour, min)
  end
end    

################################################################
##
## classes for AddressBook
##
class PilotAddressDB < PilotDB
  def initialize(pi, dbname)
    super ## set @id, @attr, @category, @data

    app_info = self .get_app_info

    @catRenamed, @catName, @catID, @catLastUniqueID, 
      # [22]       [22]          [8]
      @labels, @labelsRenamed, @phoneLabels,
      @country, @sortByCompany = *PiLib .unpack_AddressAppInfo(app_info)

    ## @labels[22], @labelsRenamed[22]
    ##

    ## @phoneLabels[0..7] = 
    ##      [²ñ¼Ò, ¼«Âð, Fax, ¤½¤Î¾, E-mail, Âåɽ, ¥Ý¥±¥Ù¥ë, ·ÈÂÓ]

    @recClass = PilotAddRecord
  end
end

class PilotAddressRecord < PilotRecord


  def to_s(labels, ren, phonelabels)
    ret = ''
    # 00-03   labels[i]  ¤ËÂбþ
    # 04-11   phoneLabels[@phoneLabel[i - 4]] ¤ËÂбþ
    # 12-16   labels[i]  ¤ËÂбþ
    # 17-20   labels[i]  ¤ËÂбþ (¥«¥¹¥¿¥à)
    # 21      labels[i]  ¤ËÂбþ

    for i in (0 .. 2)
      ret += "#{labels[i]}: #{@entry[i]}\n"
    end
    for i in (3 .. 7)
      ret += "#{phonelabels[@phoneLabel[i-3]]}: #{@entry[i]}\n"
    end
    for i in (8 .. 18)
      ret += "#{labels[i]}: #{@entry[i]}\n"
    end

    return ret
  end

  private
  def unpack
    # i[5]         i         s[19]
    @phoneLabel, @showPhone, @entry = *PiLib .unpack_Address(@data)
  end

  def pack
    @data = PiLib .pack_Address([@phoneLabel, @showPhone, @entry])
  end
end

### Copyright Notice:

## Copyright (C) 1999, 2000 Yoshinari Nomura. All rights reserved.
## Copyright (C) 2000 MHC developing team. All rights reserved.

## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions
## are met:
## 
## 1. Redistributions of source code must retain the above copyright
##    notice, this list of conditions and the following disclaimer.
## 2. Redistributions in binary form must reproduce the above copyright
##    notice, this list of conditions and the following disclaimer in the
##    documentation and/or other materials provided with the distribution.
## 3. Neither the name of the team nor the names of its contributors
##    may be used to endorse or promote products derived from this software
##    without specific prior written permission.
## 
## THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS''
## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
## FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
## THE TEAM OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
## INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
## (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
## STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
## OF THE POSSIBILITY OF SUCH DAMAGE.

### mhc-palm.rb ends here