Commit 3c0adb38e13e3d33409f3b00887541318b36186c
- Diff rendering mode:
- inline
- side by side
Makefile
(1 / 5)
|   | |||
| 1 | 1 | CC = gcc | |
| 2 | 2 | CFLAGS = -Wall -Wextra -std=c99 -pedantic | |
| 3 | 3 | LDFLAGS = -lcurl -lssl -lcrypto -lrt -lssl -lcrypto -ldl -lz -lz | |
| 4 | OBJECTS = main.o check_auth.o update_status.o get_friends_timeline.o write_preferences.o get_credentials.o access_url.o url_encode.o | ||
| 5 | |||
| 6 | timeline: | ||
| 7 | $(CC) $(CFLAGS) -o get_friends_timeline $(LDFLAGS) get_friends_timeline.c | ||
| 8 | |||
| 4 | OBJECTS = main.o twitter-actions.o access_url.o url_encode.o authorization-password.o | ||
| 9 | 5 | ||
| 10 | 6 | ctweet : $(OBJECTS) | |
| 11 | 7 | $(CC) -o ctweet $(LDFLAGS) $(OBJECTS) |
TODO.txt
(5 / 0)
|   | |||
| 1 | * Better friends timeline output | ||
| 2 | * Alert when no network connection | ||
| 3 | * Refuse to post when status udate exceeds 140 characters; tell by how much | ||
| 4 | it went over. | ||
| 5 | * oauth authentication... |
access_url.c
(2 / 2)
|   | |||
| 1 | 1 | #include <curl/curl.h> | |
| 2 | 2 | #include "access_url.h" | |
| 3 | #include "constants.h" | ||
| 4 | 3 | ||
| 5 | int access_url(char *username_password, char* url, char* post_data, int operation) { | ||
| 4 | int | ||
| 5 | access_url(char *username_password, char* url, char* post_data, int operation) { | ||
| 6 | 6 | CURL *curl; | |
| 7 | 7 | CURLcode res; | |
| 8 | 8 |
access_url.h
(4 / 0)
|   | |||
| 1 | 1 | #ifndef ACCESS_URL_h | |
| 2 | 2 | #define ACCESS_URL_h | |
| 3 | |||
| 4 | #define DO_GET 1 | ||
| 5 | #define DO_POST 2 | ||
| 6 | |||
| 3 | 7 | int access_url(char*, char*, char*, int); | |
| 4 | 8 | #endif |
authorization-password.c
(59 / 0)
|   | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <string.h> | ||
| 3 | #include "authorization-password.h" | ||
| 4 | |||
| 5 | static int | ||
| 6 | write_preferences(void) { | ||
| 7 | char *filename = "preferences.txt"; | ||
| 8 | char username_password[USER_PASSWORD_SIZE]; | ||
| 9 | FILE *fp = NULL; | ||
| 10 | |||
| 11 | puts("Please enter your username and password, separated by a colon, like this: username:password"); | ||
| 12 | |||
| 13 | fgets(username_password, sizeof username_password, stdin); | ||
| 14 | |||
| 15 | fp = fopen(filename, "w"); | ||
| 16 | if (fp == NULL) { | ||
| 17 | perror("Cannot write to preferences file"); | ||
| 18 | return 1; | ||
| 19 | } | ||
| 20 | fputs(username_password, fp); | ||
| 21 | fclose(fp); | ||
| 22 | puts("Username and password saved."); | ||
| 23 | return 0; | ||
| 24 | } | ||
| 25 | |||
| 26 | int | ||
| 27 | check_auth(void) { | ||
| 28 | FILE *fp = NULL; | ||
| 29 | int success = 0; | ||
| 30 | |||
| 31 | puts("Checking authentication..."); | ||
| 32 | fp = fopen("preferences.txt", "r"); | ||
| 33 | if (fp == NULL) { | ||
| 34 | perror("Unreadable preferences file"); | ||
| 35 | write_preferences(); | ||
| 36 | } | ||
| 37 | |||
| 38 | return success; | ||
| 39 | } | ||
| 40 | |||
| 41 | int | ||
| 42 | get_credentials(char *buffer, int length) { | ||
| 43 | char *p; | ||
| 44 | FILE *fp = NULL; | ||
| 45 | |||
| 46 | fp = fopen("preferences.txt", "r"); | ||
| 47 | if (fp == NULL) { | ||
| 48 | perror("Cannot open file"); | ||
| 49 | return 1; | ||
| 50 | } | ||
| 51 | |||
| 52 | fgets(buffer, length, fp); | ||
| 53 | fclose(fp); | ||
| 54 | |||
| 55 | if ((p = strchr(buffer, '\n')) != NULL) { | ||
| 56 | *p = '\0'; | ||
| 57 | } | ||
| 58 | return 0; | ||
| 59 | } |
authorization-password.h
(8 / 0)
|   | |||
| 1 | #ifndef AUTHORIZATION_PASSWORD_h | ||
| 2 | #define AUTHORIZATION_PASSWORD_h | ||
| 3 | |||
| 4 | #define USER_PASSWORD_SIZE 80 | ||
| 5 | int check_auth(void); | ||
| 6 | int get_credentials(char *, int); | ||
| 7 | |||
| 8 | #endif |
check_auth.c
(0 / 17)
|   | |||
| 1 | #include <stdio.h> | ||
| 2 | #include "check_auth.h" | ||
| 3 | #include "write_preferences.h" | ||
| 4 | |||
| 5 | int check_auth(void) { | ||
| 6 | FILE *fp = NULL; | ||
| 7 | int success = 0; | ||
| 8 | |||
| 9 | puts("Checking authentication..."); | ||
| 10 | fp = fopen("preferences.txt", "r"); | ||
| 11 | if (fp == NULL) { | ||
| 12 | perror("Unreadable preferences file"); | ||
| 13 | write_preferences(); | ||
| 14 | } | ||
| 15 | |||
| 16 | return success; | ||
| 17 | } |
check_auth.h
(0 / 4)
|   | |||
| 1 | #ifndef CHECK_AUTH_h | ||
| 2 | #define CHECK_AUTH_h | ||
| 3 | int check_auth(void); | ||
| 4 | #endif |
constants.h
(0 / 5)
|   | |||
| 1 | #define DO_GET 1 | ||
| 2 | #define DO_POST 2 | ||
| 3 | #define USER_PASSWORD_SIZE 80 | ||
| 4 | #define STATUS_PREFIX "status=" | ||
| 5 | #define STATUS_PREFIX_LENGTH 7 |
get_credentials.c
(0 / 22)
|   | |||
| 1 | #include <string.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include "get_credentials.h" | ||
| 4 | |||
| 5 | int get_credentials(char *buffer, int length) { | ||
| 6 | char *p; | ||
| 7 | FILE *fp = NULL; | ||
| 8 | |||
| 9 | fp = fopen("preferences.txt", "r"); | ||
| 10 | if (fp == NULL) { | ||
| 11 | perror("Cannot open file"); | ||
| 12 | return 1; | ||
| 13 | } | ||
| 14 | |||
| 15 | fgets(buffer, length, fp); | ||
| 16 | fclose(fp); | ||
| 17 | |||
| 18 | if ((p = strchr(buffer, '\n')) != NULL) { | ||
| 19 | *p = '\0'; | ||
| 20 | } | ||
| 21 | return 0; | ||
| 22 | } |
get_credentials.h
(0 / 4)
|   | |||
| 1 | #ifndef GET_CREDENTIALS_h | ||
| 2 | #define GET_CREDENTIALS_h | ||
| 3 | int get_credentials(char*, int); | ||
| 4 | #endif |
get_friends_timeline.c
(0 / 15)
|   | |||
| 1 | #include <stdio.h> | ||
| 2 | #include "access_url.h" | ||
| 3 | #include "get_credentials.h" | ||
| 4 | #include "get_friends_timeline.h" | ||
| 5 | #include "constants.h" | ||
| 6 | |||
| 7 | int get_friends_timeline(void) { | ||
| 8 | char username_password[USER_PASSWORD_SIZE]; | ||
| 9 | |||
| 10 | get_credentials(username_password, USER_PASSWORD_SIZE + 1); | ||
| 11 | |||
| 12 | access_url(username_password, "http://twitter.com/statuses/friends_timeline.xml", NULL, DO_GET); | ||
| 13 | |||
| 14 | return 0; | ||
| 15 | } |
get_friends_timeline.h
(0 / 4)
|   | |||
| 1 | #ifndef GET_FRIENDS_TIMELINE_h | ||
| 2 | #define GET_FRIENDS_TIMELINE_h | ||
| 3 | int get_friends_timeline(void); | ||
| 4 | #endif |
main.c
(4 / 4)
|   | |||
| 1 | 1 | #include <stdio.h> | |
| 2 | #include "check_auth.h" | ||
| 3 | #include "get_friends_timeline.h" | ||
| 4 | #include "update_status.h" | ||
| 2 | #include "authorization-password.h" | ||
| 3 | #include "twitter-actions.h" | ||
| 5 | 4 | ||
| 6 | int main(int argc, char *argv[]) { | ||
| 5 | int | ||
| 6 | main(int argc, char *argv[]) { | ||
| 7 | 7 | switch(argc) { | |
| 8 | 8 | case 1: | |
| 9 | 9 | check_auth(); |
twitter-actions.c
(35 / 0)
|   | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <string.h> | ||
| 4 | #include "authorization-password.h" | ||
| 5 | #include "access_url.h" | ||
| 6 | #include "url_encode.h" | ||
| 7 | #include "twitter-actions.h" | ||
| 8 | |||
| 9 | int | ||
| 10 | get_friends_timeline(void) { | ||
| 11 | char username_password[USER_PASSWORD_SIZE]; | ||
| 12 | |||
| 13 | get_credentials(username_password, USER_PASSWORD_SIZE + 1); | ||
| 14 | |||
| 15 | access_url(username_password, "http://twitter.com/statuses/friends_timeline.xml", NULL, DO_GET); | ||
| 16 | |||
| 17 | return 0; | ||
| 18 | } | ||
| 19 | |||
| 20 | int | ||
| 21 | update_status(char *message) { | ||
| 22 | char username_password[USER_PASSWORD_SIZE]; | ||
| 23 | const char *status_prefix = STATUS_PREFIX; | ||
| 24 | char *encoded = url_encode(message); | ||
| 25 | char *status_string = calloc(strlen(encoded) + strlen(status_prefix) + 1, sizeof(char)); | ||
| 26 | strncat(status_string, status_prefix, STATUS_PREFIX_LENGTH); | ||
| 27 | strncat(status_string, encoded, 420 - STATUS_PREFIX_LENGTH); | ||
| 28 | get_credentials(username_password, USER_PASSWORD_SIZE + 1); | ||
| 29 | access_url(username_password, "http://twitter.com/statuses/update.xml", status_string, DO_POST); | ||
| 30 | free(status_string); | ||
| 31 | free (encoded); | ||
| 32 | printf("Updating status...\n"); | ||
| 33 | |||
| 34 | return 0; | ||
| 35 | } |
twitter-actions.h
(10 / 0)
|   | |||
| 1 | #ifndef TWITTER_ACTIONS_h | ||
| 2 | #define TWITTER_ACTIONS_h | ||
| 3 | |||
| 4 | #define STATUS_PREFIX_LENGTH 7 | ||
| 5 | #define STATUS_PREFIX "status=" | ||
| 6 | |||
| 7 | int update_status(char*); | ||
| 8 | int get_friends_timeline(void); | ||
| 9 | |||
| 10 | #endif |
twitter_client.c
(0 / 17)
|   | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <curl/curl.h> | ||
| 3 | |||
| 4 | int main(void) { | ||
| 5 | CURL *curl; | ||
| 6 | CURLcode res; | ||
| 7 | |||
| 8 | curl = curl_easy_init(); | ||
| 9 | if (curl) { | ||
| 10 | curl_easy_setopt(curl, CURLOPT_URL, "http://twitter.com/statuses/public_timeline.rss"); | ||
| 11 | res = curl_easy_perform(curl); | ||
| 12 | |||
| 13 | curl_easy_cleanup(curl); | ||
| 14 | } | ||
| 15 | |||
| 16 | return 0; | ||
| 17 | } |
update_status.c
(0 / 24)
|   | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <string.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include "update_status.h" | ||
| 5 | #include "access_url.h" | ||
| 6 | #include "get_credentials.h" | ||
| 7 | #include "url_encode.h" | ||
| 8 | #include "constants.h" | ||
| 9 | |||
| 10 | int update_status(char *message) { | ||
| 11 | const char *status_prefix = STATUS_PREFIX; | ||
| 12 | char *encoded = url_encode(message); | ||
| 13 | char *status_string = calloc(strlen(encoded) + strlen(status_prefix) + 1, sizeof(char)); | ||
| 14 | strncat(status_string, status_prefix, STATUS_PREFIX_LENGTH); | ||
| 15 | strncat(status_string, encoded, 420 - STATUS_PREFIX_LENGTH); | ||
| 16 | char username_password[USER_PASSWORD_SIZE]; | ||
| 17 | get_credentials(username_password, USER_PASSWORD_SIZE + 1); | ||
| 18 | access_url(username_password, "http://twitter.com/statuses/update.xml", status_string, DO_POST); | ||
| 19 | free(status_string); | ||
| 20 | free (encoded); | ||
| 21 | printf("Updating status...\n"); | ||
| 22 | |||
| 23 | return 0; | ||
| 24 | } |
update_status.h
(0 / 4)
|   | |||
| 1 | #ifndef UPDATE_STATUS_h | ||
| 2 | #define UPDATE_STATUS_h | ||
| 3 | int update_status(char*); | ||
| 4 | #endif |
write_preferences.c
(0 / 22)
|   | |||
| 1 | #include <stdio.h> | ||
| 2 | #include "constants.h" | ||
| 3 | |||
| 4 | int write_preferences(void) { | ||
| 5 | char *filename = "preferences.txt"; | ||
| 6 | char username_password[USER_PASSWORD_SIZE]; | ||
| 7 | FILE *fp = NULL; | ||
| 8 | |||
| 9 | puts("Please enter your username and password, separated by a colon, like this: username:password"); | ||
| 10 | |||
| 11 | fgets(username_password, sizeof username_password, stdin); | ||
| 12 | |||
| 13 | fp = fopen(filename, "w"); | ||
| 14 | if (fp == NULL) { | ||
| 15 | perror("Cannot write to preferences file"); | ||
| 16 | return 1; | ||
| 17 | } | ||
| 18 | fputs(username_password, fp); | ||
| 19 | fclose(fp); | ||
| 20 | puts("Username and password saved."); | ||
| 21 | return 0; | ||
| 22 | } |
write_preferences.h
(0 / 4)
|   | |||
| 1 | #ifndef WRITE_PREFERENCES_h | ||
| 2 | #define WRITE_PREFERENCES_h | ||
| 3 | int write_preferences(void); | ||
| 4 | #endif |

