1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-12-22 22:43:25 +01:00

Add ability to print your Tox ID's QR code to a file

This commit is contained in:
Jfreegman 2015-11-08 22:51:46 -05:00
parent c5d9aca3e1
commit 39c4b7ecdd
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
14 changed files with 259 additions and 26 deletions

View File

@ -16,6 +16,7 @@
| [LibConfig](http://www.hyperrealm.com/libconfig) | BASE | libconfig-dev |
| [GNUmake](https://www.gnu.org/software/make) | BASE | make |
| [libcurl](http://curl.haxx.se/) | BASE | libcurl4-openssl-dev|
| [libqrencode](https://fukuchi.org/works/qrencode/) | BASE | libqrencode-dev |
| [Tox Core AV](https://github.com/irungentoo/toxcore) | AUDIO | *None* |
| [OpenAL](http://openal.org) | AUDIO, SOUND NOTIFICATIONS | libopenal-dev |
| [OpenALUT](http://openal.org) | SOUND NOTIFICATIONS | libalut-dev |

View File

@ -3,7 +3,7 @@ CFG_DIR = $(BASE_DIR)/cfg
-include $(CFG_DIR)/global_vars.mk
LIBS = libtoxcore ncursesw libconfig
LIBS = libtoxcore ncursesw libconfig libqrencode
CFLAGS = -std=gnu99 -pthread -Wall -g
CFLAGS += '-DTOXICVER="$(VERSION)"' -DHAVE_WIDECHAR -D_XOPEN_SOURCE_EXTENDED -D_FILE_OFFSET_BITS=64
@ -14,7 +14,7 @@ LDFLAGS = $(USER_LDFLAGS)
OBJ = chat.o chat_commands.o configdir.o execute.o file_transfers.o notify.o
OBJ += friendlist.o global_commands.o groupchat.o line_info.o input.o help.o autocomplete.o
OBJ += log.o misc_tools.o prompt.o settings.o toxic.o toxic_strings.o windows.o message_queue.o
OBJ += group_commands.o term_mplex.o avatars.o name_lookup.o
OBJ += group_commands.o term_mplex.o avatars.o name_lookup.o qr_code.o
# Check on wich system we are running
UNAME_S = $(shell uname -s)

View File

@ -63,9 +63,9 @@ static void kill_infobox(ToxWindow *self);
#endif /* AUDIO */
#ifdef AUDIO
#define AC_NUM_CHAT_COMMANDS 28
#define AC_NUM_CHAT_COMMANDS 29
#else
#define AC_NUM_CHAT_COMMANDS 21
#define AC_NUM_CHAT_COMMANDS 22
#endif /* AUDIO */
/* Array of chat command names used for tab completion. */
@ -84,6 +84,7 @@ static const char chat_cmd_list[AC_NUM_CHAT_COMMANDS][MAX_CMDNAME_SIZE] = {
{ "/join" },
{ "/log" },
{ "/myid" },
{ "/myqr" },
{ "/nick" },
{ "/note" },
{ "/nospam" },

View File

@ -51,6 +51,7 @@ static struct cmd_func global_commands[] = {
{ "/help", cmd_prompt_help },
{ "/log", cmd_log },
{ "/myid", cmd_myid },
{ "/myqr", cmd_myqr },
{ "/nick", cmd_nick },
{ "/note", cmd_note },
{ "/nospam", cmd_nospam },

View File

@ -26,7 +26,6 @@
#include <time.h>
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <tox/tox.h>

View File

@ -22,7 +22,6 @@
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include "toxic.h"
#include "windows.h"
@ -36,6 +35,7 @@
#include "term_mplex.h"
#include "avatars.h"
#include "name_lookup.h"
#include "qr_code.h"
extern char *DATA_FILE;
extern ToxWindow *prompt;
@ -415,19 +415,57 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
void cmd_myid(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
char id[TOX_ADDRESS_SIZE * 2 + 1] = {0};
char address[TOX_ADDRESS_SIZE];
tox_self_get_address(m, (uint8_t *) address);
char id_string[TOX_ADDRESS_SIZE * 2 + 1];
char bin_id[TOX_ADDRESS_SIZE];
tox_self_get_address(m, (uint8_t *) bin_id);
size_t i;
for (i = 0; i < TOX_ADDRESS_SIZE; ++i) {
char xx[3];
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
strcat(id, xx);
if (bin_id_to_string(bin_id, sizeof(bin_id), id_string, sizeof(id_string)) == -1) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to print ID.");
return;
}
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", id);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", id_string);
}
void cmd_myqr(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
char id_string[TOX_ADDRESS_SIZE * 2 + 1];
char bin_id[TOX_ADDRESS_SIZE];
tox_self_get_address(m, (uint8_t *) bin_id);
if (bin_id_to_string(bin_id, sizeof(bin_id), id_string, sizeof(id_string)) == -1) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code.");
return;
}
char nick[TOX_MAX_NAME_LENGTH];
tox_self_get_name(m, (uint8_t *) nick);
size_t nick_len = tox_self_get_name_size(m);
nick[nick_len] = '\0';
size_t data_file_len = strlen(DATA_FILE);
char dir[data_file_len];
size_t dir_len = get_base_dir(DATA_FILE, data_file_len, dir);
char qr_path[dir_len + nick_len + strlen(QRCODE_FILENAME_EXT) + 1];
snprintf(qr_path, sizeof(qr_path), "%s%s%s", dir, nick, QRCODE_FILENAME_EXT);
FILE *output = fopen(qr_path, "wb");
if (output == NULL) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code.");
return;
}
if (ID_to_QRcode(id_string, output) == -1) {
fclose(output);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code.");
return;
}
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "QR code has been printed to the file '%s'", qr_path);
fclose(output);
}
void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])

View File

@ -35,6 +35,7 @@ void cmd_decline(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)
void cmd_groupchat(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_log(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_myid(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_myqr(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_nick(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_note(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_nospam(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);

View File

@ -70,9 +70,9 @@ extern struct user_settings *user_settings;
extern struct Winthread Winthread;
#ifdef AUDIO
#define AC_NUM_GROUP_COMMANDS 23
#define AC_NUM_GROUP_COMMANDS 24
#else
#define AC_NUM_GROUP_COMMANDS 19
#define AC_NUM_GROUP_COMMANDS 20
#endif /* AUDIO */
/* Array of groupchat command names used for tab completion. */
@ -89,6 +89,7 @@ static const char group_cmd_list[AC_NUM_GROUP_COMMANDS][MAX_CMDNAME_SIZE] = {
{ "/help" },
{ "/log" },
{ "/myid" },
{ "/myqr" },
{ "/nick" },
{ "/note" },
{ "/nospam" },

View File

@ -158,6 +158,7 @@ static void help_draw_global(ToxWindow *self)
wprintw(win, " /log <on> or <off> : Enable/disable logging\n");
wprintw(win, " /group <type> : Create a group chat where type: text | audio\n");
wprintw(win, " /myid : Print your Tox ID\n");
wprintw(win, " /myqr : Print your Tox ID's QR code to a file.\n");
wprintw(win, " /clear : Clear window history\n");
wprintw(win, " /close : Close the current chat window\n");
wprintw(win, " /quit or /exit : Exit Toxic\n");
@ -314,11 +315,11 @@ void help_onKey(ToxWindow *self, wint_t key)
case 'g':
#ifdef VIDEO
help_init_window(self, 29, 80);
help_init_window(self, 30, 80);
#elif AUDIO
help_init_window(self, 25, 80);
help_init_window(self, 26, 80);
#else
help_init_window(self, 21, 80);
help_init_window(self, 22, 80);
#endif
self->help->type = HELP_GLOBAL;
break;

View File

@ -149,6 +149,24 @@ int hex_string_to_bytes(char *buf, int size, const char *keystr)
return 0;
}
/* Converts a binary representation of a Tox ID into a string.
*
* Returns 0 on success.
* Returns -1 on failure.
*/
int bin_id_to_string(const char *bin_id, size_t bin_id_size, char *output, size_t output_size)
{
if (bin_id_size != TOX_ADDRESS_SIZE || output_size < (TOX_ADDRESS_SIZE * 2 + 1))
return -1;
size_t i;
for (i = 0; i < TOX_ADDRESS_SIZE; ++i)
snprintf(&output[i*2], output_size - (i * 2), "%02X", bin_id[i] & 0xff);
return 0;
}
/* Returns 1 if the string is empty, 0 otherwise */
int string_is_empty(const char *string)
{
@ -263,6 +281,24 @@ size_t get_file_name(char *namebuf, size_t bufsize, const char *pathname)
return strlen(namebuf);
}
/* Gets the base directory of path and puts it in dir.
* dir must have at least as much space as path_len.
*
* Returns the length of the base directory.
*/
size_t get_base_dir(const char *path, size_t path_len, char *dir)
{
size_t dir_len = char_rfind(path, '/', path_len);
if (dir_len != 0 && dir_len < path_len)
++dir_len; /* Leave trailing slash */
memcpy(dir, path, dir_len);
dir[dir_len] = '\0';
return dir_len;
}
/* converts str to all lowercase */
void str_to_lower(char *str)
{
@ -332,8 +368,8 @@ int char_find(int idx, const char *s, char ch)
return i;
}
/* returns index of the last instance of ch in s starting at len
returns 0 if char not found (skips 0th index) */
/* returns index of the last instance of ch in s starting at len.
returns 0 if char not found (skips 0th index). */
int char_rfind(const char *s, char ch, int len)
{
int i = 0;

View File

@ -53,6 +53,13 @@ int hex_string_to_bin(const char *hex_string, size_t hex_len, char *output, size
/* convert a hex string to bytes. returns 0 on success, -1 on failure */
int hex_string_to_bytes(char *buf, int size, const char *keystr);
/* Converts a binary representation of a Tox ID into a string.
*
* Returns 0 on success.
* Returns -1 on failure.
*/
int bin_id_to_string(const char *bin_id, size_t bin_id_size, char *output, size_t output_size);
/* get the current unix time (not thread safe) */
uint64_t get_unix_time(void);
@ -103,6 +110,14 @@ void filter_str(char *str, size_t len);
/* gets base file name from path or original file name if no path is supplied */
size_t get_file_name(char *namebuf, size_t bufsize, const char *pathname);
/* Gets the base directory of path and puts it in dir.
* dir must have at least as much space as path_len.
*
* Returns the length of the base directory on success.
* Returns -1 on failure.
*/
size_t get_base_dir(const char *path, size_t path_len, char *dir);
/* converts str to all lowercase */
void str_to_lower(char *str);

View File

@ -50,11 +50,11 @@ extern struct Winthread Winthread;
extern FriendsList Friends;
FriendRequests FrndRequests;
#ifdef VIDEO
#define AC_NUM_GLOB_COMMANDS 21
#define AC_NUM_GLOB_COMMANDS 22
#elif AUDIO
#define AC_NUM_GLOB_COMMANDS 19
#define AC_NUM_GLOB_COMMANDS 20
#else
#define AC_NUM_GLOB_COMMANDS 17
#define AC_NUM_GLOB_COMMANDS 18
#endif
/* Array of global command names used for tab completion. */
@ -70,6 +70,7 @@ static const char glob_cmd_list[AC_NUM_GLOB_COMMANDS][MAX_CMDNAME_SIZE] = {
{ "/help" },
{ "/log" },
{ "/myid" },
{ "/myqr" },
{ "/nick" },
{ "/note" },
{ "/nospam" },

103
src/qr_code.c Normal file
View File

@ -0,0 +1,103 @@
/* qr_obj_code.c
*
*
* Copyright (C) 2015 Toxic All Rights Reserved.
*
* This file is part of Toxic.
*
* Toxic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Toxic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <qrencode.h>
#include <stdbool.h>
#include "toxic.h"
#include "windows.h"
#include "qr_code.h"
#define BORDER_CHAR "|"
#define BORDER_LEN 1
#define CHAR_1 "\342\226\210"
#define CHAR_2 "\342\226\204"
#define CHAR_3 "\342\226\200"
static void add_border(FILE *fp, size_t width)
{
size_t i, j;
for (i = 0; i < BORDER_LEN; i += 2) {
fprintf(fp, BORDER_CHAR);
for (j = 0; j < BORDER_LEN + width + BORDER_LEN; ++j)
fprintf(fp, "%s", CHAR_1);
fprintf(fp, "%s\n", BORDER_CHAR);
}
}
/* Converts a tox ID string into a QRcode and prints it to the given file stream.
*
* Returns 0 on success.
* Returns -1 on failure.
*/
int ID_to_QRcode(const char *tox_id, FILE *fp)
{
if (fp == NULL)
return -1;
QRcode *qr_obj = QRcode_encodeString(tox_id, 0, QR_ECLEVEL_L, QR_MODE_8, 0);
if (qr_obj == NULL)
return -1;
size_t width = qr_obj->width;
add_border(fp, width);
size_t i, j;
for (i = 0; i < width; i += 2) {
const unsigned char *row_1 = qr_obj->data + width * i;
const unsigned char *row_2 = row_1 + width;
fprintf(fp, BORDER_CHAR);
for (j = 0; j < BORDER_LEN; ++j)
fprintf(fp, "%s", CHAR_1);
for (j = 0; j < width; ++j) {
bool x = row_1[j] & 1;
bool y = (i + 1) < width ? (row_2[j] & 1) : false;
if (x && y)
fprintf(fp, " ");
else if (x)
fprintf(fp, "%s", CHAR_2);
else if (y)
fprintf(fp, "%s", CHAR_3);
else
fprintf(fp, "%s", CHAR_1);
}
for (j = 0; j < BORDER_LEN; ++j)
fprintf(fp, "%s", CHAR_1);
fprintf(fp, "%s\n", BORDER_CHAR);
}
QRcode_free(qr_obj);
return 0;
}

35
src/qr_code.h Normal file
View File

@ -0,0 +1,35 @@
/* qr_code.h
*
*
* Copyright (C) 2015 Toxic All Rights Reserved.
*
* This file is part of Toxic.
*
* Toxic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Toxic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef QR_CODE
#define QR_CODE
#define QRCODE_FILENAME_EXT ".QRcode"
/* Converts a tox ID string into a QRcode and prints it to the given file stream.
*
* Returns 0 on success.
* Returns -1 on failure.
*/
int ID_to_QRcode(const char *tox_id, FILE *fp);
#endif /* QR_CODE */