Class: RIMS::Protocol::UserMailboxDecoder::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/rims/protocol/decoder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unique_user_id, mail_store, logger, bulk_response_count: 100, bulk_response_size: 1024**2 * 10, read_lock_timeout_seconds: ReadWriteLock::DEFAULT_TIMEOUT_SECONDS, write_lock_timeout_seconds: ReadWriteLock::DEFAULT_TIMEOUT_SECONDS, cleanup_write_lock_timeout_seconds: 1, charset_aliases: RFC822::DEFAULT_CHARSET_ALIASES, charset_convert_options: nil) ⇒ Engine

Returns a new instance of Engine.



757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
# File 'lib/rims/protocol/decoder.rb', line 757

def initialize(unique_user_id, mail_store, logger,
               bulk_response_count: 100,
               bulk_response_size: 1024**2 * 10,
               read_lock_timeout_seconds: ReadWriteLock::DEFAULT_TIMEOUT_SECONDS,
               write_lock_timeout_seconds: ReadWriteLock::DEFAULT_TIMEOUT_SECONDS,
               cleanup_write_lock_timeout_seconds: 1,
               charset_aliases: RFC822::DEFAULT_CHARSET_ALIASES,
               charset_convert_options: nil)
  @unique_user_id = unique_user_id
  @mail_store = mail_store
  @logger = logger
  @bulk_response_count = bulk_response_count
  @bulk_response_size = bulk_response_size
  @read_lock_timeout_seconds = read_lock_timeout_seconds
  @write_lock_timeout_seconds = write_lock_timeout_seconds
  @cleanup_write_lock_timeout_seconds = cleanup_write_lock_timeout_seconds
  @charset_aliases = charset_aliases
  @charset_convert_options = charset_convert_options
  @folders = {}
  @logger.debug("RIMS::Protocol::UserMailboxDecoder::Engine#initialize at #{self}") if @logger.debug?
end

Instance Attribute Details

#mail_storeObject (readonly)

for test only



780
781
782
# File 'lib/rims/protocol/decoder.rb', line 780

def mail_store
  @mail_store
end

#unique_user_idObject (readonly)

Returns the value of attribute unique_user_id.



779
780
781
# File 'lib/rims/protocol/decoder.rb', line 779

def unique_user_id
  @unique_user_id
end

Class Method Details

.imap_command_authenticated(name, **guard_optional) ⇒ Object



906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
# File 'lib/rims/protocol/decoder.rb', line 906

