presence_mwi/add_events.c

Go to the documentation of this file.
00001 /*
00002  * Add "message-summary" event to presence module
00003  *
00004  * Copyright (C) 2007 Juha Heinanen
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  * History:
00023  * --------
00024  *  2007-05-1  initial version (jih)
00025  */
00026 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include "../../parser/parse_content.h"
00031 #include "../presence/event_list.h"
00032 #include "presence_mwi.h"
00033 
00034 /* utility function that skips spaces and tabs */
00035 inline char *eat_sp_tab(char *at, char *over)
00036 {
00037     while((at < over) && ((*at == ' ') || (*at == '\t'))) at++;
00038     return at;
00039 }
00040 
00041 /* utility function that skips printable ascii chars */
00042 inline char *eat_printable(char *at, char *over)
00043 {
00044     while ((at < over) && ((*at == '\t') || ((*at >= 32) && (*at <= 126))))
00045    at++;
00046     return at;
00047 }
00048 
00049 /*
00050  * event specific publish handling - check if body format is ok
00051  */
00052 int mwi_publ_handl(struct sip_msg* msg)
00053 {  
00054     str body;
00055     char *at, *over;
00056 
00057     if (get_content_length(msg) == 0)
00058    return 1;
00059    
00060     body.s = get_body(msg);
00061     if (body.s == NULL) {
00062    LM_ERR("cannot extract body from msg\n");
00063    return -1;
00064     }
00065 
00066     /* content-length (if present) must be already parsed */
00067     body.len = get_content_length(msg);
00068     at = body.s;
00069     over = body.s + body.len;
00070 
00071     /* check msg-status-line */
00072     if (body.len <= 16) goto err;
00073     if (strncmp(body.s, "Messages-Waiting", 16) != 0) goto err;
00074     at = at + 16;
00075     at = eat_sp_tab(at, over);
00076     if ((at >= over) || (*at != ':')) goto err;
00077     at++;
00078     if ((at >= over) || ((*at != ' ') && (*at != '\t'))) goto err;
00079     at++;
00080     at = eat_sp_tab(at, over);
00081     if (at + 3 >= over) goto err;
00082     if (strncmp(at, "yes", 3) == 0) at = at + 3;
00083     else
00084    if (strncmp(at, "no", 2) == 0) at = at + 2;
00085    else
00086        goto err;
00087     if ((at + 1 >= over) || (*at != '\r') || (*(at + 1) != '\n')) goto err;
00088     at = at + 2;
00089     
00090     /* check that remaining body consists of lines that only contain
00091        printable ascii chars */
00092     while (at < over) {
00093    at = eat_printable(at, over);
00094    if ((at + 1 >= over) || (*at != '\r') || (*(at + 1) != '\n')) goto err;
00095    at = at + 2;
00096     }
00097     
00098     return 1;
00099 
00100 err:
00101     LM_ERR("check of body <%.*s> failed at character number %d\n",
00102       body.len, body.s, (int)(at - body.s + 1));
00103     return -1;
00104 
00105 }
00106 
00107 int mwi_add_events(void)
00108 {
00109     pres_ev_t event;
00110    
00111     /* constructing message-summary event */
00112     memset(&event, 0, sizeof(pres_ev_t));
00113     event.name.s = "message-summary";
00114     event.name.len = 15;
00115 
00116     event.content_type.s = "application/simple-message-summary";
00117     event.content_type.len = 34;
00118 
00119    event.default_expires= 3600;
00120     event.type = PUBL_TYPE;
00121    event.req_auth = 0;
00122     event.evs_publ_handl = mwi_publ_handl;
00123    
00124     if (pres_add_event(&event) < 0) {
00125    LM_ERR("failed to add event \"message-summary\"\n");
00126    return -1;
00127     }    
00128    
00129     return 0;
00130 }

Generated on Thu May 24 08:00:46 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6