Make SIP message notify send detail MWI info
authorMark Spencer <markster@digium.com>
Wed, 26 Mar 2003 00:15:11 +0000 (00:15 +0000)
committerMark Spencer <markster@digium.com>
Wed, 26 Mar 2003 00:15:11 +0000 (00:15 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@688 65c4cc65-6c06-0410-ace0-fbb531ad65f3

app.c
channels/chan_sip.c
include/asterisk/app.h

diff --git a/app.c b/app.c
index 192b9d9..81f31ca 100755 (executable)
--- a/app.c
+++ b/app.c
@@ -70,7 +70,7 @@ int ast_app_getvoice(struct ast_channel *c, char *dest, char *dstfmt, char *prom
        int res;
        struct ast_filestream *writer;
        int rfmt;
-       int totalms, total;
+       int totalms=0, total;
        
        struct ast_frame *f;
        struct ast_dsp *sildet;
@@ -141,7 +141,7 @@ int ast_app_getvoice(struct ast_channel *c, char *dest, char *dstfmt, char *prom
        return 0;
 }
 
-int ast_app_has_voicemail(char *mailbox)
+int ast_app_has_voicemail(const char *mailbox)
 {
        DIR *dir;
        struct dirent *de;
@@ -177,3 +177,66 @@ int ast_app_has_voicemail(char *mailbox)
                return 1;
        return 0;
 }
+
+int ast_app_messagecount(const char *mailbox, int *newmsgs, int *oldmsgs)
+{
+       DIR *dir;
+       struct dirent *de;
+       char fn[256];
+       char tmp[256]="";
+       char *mb, *cur;
+       int ret;
+       if (newmsgs)
+               *newmsgs = 0;
+       if (oldmsgs)
+               *oldmsgs = 0;
+       /* If no mailbox, return immediately */
+       if (!strlen(mailbox))
+               return 0;
+       if (strchr(mailbox, ',')) {
+               int tmpnew, tmpold;
+               strncpy(tmp, mailbox, sizeof(tmp));
+               mb = tmp;
+               ret = 0;
+               while((cur = strsep(&mb, ", "))) {
+                       if (strlen(cur)) {
+                               if (ast_app_messagecount(cur, newmsgs ? &tmpnew : NULL, oldmsgs ? &tmpold : NULL))
+                                       return -1;
+                               else {
+                                       if (newmsgs)
+                                               *newmsgs += tmpnew; 
+                                       if (oldmsgs)
+                                               *oldmsgs += tmpold;
+                               }
+                       }
+               }
+               return 0;
+       }
+       if (newmsgs) {
+               snprintf(fn, sizeof(fn), "%s/vm/%s/INBOX", (char *)ast_config_AST_SPOOL_DIR, mailbox);
+               dir = opendir(fn);
+               if (dir) {
+                       while ((de = readdir(dir))) {
+                               if ((strlen(de->d_name) > 3) && !strncasecmp(de->d_name, "msg", 3) &&
+                                       !strcasecmp(de->d_name + strlen(de->d_name) - 3, "txt"))
+                                               *newmsgs++;
+                                       
+                       }
+                       closedir(dir);
+               }
+       }
+       if (oldmsgs) {
+               snprintf(fn, sizeof(fn), "%s/vm/%s/Old", (char *)ast_config_AST_SPOOL_DIR, mailbox);
+               dir = opendir(fn);
+               if (dir) {
+                       while ((de = readdir(dir))) {
+                               if ((strlen(de->d_name) > 3) && !strncasecmp(de->d_name, "msg", 3) &&
+                                       !strcasecmp(de->d_name + strlen(de->d_name) - 3, "txt"))
+                                               *oldmsgs++;
+                                       
+                       }
+                       closedir(dir);
+               }
+       }
+       return 0;
+}
index 2582a1b..3ec1a29 100755 (executable)
@@ -2027,19 +2027,22 @@ static int transmit_invite(struct sip_pvt *p, char *cmd, int sdp, char *auth, ch
        return send_request(p, &req);
 }
 
-static int transmit_notify(struct sip_pvt *p, int hasmsgs)
+static int transmit_notify(struct sip_pvt *p, int newmsgs, int oldmsgs)
 {
        struct sip_request req;
        char tmp[256];
+       char tmp2[256];
        char clen[20];
        initreqprep(&req, p, "NOTIFY", NULL);
        add_header(&req, "Event", "message-summary");
        add_header(&req, "Content-Type", "text/plain");
 
-       snprintf(tmp, sizeof(tmp), "Message-Waiting: %s\n", hasmsgs ? "yes" : "no");
-       snprintf(clen, sizeof(clen), "%d", strlen(tmp));
+       snprintf(tmp, sizeof(tmp), "Message-Waiting: %s\n", (newmsgs + oldmsgs) ? "yes" : "no");
+       snprintf(tmp2, sizeof(tmp2), "Voicemail: %d/%d\n", newmsgs, oldmsgs);
+       snprintf(clen, sizeof(clen), "%d", 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 */
@@ -3694,15 +3697,15 @@ static int sip_send_mwi_to_peer(struct sip_peer *peer)
 {
        /* Called with peerl lock, but releases it */
        struct sip_pvt *p;
-       int hasmsgs;
        char name[256] = "";
+       int newmsgs, oldmsgs;
        /* Check for messages */
-       hasmsgs = ast_app_has_voicemail(peer->mailbox);
+       ast_app_messagecount(peer->mailbox, &newmsgs, &oldmsgs);
        
        time(&peer->lastmsgcheck);
        
        /* Return now if it's the same thing we told them last time */
-       if (hasmsgs == peer->lastmsgssent) {
+       if (((newmsgs << 8) | (oldmsgs)) == peer->lastmsgssent) {
                ast_pthread_mutex_unlock(&peerl.lock);
                return 0;
        }
@@ -3714,7 +3717,7 @@ static int sip_send_mwi_to_peer(struct sip_peer *peer)
                return -1;
        }
        strncpy(name, peer->name, sizeof(name) - 1);
-       peer->lastmsgssent = hasmsgs;
+       peer->lastmsgssent = ((newmsgs << 8) | (oldmsgs));
        ast_pthread_mutex_unlock(&peerl.lock);
        if (create_addr(p, peer->name)) {
                /* Maybe they're not registered, etc. */
@@ -3726,7 +3729,7 @@ static int sip_send_mwi_to_peer(struct sip_peer *peer)
        snprintf(p->via, sizeof(p->via), "SIP/2.0/UDP %s:%d;branch=%08x", inet_ntoa(p->ourip), ourport, p->branch);
        build_callid(p->callid, sizeof(p->callid), p->ourip);
        /* Send MWI */
-       transmit_notify(p, hasmsgs);
+       transmit_notify(p, newmsgs, oldmsgs);
        /* Destroy channel */
        sip_destroy(p);
        return 0;
index bcbccb7..ef7b05c 100755 (executable)
@@ -40,7 +40,10 @@ extern int ast_app_getdata_full(struct ast_channel *c, char *prompt, char *s, in
 int ast_app_getvoice(struct ast_channel *c, char *dest, char *dstfmt, char *prompt, int silence, int maxsec);
 
 //! Determine if a given mailbox has any voicemail
-extern int ast_app_has_voicemail(char *mailbox);
+extern int ast_app_has_voicemail(const char *mailbox);
+
+//! Determine number of new/old messages in a mailbox
+extern int ast_app_messagecount(const char *mailbox, int *newmsgs, int *oldmsgs);
 
 #if defined(__cplusplus) || defined(c_plusplus)
 }