Commit 3c0adb38e13e3d33409f3b00887541318b36186c

Source files moved around.
Makefile
(1 / 5)
  
11CC = gcc
22CFLAGS = -Wall -Wextra -std=c99 -pedantic
33LDFLAGS = -lcurl -lssl -lcrypto -lrt -lssl -lcrypto -ldl -lz -lz
4OBJECTS = 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
6timeline:
7 $(CC) $(CFLAGS) -o get_friends_timeline $(LDFLAGS) get_friends_timeline.c
8
4OBJECTS = main.o twitter-actions.o access_url.o url_encode.o authorization-password.o
95
106ctweet : $(OBJECTS)
117 $(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
4it went over.
5* oauth authentication...
  
11#include <curl/curl.h>
22#include "access_url.h"
3#include "constants.h"
43
5int access_url(char *username_password, char* url, char* post_data, int operation) {
4int
5access_url(char *username_password, char* url, char* post_data, int operation) {
66 CURL *curl;
77 CURLcode res;
88
  
11#ifndef ACCESS_URL_h
22#define ACCESS_URL_h
3
4#define DO_GET 1
5#define DO_POST 2
6
37int access_url(char*, char*, char*, int);
48#endif
  
1#include <stdio.h>
2#include <string.h>
3#include "authorization-password.h"
4
5static int
6write_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
26int
27check_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
41int
42get_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}
  
1#ifndef AUTHORIZATION_PASSWORD_h
2#define AUTHORIZATION_PASSWORD_h
3
4#define USER_PASSWORD_SIZE 80
5int check_auth(void);
6int 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
5int 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}
  
1#ifndef CHECK_AUTH_h
2#define CHECK_AUTH_h
3int check_auth(void);
4#endif
  
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
  
1#include <string.h>
2#include <stdio.h>
3#include "get_credentials.h"
4
5int 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}
  
1#ifndef GET_CREDENTIALS_h
2#define GET_CREDENTIALS_h
3int get_credentials(char*, int);
4#endif
  
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
7int 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}
  
1#ifndef GET_FRIENDS_TIMELINE_h
2#define GET_FRIENDS_TIMELINE_h
3int get_friends_timeline(void);
4#endif
main.c
(4 / 4)
  
11#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"
54
6int main(int argc, char *argv[]) {
5int
6main(int argc, char *argv[]) {
77 switch(argc) {
88 case 1:
99 check_auth();
  
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
9int
10get_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
20int
21update_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}
  
1#ifndef TWITTER_ACTIONS_h
2#define TWITTER_ACTIONS_h
3
4#define STATUS_PREFIX_LENGTH 7
5#define STATUS_PREFIX "status="
6
7int update_status(char*);
8int get_friends_timeline(void);
9
10#endif
  
1#include <stdio.h>
2#include <curl/curl.h>
3
4int 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}
  
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
10int 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}
  
1#ifndef UPDATE_STATUS_h
2#define UPDATE_STATUS_h
3int update_status(char*);
4#endif
  
1#include <stdio.h>
2#include "constants.h"
3
4int 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 }
  
1#ifndef WRITE_PREFERENCES_h
2#define WRITE_PREFERENCES_h
3int write_preferences(void);
4#endif