[Serusers] Injecting SIP NOTIFY into ser FIFO

Java Rockx javarockx at yahoo.com
Sun Sep 19 15:47:11 CEST 2004


Dave,

I finally have my MWI working end-to-end. Here's all I did for anyone wishing
to get theirs working. Feel free to point out any weaknesses in my
implementation.

I have SER and Asterisk on different servers. For this example I will call my
server IPs as follows:

  ser      = 67.72.100.20 (sip01.mycompany.com)
  Asterisk = 67.72.100.21 (vm01.mycompany.com)

As a note to everyone, please review this and let me know if you can improve
upon the MWI usage.

Regards,
Paul

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
One the SER side:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

* I have a user called "vmserver" so that Asterisk can use sipsak
  to send SIP NOTIFY messages to ser

* in my ser.cfg file I have the following to handle NOTIFY messages

  # allow NOTIFY messages only from the Asterisk server
  if (method=="NOTIFY" & src_ip==67.72.100.20) {
    if (!t_relay()) {
      sl_reply_error();
      break;
    };
  };
                                                                               

  if (method=="SUBSCRIBE") {
    # challenge/response
    if (!www_authorize("mycompany.com", "subscriber")) {
      www_challenge("mycompany.com", "0");
      break;
    };

    # notify the Asterisk server to update the UAs MWI. I
    # believe most UAs that support SUBSCRIBE will 
    # periodically send a SUBSCRIBE message. These cause
    # the Asterisk server to update their MWI.
    exec_msg("/usr/bin/mwi/subscribe $SIP_RURI");
    break;
  };

  if (uri=~"^sip:7[0-9]*@") {
        log(1, "LOG: divert to voice mail\n");
        route(6);
        break;
  }
}

# route for sending UAs to the Asterisk server. We use
# 7 + [Extension] to access the voice mail server. This
# means we strip off the 7 because we need to have the
# ser extension the same as the asterisk extension.
route[6] {                                                                     
        
        strip(1);
        rewritehost("vm01.mycompany.com");
        forward(uri:host, uri:port);
}


* The /usr/bin/mwi/subscribe script that is referenced above
  is shown here. This is responsible for notifying the 
  Asterisk server that someone needs their MWI updated

#!/bin/sh

# this script simply accepts as input the subscriber URI such 
# as sip:1001 at sip01.mycompany.com and "touches" a file with the
# same name. It stores the file in /var/spool/mwi/immediate 
# which is later processed in a CRON job
 
USER=$1

# some input arguments may contain ";user=phone" so we need to
# strip it off. We also need to strip of the "sip:" piece so that
# all we are left with is "1001 at sip.mycompany.com"

IDX1=`expr index "$1" ":"`
IDX1=$(($IDX1 + 1))
IDX2=`expr index "$1" ";"`

if [ $IDX2 != 0 ]; then
  IDX2=$(($IDX2 - $IDX1))
fi

USER=`expr substr $1 $IDX1 $IDX2`
touch /var/spool/mwi/$USER


* I have in CRON a jobs that runs once per minute. This is the script
  that CRON executes

#!/bin/sh

# this script uses scp to copy the files from /var/spool/mwi/immediate
# to the Asterisk server. It is important to note that the scp works
# without being prompted for a password.
 
MSG_HOME=/var/spool/mwi
VM_HOME=/var/spool/mwi/immediate

