Index: channels/chan_sip.c =================================================================== --- channels/chan_sip.c (revision 340107) +++ channels/chan_sip.c (working copy) @@ -14231,6 +14231,7 @@ struct sip_request *req, const char *uri) { enum check_auth_result res = AUTH_NOT_FOUND; + int sendmwi = 0; struct sip_peer *peer; char tmp[256]; char *name = NULL, *c, *domain = NULL, *dummy = NULL; @@ -14311,33 +14312,30 @@ } else { /* We have a successful registration attempt with proper authentication, - now, update the peer */ + now, update the peer */ switch (parse_register_contact(p, peer, req)) { case PARSE_REGISTER_DENIED: ast_log(LOG_WARNING, "Registration denied because of contact ACL\n"); transmit_response_with_date(p, "603 Denied", req); - peer->lastmsgssent = -1; res = 0; break; case PARSE_REGISTER_FAILED: ast_log(LOG_WARNING, "Failed to parse contact info\n"); transmit_response_with_date(p, "400 Bad Request", req); - peer->lastmsgssent = -1; res = 0; break; case PARSE_REGISTER_QUERY: ast_string_field_set(p, fullcontact, peer->fullcontact); transmit_response_with_date(p, "200 OK", req); - peer->lastmsgssent = -1; res = 0; + sendmwi = 1; break; case PARSE_REGISTER_UPDATE: ast_string_field_set(p, fullcontact, peer->fullcontact); update_peer(peer, p->expiry); /* Say OK and ask subsystem to retransmit msg counter */ transmit_response_with_date(p, "200 OK", req); - if (!ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY)) - peer->lastmsgssent = -1; + sendmwi = 1; res = 0; break; } @@ -14362,19 +14360,17 @@ case PARSE_REGISTER_DENIED: ast_log(LOG_WARNING, "Registration denied because of contact ACL\n"); transmit_response_with_date(p, "403 Forbidden (ACL)", req); - peer->lastmsgssent = -1; res = 0; break; case PARSE_REGISTER_FAILED: ast_log(LOG_WARNING, "Failed to parse contact info\n"); transmit_response_with_date(p, "400 Bad Request", req); - peer->lastmsgssent = -1; res = 0; break; case PARSE_REGISTER_QUERY: ast_string_field_set(p, fullcontact, peer->fullcontact); transmit_response_with_date(p, "200 OK", req); - peer->lastmsgssent = -1; + sendmwi = 1; res = 0; break; case PARSE_REGISTER_UPDATE: @@ -14382,8 +14378,8 @@ /* Say OK and ask subsystem to retransmit msg counter */ transmit_response_with_date(p, "200 OK", req); manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Registered\r\nAddress: %s\r\n", peer->name, ast_sockaddr_stringify(addr)); - peer->lastmsgssent = -1; res = 0; + sendmwi = 1; break; } ao2_unlock(peer); @@ -14398,6 +14394,9 @@ sched_yield(); } if (!res) { + if (!sendmwi || sip_send_mwi_to_peer(peer, 0)) { + peer->lastmsgssent = -1; + } ast_devstate_changed(AST_DEVICE_UNKNOWN, "SIP/%s", peer->name); } if (res < 0) { @@ -24995,25 +24994,33 @@ return in_cache; } -/*! \brief Send message waiting indication to alert peer that they've got voicemail */ +/*! \brief Send message waiting indication to alert peer that they've got voicemail + * \returns -1 on failure, 0 on success + */ static int sip_send_mwi_to_peer(struct sip_peer *peer, int cache_only) { /* Called with peerl lock, but releases it */ struct sip_pvt *p; int newmsgs = 0, oldmsgs = 0; - if (ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY) && !peer->mwipvt) - return 0; + if (ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY) && !peer->mwipvt) { + return -1; + } /* Do we have an IP address? If not, skip this peer */ - if (ast_sockaddr_isnull(&peer->addr) && ast_sockaddr_isnull(&peer->defaddr)) - return 0; + if (ast_sockaddr_isnull(&peer->addr) && ast_sockaddr_isnull(&peer->defaddr)) { + return -1; + } /* Attempt to use cached mwi to get message counts. */ if (!get_cached_mwi(peer, &newmsgs, &oldmsgs) && !cache_only) { /* Fall back to manually checking the mailbox if not cache_only and get_cached_mwi failed */ struct ast_str *mailbox_str = ast_str_alloca(512); peer_mailboxes_to_str(&mailbox_str, peer); + /* if there is no mailbox do nothing */ + if (ast_strlen_zero(mailbox_str->str)) { + return -1; + } ast_app_inboxcount(mailbox_str->str, &newmsgs, &oldmsgs); } ao2_lock(peer); @@ -25039,7 +25046,7 @@ dialog_unref(p, "unref dialog p just created via sip_alloc"); /* sip_destroy(p); */ ao2_unlock(peer); - return 0; + return -1; } /* Recalculate our side, and recalculate Call ID */ ast_sip_ouraddrfor(&p->sa, &p->ourip, p); @@ -25061,6 +25068,7 @@ sip_pvt_lock(p); /* Send MWI */ + peer->lastmsgssent = ((newmsgs > 0x7fff ? 0x7fff0000 : (newmsgs << 16)) | (oldmsgs > 0xffff ? 0xffff : oldmsgs)); ast_set_flag(&p->flags[0], SIP_OUTGOING); /* the following will decrement the refcount on p as it finishes */ transmit_notify_with_mwi(p, newmsgs, oldmsgs, peer->vmexten);