--- chan_sip.c.orig 2005-04-01 12:34:59.000000000 -0700 +++ chan_sip.c 2005-05-24 14:30:32.000000000 -0600 @@ -486,6 +486,8 @@ char useragent[256]; /* User agent in SIP request (saved from registration) */ struct ast_codec_pref prefs; /* codec prefs */ int lastmsgssent; + int *lastmsgsent_switch; /* maintaining last message sent for EPS on a Third Party Softswitch */ + time_t lastmsgcheck; /* Last time we checked for MWI */ unsigned int flags; /* SIP_ flags */ struct ast_flags flags_page2; /* SIP_PAGE2 flags */ @@ -1202,10 +1204,12 @@ if (!var) return NULL; - + tmp = var; + /* If this is type=user, then skip this object. */ while(tmp) { + if (!strcasecmp(tmp->name, "type") && !strcasecmp(tmp->value, "user")) { ast_variables_destroy(var); @@ -3923,6 +3927,9 @@ } else { snprintf(invite, sizeof(invite), "sip:%s%s", p->tohost, urioptions); } + + + strncpy(p->uri, invite, sizeof(p->uri) - 1); /* If there is a VXML URL append it to the SIP URL */ if (vxml_url) @@ -4207,6 +4214,61 @@ return send_request(p, req, 0, p->ocseq); } +/*--- transmit_notify_to_softswitch_with_mwi: Notify Softswitch user of + messages waiting in voicemail ---*/ +/* Notification works for Softswitch peers with mailbox= definitions + * in sip.conf + * We use the SIP Event package message-summary + * MIME type defaults to "application/simple-message-summary"; + */ +static int transmit_notify_to_softswitch_with_mwi(struct sip_pvt *p, char *mbox, int newmsgs, int oldmsgs) +{ + struct sip_request req; + char tmp[256]; + char tmp2[256]; + char *tmp_mbox; + char clen[20]; + + /* I will put the Mailbox Number - the part before the @ + * in p->username, and then call initreqprep as initreqprep + * looks for a p->username and puts it in TO Header + */ + + tmp_mbox = (char*) calloc(strlen(mbox), sizeof(char)); + + strncpy(tmp_mbox, mbox, strlen(mbox)); + + tmp_mbox = strsep(&tmp_mbox, "@"); + + strncpy(p->username, tmp_mbox, strlen(tmp_mbox)); + + + + initreqprep(&req, p, "NOTIFY", NULL); + + add_header(&req, "Event", "message-summary"); + add_header(&req, "Content-Type", default_notifymime); + + snprintf(tmp, sizeof(tmp), "Messages-Waiting: %s\r\n", newmsgs ? "yes" : "no"); + snprintf(tmp2, sizeof(tmp2), "Voice-Message: %d/%d\r\n", newmsgs, oldmsgs); + snprintf(clen, sizeof(clen), "%d", (int)(strlen(tmp) + strlen(tmp2))); + add_header(&req, "Content-Length", clen); + add_line(&req, tmp); + add_line(&req, tmp2); + + if (!p->initreq.headers) { + /* Use this as the basis */ + copy_request(&p->initreq, &req); + parse(&p->initreq); + if (sip_debug_test_pvt(p)) + ast_verbose("%d headers, %d lines\n", p->initreq.headers, p->initreq.lines); + determine_firstline_parts(&p->initreq); + } + + return send_request(p, &req, 1, p->ocseq); +} + + /*--- transmit_notify_with_sipfrag: Notify a transferring party of the status of trasnfer ---*/ /* Apparently the draft SIP REFER structure was too simple, so it was decided that the * status of transfers also needed to be sent via NOTIFY instead of just the 202 Accepted @@ -8563,6 +8625,113 @@ return 1; } +/* --- sip_send_mwi_to_softswitch : Sending MWI to another Softswitch.---*/ +static int sip_send_mwi_to_softswitch(struct sip_peer *peer) +{ + /* Checking for all the malboxes configured for a softswitch + and sending NOTIFY w/MWI for all */ + struct sip_pvt *p; + char name[256] = ""; + int newmsgs, oldmsgs; + + if(peer->mailbox==NULL){ + ast_log(LOG_WARNING, "No Mailboxes defined in sip.conf and voicemail.conf for this softswitch"); + return 0; + } + + int i; + int mbox_count=0; + char *mbox[500]; /*Pointer to individual malboxes*/ + char *tmp=NULL; /*Temp Pointer to individual mailboxes*/ + char *mailboxes;/* storing full comma separated mailbox string here */ + + mailboxes=(char *)calloc(strlen(peer->mailbox),sizeof(char)); + + strcpy(mailboxes,peer->mailbox); + + + tmp=strchr(mailboxes,','); + //**mbox=(char **)malloc(sizeof(char *)*256); + //This copies first Mailbox in mbox[0] + mbox[mbox_count]=mailboxes; + mbox[mbox_count]=strsep(&mbox[mbox_count], ","); + + //This copies rest of the mailboxes in mbox[1, 2, 3......] + while(tmp!=NULL) + { + mbox_count++; + tmp+=1; + mbox[mbox_count]=(char*) calloc(strlen(tmp), sizeof(char)); + strncpy(mbox[mbox_count],tmp, strlen(tmp)); + mbox[mbox_count]=strsep(&mbox[mbox_count], ","); + tmp=strchr(tmp,','); + } + + + /* Allocating memory for PTR to last msg for each mailbox for EPs on Softswitch */ + if (peer->lastmsgsent_switch == NULL) { + peer->lastmsgsent_switch = (int *) calloc(mbox_count,sizeof(int)); + for (i=0;i<=mbox_count;i++) + peer->lastmsgsent_switch[i]=-1; + } + + + //Now looping through all the Mailboxes to send individual Notify's + for(i=0;i<=mbox_count;i++) { + + + ast_app_messagecount(mbox[i], &newmsgs, &oldmsgs); + + time(&peer->lastmsgcheck); + + /* Return now if it's the same thing we told them last time */ + if (((newmsgs << 8) | (oldmsgs)) == peer->lastmsgsent_switch[i]) { + if (i==mbox_count) + return 0; + else + continue; + } + + p = sip_alloc(NULL, NULL, 0); + if (!p) { + ast_log(LOG_WARNING, "Unable to build sip pvt data for MWI\n"); + return -1; + } + strncpy(name, peer->name, sizeof(name) - 1); + peer->lastmsgsent_switch[i] = ((newmsgs << 8) | (oldmsgs)); + if (create_addr(p, name)) { + /* Maybe they're not registered, etc. */ + sip_destroy(p); + return 0; + } + /* Recalculate our side, and recalculate Call ID */ + if (ast_sip_ouraddrfor(&p->sa.sin_addr,&p->ourip)) + memcpy(&p->ourip, &__ourip, sizeof(p->ourip)); + build_via(p, p->via, sizeof(p->via)); + build_callid(p->callid, sizeof(p->callid), p->ourip, p->fromdomain); + /* Here I call another Function to Build Proper to header */ + /* Send MWI */ + ast_set_flag(p, SIP_OUTGOING); + + /*Set the p->peername pointer to char to NULL if its length is 0 */ + + if (!strlen(p->peername)){ + strncpy(p->peername, peer->name, sizeof(p->peername)); + } + + + transmit_notify_to_softswitch_with_mwi(p, mbox[i], newmsgs, oldmsgs); + sip_scheddestroy(p, 15000); + } + + + + return 0; + +} + + + /*--- sip_send_mwi_to_peer: Send message waiting indication ---*/ static int sip_send_mwi_to_peer(struct sip_peer *peer) { @@ -8571,7 +8740,14 @@ char name[256] = ""; int newmsgs, oldmsgs; - /* Check for messages */ + /* Here we call function to separate all the mailboxes if tohost ne NULL and + then call the below func - sip_send_mwi_to_softswitch - for each of those mailboxes */ + + if (strlen(peer->tohost) != 0){ + sip_send_mwi_to_softswitch(peer); + return 0; + } + ast_app_messagecount(peer->mailbox, &newmsgs, &oldmsgs); time(&peer->lastmsgcheck);