2 * SMSD message inject program
4 /* Copyright (c) 2009 - 2011 Michal Cihar <michal@cihar.com> */
5 /* Licensend under GNU GPL 2 */
7 #include <gammu-smsd.h>
15 #ifdef HAVE_GETOPT_LONG
21 #include "../helper/message-cmdline.h"
23 #if !defined(WIN32) && (defined(HAVE_GETOPT) || defined(HAVE_GETOPT_LONG))
24 #define HAVE_DEFAULT_CONFIG
25 const char default_config
[] = "/etc/gammu-smsdrc";
28 NORETURN
void version(void)
30 printf("Gammu-smsd-inject version %s\n", GAMMU_VERSION
);
31 printf("Built %s on %s using %s\n", __TIME__
, __DATE__
, GetCompiler());
32 printf("Compiled in features:\n");
33 printf("OS support:\n");
35 printf(" - %s\n", "SHM");
38 printf(" - %s\n", "GETOPT");
40 #ifdef HAVE_GETOPT_LONG
41 printf(" - %s\n", "GETOPT_LONG");
43 printf("Backend services:\n");
44 printf(" - %s\n", "NULL");
45 printf(" - %s\n", "FILES");
46 #ifdef HAVE_MYSQL_MYSQL_H
47 printf(" - %s\n", "MYSQL");
49 #ifdef HAVE_POSTGRESQL_LIBPQ_FE_H
50 printf(" - %s\n", "POSTGRESQL");
53 printf(" - %s\n", "DBI");
56 printf(" - %s\n", "ODBC");
59 printf("Copyright (C) 2003 - 2011 Michal Cihar <michal@cihar.com> and other authors.\n");
61 printf("License GPLv2: GNU GPL version 2 <http://creativecommons.org/licenses/GPL/2.0/>.\n");
62 printf("This is free software: you are free to change and redistribute it.\n");
63 printf("There is NO WARRANTY, to the extent permitted by law.\n");
65 printf("Check <http://wammu.eu/gammu/> for updates.\n");
70 #ifdef HAVE_GETOPT_LONG
71 #define print_option(name, longname, help) \
72 printf("-%s / --%s - %s\n", name, longname, help);
73 #define print_option_param(name, longname, paramname, help) \
74 printf("-%s / --%s %s - %s\n", name, longname, paramname, help);
76 #define print_option(name, longname, help) \
77 printf("-%s - %s\n", name, help);
78 #define print_option_param(name, longname, paramname, help) \
79 printf("-%s %s - %s\n", name, paramname, help);
84 printf("usage: gammu-smsd-inject [OPTION]... MSGTYPE RECIPIENT [MESSAGE_PARAMETER]...\n");
86 print_option("h", "help", "shows this help");
87 print_option("v", "version", "shows version information");
88 print_option("l", "use-log", "use logging configuration from config file");
89 print_option("L", "no-use-log", "do not use logging configuration from config file (default)");
90 print_option_param("c", "config", "CONFIG_FILE",
91 "defines path to config file");
93 printf("MSGTYPE and it's parameters are described in man page and Gammu documentation\n");
96 NORETURN
void wrong_params(void)
98 fprintf(stderr
, "Invalid parameter, use -h for help.\n");
102 int process_commandline(int argc
, char **argv
, SMSD_Parameters
* params
)
106 #ifdef HAVE_GETOPT_LONG
107 struct option long_options
[] = {
109 {"version", 0, 0, 'v'},
110 {"config", 1, 0, 'c'},
111 {"use-log", 0, 0, 'l'},
112 {"no-use-log", 0, 0, 'L'},
118 getopt_long(argc
, argv
, "+hvc:lL", long_options
,
119 &option_index
)) != -1) {
120 #elif defined(HAVE_GETOPT)
121 while ((opt
= getopt(argc
, argv
, "+hvc:lL")) != -1) {
123 /* Poor mans getopt replacement */
126 #define optarg argv[++i]
128 for (i
= 1; i
< argc
; i
++) {
129 if (strlen(argv
[i
]) != 2 || argv
[i
][0] != '-') {
136 params
->config_file
= optarg
;
142 params
->use_log
= TRUE
;
145 params
->use_log
= FALSE
;
153 #if defined(HAVE_GETOPT) || defined(HAVE_GETOPT_LONG)
160 #if !defined(HAVE_GETOPT) && !defined(HAVE_GETOPT_LONG)
173 int main(int argc
, char **argv
)
177 GSM_MultiSMSMessage sms
;
178 GSM_Message_Type type
= SMS_SMSD
;
179 GSM_SMSDConfig
*config
;
180 const char program_name
[] = "gammu-smsd-inject";
181 char newid
[200] = { 0 };
183 SMSD_Parameters params
= {
201 * We don't need gettext, but need to set locales so that
202 * charset conversion works.
204 GSM_InitLocales(NULL
);
206 startarg
= process_commandline(argc
, argv
, ¶ms
);
208 if (params
.config_file
== NULL
) {
209 #ifdef HAVE_DEFAULT_CONFIG
210 params
.config_file
= default_config
;
212 fprintf(stderr
, "No config file specified!\n");
218 error
= CreateMessage(&type
, &sms
, argc
, startarg
, argv
, NULL
);
219 if (error
!= ERR_NONE
) {
220 printf("Failed to create message: %s\n",
221 GSM_ErrorString(error
));
225 config
= SMSD_NewConfig(program_name
);
226 assert(config
!= NULL
);
228 error
= SMSD_ReadConfig(params
.config_file
, config
, params
.use_log
);
229 if (error
!= ERR_NONE
) {
230 printf("Failed to read config: %s\n", GSM_ErrorString(error
));
231 SMSD_FreeConfig(config
);
235 error
= SMSD_InjectSMS(config
, &sms
, newid
);
236 if (error
!= ERR_NONE
) {
237 printf("Failed to inject message: %s\n",
238 GSM_ErrorString(error
));
239 SMSD_FreeConfig(config
);
242 if (strlen(newid
) == 0) {
243 printf("Written message without ID\n");
245 printf("Written message with ID %s\n", newid
);
248 SMSD_FreeConfig(config
);
253 /* How should editor hadle tabs in this file? Add editor commands here.
254 * vim: noexpandtab sw=8 ts=8 sts=8: