6 #include "../helper/locales.h"
9 #include "../helper/formats.h"
10 #include "../helper/printing.h"
11 #include "../helper/cmdline.h"
14 #include <curl/curl.h>
17 GSM_StateMachine
*gsm
= NULL
;
18 INI_Section
*cfg
= NULL
;
20 gboolean always_answer_yes
= FALSE
;
21 gboolean always_answer_no
= FALSE
;
22 volatile gboolean gshutdown
= FALSE
;
23 gboolean batch
= FALSE
;
24 gboolean batchConn
= FALSE
;
26 void interrupt(int sign
)
28 signal(sign
, SIG_IGN
);
29 GSM_AbortOperation(gsm
);
33 void PrintSecurityStatus(void)
35 GSM_SecurityCodeType Status
;
38 error
= GSM_GetSecurityStatus(gsm
, &Status
);
41 case SEC_SecurityCode
:
42 printf("%s\n", _("Waiting for Security Code."));
45 printf("%s\n", _("Waiting for PIN."));
48 printf("%s\n", _("Waiting for PIN2."));
51 printf("%s\n", _("Waiting for PUK."));
54 printf("%s\n", _("Waiting for PUK2."));
57 printf("%s\n", _("Waiting for phone code."));
60 printf("%s\n", _("Waiting for network code."));
63 printf("%s\n", _("Nothing to enter."));
67 printf("%s\n", _("Unknown security status."));
72 const char *GetMonthName(const int month
)
74 static char result
[100];
78 strcpy(result
, _("January"));
81 strcpy(result
, _("February"));
84 strcpy(result
, _("March"));
87 strcpy(result
, _("April"));
90 strcpy(result
, _("May"));
93 strcpy(result
, _("June"));
96 strcpy(result
, _("July"));
99 strcpy(result
, _("August"));
102 strcpy(result
, _("September"));
105 strcpy(result
, _("October"));
108 strcpy(result
, _("November"));
111 strcpy(result
, _("December"));
114 strcpy(result
, _("Bad month!"));
120 const char *GetDayName(const int day
)
122 static char result
[100];
126 strcpy(result
, _("Monday"));
129 strcpy(result
, _("Tuesday"));
132 strcpy(result
, _("Wednesday"));
135 strcpy(result
, _("Thursday"));
138 strcpy(result
, _("Friday"));
141 strcpy(result
, _("Saturday"));
144 strcpy(result
, _("Sunday"));
147 strcpy(result
, _("Bad day!"));
154 void Terminate(int code
)
165 /* Disconnect from phone */
166 if (GSM_IsConnected(gsm
)) {
167 GSM_TerminateConnection(gsm
);
170 /* Free state machine */
171 GSM_FreeStateMachine(gsm
);
175 /* Close debug output if opened */
176 di
= GSM_GetGlobalDebug();
177 GSM_SetDebugFileDescriptor(NULL
, FALSE
, di
);
180 /* Free CURL memory */
181 curl_global_cleanup();
188 void Print_Error(GSM_Error error
)
190 if (error
!= ERR_NONE
) {
191 printf("%s\n", GSM_ErrorString(error
));
192 /* Check for security error */
193 if (error
== ERR_SECURITYERROR
) {
194 printf(LISTFORMAT
, _("Security status"));
195 PrintSecurityStatus();
198 Terminate(100 + error
);
203 * Callback from CURL to get data.
205 size_t write_mem(void *ptr
, size_t size
, size_t nmemb
, void *data
) {
206 size_t realsize
= size
* nmemb
;
207 GSM_File
*file
= (GSM_File
*)data
;
209 file
->Buffer
= realloc(file
->Buffer
,file
->Used
+ realsize
+ 1);
212 memcpy(file
->Buffer
+ file
->Used
, ptr
, realsize
);
213 file
->Used
+= realsize
;
214 file
->Buffer
[file
->Used
] = 0;
221 * Downloads file from arbitrary URL.
223 * \param url URL to download.
224 * \param file Storage for data.
226 * \returns ERR_NONE on success.
228 GSM_Error
GSM_ReadHTTPFile(const char *url
, GSM_File
*file
)
231 CURL
*dl_handle
= NULL
;
234 dl_handle
= curl_easy_init();
235 if (dl_handle
== NULL
) return FALSE
;
237 curl_easy_setopt(dl_handle
, CURLOPT_URL
, url
);
238 curl_easy_setopt(dl_handle
, CURLOPT_USERAGENT
, "Gammu/" GAMMU_VERSION
);
239 curl_easy_setopt(dl_handle
, CURLOPT_WRITEFUNCTION
, write_mem
);
240 curl_easy_setopt(dl_handle
, CURLOPT_WRITEDATA
, file
);
241 curl_easy_setopt(dl_handle
, CURLOPT_FOLLOWLOCATION
, 1);
242 curl_easy_setopt(dl_handle
, CURLOPT_MAXREDIRS
, 10);
244 /* Enable verbose outpuf from CURL */
245 curl_easy_setopt(dl_handle
, CURLOPT_VERBOSE
, 1);
248 result
= curl_easy_perform(dl_handle
);
250 curl_easy_cleanup(dl_handle
);
255 case CURLE_URL_MALFORMAT
:
257 case CURLE_COULDNT_CONNECT
:
258 case CURLE_RECV_ERROR
:
259 return ERR_COULDNT_CONNECT
;
260 case CURLE_COULDNT_RESOLVE_HOST
:
261 return ERR_COULDNT_RESOLVE
;
263 printf_err("Unknown curl error: %d\n", result
);
272 * Initiates connection to phone.
274 * \param checkerror Whether we should check for error.
276 void GSM_Init(gboolean checkerror
)
280 /* No checks on existing batch mode */
281 if (batch
&& batchConn
)
284 /* Connect to phone */
285 error
= GSM_InitConnection(gsm
, 1);
289 /* Check for batch mode */
291 if (error
== ERR_NONE
) {
297 void GSM_Terminate(void)
302 error
= GSM_TerminateConnection(gsm
);
307 void GetStartStop(int *start
, int *stop
, int num
, int argc
, char *argv
[])
312 printf_err("%s\n", _("More parameters required!"));
316 *start
= GetInt(argv
[num
]);
318 printf_err("%s\n", _("Please enumerate locations from 1"));
325 *stop
= GetInt(argv
[num
+ 1]);
328 _("Please enumerate locations from 1"));
331 if (*stop
< *start
) {
333 _("Swapping start and end location"));
342 gboolean
answer_yes(const char *format
, ...)
349 va_start(ap
, format
);
350 vsnprintf(buffer
, sizeof(buffer
) - 1, format
, ap
);
354 fprintf(stderr
, "%s (%s/%s/%s/%s/%s) ", buffer
,
356 _("ALL"), _("ONLY"), _("NONE"));
357 if (always_answer_yes
) {
358 fprintf(stderr
, "%s\n", _("YES (always)"));
361 if (always_answer_no
) {
362 fprintf(stderr
, "%s\n", _("NO (always)"));
365 len
= GetLine(stdin
, ans
, 99);
368 /* l10n: Answer to (yes/no/ALL/ONLY/NONE) question */
369 if (!strcmp(ans
, _("NONE"))) {
370 always_answer_no
= TRUE
;
373 /* l10n: Answer to (yes/no/ALL/ONLY/NONE) question */
374 if (!strcmp(ans
, _("ONLY"))) {
375 always_answer_no
= TRUE
;
378 /* l10n: Answer to (yes/no/ALL/ONLY/NONE) question */
379 if (!strcmp(ans
, _("ALL"))) {
380 always_answer_yes
= TRUE
;
383 /* l10n: Answer to (yes/no/ALL/ONLY/NONE) question */
384 if (strcasecmp(ans
, _("yes")) == 0)
386 /* l10n: Answer to (yes/no/ALL/ONLY/NONE) question */
387 if (strcasecmp(ans
, _("no")) == 0)
392 /* How should editor hadle tabs in this file? Add editor commands here.
393 * vim: noexpandtab sw=8 ts=8 sts=8: