Skip to content

Commit

Permalink
imc: make behavior of "#join" when room does not exist configurable
Browse files Browse the repository at this point in the history
Before, the command "#join" would automatically create the room and
add the user to the room if it does not exist. That behavior can be
confusing, especially on mobile where it is easy to miss-type room
name. With the default behavior, the user would be added to the wrong
room in that case.

This patch introduces module parameter "create_on_join" which makes
this behavior configurable. If set to 0, command join will not create
the room if it does not exist and would send an error back to the user
instead.
  • Loading branch information
janakj authored and miconda committed Feb 26, 2019
1 parent b66870a commit 4e2a368
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
22 changes: 21 additions & 1 deletion src/modules/imc/doc/imc_admin.xml
Expand Up @@ -213,7 +213,27 @@ modparam("imc", "extra_hdrs", "P-Flags: 3\r\n")
</programlisting>
</example>
</section>

<section>
<title><varname>create_on_join</varname> (integer)</title>
<para>
If set to 1 and user requests to join a non-existing
room, the room will be automatically created. If set
to 0, joinin a non-existing room returns an error.
</para>
<para>
<emphasis>
The default value is 1.
</emphasis>
</para>
<example>
<title>Set <varname>create_on_join</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("imc", "create_on_join", 0)
...
</programlisting>
</example>
</section>
</section>
<section>
<title>Functions</title>
Expand Down
2 changes: 2 additions & 0 deletions src/modules/imc/imc.c
Expand Up @@ -81,6 +81,7 @@ int imc_hash_size = 4;
str imc_cmd_start_str = str_init(IMC_CMD_START_STR);
char imc_cmd_start_char;
str extra_hdrs = {NULL, 0};
int imc_create_on_join = 1;

/** module functions */
static int mod_init(void);
Expand Down Expand Up @@ -112,6 +113,7 @@ static param_export_t params[]={
{"members_table", PARAM_STR, &members_table},
{"outbound_proxy", PARAM_STR, &outbound_proxy},
{"extra_hdrs", PARAM_STR, &extra_hdrs},
{"create_on_join", INT_PARAM, &imc_create_on_join},
{0,0,0}
};

Expand Down
1 change: 1 addition & 0 deletions src/modules/imc/imc.h
Expand Up @@ -34,5 +34,6 @@ extern struct tm_binds tmb;
extern str outbound_proxy;
extern str all_hdrs;
extern str extra_hdrs;
extern int imc_create_on_join;

#endif
39 changes: 23 additions & 16 deletions src/modules/imc/imc_cmd.c
Expand Up @@ -43,6 +43,7 @@ static str imc_msg_type = { "MESSAGE", 7 };

static str msg_room_created = STR_STATIC_INIT("*** room was created");
static str msg_room_destroyed = STR_STATIC_INIT("*** The room has been destroyed");
static str msg_room_not_found = STR_STATIC_INIT("*** Room not found");
static str msg_user_joined = STR_STATIC_INIT("*** <%.*s> has joined the room");
static str msg_user_joined2 = STR_STATIC_INIT("*** <%.*s@%.*s> has joined the room");
static str msg_user_left = STR_STATIC_INIT("*** <%.*s> has left the room");
Expand Down Expand Up @@ -271,24 +272,30 @@ int imc_handle_join(struct sip_msg* msg, imc_cmd_t *cmd,
room=imc_get_room(&room_name, &dst->host);
if(room== NULL || (room->flags&IMC_ROOM_DELETED))
{
LM_DBG("could not find room [%.*s]- adding\n", STR_FMT(&room_name));
room= imc_add_room(&room_name, &dst->host, flag_room);
if(room == NULL)
{
LM_ERR("failed to add new room [%.*s]\n", STR_FMT(&room_name));
goto error;
}
LM_DBG("created a new room [%.*s]\n", STR_FMT(&room->name));
LM_DBG("could not find room [%.*s]\n", STR_FMT(&room_name));

flag_member |= IMC_MEMBER_OWNER;
member= imc_add_member(room, &src->user, &src->host, flag_member);
if(member == NULL)
{
LM_ERR("failed to add new member [%.*s]\n", STR_FMT(&src->user));
goto error;
if (imc_create_on_join) {
LM_DBG("Creating room [%.*s]\n", STR_FMT(&room_name));
room= imc_add_room(&room_name, &dst->host, flag_room);
if(room == NULL)
{
LM_ERR("failed to add new room [%.*s]\n", STR_FMT(&room_name));
goto error;
}
LM_DBG("created a new room [%.*s]\n", STR_FMT(&room->name));

flag_member |= IMC_MEMBER_OWNER;
member= imc_add_member(room, &src->user, &src->host, flag_member);
if(member == NULL)
{
LM_ERR("failed to add new member [%.*s]\n", STR_FMT(&src->user));
goto error;
}
/* send info message */
imc_send_message(&room->uri, &member->uri, &all_hdrs, &msg_room_created);
} else {
imc_send_message(&room->uri, &member->uri, &all_hdrs, &msg_room_not_found);
}
/* send info message */
imc_send_message(&room->uri, &member->uri, &all_hdrs, &msg_room_created);
goto done;
}

Expand Down

0 comments on commit 4e2a368

Please sign in to comment.