24 |
#include <ctype.h> |
#include <ctype.h> |
25 |
#include <fcntl.h> |
#include <fcntl.h> |
26 |
#include <unistd.h> |
#include <unistd.h> |
27 |
|
#include <time.h> |
28 |
#include <sys/stat.h> |
#include <sys/stat.h> |
29 |
#include <curl/curl.h> |
#include <curl/curl.h> |
30 |
|
#include <readline/readline.h> |
31 |
#include "bti_version.h" |
#include "bti_version.h" |
32 |
|
|
33 |
|
|
52 |
char *account; |
char *account; |
53 |
char *tweet; |
char *tweet; |
54 |
char *proxy; |
char *proxy; |
55 |
|
char *time; |
56 |
|
char *homedir; |
57 |
int bash; |
int bash; |
58 |
enum host host; |
enum host host; |
59 |
}; |
}; |
87 |
|
|
88 |
static char *get_string_from_stdin(void) |
static char *get_string_from_stdin(void) |
89 |
{ |
{ |
90 |
char *temp; |
static char *string = (char *)NULL; |
91 |
char *string; |
if (string) { |
92 |
|
free(string); |
93 |
|
string = (char *)NULL; |
94 |
|
} |
95 |
|
|
96 |
string = zalloc(1000); |
string = readline("tweet: "); |
|
if (!string) |
|
|
return NULL; |
|
97 |
|
|
|
if (!fgets(string, 999, stdin)) |
|
|
return NULL; |
|
|
temp = strchr(string, '\n'); |
|
|
*temp = '\0'; |
|
98 |
return string; |
return string; |
99 |
} |
} |
100 |
|
|
116 |
free(session->account); |
free(session->account); |
117 |
free(session->tweet); |
free(session->tweet); |
118 |
free(session->proxy); |
free(session->proxy); |
119 |
|
free(session->time); |
120 |
|
free(session->homedir); |
121 |
free(session); |
free(session); |
122 |
} |
} |
123 |
|
|
274 |
char *host = NULL; |
char *host = NULL; |
275 |
char *proxy = NULL; |
char *proxy = NULL; |
276 |
char *file; |
char *file; |
|
char *home = getenv("HOME"); |
|
277 |
|
|
278 |
/* config file is ~/.bti */ |
/* config file is ~/.bti */ |
279 |
file = alloca(strlen(home) + 7); |
file = alloca(strlen(session->homedir) + 7); |
280 |
|
|
281 |
sprintf(file, "%s/.bti", home); |
sprintf(file, "%s/.bti", session->homedir); |
282 |
|
|
283 |
config_file = fopen(file, "r"); |
config_file = fopen(file, "r"); |
284 |
|
|
349 |
fclose(config_file); |
fclose(config_file); |
350 |
} |
} |
351 |
|
|
352 |
|
static void log_session(struct session *session, int retval) |
353 |
|
{ |
354 |
|
FILE *log_file; |
355 |
|
char *filename; |
356 |
|
char *host; |
357 |
|
|
358 |
|
/* logfile is ~/.bti.log */ |
359 |
|
filename = alloca(strlen(session->homedir) + 10); |
360 |
|
|
361 |
|
sprintf(filename, "%s/.bti.log", session->homedir); |
362 |
|
|
363 |
|
log_file = fopen(filename, "a+"); |
364 |
|
if (log_file == NULL) |
365 |
|
return; |
366 |
|
switch (session->host) { |
367 |
|
case HOST_TWITTER: |
368 |
|
host = "twitter"; |
369 |
|
break; |
370 |
|
case HOST_IDENTICA: |
371 |
|
host = "identi.ca"; |
372 |
|
break; |
373 |
|
default: |
374 |
|
host = "unknown"; |
375 |
|
break; |
376 |
|
} |
377 |
|
|
378 |
|
if (retval) |
379 |
|
fprintf(log_file, "%s: host=%s tweet failed\n", session->time, host); |
380 |
|
else |
381 |
|
fprintf(log_file, "%s: host=%s tweet=%s\n", session->time, host, session->tweet); |
382 |
|
|
383 |
|
fclose(log_file); |
384 |
|
} |
385 |
|
|
386 |
int main(int argc, char *argv[], char *envp[]) |
int main(int argc, char *argv[], char *envp[]) |
387 |
{ |
{ |
388 |
static const struct option options[] = { |
static const struct option options[] = { |
399 |
struct session *session; |
struct session *session; |
400 |
pid_t child; |
pid_t child; |
401 |
char *tweet; |
char *tweet; |
402 |
int retval; |
int retval = 0; |
403 |
int option; |
int option; |
404 |
char *http_proxy; |
char *http_proxy; |
405 |
|
time_t t; |
406 |
#if 0 |
#if 0 |
|
char *home = getenv("HOME"); |
|
407 |
char *pwd = getenv("PWD"); |
char *pwd = getenv("PWD"); |
408 |
char *dir; |
char *dir; |
409 |
#endif |
#endif |
410 |
|
|
411 |
|
rl_bind_key('\t', rl_insert); |
412 |
session = session_alloc(); |
session = session_alloc(); |
413 |
if (!session) { |
if (!session) { |
414 |
fprintf(stderr, "no more memory...\n"); |
fprintf(stderr, "no more memory...\n"); |
415 |
return -1; |
return -1; |
416 |
} |
} |
417 |
|
|
418 |
|
/* get the current time so that we can log it later */ |
419 |
|
time(&t); |
420 |
|
session->time = strdup(ctime(&t)); |
421 |
|
session->time[strlen(session->time)-1] = 0x00; |
422 |
|
|
423 |
|
session->homedir = strdup(getenv("HOME")); |
424 |
|
|
425 |
curl_global_init(CURL_GLOBAL_ALL); |
curl_global_init(CURL_GLOBAL_ALL); |
426 |
|
|
427 |
/* Set environment variables first, before reading command line options |
/* Set environment variables first, before reading command line options |
541 |
} |
} |
542 |
|
|
543 |
retval = send_tweet(session); |
retval = send_tweet(session); |
544 |
if (retval && !session->bash) { |
if (retval && !session->bash) |
545 |
fprintf(stderr, "tweet failed\n"); |
fprintf(stderr, "tweet failed\n"); |
|
return -1; |
|
|
} |
|
546 |
|
|
547 |
session_free(session); |
// log_session(session, retval); |
548 |
exit: |
exit: |
549 |
return 0; |
session_free(session); |
550 |
|
return retval;; |
551 |
} |
} |