Translation update done using Pootle.
[gammu.git] / smsd / inject.c
blobe65ea6610201832034327946b1e4d26a5e201313
1 /**
2 * SMSD message inject program
3 */
4 /* Copyright (c) 2009 - 2011 Michal Cihar <michal@cihar.com> */
5 /* Licensend under GNU GPL 2 */
7 #include <gammu-smsd.h>
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <signal.h>
11 #include <string.h>
12 #ifndef WIN32
13 #include <unistd.h>
14 #endif
15 #ifdef HAVE_GETOPT_LONG
16 #include <getopt.h>
17 #endif
19 #include "common.h"
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";
26 #endif
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");
34 #ifdef HAVE_SHM
35 printf(" - %s\n", "SHM");
36 #endif
37 #ifdef HAVE_GETOPT
38 printf(" - %s\n", "GETOPT");
39 #endif
40 #ifdef HAVE_GETOPT_LONG
41 printf(" - %s\n", "GETOPT_LONG");
42 #endif
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");
48 #endif
49 #ifdef HAVE_POSTGRESQL_LIBPQ_FE_H
50 printf(" - %s\n", "POSTGRESQL");
51 #endif
52 #ifdef LIBDBI_FOUND
53 printf(" - %s\n", "DBI");
54 #endif
55 #ifdef ODBC_FOUND
56 printf(" - %s\n", "ODBC");
57 #endif
58 printf("\n");
59 printf("Copyright (C) 2003 - 2011 Michal Cihar <michal@cihar.com> and other authors.\n");
60 printf("\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");
64 printf("\n");
65 printf("Check <http://wammu.eu/gammu/> for updates.\n");
66 printf("\n");
67 exit(0);
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);
75 #else
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);
80 #endif
82 void help(void)
84 printf("usage: gammu-smsd-inject [OPTION]... MSGTYPE RECIPIENT [MESSAGE_PARAMETER]...\n");
85 printf("options:\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");
92 printf("\n");
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");
99 exit(1);
102 int process_commandline(int argc, char **argv, SMSD_Parameters * params)
104 int opt;
106 #ifdef HAVE_GETOPT_LONG
107 struct option long_options[] = {
108 {"help", 0, 0, 'h'},
109 {"version", 0, 0, 'v'},
110 {"config", 1, 0, 'c'},
111 {"use-log", 0, 0, 'l'},
112 {"no-use-log", 0, 0, 'L'},
113 {0, 0, 0, 0}
115 int option_index;
117 while ((opt =
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) {
122 #else
123 /* Poor mans getopt replacement */
124 int i, optind = -1;
126 #define optarg argv[++i]
128 for (i = 1; i < argc; i++) {
129 if (strlen(argv[i]) != 2 || argv[i][0] != '-') {
130 wrong_params();
132 opt = argv[i][1];
133 #endif
134 switch (opt) {
135 case 'c':
136 params->config_file = optarg;
137 break;
138 case 'v':
139 version();
140 break;
141 case 'l':
142 params->use_log = TRUE;
143 break;
144 case 'L':
145 params->use_log = FALSE;
146 break;
147 case '?':
148 wrong_params();
149 case 'h':
150 help();
151 exit(0);
152 default:
153 #if defined(HAVE_GETOPT) || defined(HAVE_GETOPT_LONG)
154 wrong_params();
155 #else
156 optind = 1;
157 #endif
158 break;
160 #if !defined(HAVE_GETOPT) && !defined(HAVE_GETOPT_LONG)
161 if (optind != -1)
162 break;
163 #endif
166 return optind;
170 #ifndef WIN32
171 #endif
173 int main(int argc, char **argv)
175 GSM_Error error;
176 int startarg;
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 = {
184 NULL,
185 NULL,
188 NULL,
189 NULL,
190 FALSE,
191 FALSE,
192 FALSE,
193 FALSE,
194 FALSE,
195 FALSE,
196 FALSE,
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, &params);
208 if (params.config_file == NULL) {
209 #ifdef HAVE_DEFAULT_CONFIG
210 params.config_file = default_config;
211 #else
212 fprintf(stderr, "No config file specified!\n");
213 help();
214 exit(1);
215 #endif
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));
222 return 1;
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);
232 return 2;
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);
240 return 3;
242 if (strlen(newid) == 0) {
243 printf("Written message without ID\n");
244 } else {
245 printf("Written message with ID %s\n", newid);
248 SMSD_FreeConfig(config);
250 return 0;
253 /* How should editor hadle tabs in this file? Add editor commands here.
254 * vim: noexpandtab sw=8 ts=8 sts=8: