Kannel: Open Source WAP and SMS gateway  svn-r5335
bb_store.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  * bb_store.c : wrapper for the different bearerbox box SMS storage/retrieval module
59  *
60  * Author: Alexander Malysh, 2006
61  */
62 
63 #include "gw-config.h"
64 
65 #include "gwlib/gwlib.h"
66 #include "msg.h"
67 #include "bearerbox.h"
68 #include "bb_store.h"
69 
70 
71 long (*store_messages)(void);
72 int (*store_save)(Msg *msg);
74 int (*store_load)(void(*receive_msg)(Msg*));
75 int (*store_dump)(void);
76 void (*store_shutdown)(void);
77 Octstr* (*store_msg_pack)(Msg *msg);
78 Msg* (*store_msg_unpack)(Octstr *os);
79 void (*store_for_each_message)(void(*callback_fn)(Msg* msg, void *data), void *data);
80 
81 
82 int store_init(Cfg *cfg, const Octstr *type, const Octstr *fname, long dump_freq,
83  void *pack_func, void *unpack_func)
84 {
85  int ret;
86 
87  store_msg_pack = pack_func;
88  store_msg_unpack = unpack_func;
89 
90  if (type == NULL || octstr_str_compare(type, "file") == 0) {
91  ret = store_file_init(fname, dump_freq);
92  } else if (octstr_str_compare(type, "spool") == 0) {
93  ret = store_spool_init(fname);
94 #ifdef HAVE_REDIS
95  } else if (octstr_str_compare(type, "redis") == 0) {
96  ret = store_redis_init(cfg);
97 #endif
98  } else {
99  error(0, "Unknown 'store-type' defined.");
100  ret = -1;
101  }
102 
103  return ret;
104 }
105 
106 struct status {
107  const char *format;
109 };
110 
111 static void status_cb(Msg *msg, void *d)
112 {
113  struct status *data = d;
114  struct tm tm;
115  char id[UUID_STR_LEN + 1];
116 
117  if (msg == NULL)
118  return;
119 
120  /* transform the time value */
121 #if LOG_TIMESTAMP_LOCALTIME
122  tm = gw_localtime(msg->sms.time);
123 #else
124  tm = gw_gmtime(msg->sms.time);
125 #endif
126 
127  uuid_unparse(msg->sms.id, id);
128 
129  octstr_format_append(data->status, data->format,
130  id,
131  (msg->sms.sms_type == mo ? "MO" :
132  msg->sms.sms_type == mt_push ? "MT-PUSH" :
133  msg->sms.sms_type == mt_reply ? "MT-REPLY" :
134  msg->sms.sms_type == report_mo ? "DLR-MO" :
135  msg->sms.sms_type == report_mt ? "DLR-MT" : ""),
136  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
137  tm.tm_hour, tm.tm_min, tm.tm_sec,
138  (msg->sms.sender ? octstr_get_cstr(msg->sms.sender) : ""),
139  (msg->sms.receiver ? octstr_get_cstr(msg->sms.receiver) : ""),
140  (msg->sms.smsc_id ? octstr_get_cstr(msg->sms.smsc_id) : ""),
141  (msg->sms.boxc_id ? octstr_get_cstr(msg->sms.boxc_id) : ""),
142  msg->sms.mclass, msg->sms.coding, msg->sms.mwi, msg->sms.compress,
143  msg->sms.dlr_mask,
144  (msg->sms.udhdata ? msg->sms.udhdata : octstr_imm("")),
145  (msg->sms.msgdata ? msg->sms.msgdata : octstr_imm("")));
146 }
147 
148 Octstr *store_status(int status_type)
149 {
150  Octstr *ret = octstr_create("");
151  const char *format;
152  struct status data;
153 
154  /* check if we are active */
155  if (store_for_each_message == NULL)
156  return ret;
157 
158  /* set the type based header */
159  if (status_type == BBSTATUS_HTML) {
160  octstr_append_cstr(ret, "<table border=1>\n"
161  "<tr><td>SMS ID</td><td>Type</td><td>Time</td><td>Sender</td><td>Receiver</td>"
162  "<td>SMSC ID</td><td>BOX ID</td><td>Flags</td>"
163  "<td>UDH</td><td>Message</td>"
164  "</tr>\n");
165 
166  format = "<tr><td>%s</td><td>%s</td>"
167  "<td>%04d-%02d-%02d %02d:%02d:%02d</td>"
168  "<td>%s</td><td>%s</td><td>%s</td>"
169  "<td>%s</td><td>%ld:%ld:%ld:%ld:%ld</td><td>%E</td><td>%E</td></tr>\n";
170  } else if (status_type == BBSTATUS_XML) {
171  format = "\t<message>\n\t<id>%s</id>\n\t<type>%s</type>\n\t"
172  "<time>%04d-%02d-%02d %02d:%02d:%02d</time>\n\t"
173  "<sender>%s</sender>\n\t"
174  "<receiver>%s</receiver>\n\t<smsc-id>%s</smsc-id>\n\t"
175  "<box-id>%s</box-id>\n\t"
176  "<flags>%ld:%ld:%ld:%ld:%ld</flags>\n\t"
177  "<udh-data>%E</udh-data>\n\t<msg-data>%E</msg-data>\n\t"
178  "</message>\n";
179  } else {
180  octstr_append_cstr(ret, "[SMS ID] [Type] [Time] [Sender] [Receiver] [SMSC ID] [BOX ID] [Flags] [UDH] [Message]\n");
181  format = "[%s] [%s] [%04d-%02d-%02d %02d:%02d:%02d] [%s] [%s] [%s] [%s] [%ld:%ld:%ld:%ld:%ld] [%E] [%E]\n";
182  }
183 
184  data.format = format;
185  data.status = ret;
186 
188 
189  /* set the type based footer */
190  if (status_type == BBSTATUS_HTML) {
191  octstr_append_cstr(ret,"</table>");
192  }
193 
194  return ret;
195 }
void error(int err, const char *fmt,...)
Definition: log.c:648
Definition: msg.h:106
struct tm gw_gmtime(time_t t)
Definition: protected.c:137
Definition: msg.h:109
int(* store_save_ack)(Msg *msg, ack_status_t status)
Definition: bb_store.c:73
long(* store_messages)(void)
Definition: bb_store.c:71
const char * format
Definition: bb_store.c:107
int type
Definition: smsc_cimd2.c:215
void uuid_unparse(const uuid_t uu, char *out)
Definition: gw_uuid.c:562
Octstr * store_status(int status_type)
Definition: bb_store.c:148
Msg *(* store_msg_unpack)(Octstr *os)
Definition: bb_store.c:78
static void format(char *buf, const char *fmt)
Definition: accesslog.c:174
void octstr_append_cstr(Octstr *ostr, const char *cstr)
Definition: octstr.c:1511
Definition: msg.h:110
static Cfg * cfg
Definition: opensmppbox.c:95
int(* store_dump)(void)
Definition: bb_store.c:75
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
void(* store_for_each_message)(void(*callback_fn)(Msg *msg, void *data), void *data)
Definition: bb_store.c:79
Definition: msg.h:108
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
Definition: msg.h:79
Definition: cfg.c:164
int store_init(Cfg *cfg, const Octstr *type, const Octstr *fname, long dump_freq, void *pack_func, void *unpack_func)
Definition: bb_store.c:82
#define octstr_create(cstr)
Definition: octstr.h:125
void * data
Octstr *(* store_msg_pack)(Msg *msg)
Definition: bb_store.c:77
#define UUID_STR_LEN
Definition: gw_uuid.h:19
void(* store_shutdown)(void)
Definition: bb_store.c:76
Definition: octstr.c:118
struct tm gw_localtime(time_t t)
Definition: protected.c:121
int(* store_load)(void(*receive_msg)(Msg *))
Definition: bb_store.c:74
int octstr_str_compare(const Octstr *ostr, const char *str)
Definition: octstr.c:973
void octstr_format_append(Octstr *os, const char *fmt,...)
Definition: octstr.c:2507
static void status_cb(Msg *msg, void *d)
Definition: bb_store.c:111
int(* store_save)(Msg *msg)
Definition: bb_store.c:72
ack_status_t
Definition: msg.h:124
Definition: msg.h:107
int store_spool_init(const Octstr *fname)
int store_file_init(const Octstr *fname, long dump_freq)
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
Octstr * status
Definition: bb_store.c:108
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.