Changeset View
Changeset View
Standalone View
Standalone View
apps/kolab_guam/src/kolab_guam_session.erl
Show First 20 Lines • Show All 195 Lines • ▼ Show 20 Lines | case check_for_transmission_change_commands(TLS, TLSConfig, PreprocessData, Deflator, Socket) of | ||||
nochange -> | nochange -> | ||||
%%lager:debug("... now applying rules"), | %%lager:debug("... now applying rules"), | ||||
{ ModifiedData, NewSplitCommand, NewSplitResetTrigger, NewUndecidedRules, NewActiveRules, PostAction } = apply_ruleset_clientside(ImapSession, Socket, PreprocessData, CurrentCommandSplit, UndecidedRules, ActiveRules), | { ModifiedData, NewSplitCommand, NewSplitResetTrigger, NewUndecidedRules, NewActiveRules, PostAction } = apply_ruleset_clientside(ImapSession, Socket, PreprocessData, CurrentCommandSplit, UndecidedRules, ActiveRules), | ||||
%%lager:info("The modified data is: ~s", [ModifiedData]), | %%lager:info("The modified data is: ~s", [ModifiedData]), | ||||
%lager:info("The post-processed data is: ~s", [PostProcessed]), | %lager:info("The post-processed data is: ~s", [PostProcessed]), | ||||
BufferThisData = | BufferThisData = | ||||
case PostAction of | case PostAction of | ||||
perform_passthrough -> | perform_passthrough -> | ||||
%lager:info("sending (no buffer): ~s", [ModifiedData]), | |||||
eimap:passthrough_data(ImapSession, ModifiedData), | eimap:passthrough_data(ImapSession, ModifiedData), | ||||
<<>>; | <<>>; | ||||
buffer_data -> | buffer_data -> | ||||
Data | % Originally Aaron uses Data here, but later on this buffer is assumed to be | ||||
% already decoded, so we do have to use PreprocessData here, I think. | |||||
case binary:matches(PreprocessData, <<"\r\n">>) of | |||||
[] -> | |||||
%lager:info("buffering: ~s", [PreprocessData]), | |||||
PreprocessData; | |||||
List -> | |||||
{FoundPos, _} = lists:last(List), | |||||
% I would like to have some binary:match for the last instead of the | |||||
% first occurrence; but I'm really inexperienced in erlang so I don't | |||||
% know how to solve this efficiently, so I'm using binary:matches with | |||||
% using the last element only | |||||
SplitPos = FoundPos + 2, | |||||
eimap:passthrough_data(ImapSession, binary:part(PreprocessData, 0, SplitPos)), | |||||
%lager:info("sending first part: ~s", [binary:part(PreprocessData, 0, SplitPos)] ), | |||||
%lager:info("buffering second part: ~s", [binary:part(PreprocessData, SplitPos, size(PreprocessData)-SplitPos)]), | |||||
binary:part(PreprocessData, SplitPos, size(PreprocessData)-SplitPos) | |||||
end | |||||
end, | end, | ||||
{ TLS, Socket, Inflator, Deflator, NewUndecidedRules, NewActiveRules, BufferThisData, NewSplitCommand, NewSplitResetTrigger } | { TLS, Socket, Inflator, Deflator, NewUndecidedRules, NewActiveRules, BufferThisData, NewSplitCommand, NewSplitResetTrigger } | ||||
end, | end, | ||||
set_socket_active(TLSActive, CurrentSocket), | set_socket_active(TLSActive, CurrentSocket), | ||||
PrevBuffered = State#state.buffered_client_data, | %buffered_client_data is already in DataToBuffer via preprocess_client_data | ||||
{ noreply, State#state{ rules_deciding = CurrentUndecidedRules, rules_active = CurrentActiveRules, | { noreply, State#state{ rules_deciding = CurrentUndecidedRules, rules_active = CurrentActiveRules, | ||||
socket = CurrentSocket, client_tls_active = TLSActive, | socket = CurrentSocket, client_tls_active = TLSActive, | ||||
inflator = CurrentInflator, deflator = CurrentDeflator, | inflator = CurrentInflator, deflator = CurrentDeflator, | ||||
buffered_client_data = <<PrevBuffered/binary, DataToBuffer/binary>>, | buffered_client_data = <<DataToBuffer/binary>>, | ||||
current_command_split = SplitCommand, | current_command_split = SplitCommand, | ||||
command_split_reset_trigger = SplitResetTrigger } }. | command_split_reset_trigger = SplitResetTrigger } }. | ||||
preprocess_client_data(undefined, Data, #state{ buffered_client_data = Buffered }) -> | preprocess_client_data(undefined, Data, #state{ buffered_client_data = Buffered }) -> | ||||
<<Buffered/binary, Data/binary>>; | <<Buffered/binary, Data/binary>>; | ||||
preprocess_client_data(Z, Data, #state{ buffered_client_data = Buffered }) -> | preprocess_client_data(Z, Data, #state{ buffered_client_data = Buffered }) -> | ||||
Inflated = joined(zlib:inflate(Z, Data), <<>>), | Inflated = iolist_to_binary(zlib:inflate(Z, Data)), | ||||
<<Buffered/binary, Inflated/binary>>. | <<Buffered/binary, Inflated/binary>>. | ||||
postprocess_server_data(undefined, Data) -> | postprocess_server_data(undefined, Data) -> | ||||
%% we aren't compressing so there is nothing to do | %% we aren't compressing so there is nothing to do | ||||
Data; | Data; | ||||
postprocess_server_data(Z, Data) -> | postprocess_server_data(Z, Data) -> | ||||
joined(zlib:deflate(Z, Data, sync), <<>>). | iolist_to_binary(zlib:deflate(Z, Data, sync)). | ||||
joined([], Binary) -> Binary; | |||||
joined([H|Rest], Binary) -> joined(Rest, <<Binary/binary, H/binary>>). | |||||
init_rules(RuleConfig) -> init_rule(RuleConfig, []). | init_rules(RuleConfig) -> init_rule(RuleConfig, []). | ||||
init_rule([], Acc) -> Acc; | init_rule([], Acc) -> Acc; | ||||
init_rule([{ RuleName, Config }|RuleConfig], Acc) -> | init_rule([{ RuleName, Config }|RuleConfig], Acc) -> | ||||
Module = full_rule_name(RuleName), | Module = full_rule_name(RuleName), | ||||
%% we try to new the module, but if something goes wrong, e.g. it does not exist, | %% we try to new the module, but if something goes wrong, e.g. it does not exist, | ||||
%% then we skip this config block because it is BROKEN | %% then we skip this config block because it is BROKEN | ||||
try Module:new(Config) of | try Module:new(Config) of | ||||
Show All 14 Lines | |||||
apply_next_rule_serverside(_ImapSession, ServerData, ActiveRulesAcc, []) -> { ServerData, lists:reverse(ActiveRulesAcc) }; | apply_next_rule_serverside(_ImapSession, ServerData, ActiveRulesAcc, []) -> { ServerData, lists:reverse(ActiveRulesAcc) }; | ||||
apply_next_rule_serverside(ImapSession, ServerData, ActiveRulesAcc, [{ Module, RuleState } | ActiveRules]) -> | apply_next_rule_serverside(ImapSession, ServerData, ActiveRulesAcc, [{ Module, RuleState } | ActiveRules]) -> | ||||
%TODO: allow rules to remove themselves from the action during serverside processing? | %TODO: allow rules to remove themselves from the action during serverside processing? | ||||
{ ModifiedData, ModifiedRuleState } = Module:apply_to_server_message(ImapSession, ServerData, RuleState), | { ModifiedData, ModifiedRuleState } = Module:apply_to_server_message(ImapSession, ServerData, RuleState), | ||||
apply_next_rule_serverside(ImapSession, ModifiedData, [{ Module, ModifiedRuleState } | ActiveRulesAcc], ActiveRules). | apply_next_rule_serverside(ImapSession, ModifiedData, [{ Module, ModifiedRuleState } | ActiveRulesAcc], ActiveRules). | ||||
apply_ruleset_clientside(_ImapSession, _Socket, ClientData, _CurrentCommandSplit, [], []) -> | apply_ruleset_clientside(_ImapSession, _Socket, ClientData, _CurrentCommandSplit, [], []) -> | ||||
{ ClientData, [], [], [], [], perform_passthrough }; | { ClientData, undefined, [], [], [], perform_passthrough }; | ||||
apply_ruleset_clientside(ImapSession, Socket, ClientData, CurrentCommandSplit, UndecidedRules, CurrentlyActiveRules) -> | apply_ruleset_clientside(ImapSession, Socket, ClientData, CurrentCommandSplit, UndecidedRules, CurrentlyActiveRules) -> | ||||
{ PostAction, SplitCommand, SplitResetTrigger } = | { PostAction, SplitCommand, SplitResetTrigger } = | ||||
case CurrentCommandSplit of | case CurrentCommandSplit of | ||||
undefined -> | undefined -> | ||||
%We first have to check whether the command is an empty line. In such a case split_command_into_components would return an empty command, | %We first have to check whether the command is an empty line. In such a case split_command_into_components would return an empty command, | ||||
%even though the command is complete. | %even though the command is complete. | ||||
case ClientData of | case ClientData of | ||||
<<"\r\n">> -> { perform_passthrough, CurrentCommandSplit, reset_for_next_client_command }; | <<"\r\n">> -> { perform_passthrough, CurrentCommandSplit, reset_for_next_client_command }; | ||||
▲ Show 20 Lines • Show All 155 Lines • Show Last 20 Lines |