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 
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include "../../parser/parse_content.h"
00039 #include "../presence/event_list.h"
00040 #include "presence_mwi.h"
00041 
00043 inline char *eat_sp_tab(char *at, char *over)
00044 {
00045     while((at < over) && ((*at == ' ') || (*at == '\t'))) at++;
00046     return at;
00047 }
00048 
00050 inline char *eat_printable(char *at, char *over)
00051 {
00052     while ((at < over) && ((*at == '\t') || ((*at >= 32) && (*at <= 126))))
00053         at++;
00054     return at;
00055 }
00056 
00060 int mwi_publ_handl(struct sip_msg* msg)
00061 {       
00062     str body;
00063     char *at, *over;
00064 
00065     if (get_content_length(msg) == 0)
00066         return 1;
00067         
00068     body.s = get_body(msg);
00069     if (body.s == NULL) {
00070         LM_ERR("cannot extract body from msg\n");
00071         return -1;
00072     }
00073 
00074     /* content-length (if present) must be already parsed */
00075     body.len = get_content_length(msg);
00076     at = body.s;
00077     over = body.s + body.len;
00078 
00079     /* check msg-status-line */
00080     if (body.len <= 16) goto err;
00081     if (strncmp(body.s, "Messages-Waiting", 16) != 0) goto err;
00082     at = at + 16;
00083     at = eat_sp_tab(at, over);
00084     if ((at >= over) || (*at != ':')) goto err;
00085     at++;
00086     if ((at >= over) || ((*at != ' ') && (*at != '\t'))) goto err;
00087     at++;
00088     at = eat_sp_tab(at, over);
00089     if (at + 3 >= over) goto err;
00090     if (strncmp(at, "yes", 3) == 0) at = at + 3;
00091     else
00092         if (strncmp(at, "no", 2) == 0) at = at + 2;
00093         else
00094             goto err;
00095     if ((at + 1 >= over) || (*at != '\r') || (*(at + 1) != '\n')) goto err;
00096     at = at + 2;
00097     
00098     /* check that remaining body consists of lines that only contain
00099        printable ascii chars */
00100     while (at < over) {
00101         at = eat_printable(at, over);
00102         if ((at + 1 >= over) || (*at != '\r') || (*(at + 1) != '\n')) goto err;
00103         at = at + 2;
00104     }
00105     
00106     return 1;
00107 
00108 err:
00109     LM_ERR("check of body <%.*s> failed at character number %d\n",
00110            body.len, body.s, (int)(at - body.s + 1));
00111     return -1;
00112 
00113 }
00114 
00115 int mwi_add_events(void)
00116 {
00117     pres_ev_t event;
00118         
00119     /* constructing message-summary event */
00120     memset(&event, 0, sizeof(pres_ev_t));
00121     event.name.s = "message-summary";
00122     event.name.len = 15;
00123 
00124     event.content_type.s = "application/simple-message-summary";
00125     event.content_type.len = 34;
00126 
00127         event.default_expires= 3600;
00128     event.type = PUBL_TYPE;
00129         event.req_auth = 0;
00130     event.evs_publ_handl = mwi_publ_handl;
00131         
00132     if (pres_add_event(&event) < 0) {
00133         LM_ERR("failed to add event \"message-summary\"\n");
00134         return -1;
00135     }           
00136         
00137     return 0;
00138 }