]> granicus.if.org Git - ejabberd/commitdiff
Remove now() - part 1
authorPaweł Chmielowski <pchmielowski@process-one.net>
Fri, 4 Dec 2015 14:08:43 +0000 (15:08 +0100)
committerPaweł Chmielowski <pchmielowski@process-one.net>
Fri, 4 Dec 2015 14:08:43 +0000 (15:08 +0100)
src/cyrsasl_anonymous.erl
src/ejabberd.erl
src/ejabberd_captcha.erl
src/ejabberd_odbc.erl
src/gen_iq_handler.erl
src/mod_admin_extra.erl
src/mod_configure2.erl
src/mod_muc_room.erl
src/mod_multicast.erl
src/mod_pres_counter.erl
src/mod_roster.erl

index af403645ab2759af143205c88d1c3573bf4eb9ab..adbde246d4b59f4dcf9e9290364ed65e3d6a49d0 100644 (file)
@@ -43,11 +43,11 @@ stop() -> ok.
 mech_new(Host, _GetPassword, _CheckPassword, _CheckPasswordDigest) ->
     {ok, #state{server = Host}}.
 
-mech_step(#state{server = Server}, _ClientIn) ->
-    User = iolist_to_binary([randoms:get_string()
-                            | [jlib:integer_to_binary(X)
-                               || X <- tuple_to_list(now())]]),
+mech_step(#state{server = Server} = S, ClientIn) ->
+    User = iolist_to_binary([randoms:get_string(),
+                             randoms:get_string(),
+                             randoms:get_string()]),
     case ejabberd_auth:is_user_exists(User, Server) of
-        true  -> {error, <<"not-authorized">>};
+        true  -> mech_step(S, ClientIn);
         false -> {ok, [{username, User}, {auth_module, ejabberd_auth_anonymous}]}
     end.
index 14404cd4c3154e160badd52cf71f70eed09b396f..3191e9817a56a965501bc03c5d30fb3ab291cf6a 100644 (file)
@@ -105,7 +105,7 @@ start_app([], _Type, _StartFlag) ->
     ok.
 
 check_app_modules(App, StartFlag) ->
-    {A, B, C} = now(),
+    {A, B, C} = p1_time_compat:timestamp(),
     random:seed(A, B, C),
     sleep(5000),
     case application:get_key(App, modules) of
index 6006b28513cdd8ae4d807d7c37840379475972fb..4d906f3248e53fbd899410c27645024924d0b848 100644 (file)
@@ -661,8 +661,7 @@ clean_treap(Treap, CleanPriority) ->
     end.
 
 now_priority() ->
-    {MSec, Sec, USec} = now(),
-    -((MSec * 1000000 + Sec) * 1000000 + USec).
+    -p1_time_compat:system_time(micro_seconds).
 
 opt_type(captcha_cmd) ->
     fun (FileName) ->
index f86dcca2e088153fec0b09a8d12354f883447cf8..123f0d558c1c878ac563c16b685238fb614b51a1 100644 (file)
@@ -153,7 +153,8 @@ sql_call(Host, Msg) ->
         case ejabberd_odbc_sup:get_random_pid(Host) of
           none -> {error, <<"Unknown Host">>};
           Pid ->
-            (?GEN_FSM):sync_send_event(Pid,{sql_cmd, Msg, now()},
+            (?GEN_FSM):sync_send_event(Pid,{sql_cmd, Msg,
+                                            p1_time_compat:monotonic_time(milli_seconds)},
                                        ?TRANSACTION_TIMEOUT)
           end;
       _State -> nested_op(Msg)
@@ -161,7 +162,8 @@ sql_call(Host, Msg) ->
 
 keep_alive(PID) ->
     (?GEN_FSM):sync_send_event(PID,
-                              {sql_cmd, {sql_query, ?KEEPALIVE_QUERY}, now()},
+                              {sql_cmd, {sql_query, ?KEEPALIVE_QUERY},
+                                p1_time_compat:monotonic_time(milli_seconds)},
                               ?KEEPALIVE_TIMEOUT).
 
 -spec sql_query_t(sql_query()) -> sql_query_result().
@@ -373,7 +375,7 @@ print_state(State) -> State.
 %%%----------------------------------------------------------------------
 
 run_sql_cmd(Command, From, State, Timestamp) ->
-    case timer:now_diff(now(), Timestamp) div 1000 of
+    case p1_time_compat:monotonic_time(milli_seconds) - Timestamp of
       Age when Age < (?TRANSACTION_TIMEOUT) ->
          put(?NESTING_KEY, ?TOP_LEVEL_TXN),
          put(?STATE_KEY, State),
index 2b712aadc42dc8d608ef06b26081bd0c62b0095e..fa2a97737ebd4de9587ad69cdeea73f730d2f86c 100644 (file)
@@ -113,7 +113,8 @@ handle(Host, Module, Function, Opts, From, To, IQ) ->
        {one_queue, Pid} ->
            Pid ! {process_iq, From, To, IQ};
        {queues, Pids} ->
-           Pid = lists:nth(erlang:phash(now(), length(Pids)), Pids),
+           Pid = lists:nth(erlang:phash(p1_time_compat:unique_integer(),
+                                         length(Pids)), Pids),
            Pid ! {process_iq, From, To, IQ};
        parallel ->
            spawn(?MODULE, process_iq,
@@ -179,4 +180,3 @@ code_change(_OldVsn, State, _Extra) -> {ok, State}.
 %%--------------------------------------------------------------------
 %%% Internal functions
 %%--------------------------------------------------------------------
-
index cebd5cea4e0871e987d6ca8c0d738da0ee6a407d..ee0e9d0921eab88920536bd5f2c9b33973d37c5b 100644 (file)
@@ -554,8 +554,7 @@ num_active_users(Host, Days) ->
 
 %% Code based on ejabberd/src/web/ejabberd_web_admin.erl
 list_last_activity(Host, Integral, Days) ->
-    {MegaSecs, Secs, _MicroSecs} = now(),
-    TimeStamp = MegaSecs * 1000000 + Secs,
+    TimeStamp = p1_time_compat:system_time(seconds),
     TS = TimeStamp - Days * 86400,
     case catch mnesia:dirty_select(
                 last_activity, [{{last_activity, {'_', Host}, '$1', '_'},
@@ -620,8 +619,7 @@ delete_old_users(Days, Users) ->
     SecOlder = Days*24*60*60,
 
     %% Get current time
-    {MegaSecs, Secs, _MicroSecs} = now(),
-    TimeStamp_now = MegaSecs * 1000000 + Secs,
+    TimeStamp_now = p1_time_compat:system_time(seconds),
 
     %% For a user, remove if required and answer true
     F = fun({LUser, LServer}) ->
index efe665d2305b2b4e258118765586095b201a19d1..109df2455fa5b402d354b21696642fb186099d78 100644 (file)
@@ -186,8 +186,7 @@ process_get(#xmlel{name = <<"last">>, attrs = Attrs}) ->
       {'EXIT', _Reason} ->
          {error, ?ERR_INTERNAL_SERVER_ERROR};
       Vals ->
-         {MegaSecs, Secs, _MicroSecs} = now(),
-         TimeStamp = MegaSecs * 1000000 + Secs,
+         TimeStamp = p1_time_compat:system_time(seconds),
          Str = list_to_binary(
                   [[jlib:integer_to_binary(TimeStamp - V),
                     <<" ">>] || V <- Vals]),
index cb85f2963a1ac1a46e5515dcdcb072c8e6432b6f..38fed6eab2c330338eb90b4d0ab13809763e9e11 100644 (file)
@@ -149,7 +149,7 @@ normal_state({route, From, <<"">>,
          case xml:get_attr_s(<<"type">>, Attrs) of
            <<"groupchat">> ->
                Activity = get_user_activity(From, StateData),
-               Now = now_to_usec(now()),
+               Now = p1_time_compat:system_time(micro_seconds),
                MinMessageInterval =
                    trunc(gen_mod:get_module_opt(StateData#state.server_host,
                                                 mod_muc, min_message_interval, fun(MMI) when is_number(MMI) -> MMI end, 0)
@@ -288,7 +288,7 @@ normal_state({route, From, <<"">>,
                                                (StateData#state.config)#config.voice_request_min_interval,
                                            BareFrom =
                                                jid:remove_resource(jid:tolower(From)),
-                                           NowPriority = -now_to_usec(now()),
+                                           NowPriority = -p1_time_compat:system_time(micro_seconds),
                                            CleanPriority = NowPriority +
                                                              MinInterval *
                                                                1000000,
@@ -472,7 +472,7 @@ normal_state({route, From, Nick,
              #xmlel{name = <<"presence">>} = Packet},
             StateData) ->
     Activity = get_user_activity(From, StateData),
-    Now = now_to_usec(now()),
+    Now = p1_time_compat:system_time(micro_seconds),
     MinPresenceInterval =
        trunc(gen_mod:get_module_opt(StateData#state.server_host,
                                     mod_muc, min_presence_interval,
@@ -1526,7 +1526,7 @@ store_user_activity(JID, UserActivity, StateData) ->
                                     0)
              * 1000),
     Key = jid:tolower(JID),
-    Now = now_to_usec(now()),
+    Now = p1_time_compat:system_time(micro_seconds),
     Activity1 = clean_treap(StateData#state.activity,
                            {1, -Now}),
     Activity = case treap:lookup(Key, Activity1) of
@@ -1997,9 +1997,8 @@ count_stanza_shift(Nick, Els, StateData) ->
     Shift1 = case Seconds of
               false -> 0;
               _ ->
-                  Sec =
-                      calendar:datetime_to_gregorian_seconds(calendar:now_to_universal_time(now()))
-                        - Seconds,
+                  Sec = calendar:datetime_to_gregorian_seconds(calendar:universal_time())
+                         - Seconds,
                   count_seconds_shift(Sec, HL)
             end,
     MaxStanzas = extract_history(Els, <<"maxstanzas">>),
@@ -2284,9 +2283,6 @@ send_existing_presences1(ToJID, StateData) ->
                  end,
                  (?DICT):to_list(StateData#state.nicks)).
 
-now_to_usec({MSec, Sec, USec}) ->
-    (MSec * 1000000 + Sec) * 1000000 + USec.
-
 change_nick(JID, Nick, StateData) ->
     LJID = jid:tolower(JID),
     {ok, #user{nick = OldNick}} = (?DICT):find(LJID,
@@ -2454,7 +2450,7 @@ add_message_to_history(FromNick, FromJID, Packet, StateData) ->
                    false -> false;
                    _ -> true
                  end,
-    TimeStamp = now(),
+    TimeStamp = p1_time_compat:timestamp(),
     AddrPacket = case (StateData#state.config)#config.anonymous of
                   true -> Packet;
                   false ->
@@ -4518,7 +4514,7 @@ handle_roommessage_from_nonparticipant(Packet, Lang,
 
 %% Check in the packet is a decline.
 %% If so, also returns the splitted packet.
-%% This function must be catched, 
+%% This function must be catched,
 %% because it crashes when the packet is not a decline message.
 check_decline_invitation(Packet) ->
     #xmlel{name = <<"message">>} = Packet,
@@ -4546,7 +4542,7 @@ send_decline_invitation({Packet, XEl, DEl, ToJID},
     Packet2 = replace_subelement(Packet, XEl2),
     ejabberd_router:route(RoomJID, ToJID, Packet2).
 
-%% Given an element and a new subelement, 
+%% Given an element and a new subelement,
 %% replace the instance of the subelement in element with the new subelement.
 replace_subelement(#xmlel{name = Name, attrs = Attrs,
                          children = SubEls},
@@ -4630,4 +4626,3 @@ has_body_or_subject(Packet) ->
        (#xmlel{name = <<"subject">>}) -> false;
        (_) -> true
     end, Packet#xmlel.children).
-
index 1c8525f52fef12cdf5796dea2d67f72d7677cc32..bc166b81d35bfa2faa36c26525709a4fe5a5fe96 100644 (file)
@@ -937,8 +937,7 @@ create_cache() ->
                         {attributes, record_info(fields, multicastc)}]).
 
 add_response(RServer, Response) ->
-    Secs =
-       calendar:datetime_to_gregorian_seconds(calendar:now_to_datetime(now())),
+    Secs = calendar:datetime_to_gregorian_seconds(calendar:local_time()),
     mnesia:dirty_write(#multicastc{rserver = RServer,
                                   response = Response, ts = Secs}).
 
@@ -949,8 +948,7 @@ search_server_on_cache(RServer, _LServerS, Maxmins) ->
     case look_server(RServer) of
       not_cached -> not_cached;
       {cached, Response, Ts} ->
-         Now =
-             calendar:datetime_to_gregorian_seconds(calendar:now_to_datetime(now())),
+         Now = calendar:datetime_to_gregorian_seconds(calendar:local_time()),
          case is_obsolete(Response, Ts, Now, Maxmins) of
            false -> {cached, Response};
            true -> {obsolete, Response}
@@ -978,7 +976,7 @@ purge() ->
     Maxmins_positive = (?MAXTIME_CACHE_POSITIVE),
     Maxmins_negative = (?MAXTIME_CACHE_NEGATIVE),
     Now =
-       calendar:datetime_to_gregorian_seconds(calendar:now_to_datetime(now())),
+        calendar:datetime_to_gregorian_seconds(calendar:local_time()),
     purge(Now, {Maxmins_positive, Maxmins_negative}).
 
 purge(Now, Maxmins) ->
index 72db36084ded427678d47ccb9bd31cc457a21a07..9834c4360a944723151723684952cec5ca05f2e7 100644 (file)
@@ -78,8 +78,7 @@ update(Server, JID, Dir) ->
     TimeInterval = gen_mod:get_module_opt(Server, ?MODULE, interval,
                                           fun(I) when is_integer(I), I>0 -> I end,
                                           60),
-    {MegaSecs, Secs, _MicroSecs} = now(),
-    TimeStamp = MegaSecs * 1000000 + Secs,
+    TimeStamp = p1_time_compat:system_time(seconds),
     case read(Dir) of
       undefined ->
          write(Dir,
index c75e128f5e896ed641ef8f204152c2f17296bc2f..eb987a2df216029840e2221c0ebb1d78c75225d6 100644 (file)
@@ -223,7 +223,7 @@ write_roster_version_t(LUser, LServer) ->
     write_roster_version(LUser, LServer, true).
 
 write_roster_version(LUser, LServer, InTransaction) ->
-    Ver = p1_sha:sha(term_to_binary(now())),
+    Ver = p1_sha:sha(term_to_binary(p1_time_compat:unique_integer())),
     write_roster_version(LUser, LServer, InTransaction, Ver,
                         gen_mod:db_type(LServer, ?MODULE)),
     Ver.
@@ -256,7 +256,7 @@ write_roster_version(LUser, LServer, _InTransaction, Ver,
     ejabberd_riak:put(#roster_version{us = US, version = Ver},
                      roster_version_schema()).
 
-%% Load roster from DB only if neccesary. 
+%% Load roster from DB only if neccesary.
 %% It is neccesary if
 %%     - roster versioning is disabled in server OR
 %%     - roster versioning is not used by the client OR
@@ -1391,7 +1391,7 @@ update_roster_table() ->
     end.
 
 %% Convert roster table to support virtual host
-%% Convert roster table: xattrs fields become 
+%% Convert roster table: xattrs fields become
 update_roster_version_table() ->
     Fields = record_info(fields, roster_version),
     case mnesia:table_info(roster_version, attributes) of