for file in $MSG_HOME/*
do
  if [ -f $file ]; then
    scp $file vm01.mycompany.com:$VM_HOME
    rm -f $file
  fi
done


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
One the Asterisk side:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

* In /etc/asterisk/voicemail.conf I have the following:

[general]
; this script is executed 
externnotify=/usr/bin/mwi/mwi-immediate

* In /etc/asterisk/extensions.conf I have the following:

[sip01.mycompany.com]
                                                                               
                  
exten => vmserver,1,Wait(1)
exten => vmserver,2,Background(please-enter-your)
exten => vmserver,3,VoicemailMain
                                                                               
                  
exten => 1000,1,Macro(vmanswer,1000)
exten => 1001,1,Macro(vmanswer,1001)

[macro-vmanswer]
                                                                               
                  
exten => s,1,Wait(1)
exten => s,2,GotoIf($[${ARG1} = ${CALLERIDNUM}]?3:5)
exten => s,3,VoicemailMain(${ARG1})
exten => s,4,Goto(6)
exten => s,5,Voicemail(u${ARG1})
exten => s,6,System(/usr/bin/mwi/mwi-immediate ${CONTEXT}
${ARG1}@sip01.mycompany.com 0)
exten => s,7,Hangup
                                                                               
                  
;NOTE: See the call to mwi-immediate on the hangup. This
;      will cause asterisk to update the MWI when a user
;      hangs up.


* In /etc/asterisk/sip.conf I have the usual entries for
  SIP user accounts

* In /usr/bin/mwi/mwi-immediate I have:

#!/bin/sh

# this script "touches" files in the spool directory which a 
# cron jobs later processes. The cron job is where we actually
# send the SIP NOTIFY back to the UAs
CONTEXT=$1
EXTENSION=$2
VM_COUNT=$3
                                                                               
                  touch /var/spool/mwi/immediate/$EXTENSION

* This is the cron job mentioned in the previous note:

#!/bin/sh
                                                                               
                                                                        
# This script sends the SIP NOTIFY message to UAs. The NOTIFY message
# can either enable or disable the UA message indicator. The script
# looks for any file in $VM_HOME and creates the NOTIFY message via
# the template in $TEMPLATE.
#
# The actual NOTIFY message is sent to the SIP proxy by the sipsak
# utilty. After the message is send we delete the processed file
# from VM_HOME so we don't keep resending the same message. Most
# UAs will periodically send a SUBSCRIBE message to the SIP proxy.
# When this occurs the SIP proxy will place a file in the location
# /var/spool/mwi/delay and a less frequent CRON job will process it.
                                                                               
                                                                        #
# ALSO NOTE: I am not using the Asterisk "telnet API". I can't
# remember what it's called, but I think looking for voice mail 
# files on disk is quicker. I do not know if this is 100% safe since
# I am totally bypassing Asterisk in order to determine if a user has
# new voice mail messages. Can anyone verify this as a good way to
# handle this?

VM_ROOT=/var/spool/asterisk/voicemail
VM_HOME=/var/spool/mwi/immediate
TEMPLATE=/etc/mwi/notify.msg
                                                                               
                                                                        
cd $VM_HOME
                                                                               
                                                                        
for file in *
do
        LEN=`expr length $file`
        IDX1=`expr index "$file" \@`
        IDX2=$(($IDX1 + 1))
        IDX1=$(($IDX1 - 1))
        MAILBOX=`expr substr $file 1 $IDX1`
        DOMAIN=`expr substr $file $IDX2 $LEN`
        TOTAL_MESSAGES=`find $VM_ROOT/$DOMAIN/$MAILBOX -name "*.txt" | 
                        wc -l | sed 's/^ *\(.*\) *$/\1/'`
        NEW_MESSAGES=`find $VM_ROOT/$DOMAIN/$MAILBOX/INBOX -name "*.txt" | 
                        wc -l | sed 's/^ *\(.*\) *$/\1/'`
        OLD_MESSAGES=$(($TOTAL_MESSAGES - $NEW_MESSAGES))
                                                                               
                                                                        
        if [ "$NEW_MESSAGES" == "0" ]; then
                HAS_NEW="no"
        else
                HAS_NEW="yes"
        fi
                                                                               
                                                                        
        CONTENT_LENGTH=$((34 + `expr length $HAS_NEW` + 
                               `expr length $NEW_MESSAGES` + 
                               `expr length $OLD_MESSAGES`))
                                                                               
                                                                        
 
        CMD="s/!SUBSCRIBER!/$file/g;s/!MAILBOX!/$MAILBOX/g;
             s/!CONTENT_LENGTH!/$CONTENT_LENGTH/g;
             s/!HAS_MESSAGE!/$HAS_NEW/g;
             s/!NEW_COUNT!/$NEW_MESSAGES/g;
             s/!OLD_COUNT!/$OLD_MESSAGES/g"
                                                                               
                                                                        
        cat /etc/mwi/notify.msg | sed -e $CMD > /tmp/mwi-immediate
                                                                               
                                                                        
        echo "Notifying $file"
        `sipsak shoot -a number4 -f /tmp/mwi-immediate -s sip:$file`
                                                                               
                                                                        
        rm $file
done


* The final piece is the $TEMPLATE used in the previous step. This
  file is /etc/mwi/notify.msg NOTE: a VERY important detail with this
  file is that the line terminator must be <CR><LF> rather than the 
  Un*x <LF> because if you omit the <CR><LF> you will crash your SIP
  phone. There must also be two (2) <CR><LF>'s before the message
  body. Also as noted in the previous step, I use sed here to replace
  all the !VARIABLE! items prior to using sipsak.

NOTIFY sip:!SUBSCRIBER! SIP/2.0
Via: SIP/2.0/UDP 67.72.100.21:5060
From: <sip:vmserver at sip01.mycompany.com>
To: <sip:!SUBSCRIBER!>
Contact: <sip:!MAILBOX!@67.72.100.21>
Call-ID: 4d61cf9e505d40e905032a18329d61ec at 67.72.100.21
CSeq: 1 NOTIFY
User-Agent: VoiceMail
Event: message-summary
Content-Type: application/simple-message-summary
Content-Length: !CONTENT_LENGTH!
                                                                               
                                                                        
                                                                               
                                                                        
Messages-Waiting: !HAS_MESSAGE!
Voicemail: !NEW_COUNT!/!OLD_COUNT!


--- Dave Bath <dave at fuuz.com> wrote:

> Hi Java,
> 
> Presumably your users are registered with SER?  And you have some
> specific routing for when they check their mailbox? Either forwarding a
> short code to VoiceMailMain or forwarding a variation of their extension
> or something?  How about simply checking for the "BYE" method in this
> routing block and using the exec module in SER to force a recheck of the
> script on the asterisk box?
> 
> D
> 
> -----Original Message-----
> From: Java Rockx [mailto:javarockx at yahoo.com] 
> Sent: 19 September 2004 05:56
> To: serusers at lists.iptel.org; Dave Bath
> Subject: RE: [Serusers] Injecting SIP NOTIFY into ser FIFO
> 
> Dave,
> 
> I cheated big time. I use sipsak to send the message from my Asterisk
> server to
> my ser proxy. I realized to late that I mentioned FIFO in my original
> post. 
> 
> I use the Asterisk "externnotify" in the voicemail.conf file to specify
> an
> external bash script. This script then simply "touches" a file in
> /var/spool/mwi/ for later processing by cron.
> 
> My cron job comes along after the fact and picks up all the files in
> /var/spool/mwi/ and generates the appropriate SIP NOTIFY message and
> then it
> uses sipsak to "shoot" it to the ser proxy.
> 
> This works really well for me when I need to turn on the MWI for a UA.
> What I
> still can't do is turn the MWI off when there are no new messages. I'm
> really
> having a hard time trying to determine how to hook the "Hang-Up" event
> in
> Asterisk. I need to essentially have an "externnotify" for hang ups so I
> can
> check the message status for a the mailbox and cancel the MWI if there
> are no
> new messages.
> 
> I also need to handle SUBSCRIBE messages that the SIP proxy gets. I'm
> planning
> on have the ser.cfg file execute an external script that will get a
> message to
> the cron job on my Asterisk box. Once the Asterisk box gets notified of
> the
> SUBSCRIBE message, it will process the same as if the user just had a
> voice
> mail left and the externnotify event got fired.
> 
> Paul
> 
> 
> --- Dave Bath <dave at fuuz.com> wrote:
> 
> > Hey Java,
> > 
> > I know it's cheeky, but I was wondering if you might let me see the
> > script you wrote to create the mwi message?  Great piece of work!
> > 
> > Dave
> > 
> > -----Original Message-----
> > From: serusers-bounces at iptel.org [mailto:serusers-bounces at lists.iptel.org]
> On
> > Behalf Of Java Rockx
> > Sent: 18 September 2004 03:49
> > To: Java Rockx; serusers at lists.iptel.org; Fred Tips
> > Subject: Re: [Serusers] Injecting SIP NOTIFY into ser FIFO
> > 
> > I found my problem. I had to convert the NOTIFY message from Unix
> format
> > to
> > Windoz format. I used the unix2dos command to do this.
> > 
> > Everything is good now.
> > 
> > P
> > 
> > --- Java Rockx <javarockx at yahoo.com> wrote:
> > 
> > > Fred,
> > > 
> > > I actually messed up my original post. I stated that I am placing my
> > NOTIFY
> > > messages directly in the ser FIFO. In reality I should have said I'm
> > using
> > > sipsak to send the message to ser just like any normal UA would.
> > > 
> > > In my asterisk file sip.conf I have the following:
> > > 
> > > [general]
> > > register => vmserver:number4 at sip01.mycompany.com/vmserver
> > > 
> > > So asterisk is a registered UA with ser. Doing this I assumed I
> could
> > simply
> > > use sipsak from the asterisk server to send NOTIFY messages to the
> ser
> > box.
> > > 
> > > I can infact do this, becuase when I do so the "TO:" SIP UA crashes.
> > That
> > > tells
> > > me that everything is working, but my message is not 100% correct.
> > > 
> > > I also have the user extension configured in ser and asterisk and
> all
> > is
> > > working because I can leave voice mail and listen to voice mail
> > normally.
> > > It's
> > > just the MWI that is giving me grief.
> > > 
> > > Paul
> > > 
> > > --- Fred Tips <fred at callcarrera.com> wrote:
> > > 
> > > > Do you have mailbox= in * under the sip peer ?
> > > > 
> > > > ----- Original Message ----- 
> > > > From: "Java Rockx" <javarockx at yahoo.com>
> > > > To: <serusers at lists.iptel.org>
> > > > Sent: Friday, September 17, 2004 3:14 PM
> > > > Subject: [Serusers] Injecting SIP NOTIFY into ser FIFO
> > > > 
> > > > 
> > > > > Hello everyone.
> > > > >
> > > > > Can anyone tell me what I'm doing wrong? I am using Asterisk as
> a
> > voice
> > > > mail
> > > > > server for my ser SIP proxy. Everything works fine expect the
> > Message
> > > > Waiting
> > > > > Indicator (MWI). I've read a few articles and got some good feed
> > back
> > > from
> > > > this
> > > > > mailing list but here is my problem.
> > > > >
> > > > > My NOTIFY message is crashing my SIP phone. Here is my setup.
> I'm
> > using
> > > > > "externnotify" in Asterisk to call a /usr/bin/mwi script that I
> > wrote.
> > > All
> > > > this
> > > > > script does is create file in /var/spool/mwi that contains the
> > > > externnotify
> > > > > information.
> > > > >
> > > > > Then cron periodically processes the messages in /var/spool/mwi.
> > By
> > > > process I
> > > > > mean it will create the SIP NOTIFY messages and place them
> > directly in to
> > > > the
> > > > > ser FIFO. The problem is when this happens the SIP phone
> crashes.
> > > > >
> > > > > Here is a sample of my NOTIFY message that my Asterisk server
> cron
> > job is
> > > > > sending to the ser FIFO. I wish there was a way to just have
> > Asterisk
> > > > register
> > > > > with the ser proxy and handle this automatically.
> > > > >
> > > > > Can anyone see a problem?
> > > > > Regards,
> > > > > Paul
> > > > >
> > > > > --------------------------------------------------
> > > > >
> > > > > NOTIFY sip:1002 at sip01.mycompany.com SIP/2.0
> > > > > Via: SIP/2.0/UDP 4.4.242.201:5060
> > > > > From: "vmserver" <sip:vmserver at sip01.mycompany.com>
> > > > > To: <sip:1002 at sip01.mycomany.com>
> > > > > Contact: <sip:vmserver at sip01.mycompany.com>
> > > > > Call-ID: 5b8be1521efe68b5365a36466ad3b87 at 4.4.242.201
> > > > > CSeq: 1101 NOTIFY
> > > > > User-Agent: VoiceMail
> > > > > Event: message-summary
> > > > > Content-Type: application/simple-message-summary
> > > > > Content-Length: 38
> > > > >
> > > > >
> > > > > Messages-Waiting: yes
> > > > > Voicemail: 13/0
> > > > >
> > > > 
> > > > >
> > > > >
> 
=== message truncated ===



		
_______________________________
Do you Yahoo!?
Declare Yourself - Register online to vote today!
http://vote.yahoo.com




More information about the sr-users mailing list