int imc_handle_destroy(struct sip_msg* msg, imc_cmd_t *cmd, struct sip_uri *src, struct sip_uri *dst) { imc_room_p room = 0; imc_member_p member = 0; str room_name; str body; /* distrugere camera */ room_name = cmd->param[0].s?cmd->param[0]:dst->user; room= imc_get_room(&room_name, &dst->host); if(room== NULL || (room->flags&IMC_ROOM_DELETED)) { LM_ERR("room [%.*s] does not exist!\n", room_name.len, room_name.s); goto error; } /* verify is the user is a member of the room*/ member= imc_get_member(room, &src->user, &src->host); if(member== NULL) { LM_ERR("user [%.*s] is not a member of room [%.*s]!\n", src->user.len, src->user.s, room_name.len, room_name.s); goto error; } if(!(member->flags & IMC_MEMBER_OWNER)) { LM_ERR("user [%.*s] is not owner of room [%.*s] -- cannot destroy it" "!\n", src->user.len, src->user.s, room_name.len, room_name.s); goto error; } room->flags |= IMC_ROOM_DELETED; body.s = imc_body_buf; strcpy(body.s, "The room has been destroyed"); body.len = strlen(body.s); /* braodcast message */ imc_room_broadcast(room, &all_hdrs, &body); imc_release_room(room); LM_DBG("deleting room\n"); imc_del_room(&room_name, &dst->host); return 0; error: if(room!=NULL) imc_release_room(room); return -1; }
int imc_handle_message(struct sip_msg* msg, str *msgbody, struct sip_uri *src, struct sip_uri *dst) { imc_room_p room = 0; imc_member_p member = 0; str body; room = imc_get_room(&dst->user, &dst->host); if(room==NULL || (room->flags&IMC_ROOM_DELETED)) { LM_ERR("room [%.*s] does not exist!\n", dst->user.len, dst->user.s); goto error; } member= imc_get_member(room, &src->user, &src->host); if(member== NULL || (member->flags & IMC_MEMBER_INVITED)) { LM_ERR("user [%.*s] has no rights to send messages in room [%.*s]!\n", src->user.len, src->user.s, dst->user.len, dst->user.s); goto error; } LM_DBG("broadcast to room [%.*s]\n", room->uri.len, room->uri.s); body.s = imc_body_buf; body.len = msgbody->len + member->uri.len /* -4 (sip:) +4 (<>: ) */; if(body.len>=IMC_BUF_SIZE) { LM_ERR("buffer overflow [%.*s]\n", msgbody->len, msgbody->s); goto error; } body.s[0] = '<'; memcpy(body.s + 1, member->uri.s + 4, member->uri.len - 4); memcpy(body.s + 1 + member->uri.len - 4, ">: ", 3); memcpy(body.s + 1 + member->uri.len - 4 +3, msgbody->s, msgbody->len); body.s[body.len] = '\0'; member->flags |= IMC_MEMBER_SKIP; imc_room_broadcast(room, &all_hdrs, &body); member->flags &= ~IMC_MEMBER_SKIP; imc_release_room(room); return 0; error: if(room!=NULL) imc_release_room(room); return -1; }
int imc_handle_accept(struct sip_msg* msg, imc_cmd_t *cmd, struct sip_uri *src, struct sip_uri *dst) { imc_room_p room = 0; imc_member_p member = 0; str room_name; str body; /* accepting the invitation */ room_name = cmd->param[0].s?cmd->param[0]:dst->user; room=imc_get_room(&room_name, &dst->host); if(room== NULL || (room->flags&IMC_ROOM_DELETED)) { LM_ERR("room [%.*s] is not created!\n", room_name.len, room_name.s); goto error; } /* if aready invited add as a member */ member=imc_get_member(room, &src->user, &src->host); if(member==NULL || !(member->flags & IMC_MEMBER_INVITED)) { LM_ERR("user [%.*s] not invited in the room!\n", src->user.len, src->user.s); goto error; } member->flags &= ~IMC_MEMBER_INVITED; /* send info message */ body.s = imc_body_buf; body.len = snprintf(body.s, IMC_BUF_SIZE, "*** <%.*s> has joined the room", member->uri.len, member->uri.s); if(body.len>0) imc_room_broadcast(room, &all_hdrs, &body); if(body.len>=IMC_BUF_SIZE) LM_ERR("member name %.*s truncated\n", member->uri.len, member->uri.s); imc_release_room(room); return 0; error: if(room!=NULL) imc_release_room(room); return -1; }
int imc_handle_exit(struct sip_msg* msg, imc_cmd_t *cmd, struct sip_uri *src, struct sip_uri *dst) { imc_room_p room = 0; imc_member_p member = 0; str room_name; str body; /* the user wants to leave the room */ room_name = cmd->param[0].s?cmd->param[0]:dst->user; room= imc_get_room(&room_name, &dst->host); if(room== NULL || (room->flags&IMC_ROOM_DELETED)) { LM_ERR("room [%.*s] does not exist!\n", room_name.len, room_name.s); goto error; } /* verify if the user is a member of the room */ member= imc_get_member(room, &src->user, &src->host); if(member== NULL) { LM_ERR("user [%.*s] is not member of room [%.*s]!\n", src->user.len, src->user.s, room_name.len, room_name.s); goto error; } if(member->flags & IMC_MEMBER_OWNER) { /*If the user is the owner of the room, the room is distroyed */ room->flags |=IMC_ROOM_DELETED; body.s = imc_body_buf; strcpy(body.s, "The room has been destroyed"); body.len = strlen(body.s); imc_room_broadcast(room, &all_hdrs, &body); imc_release_room(room); imc_del_room(&room_name, &dst->host); room = NULL; goto done; } else { /* delete user */ member->flags |= IMC_MEMBER_DELETED; imc_del_member(room, &src->user, &src->host); body.s = imc_body_buf; body.len = snprintf(body.s, IMC_BUF_SIZE, "The user [%.*s] has left the room", src->user.len, src->user.s); if(body.len>0) imc_room_broadcast(room, &all_hdrs, &body); if(body.len>=IMC_BUF_SIZE) LM_ERR("user name %.*s truncated\n", src->user.len, src->user.s); } done: if(room!=NULL) imc_release_room(room); return 0; error: if(room!=NULL) imc_release_room(room); return -1; }
int imc_handle_remove(struct sip_msg* msg, imc_cmd_t *cmd, struct sip_uri *src, struct sip_uri *dst) { imc_room_p room = 0; imc_member_p member = 0; str room_name; str body; str uri = {0, 0}; int size =0; int i = 0; int add_domain = 0; int add_sip = 0; struct sip_uri inv_uri; size= cmd->param[0].len+2; add_domain = 1; while (i<size ) { if(cmd->param[0].s[i]== '@') { add_domain =0; break; } i++; } if(add_domain) size += dst->host.len; if(cmd->param[0].len<=4 || strncmp(cmd->param[0].s, "sip:", 4)!=0) { size+= 4; add_sip = 1; } uri.s = (char*)pkg_malloc(size*sizeof(char)); if(uri.s == NULL) { LM_ERR("no more pkg memory\n"); goto error; } size= 0; if(add_sip) { strcpy(uri.s, "sip:"); size = 4; } memcpy(uri.s+size, cmd->param[0].s, cmd->param[0].len); size+= cmd->param[0].len; if(add_domain) { uri.s[size] = '@'; size++; memcpy(uri.s+size, dst->host.s, dst->host.len); size+= dst->host.len; } uri.len = size; if(parse_uri(uri.s, uri.len, &inv_uri)<0) { LM_ERR("invalid uri [%.*s]\n", uri.len, uri.s); goto error; } room_name = cmd->param[1].s?cmd->param[1]:dst->user; room= imc_get_room(&room_name, &dst->host); if(room==NULL || (room->flags&IMC_ROOM_DELETED)) { LM_ERR("room [%.*s]does not exist!\n", room_name.len, room_name.s); goto error; } /* verify if the user who sent the request is a member in the room * and has the right to remove other users */ member= imc_get_member(room, &src->user, &src->host); if(member== NULL) { LM_ERR("user [%.*s] is not member of room [%.*s]!\n", src->user.len, src->user.s, room_name.len, room_name.s); goto error; } if(!(member->flags & IMC_MEMBER_OWNER) && !(member->flags & IMC_MEMBER_ADMIN)) { LM_ERR("user [%.*s] has no right to remove other users [%.*s]!\n", src->user.len, src->user.s, uri.len, uri.s); goto error; } /* verify if the user that is to be removed is a member of the room */ member= imc_get_member(room, &inv_uri.user, &inv_uri.host); if(member== NULL) { LM_ERR("user [%.*s] is not member of room [%.*s]!\n", inv_uri.user.len, inv_uri.user.s, room_name.len, room_name.s); goto error; } if(member->flags & IMC_MEMBER_OWNER) { LM_ERR("user [%.*s] is owner of room [%.*s]" " -- cannot be removed!\n", inv_uri.user.len, inv_uri.user.s, room_name.len, room_name.s); goto error; } /* send message to the removed person */ body.s = "You have been removed from this room"; body.len = strlen(body.s); LM_DBG("to: [%.*s]\nfrom: [%.*s]\nbody: [%.*s]\n", member->uri.len, member->uri.s , room->uri.len, room->uri.s, body.len, body.s); imc_send_message(&room->uri, &member->uri, &all_hdrs, &body); member->flags |= IMC_MEMBER_DELETED; imc_del_member(room, &inv_uri.user, &inv_uri.host); body.s = imc_body_buf; body.len = snprintf(body.s, IMC_BUF_SIZE, "*** <%.*s> has joined the room", member->uri.len, member->uri.s); if(body.len>0) imc_room_broadcast(room, &all_hdrs, &body); if(body.len>=IMC_BUF_SIZE) LM_ERR("member name %.*s truncated\n", member->uri.len, member->uri.s); if(uri.s!=0) pkg_free(uri.s); imc_release_room(room); return 0; error: if(uri.s!=0) pkg_free(uri.s); if(room!=NULL) imc_release_room(room); return -1; }
int imc_handle_join(struct sip_msg* msg, imc_cmd_t *cmd, struct sip_uri *src, struct sip_uri *dst) { imc_room_p room = 0; imc_member_p member = 0; int flag_room = 0; int flag_member = 0; str room_name; str body; room_name = cmd->param[0].s?cmd->param[0]:dst->user; 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", room_name.len, room_name.s); room= imc_add_room(&room_name, &dst->host, flag_room); if(room == NULL) { LM_ERR("failed to add new room [%.*s]\n", room_name.len, room_name.s); goto error; } LM_DBG("created a new room [%.*s]\n", room->name.len, room->name.s); 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", src->user.len, src->user.s); goto error; } /* send info message */ body.s = "*** room was created"; body.len = sizeof("*** room was created")-1; imc_send_message(&room->uri, &member->uri, &all_hdrs, &body); goto done; } /* room exists */ LM_DBG("found room [%.*s]\n", room_name.len, room_name.s); member= imc_get_member(room, &src->user, &src->host); if(!(room->flags & IMC_ROOM_PRIV)) { LM_DBG("room [%.*s] is public\n", room_name.len, room_name.s); if(member== NULL) { LM_DBG("adding new member [%.*s]\n", src->user.len, src->user.s); member= imc_add_member(room, &src->user, &src->host, flag_member); if(member == NULL) { LM_ERR("adding new user [%.*s]\n", src->user.len, src->user.s); goto error; } goto build_inform; } else { LM_DBG("member [%.*s] is in room already\n", member->uri.len,member->uri.s ); } } else { if(member==NULL) { LM_ERR("attept to join private room [%.*s] from user [%.*s]\n", room_name.len, room_name.s, src->user.len, src->user.s); goto build_inform; } if(member->flags & IMC_MEMBER_INVITED) member->flags &= ~IMC_MEMBER_INVITED; } build_inform: /* send info message */ body.s = imc_body_buf; if(member!=NULL) { body.len = snprintf(body.s, IMC_BUF_SIZE, "*** <%.*s@%.*s> has joined the room", src->user.len, src->user.s, src->host.len, src->host.s); } else { body.len = snprintf(body.s, IMC_BUF_SIZE, "*** <%.*s@%.*s> attempted to join the room", src->user.len, src->user.s, src->host.len, src->host.s); } if(body.len>0) imc_room_broadcast(room, &all_hdrs, &body); if(body.len>=IMC_BUF_SIZE) LM_ERR("member name %.*s@%.*s truncated\n", src->user.len, src->user.s, src->host.len, src->host.s); done: if(room!=NULL) imc_release_room(room); return 0; error: if(room!=NULL) imc_release_room(room); return -1; }
int imc_handle_create(struct sip_msg* msg, imc_cmd_t *cmd, struct sip_uri *src, struct sip_uri *dst) { imc_room_p room = 0; imc_member_p member = 0; int flag_room = 0; int flag_member = 0; str body; room = imc_get_room(&cmd->param[0], &dst->host); if(room== NULL) { LM_DBG("new room [%.*s]\n", cmd->param[0].len, cmd->param[0].s); if(cmd->param[1].len==IMC_ROOM_PRIVATE_LEN && !strncasecmp(cmd->param[1].s, IMC_ROOM_PRIVATE, cmd->param[1].len)) { flag_room |= IMC_ROOM_PRIV; LM_DBG("room with private flag on\n"); } room= imc_add_room(&cmd->param[0], &dst->host, flag_room); if(room == NULL) { LM_ERR("failed to add new room\n"); goto error; } LM_DBG("added room uri= %.*s\n", room->uri.len, room->uri.s); flag_member |= IMC_MEMBER_OWNER; /* adding the owner as the forst member*/ member= imc_add_member(room, &src->user, &src->host, flag_member); if(member == NULL) { LM_ERR("failed to add owner [%.*s]\n", src->user.len, src->user.s); goto error; } LM_DBG("added the owner as the first member " "[%.*s]\n",member->uri.len, member->uri.s); /* send info message */ body.s = "*** room was created"; body.len = sizeof("*** room was created")-1; imc_send_message(&room->uri, &member->uri, &all_hdrs, &body); goto done; } /* room already exists */ LM_DBG("room [%.*s] already created\n", cmd->param[0].len, cmd->param[0].s); if(!(room->flags & IMC_ROOM_PRIV)) { LM_DBG("checking if the user [%.*s] is a member\n", src->user.len, src->user.s); member= imc_get_member(room, &src->user, &src->host); if(member== NULL) { member= imc_add_member(room, &src->user, &src->host, flag_member); if(member == NULL) { LM_ERR("failed to add member [%.*s]\n", src->user.len, src->user.s); goto error; } LM_DBG("added as member [%.*s]\n",member->uri.len, member->uri.s); /* send info message */ body.s = imc_body_buf; body.len = snprintf(body.s, IMC_BUF_SIZE, "*** <%.*s> has joined the room", member->uri.len, member->uri.s); if(body.len>0) imc_room_broadcast(room, &all_hdrs, &body); if(body.len>=IMC_BUF_SIZE) LM_ERR("member name %.*s truncated\n", member->uri.len, member->uri.s); } } done: if(room!=NULL) imc_release_room(room); return 0; error: if(room!=NULL) imc_release_room(room); return -1; }
int imc_handle_message(struct sip_msg* msg, str *msgbody, struct sip_uri *src, struct sip_uri *dst, str *fullname) { imc_room_p room = 0; imc_member_p member = 0; str body; room = imc_get_room(&dst->user, &dst->host); if(room==NULL || (room->flags&IMC_ROOM_DELETED)) { LM_ERR("room [%.*s] does not exist!\n", dst->user.len, dst->user.s); goto error; } member= imc_get_member(room, &src->user, &src->host); if(member== NULL || (member->flags & IMC_MEMBER_INVITED)) { LM_ERR("user [%.*s] has no rights to send messages in room [%.*s]!\n", src->user.len, src->user.s, dst->user.len, dst->user.s); goto error; } LM_DBG("broadcast to room [%.*s]\n", room->uri.len, room->uri.s); body.s = imc_body_buf; if(!use_from_displayname) { /* use name from table */ body.len = msgbody->len + member->fullname.len + 4 /* +4 []:_ */; } else { /* use name from SIP message */ body.len = msgbody->len + fullname->len + 4 /* +4 []:_ */; } if(body.len>=IMC_BUF_SIZE) { LM_ERR("buffer overflow [%.*s]\n", msgbody->len, msgbody->s); goto error; } LM_DBG("Is from DB: [%.*s], could be from user [%.*s]\n", member->fullname.len, member->fullname.s, fullname->len, fullname->s); body.s[0] = '['; /* TODO Use URI if no name available, can always be from IMC members as each user must be member and has a URI */ if(!use_from_displayname) { memcpy(body.s + 1, member->fullname.s, member->fullname.len); memcpy(body.s + 1 + member->fullname.len, "]: ", 3); memcpy(body.s + 1 + member->fullname.len + 3, msgbody->s, msgbody->len); } else { memcpy(body.s + 1, fullname->s, fullname->len); memcpy(body.s + 1 + fullname->len, "]: ", 3); memcpy(body.s + 1 + fullname->len + 3, msgbody->s, msgbody->len); } body.s[body.len] = '\0'; member->flags |= IMC_MEMBER_SKIP; imc_room_broadcast(room, &imc_hdr_ctype, &body); member->flags &= ~IMC_MEMBER_SKIP; imc_release_room(room); return 0; error: if(room!=NULL) imc_release_room(room); return -1; }