commit 6a0ae7c78c10e6b201bd973c16d6fb9d8f28feed
parent 4b4a2cc86cb26c3eb8fb1ba2c819962465b5f7f6
Author: Andreas Gruhler <agruhl@gmx.ch>
Date: Sun, 16 May 2021 22:21:14 +0200
add utils and fix scope
Diffstat:
7 files changed, 50 insertions(+), 39 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,5 +1,5 @@
TARGET = diary
-SRC = caldav.c diary.c
+SRC = utils.c caldav.c diary.c
PREFIX ?= /usr/local
BINDIR ?= $(DESTDIR)$(PREFIX)/bin
diff --git a/caldav.c b/caldav.c
@@ -146,7 +146,7 @@ void set_access_token(char* code, char* verifier) {
CURLcode res;
int token_ttl;
- char postfields[300];
+ char postfields[500];
sprintf(postfields, "client_id=%s&client_secret=%s&code=%s&code_verifier=%s&grant_type=authorization_code&redirect_uri=http://%s:%i",
GOOGLE_OAUTH_CLIENT_ID,
GOOGLE_OAUTH_CLIENT_SECRET,
@@ -224,7 +224,7 @@ void caldav_sync(struct tm* date, WINDOW* header) {
}
// Show Google OAuth URI
- char uri[300];
+ char uri[500];
sprintf(uri, "%s?scope=%s&code_challenge=%s&response_type=%s&redirect_uri=http://%s:%i&client_id=%s",
GOOGLE_OAUTH_AUTHZ_URL,
GOOGLE_OAUTH_SCOPE,
diff --git a/caldav.h b/caldav.h
@@ -15,6 +15,7 @@
#include <errno.h>
#include <ncurses.h>
#include <curl/curl.h>
+#include "utils.h"
#define STR(s) #s
#define MKSTR(s) STR(s)
@@ -24,14 +25,14 @@
#define GOOGLE_OAUTH_CODE_VERIFIER_LENGTH 43
#define GOOGLE_OAUTH_AUTHZ_URL "https://accounts.google.com/o/oauth2/auth"
#define GOOGLE_OAUTH_TOKEN_URL "https://oauth2.googleapis.com/token"
-#define GOOGLE_OAUTH_SCOPE "https://www.googleapis.com/auth/calendar.events.owned"
+#define GOOGLE_OAUTH_SCOPE "https://www.googleapis.com/auth/calendar%20https://www.googleapis.com/auth/calendar.events.owned"
#define GOOGLE_OAUTH_RESPONSE_TYPE "code"
#define GOOGLE_OAUTH_REDIRECT_HOST "127.0.0.1"
#define GOOGLE_OAUTH_REDIRECT_PORT 9004
#define GOOGLE_OAUTH_REDIRECT_URI "http://" GOOGLE_OAUTH_REDIRECT_HOST ":" MKSTR(GOOGLE_OAUTH_REDIRECT_PORT)
#define GOOGLE_OAUTH_REDIRECT_SOCKET_BACKLOG 10
-void caldav_sync(const struct tm* date, WINDOW* header);
+void caldav_sync(struct tm* date, WINDOW* header);
struct curl_mem_chunk {
char* memory;
diff --git a/diary.c b/diary.c
@@ -85,7 +85,7 @@ void update_date(WINDOW* header)
{
char dstr[16];
mktime(&curs_date);
- get_date_str(&curs_date, dstr, sizeof dstr);
+ get_date_str(&curs_date, dstr, sizeof dstr, CONFIG.fmt);
wclear(header);
mvwaddstr(header, 0, 0, dstr);
@@ -203,11 +203,6 @@ bool date_has_entry(const char* dir, size_t dir_size, const struct tm* i)
return (access(epath, F_OK) != -1);
}
-void get_date_str(const struct tm* date, char* date_str, size_t date_str_size)
-{
- strftime(date_str, date_str_size, CONFIG.fmt, date);
-}
-
/* Writes file path for 'date' entry to 'rpath'. '*rpath' is NULL on error. */
void fpath(const char* dir, size_t dir_size, const struct tm* date, char** rpath, size_t rpath_size)
{
@@ -233,7 +228,7 @@ void fpath(const char* dir, size_t dir_size, const struct tm* date, char** rpath
}
char dstr[16];
- get_date_str(date, dstr, sizeof dstr);
+ get_date_str(date, dstr, sizeof dstr, CONFIG.fmt);
// append date to the result path
if (strlen(*rpath) + strlen(dstr) > rpath_size) {
@@ -644,7 +639,7 @@ int main(int argc, char** argv) {
noecho();
// ask for confirmation
- get_date_str(&curs_date, dstr, sizeof dstr);
+ get_date_str(&curs_date, dstr, sizeof dstr, CONFIG.fmt);
mvwprintw(header, 0, 0, "Delete entry '%s'? [Y/n] ", dstr);
bool conf = false;
while (!conf) {
@@ -701,11 +696,9 @@ int main(int argc, char** argv) {
break;
// Sync with CalDAV server
case 's':
- get_date_str(&curs_date, dstr, sizeof dstr);
- fprintf(stderr, "\nCursor date: %s\n\n", dstr);
-
+ //get_date_str(&curs_date, dstr, sizeof dstr, CONFIG.fmt);
+ mktime(&curs_date);
caldav_sync(&curs_date, header);
-
break;
}
diff --git a/diary.h b/diary.h
@@ -18,6 +18,7 @@
#include <ncurses.h>
#include <locale.h>
#include <langinfo.h>
+#include "utils.h"
#include "caldav.h"
#define XDG_CONFIG_HOME_FALLBACK "~/.config"
@@ -39,28 +40,6 @@ void display_entry(const char* dir, size_t dir_size, const struct tm* date, WIND
void edit_cmd(const char* dir, size_t dir_size, const struct tm* date, char** rcmd, size_t rcmd_size);
bool date_has_entry(const char* dir, size_t dir_size, const struct tm* i);
-void get_date_str(const struct tm* date, char* date_str, size_t date_str_size);
void fpath(const char* dir, size_t dir_size, const struct tm* date, char** rpath, size_t rpath_size);
-typedef struct
-{
- // Path that holds the journal text files
- char* dir;
- // Number of years to show before/after todays date
- int range;
- // 7 = Sunday, 1 = Monday, ..., 6 = Saturday
- int weekday;
- // 2020-12-31
- char* fmt;
- // Editor to open journal files with
- char* editor;
-} config;
-
-config CONFIG = {
- .range = 1,
- .weekday = 1,
- .fmt = "%Y-%m-%d",
- .editor = ""
-};
-
#endif
diff --git a/utils.c b/utils.c
@@ -0,0 +1,13 @@
+#include "utils.h"
+
+config CONFIG = {
+ .range = 1,
+ .weekday = 1,
+ .fmt = "%Y-%m-%d",
+ .editor = ""
+};
+
+void get_date_str(const struct tm* date, char* date_str, size_t date_str_size, const char* fmt)
+{
+ strftime(date_str, date_str_size, fmt, date);
+}
diff --git a/utils.h b/utils.h
@@ -0,0 +1,25 @@
+#ifndef DIARY_UTILS_H
+#define DIARY_UTILS_H
+
+#include <stdlib.h>
+#include <time.h>
+
+typedef struct
+{
+ // Path that holds the journal text files
+ char* dir;
+ // Number of years to show before/after todays date
+ int range;
+ // 7 = Sunday, 1 = Monday, ..., 6 = Saturday
+ int weekday;
+ // 2020-12-31
+ char* fmt;
+ // Editor to open journal files with
+ char* editor;
+} config;
+
+config CONFIG;
+
+void get_date_str(const struct tm* date, char* date_str, size_t date_str_size, const char* fmt);
+
+#endif