def imap_command_authenticated(name, **guard_optional)
  name = name.to_sym
  orig_name = "_#{name}".to_sym
  alias_method orig_name, name

  guard_options_name = "_#{name}_guard_options".to_sym
  define_method guard_options_name, lambda{ guard_optional }
  private guard_options_name

  class_eval(<<-EOF, __FILE__, __LINE__ + 1)
    def #{name}(token, tag, *args, **kw_args, &block)
      guard_authenticated(:#{orig_name}, token, tag, *args, **kw_args, **#{guard_options_name}, &block)
    end
  EOF

  name
end

.imap_command_selected(name, **guard_optional) ⇒ Object



925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
# File 'lib/rims/protocol/decoder.rb', line 925

def imap_command_selected(name, **guard_optional)
  name = name.to_sym
  orig_name = "_#{name}".to_sym
  alias_method orig_name, name

  guard_options_name = "_#{name}_guard_options".to_sym
  define_method guard_options_name, lambda{ guard_optional }
  private guard_options_name

  class_eval(<<-EOF, __FILE__, __LINE__ + 1)
    def #{name}(token, tag, *args, **kw_args, &block)
      guard_selected(:#{orig_name}, token, tag, *args, **kw_args, **#{guard_options_name}, &block)
    end
  EOF

  name
end

Instance Method Details

#append(token, tag, mbox_name, *opt_args, msg_text) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
# File 'lib/rims/protocol/decoder.rb', line 1271

def append(token, tag, mbox_name, *opt_args, msg_text)
  res = new_bulk_response
  mbox_name_utf8 = Net::IMAP.decode_utf7(mbox_name)
  if (mbox_id = @mail_store.mbox_id(mbox_name_utf8)) then
    msg_flags = []
    msg_date = Time.now

    if ((! opt_args.empty?) && (opt_args[0].is_a? Array)) then
      opt_flags = opt_args.shift
      if (opt_flags[0] != :group) then
        raise SyntaxError, 'bad flag list.'
      end
      for flag_atom in opt_flags[1..-1]
        case (flag_atom.upcase)
        when '\ANSWERED'
          msg_flags << 'answered'
        when '\FLAGGED'
          msg_flags << 'flagged'
        when '\DELETED'
          msg_flags << 'deleted'
        when '\SEEN'
          msg_flags << 'seen'
        when '\DRAFT'
          msg_flags << 'draft'
        else
          raise SyntaxError, "invalid flag: #{flag_atom}"
        end
      end
    end

    if ((! opt_args.empty?) && (opt_args[0].is_a? String)) then
      begin
        msg_date = Time.parse(opt_args.shift)
      rescue ArgumentError
        raise SyntaxError, $!.message
      end
    end

    unless (opt_args.empty?) then
      raise SyntaxError, "unknown option: #{opt_args.inspect}"
    end

    uid = @mail_store.add_msg(mbox_id, msg_text, msg_date)
    for flag_name in msg_flags
      @mail_store.set_msg_flag(mbox_id, uid, flag_name, true)
    end

    mailbox_size_server_response_multicast_push(mbox_id)
    if (token) then
      folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
      folder.server_response_fetch{|untagged_response|
        res << untagged_response
        yield(res.flush) if res.full?
      }
    end

    res << "#{tag} OK [APPENDUID #{mbox_id} #{uid}] APPEND completed\r\n"
  else
    if (token) then
      folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
      folder.server_response_fetch{|untagged_response|
        res << untagged_response
        yield(res.flush) if res.full?
      }
    end
    res << "#{tag} NO [TRYCREATE] not found a mailbox\r\n"
  end
  yield(res.flush)
end

#check(token, tag) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
# File 'lib/rims/protocol/decoder.rb', line 1342

def check(token, tag)
  res = new_bulk_response
  if (token) then
    folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
    folder.server_response_fetch{|untagged_response|
      res << untagged_response
      yield(res.flush) if res.full?
    }
  end
  @mail_store.sync
  res << "#{tag} OK CHECK completed\r\n"
  yield(res.flush)
end

#cleanup(token) ⇒ Object



835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
# File 'lib/rims/protocol/decoder.rb', line 835

def cleanup(token)
  if (token) then
    begin
      @mail_store.write_synchronize(@cleanup_write_lock_timeout_seconds) {
        folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
        close_folder(token) do |untagged_response|
          folder.server_response_multicast_push(untagged_response)
        end
      }
    rescue WriteLockTimeoutError
      @logger.warn("give up to close folder becaue of write-lock timeout over #{@write_lock_timeout_seconds} seconds")
      @folders.delete(token)
    end
  end

  nil
end

#close(token, tag) {|[ "#{tag} OK CLOSE completed\r\n" ]| ... } ⇒ Object

Yields:

  • ([ "#{tag} OK CLOSE completed\r\n" ])


1369
1370
1371
1372
# File 'lib/rims/protocol/decoder.rb', line 1369

def close(token, tag)
  close_no_response(token)
  yield([ "#{tag} OK CLOSE completed\r\n" ])
end

#copy(token, tag, msg_set, mbox_name, uid: false) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
# File 'lib/rims/protocol/decoder.rb', line 1631

def copy(token, tag, msg_set, mbox_name, uid: false)
  folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
  folder.should_be_alive
  folder.reload if folder.updated?

  res = new_bulk_response
  mbox_name_utf8 = Net::IMAP.decode_utf7(mbox_name)
  msg_set = folder.parse_msg_set(msg_set, uid: uid)

  if (mbox_id = @mail_store.mbox_id(mbox_name_utf8)) then
    msg_list = folder.msg_find_all(msg_set, uid: uid)

    src_uids = []
    dst_uids = []
    for msg in msg_list
      src_uids << msg.uid
      dst_uids << @mail_store.copy_msg(msg.uid, folder.mbox_id, mbox_id)
    end

    if (msg_list.size > 0) then
      mailbox_size_server_response_multicast_push(mbox_id)
      folder.server_response_fetch{|untagged_response|
        res << untagged_response
        yield(res.flush) if res.full?
      }
      res << "#{tag} OK [COPYUID #{mbox_id} #{src_uids.join(',')} #{dst_uids.join(',')}] COPY completed\r\n"
    else
      folder.server_response_fetch{|untagged_response|
        res << untagged_response
        yield(res.flush) if res.full?
      }
      res << "#{tag} OK COPY completed\r\n"
    end
  else
    folder.server_response_fetch{|untagged_response|
      res << untagged_response
      yield(res.flush) if res.full?
    }
    res << "#{tag} NO [TRYCREATE] not found a mailbox\r\n"
  end
  yield(res.flush)
end

#create(token, tag, mbox_name) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
# File 'lib/rims/protocol/decoder.rb', line 1033

def create(token, tag, mbox_name)
  res = new_bulk_response
  if (token) then
    folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
    folder.server_response_fetch{|untagged_response|
      res << untagged_response
      yield(res.flush) if res.full?
    }
  end
  mbox_name_utf8 = Net::IMAP.decode_utf7(mbox_name)
  if (@mail_store.mbox_id(mbox_name_utf8)) then
    res << "#{tag} NO duplicated mailbox\r\n"
  else
    @mail_store.add_mbox(mbox_name_utf8)
    res << "#{tag} OK CREATE completed\r\n"
  end
  yield(res.flush)
end

#delete(token, tag, mbox_name) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
# File 'lib/rims/protocol/decoder.rb', line 1053

def delete(token, tag, mbox_name)
  res = new_bulk_response
  if (token) then
    folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
    folder.server_response_fetch{|untagged_response|
      res << untagged_response
      yield(res.flush) if res.full?
    }
  end
  mbox_name_utf8 = Net::IMAP.decode_utf7(mbox_name)
  if (id = @mail_store.mbox_id(mbox_name_utf8)) then
    if (id != @mail_store.mbox_id('INBOX')) then
      @mail_store.del_mbox(id)
      res << "#{tag} OK DELETE completed\r\n"
    else
      res << "#{tag} NO not delete inbox\r\n"
    end
  else
    res << "#{tag} NO not found a mailbox\r\n"
  end
  yield(res.flush)
end

#destroyObject



853
854
855
856
857
858
859
860
861
862
863
864
865
# File 'lib/rims/protocol/decoder.rb', line 853

def destroy
  @logger.debug("RIMS::Protocol::UserMailboxDecoder::Engine#destroy at #{self}") if @logger.debug?
  tmp_mail_store = @mail_store
  ReadWriteLock.write_lock_timeout_detach(@cleanup_write_lock_timeout_seconds, @write_lock_timeout_seconds, logger: @logger) {|timeout_seconds|
    tmp_mail_store.write_synchronize(timeout_seconds) {
      @logger.info("close mail store: #{@unique_user_id}")
      tmp_mail_store.close
    }
  }
  @mail_store = nil

  nil
end

#examine(token, tag, mbox_name) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
# File 'lib/rims/protocol/decoder.rb', line 1008

def examine(token, tag, mbox_name)
  if (token) then
    close_no_response(token)
  end

  res = new_bulk_response
  new_token = nil
  mbox_name_utf8 = Net::IMAP.decode_utf7(mbox_name)

  if (id = @mail_store.mbox_id(mbox_name_utf8)) then
    new_token = open_folder(id, read_only: true)
    folder_open_msgs(new_token) {|untagged_response|
      res << untagged_response
      yield(res.flush) if res.full?
    }
    res << "#{tag} OK [READ-ONLY] EXAMINE completed\r\n"
  else
    res << "#{tag} NO not found a mailbox\r\n"
  end
  yield(res.flush)

  new_token
end

#expunge(token, tag) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
# File 'lib/rims/protocol/decoder.rb', line 1375

def expunge(token, tag)
  folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
  folder.should_be_alive
  return yield([ "#{tag} NO cannot expunge in read-only mode\r\n" ]) if folder.read_only?
  folder.reload if folder.updated?

  res = new_bulk_response
  folder.server_response_fetch{|untagged_response|
    res << untagged_response
    yield(res.flush) if res.full?
 }

  folder.expunge_mbox do |msg_num|
    untagged_response = "* #{msg_num} EXPUNGE\r\n"
    res << untagged_response
    yield(res.flush) if res.full?
    folder.server_response_multicast_push(untagged_response)
  end

  res << "#{tag} OK EXPUNGE completed\r\n"
  yield(res.flush)
end

#fetch(token, tag, msg_set, data_item_group, uid: false) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
# File 'lib/rims/protocol/decoder.rb', line 1479

def fetch(token, tag, msg_set, data_item_group, uid: false)
  folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
  folder.should_be_alive
  folder.reload if folder.updated?

  msg_set = folder.parse_msg_set(msg_set, uid: uid)
  msg_list = folder.msg_find_all(msg_set, uid: uid)

  unless ((data_item_group.is_a? Array) && data_item_group[0] == :group) then
    data_item_group = [ :group, data_item_group ]
  end
  if (uid) then
    unless (data_item_group.find{|i| (i.is_a? String) && (i.upcase == 'UID') }) then
      data_item_group = [ :group, 'UID' ] + data_item_group[1..-1]
    end
  end

  parser = FetchParser.new(@mail_store, folder, charset_aliases: @charset_aliases)
  fetch = parser.parse(data_item_group)

  res = new_bulk_response
  folder.server_response_fetch{|untagged_response|
    res << untagged_response
    yield(res.flush) if res.full?
  }

  for msg in msg_list
    res << ('* '.b << msg.num.to_s.b << ' FETCH '.b << fetch.call(msg) << "\r\n".b)
    yield(res.flush) if res.full?
  end

  res << "#{tag} OK FETCH completed\r\n"
  yield(res.flush)
end

#idle(token, tag, client_input_gets, server_output_write, connection_timer) {|last_res| ... } ⇒ Object

Yields:

  • (last_res)


1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
# File 'lib/rims/protocol/decoder.rb', line 1675

def idle(token, tag, client_input_gets, server_output_write, connection_timer)
  folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
  folder.should_be_alive

  @logger.info('idle start...')
  server_output_write.call([ "+ continue\r\n" ])

  server_response_thread = Thread.new{
    res = new_bulk_response
    @logger.info('idle server response thread start...')
    folder.server_response_idle_wait{|server_response_list|
      for untagged_response in server_response_list
        @logger.debug("idle server response: #{untagged_response}") if @logger.debug?
        res << untagged_response
        server_output_write.call(res.flush) if res.full?
      end
      server_output_write.call(res.flush) unless res.empty?
    }
    @logger.info('idle server response thread terminated.')
  }

  begin
    connection_timer.command_wait or return
    line = client_input_gets.call
  ensure
    folder.server_response_idle_interrupt
    server_response_thread.join
  end

  last_res = []
  if (line) then
    line.chomp!("\n")
    line.chomp!("\r")
    if (line.upcase == "DONE") then
      @logger.info('idle terminated.')
      last_res << "#{tag} OK IDLE terminated\r\n"
    else
      @logger.warn('unexpected client response and idle terminated.')
      @logger.debug("unexpected client response data: #{line}") if @logger.debug?
      last_res << "#{tag} BAD unexpected client response\r\n"
    end
  else
    @logger.warn('unexpected client connection close and idle terminated.')
    last_res << "#{tag} BAD unexpected client connection close\r\n"
  end
  yield(last_res)
end

#list(token, tag, ref_name, mbox_name) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
# File 'lib/rims/protocol/decoder.rb', line 1168

def list(token, tag, ref_name, mbox_name)
  res = new_bulk_response
  if (token) then
    folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
    folder.server_response_fetch{|untagged_response|
      res << untagged_response
      yield(res.flush) if res.full?
    }
  end
  if (mbox_name.empty?) then
    res << "* LIST (\\Noselect) NIL \"\"\r\n"
  else
    list_mbox(ref_name, mbox_name) {|mbox_entry|
      res << "* LIST #{mbox_entry}\r\n"
      yield(res.flush) if res.full?
    }
  end
  res << "#{tag} OK LIST completed\r\n"
  yield(res.flush)
end

#lsub(token, tag, ref_name, mbox_name) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
# File 'lib/rims/protocol/decoder.rb', line 1190

def lsub(token, tag, ref_name, mbox_name)
  res = new_bulk_response
  if (token) then
    folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
    folder.server_response_fetch{|untagged_response|
      res << untagged_response
      yield(res.flush) if res.full?
    }
  end
  if (mbox_name.empty?) then
    res << "* LSUB (\\Noselect) NIL \"\"\r\n"
  else
    list_mbox(ref_name, mbox_name) {|mbox_entry|
      res << "* LSUB #{mbox_entry}\r\n"
      yield(res.flush) if res.full?
    }
  end
  res << "#{tag} OK LSUB completed\r\n"
  yield(res.flush)
end

#noop(token, tag) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
# File 'lib/rims/protocol/decoder.rb', line 950

def noop(token, tag)
  res = new_bulk_response
  if (token) then
    folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
    begin
      @mail_store.read_synchronize(@read_lock_timeout_seconds) {
        folder.server_response_fetch{|untagged_response|
          res << untagged_response
          yield(res.flush) if res.full?
        }
      }
    rescue ReadLockTimeoutError
      @logger.warn("give up to get folder status because of read-lock timeout over #{@read_lock_timeout_seconds} seconds")
    end
  end
  res << "#{tag} OK NOOP completed\r\n"
  yield(res.flush)
end

#recovery_if_needed(username) ⇒ Object



782
783
784
785
786
787
788
789
790
791
792
793
794
# File 'lib/rims/protocol/decoder.rb', line 782

def recovery_if_needed(username)
  @mail_store.write_synchronize(@write_lock_timeout_seconds) {
    if (@mail_store.abort_transaction?) then
      @logger.warn("user data recovery start: #{username}")
      yield("* OK [ALERT] start user data recovery.\r\n")
      @mail_store.recovery_data(logger: @logger).sync
      @logger.warn("user data recovery end: #{username}")
      yield("* OK completed user data recovery.\r\n")

      self
    end
  }
end

#rename(token, tag, src_name, dst_name) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
# File 'lib/rims/protocol/decoder.rb', line 1077

def rename(token, tag, src_name, dst_name)
  res = new_bulk_response
  if (token) then
    folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
    folder.server_response_fetch{|untagged_response|
      res << untagged_response
      yield(res.flush) if res.full?
    }
  end
  src_name_utf8 = Net::IMAP.decode_utf7(src_name)
  dst_name_utf8 = Net::IMAP.decode_utf7(dst_name)
  unless (id = @mail_store.mbox_id(src_name_utf8)) then
    res << "#{tag} NO not found a mailbox\r\n"
    return yield(res.flush)
  end
  if (id == @mail_store.mbox_id('INBOX')) then
    res << "#{tag} NO not rename inbox\r\n"
    return yield(res.flush)
  end
  if (@mail_store.mbox_id(dst_name_utf8)) then
    res << "#{tag} NO duplicated mailbox\r\n"
    return yield(res.flush)
  end
  @mail_store.rename_mbox(id, dst_name_utf8)
  res << "#{tag} OK RENAME completed\r\n"
  yield(res.flush)
end

#search(token, tag, *cond_args, uid: false) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
# File 'lib/rims/protocol/decoder.rb', line 1399

def search(token, tag, *cond_args, uid: false)
  folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
  folder.should_be_alive
  folder.reload if folder.updated?
  parser = SearchParser.new(@mail_store, folder,
                            charset_aliases: @charset_aliases,
                            charset_convert_options: @charset_convert_options)

  if (! cond_args.empty? && cond_args[0].upcase == 'CHARSET') then
    cond_args.shift
    charset_string = cond_args.shift or raise SyntaxError, 'need for a charset string of CHARSET'
    charset_string.is_a? String or raise SyntaxError, "CHARSET charset string expected as <String> but was <#{charset_string.class}>."
    begin
      parser.charset = charset_string
    rescue ArgumentError
      @logger.warn("unknown charset: #{charset_string}")
      return yield([ "#{tag} NO [BADCHARSET (#{Encoding.list.reject(&:dummy?).map(&:to_s).join(' ')})] unknown charset\r\n" ])
    end
  end

  if (cond_args.empty?) then
    raise SyntaxError, 'required search arguments.'
  end

  if (cond_args[0].upcase == 'UID' && cond_args.length >= 2) then
    begin
      msg_set = folder.parse_msg_set(cond_args[1], uid: true)
      msg_list = folder.msg_find_all(msg_set, uid: true)
      cond_args.shift(2)
    rescue MessageSetSyntaxError
      msg_list = folder.each_msg
    end
  else
    begin
      msg_set = folder.parse_msg_set(cond_args[0], uid: false)
      msg_list = folder.msg_find_all(msg_set, uid: false)
      cond_args.shift
    rescue MessageSetSyntaxError
      msg_list = folder.each_msg
    end
  end
  cond = parser.parse(cond_args)

  res = new_bulk_response
  folder.server_response_fetch{|untagged_response|
    res << untagged_response
    yield(res.flush) if res.full?
  }

  res << '* SEARCH'
  begin
    begin
      for msg in msg_list
        begin
          if (cond.call(msg)) then
            if (uid) then
              res << " #{msg.uid}"
            else
              res << " #{msg.num}"
            end
            yield(res.flush) if res.full?
          end
        rescue EncodingError
          @logger.warn("encoding error at the message: uidvalidity(#{folder.mbox_id}) uid(#{msg.uid})")
          @logger.warn("#{$!} (#{$!.class})")
        end
      end
    ensure
      res << "\r\n"
    end
  rescue
    yield(res.flush)
    raise
  end

  res << "#{tag} OK SEARCH completed\r\n"
  yield(res.flush)
end

#select(token, tag, mbox_name) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
# File 'lib/rims/protocol/decoder.rb', line 983

def select(token, tag, mbox_name)
  if (token) then
    close_no_response(token)
  end

  res = new_bulk_response
  new_token = nil
  mbox_name_utf8 = Net::IMAP.decode_utf7(mbox_name)

  if (id = @mail_store.mbox_id(mbox_name_utf8)) then
    new_token = open_folder(id)
    folder_open_msgs(new_token) {|untagged_response|
      res << untagged_response
      yield(res.flush) if res.full?
    }
    res << "#{tag} OK [READ-WRITE] SELECT completed\r\n"
  else
    res << "#{tag} NO not found a mailbox\r\n"
  end
  yield(res.flush)

  new_token
end

#status(token, tag, mbox_name, data_item_group) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
# File 'lib/rims/protocol/decoder.rb', line 1212

def status(token, tag, mbox_name, data_item_group)
  res = new_bulk_response
  if (token) then
    folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
    folder.server_response_fetch{|untagged_response|
      res << untagged_response
      yield(res.flush) if res.full?
    }
  end
  mbox_name_utf8 = Net::IMAP.decode_utf7(mbox_name)
  if (id = @mail_store.mbox_id(mbox_name_utf8)) then
    unless ((data_item_group.is_a? Array) && (data_item_group[0] == :group)) then
      raise SyntaxError, 'second arugment is not a group list.'
    end

    values = []
    for item in data_item_group[1..-1]
      case (item.upcase)
      when 'MESSAGES'
        values << 'MESSAGES' << @mail_store.mbox_msg_num(id)
      when 'RECENT'
        values << 'RECENT' << @mail_store.mbox_flag_num(id, 'recent')
      when 'UIDNEXT'
        values << 'UIDNEXT' << @mail_store.uid(id)
      when 'UIDVALIDITY'
        values << 'UIDVALIDITY' << id
      when 'UNSEEN'
        unseen_flags = @mail_store.mbox_msg_num(id) - @mail_store.mbox_flag_num(id, 'seen')
        values << 'UNSEEN' << unseen_flags
      else
        raise SyntaxError, "unknown status data: #{item}"
      end
    end

    res << "* STATUS #{Protocol.quote(mbox_name)} (#{values.join(' ')})\r\n"
    res << "#{tag} OK STATUS completed\r\n"
  else
    res << "#{tag} NO not found a mailbox\r\n"
  end
  yield(res.flush)
end

#store(token, tag, msg_set, data_item_name, data_item_value, uid: false) ⇒ Object



1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
# File 'lib/rims/protocol/decoder.rb', line 1515

def store(token, tag, msg_set, data_item_name, data_item_value, uid: false)
  folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
  folder.should_be_alive
  return yield([ "#{tag} NO cannot store in read-only mode\r\n" ]) if folder.read_only?
  folder.reload if folder.updated?

  msg_set = folder.parse_msg_set(msg_set, uid: uid)
  name, option = data_item_name.split(/\./, 2)

  case (name.upcase)
  when 'FLAGS'
    action = :flags_replace
  when '+FLAGS'
    action = :flags_add
  when '-FLAGS'
    action = :flags_del
  else
    raise SyntaxError, "unknown store action: #{name}"
  end

  case (option && option.upcase)
  when 'SILENT'
    is_silent = true
  when nil
    is_silent = false
  else
    raise SyntaxError, "unknown store option: #{option.inspect}"
  end

  if ((data_item_value.is_a? Array) && data_item_value[0] == :group) then
    flag_list = []
    for flag_atom in data_item_value[1..-1]
      case (flag_atom.upcase)
      when '\ANSWERED'
        flag_list << 'answered'
      when '\FLAGGED'
        flag_list << 'flagged'
      when '\DELETED'
        flag_list << 'deleted'
      when '\SEEN'
        flag_list << 'seen'
      when '\DRAFT'
        flag_list << 'draft'
      else
        raise SyntaxError, "invalid flag: #{flag_atom}"
      end
    end
    rest_flag_list = (MailStore::MSG_FLAG_NAMES - %w[ recent ]) - flag_list
  else
    raise SyntaxError, 'third arugment is not a group list.'
  end

  msg_list = folder.msg_find_all(msg_set, uid: uid)

  for msg in msg_list
    case (action)
    when :flags_replace
      for name in flag_list
        @mail_store.set_msg_flag(folder.mbox_id, msg.uid, name, true)
      end
      for name in rest_flag_list
        @mail_store.set_msg_flag(folder.mbox_id, msg.uid, name, false)
      end
    when :flags_add
      for name in flag_list
        @mail_store.set_msg_flag(folder.mbox_id, msg.uid, name, true)
      end
    when :flags_del
      for name in flag_list
        @mail_store.set_msg_flag(folder.mbox_id, msg.uid, name, false)
      end
    else
      raise "internal error: unknown action: #{action}"
    end
  end

  res = new_bulk_response
  folder.server_response_fetch{|untagged_response|
    res << untagged_response
    yield(res.flush) if res.full?
  }

  if (is_silent) then
    res << "#{tag} OK STORE completed\r\n"
    yield(res.flush)
  else
    for msg in msg_list
      flag_atom_list = nil

      if (@mail_store.msg_exist? folder.mbox_id, msg.uid) then
        flag_atom_list = []
        for name in MailStore::MSG_FLAG_NAMES
          if (@mail_store.msg_flag(folder.mbox_id, msg.uid, name)) then
            flag_atom_list << "\\#{name.capitalize}"
          end
        end
      end

      if (flag_atom_list) then
        if (uid) then
          res << "* #{msg.num} FETCH (UID #{msg.uid} FLAGS (#{flag_atom_list.join(' ')}))\r\n"
        else
          res << "* #{msg.num} FETCH (FLAGS (#{flag_atom_list.join(' ')}))\r\n"
        end
        yield(res.flush) if res.full?
      else
        @logger.warn("not found a message and skipped: uidvalidity(#{folder.mbox_id}) uid(#{msg.uid})")
      end
    end

    res << "#{tag} OK STORE completed\r\n"
    yield(res.flush)
  end
end

#subscribe(token, tag, mbox_name) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
# File 'lib/rims/protocol/decoder.rb', line 1106

def subscribe(token, tag, mbox_name)
  res = new_bulk_response
  if (token) then
    folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
    folder.server_response_fetch{|untagged_response|
      res << untagged_response
      yield(res.flush) if res.full?
    }
  end
  mbox_name_utf8 = Net::IMAP.decode_utf7(mbox_name)
  if (@mail_store.mbox_id(mbox_name_utf8)) then
    res << "#{tag} OK SUBSCRIBE completed\r\n"
  else
    res << "#{tag} NO not found a mailbox\r\n"
  end
  yield(res.flush)
end

#unsubscribe(token, tag, mbox_name) {|res.flush| ... } ⇒ Object

Yields:

  • (res.flush)


1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
# File 'lib/rims/protocol/decoder.rb', line 1125

def unsubscribe(token, tag, mbox_name)
  res = new_bulk_response
  if (token) then
    folder = @folders[token] or raise KeyError.new("undefined folder token: #{token}", key: token, receiver: self)
    folder.server_response_fetch{|untagged_response|
      res << untagged_response
      yield(res.flush) if res.full?
    }
  end
  mbox_name_utf8 = Net::IMAP.decode_utf7(mbox_name)
  if (@mail_store.mbox_id(mbox_name_utf8)) then
    res << "#{tag} NO not implemented subscribe/unsbscribe command\r\n"
  else
    res << "#{tag} NO not found a mailbox\r\n"
  end
  yield(res.flush)
end