Add vCard support

Only nicknames, photos, birthdays, addresses, telephone numbers, emails,
JIDs, titles, roles, notes, and URLs are supported

Due to the synopsis array not having enough space, `/vcard photo
open-self` and `/vcard photo save-self` are not documented properly in
the synopsis section of the `/vcard` command, but they are documented in
the arguments section

Fixed memory leak in vcard autocomplete (thanks to debXwoody)
This commit is contained in:
Marouane L
2022-09-06 17:29:07 +01:00
parent fc8455ba34
commit f934c5b59f
25 changed files with 3688 additions and 7 deletions

View File

@@ -41,6 +41,7 @@
#include <assert.h>
#include <libgen.h>
#include <dirent.h>
#include <ctype.h>
#include "common.h"
#include "config/preferences.h"
@@ -131,6 +132,7 @@ static char* _lastactivity_autocomplete(ProfWin* window, const char* const input
static char* _intype_autocomplete(ProfWin* window, const char* const input, gboolean previous);
static char* _mood_autocomplete(ProfWin* window, const char* const input, gboolean previous);
static char* _adhoc_cmd_autocomplete(ProfWin* window, const char* const input, gboolean previous);
static char* _vcard_autocomplete(ProfWin* window, const char* const input, gboolean previous);
static char* _script_autocomplete_func(const char* const prefix, gboolean previous, void* context);
@@ -276,6 +278,15 @@ static Autocomplete mood_ac;
static Autocomplete mood_type_ac;
static Autocomplete adhoc_cmd_ac;
static Autocomplete lastactivity_ac;
static Autocomplete vcard_ac;
static Autocomplete vcard_photo_ac;
static Autocomplete vcard_element_ac;
static Autocomplete vcard_set_ac;
static Autocomplete vcard_name_ac;
static Autocomplete vcard_set_param_ac;
static Autocomplete vcard_togglable_param_ac;
static Autocomplete vcard_toggle_ac;
static Autocomplete vcard_address_type_ac;
/*!
* \brief Initialization of auto completion for commands.
@@ -1181,6 +1192,89 @@ cmd_ac_init(void)
lastactivity_ac = autocomplete_new();
autocomplete_add(lastactivity_ac, "set");
autocomplete_add(lastactivity_ac, "get");
vcard_ac = autocomplete_new();
autocomplete_add(vcard_ac, "get");
autocomplete_add(vcard_ac, "photo");
autocomplete_add(vcard_ac, "set");
autocomplete_add(vcard_ac, "add");
autocomplete_add(vcard_ac, "remove");
autocomplete_add(vcard_ac, "save");
vcard_photo_ac = autocomplete_new();
autocomplete_add(vcard_photo_ac, "open");
autocomplete_add(vcard_photo_ac, "save");
vcard_element_ac = autocomplete_new();
autocomplete_add(vcard_element_ac, "nickname");
autocomplete_add(vcard_element_ac, "birthday");
autocomplete_add(vcard_element_ac, "address");
autocomplete_add(vcard_element_ac, "tel");
autocomplete_add(vcard_element_ac, "email");
autocomplete_add(vcard_element_ac, "jid");
autocomplete_add(vcard_element_ac, "title");
autocomplete_add(vcard_element_ac, "role");
autocomplete_add(vcard_element_ac, "note");
autocomplete_add(vcard_element_ac, "url");
vcard_set_ac = autocomplete_new();
autocomplete_add(vcard_set_ac, "fullname");
autocomplete_add(vcard_set_ac, "name");
vcard_name_ac = autocomplete_new();
autocomplete_add(vcard_name_ac, "family");
autocomplete_add(vcard_name_ac, "given");
autocomplete_add(vcard_name_ac, "middle");
autocomplete_add(vcard_name_ac, "prefix");
autocomplete_add(vcard_name_ac, "suffix");
vcard_set_param_ac = autocomplete_new();
autocomplete_add(vcard_set_param_ac, "pobox");
autocomplete_add(vcard_set_param_ac, "extaddr");
autocomplete_add(vcard_set_param_ac, "street");
autocomplete_add(vcard_set_param_ac, "locality");
autocomplete_add(vcard_set_param_ac, "region");
autocomplete_add(vcard_set_param_ac, "pocode");
autocomplete_add(vcard_set_param_ac, "country");
autocomplete_add(vcard_set_param_ac, "type");
autocomplete_add(vcard_set_param_ac, "home");
autocomplete_add(vcard_set_param_ac, "work");
autocomplete_add(vcard_set_param_ac, "voice");
autocomplete_add(vcard_set_param_ac, "fax");
autocomplete_add(vcard_set_param_ac, "pager");
autocomplete_add(vcard_set_param_ac, "msg");
autocomplete_add(vcard_set_param_ac, "cell");
autocomplete_add(vcard_set_param_ac, "video");
autocomplete_add(vcard_set_param_ac, "bbs");
autocomplete_add(vcard_set_param_ac, "modem");
autocomplete_add(vcard_set_param_ac, "isdn");
autocomplete_add(vcard_set_param_ac, "pcs");
autocomplete_add(vcard_set_param_ac, "preferred");
autocomplete_add(vcard_set_param_ac, "x400");
vcard_togglable_param_ac = autocomplete_new();
autocomplete_add(vcard_togglable_param_ac, "home");
autocomplete_add(vcard_togglable_param_ac, "work");
autocomplete_add(vcard_togglable_param_ac, "voice");
autocomplete_add(vcard_togglable_param_ac, "fax");
autocomplete_add(vcard_togglable_param_ac, "pager");
autocomplete_add(vcard_togglable_param_ac, "msg");
autocomplete_add(vcard_togglable_param_ac, "cell");
autocomplete_add(vcard_togglable_param_ac, "video");
autocomplete_add(vcard_togglable_param_ac, "bbs");
autocomplete_add(vcard_togglable_param_ac, "modem");
autocomplete_add(vcard_togglable_param_ac, "isdn");
autocomplete_add(vcard_togglable_param_ac, "pcs");
autocomplete_add(vcard_togglable_param_ac, "preferred");
autocomplete_add(vcard_togglable_param_ac, "x400");
vcard_toggle_ac = autocomplete_new();
autocomplete_add(vcard_toggle_ac, "on");
autocomplete_add(vcard_toggle_ac, "off");
vcard_address_type_ac = autocomplete_new();
autocomplete_add(vcard_address_type_ac, "domestic");
autocomplete_add(vcard_address_type_ac, "international");
}
void
@@ -1502,6 +1596,17 @@ cmd_ac_reset(ProfWin* window)
autocomplete_reset(mood_ac);
autocomplete_reset(mood_type_ac);
autocomplete_reset(adhoc_cmd_ac);
autocomplete_reset(vcard_ac);
autocomplete_reset(vcard_photo_ac);
autocomplete_reset(vcard_element_ac);
autocomplete_reset(vcard_set_ac);
autocomplete_reset(vcard_name_ac);
autocomplete_reset(vcard_set_param_ac);
autocomplete_reset(vcard_togglable_param_ac);
autocomplete_reset(vcard_toggle_ac);
autocomplete_reset(vcard_address_type_ac);
autocomplete_reset(script_ac);
autocomplete_reset(lastactivity_ac);
@@ -1672,6 +1777,15 @@ cmd_ac_uninit(void)
autocomplete_free(intype_ac);
autocomplete_free(adhoc_cmd_ac);
autocomplete_free(lastactivity_ac);
autocomplete_free(vcard_ac);
autocomplete_free(vcard_photo_ac);
autocomplete_free(vcard_element_ac);
autocomplete_free(vcard_set_ac);
autocomplete_free(vcard_name_ac);
autocomplete_free(vcard_set_param_ac);
autocomplete_free(vcard_togglable_param_ac);
autocomplete_free(vcard_toggle_ac);
autocomplete_free(vcard_address_type_ac);
}
static void
@@ -1944,6 +2058,7 @@ _cmd_ac_complete_params(ProfWin* window, const char* const input, gboolean previ
g_hash_table_insert(ac_funcs, "/intype", _intype_autocomplete);
g_hash_table_insert(ac_funcs, "/mood", _mood_autocomplete);
g_hash_table_insert(ac_funcs, "/cmd", _adhoc_cmd_autocomplete);
g_hash_table_insert(ac_funcs, "/vcard", _vcard_autocomplete);
int len = strlen(input);
char parsed[len + 1];
@@ -4322,3 +4437,138 @@ _adhoc_cmd_autocomplete(ProfWin* window, const char* const input, gboolean previ
return result;
}
static char*
_vcard_autocomplete(ProfWin* window, const char* const input, gboolean previous)
{
char* result = NULL;
gboolean parse_result = FALSE;
gchar** args = parse_args(input, 0, 7, &parse_result);
if (parse_result && (g_strcmp0(args[0], "set") == 0)) {
gboolean space_at_end = g_str_has_suffix(input, " ");
int num_args = g_strv_length(args);
gboolean is_num = TRUE;
if (num_args >= 2) {
for (int i = 0; i < strlen(args[1]); i++) {
if (!isdigit((int)args[1][i])) {
is_num = FALSE;
break;
}
}
}
if ((num_args == 2 && space_at_end && is_num) || (num_args == 3 && !space_at_end && is_num)) {
GString* beginning = g_string_new("/vcard");
g_string_append_printf(beginning, " %s %s", args[0], args[1]);
result = autocomplete_param_with_ac(input, beginning->str, vcard_set_param_ac, TRUE, previous);
g_string_free(beginning, TRUE);
if (result) {
g_strfreev(args);
return result;
}
} else if ((num_args == 3 && space_at_end && is_num && (g_strcmp0(args[2], "type") == 0)) || (num_args == 4 && !space_at_end && is_num && (g_strcmp0(args[2], "type") == 0))) {
GString* beginning = g_string_new("/vcard");
g_string_append_printf(beginning, " %s %s %s", args[0], args[1], args[2]);
result = autocomplete_param_with_ac(input, beginning->str, vcard_address_type_ac, TRUE, previous);
g_string_free(beginning, TRUE);
if (result) {
g_strfreev(args);
return result;
}
} else if ((num_args == 3 && space_at_end && is_num && autocomplete_contains(vcard_togglable_param_ac, args[2])) || (num_args == 4 && !space_at_end && is_num && autocomplete_contains(vcard_togglable_param_ac, args[2]))) {
GString* beginning = g_string_new("/vcard");
g_string_append_printf(beginning, " %s %s %s", args[0], args[1], args[2]);
result = autocomplete_param_with_ac(input, beginning->str, vcard_toggle_ac, TRUE, previous);
g_string_free(beginning, TRUE);
if (result) {
g_strfreev(args);
return result;
}
} else {
result = autocomplete_param_with_ac(input, "/vcard set name", vcard_name_ac, TRUE, previous);
if (result) {
return result;
}
result = autocomplete_param_with_ac(input, "/vcard set", vcard_set_ac, TRUE, previous);
if (result) {
return result;
}
}
}
result = autocomplete_param_with_ac(input, "/vcard add", vcard_element_ac, TRUE, previous);
if (result) {
return result;
}
if (window->type == WIN_MUC) {
char* unquoted = strip_arg_quotes(input);
ProfMucWin* mucwin = (ProfMucWin*)window;
assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
Autocomplete nick_ac = muc_roster_ac(mucwin->roomjid);
if (nick_ac) {
result = autocomplete_param_with_ac(unquoted, "/vcard get", nick_ac, TRUE, previous);
if (result) {
free(unquoted);
return result;
}
result = autocomplete_param_with_ac(unquoted, "/vcard photo open", nick_ac, TRUE, previous);
if (result) {
free(unquoted);
return result;
}
result = autocomplete_param_with_ac(unquoted, "/vcard photo save", nick_ac, TRUE, previous);
if (result) {
free(unquoted);
return result;
}
}
free(unquoted);
} else {
char* unquoted = strip_arg_quotes(input);
result = autocomplete_param_with_func(unquoted, "/vcard get", roster_contact_autocomplete, previous, NULL);
if (result) {
free(unquoted);
return result;
}
result = autocomplete_param_with_func(unquoted, "/vcard photo open", roster_contact_autocomplete, previous, NULL);
if (result) {
free(unquoted);
return result;
}
result = autocomplete_param_with_func(unquoted, "/vcard photo save", roster_contact_autocomplete, previous, NULL);
if (result) {
free(unquoted);
return result;
}
free(unquoted);
}
result = autocomplete_param_with_ac(input, "/vcard photo", vcard_photo_ac, TRUE, previous);
if (result) {
return result;
}
result = autocomplete_param_with_ac(input, "/vcard", vcard_ac, TRUE, previous);
if (result) {
return result;
}
return result;
}

View File

@@ -1268,7 +1268,8 @@ static struct cmd_t command_defs[] = {
"/time all|console|chat|muc|config|private|xml off",
"/time statusbar set <format>",
"/time statusbar off",
"/time lastactivity set <format>")
"/time lastactivity set <format>",
"/time vcard set <format>")
CMD_DESC(
"Configure time display preferences. "
"Time formats are strings supported by g_date_time_format. "
@@ -1291,7 +1292,8 @@ static struct cmd_t command_defs[] = {
{ "statusbar set <format>", "Change time format in statusbar." },
{ "statusbar off", "Do not show time in status bar." },
{ "lastactivity set <format>", "Change time format for last activity." },
{ "all set <format>", "Set time for: console, chat, muc, config, private and xml windows." },
{ "vcard set <format>", "Change the time format used to display time/dates in vCard (such as birthdays)" },
{ "all set <format>", "Set time for: console, chat, muc, config, private, and xml windows." },
{ "all off", "Do not show time for: console, chat, muc, config, private and xml windows." })
CMD_EXAMPLES(
"/time console set %H:%M:%S",
@@ -1582,6 +1584,124 @@ static struct cmd_t command_defs[] = {
"/autoconnect off")
},
{ "/vcard",
parse_args, 0, 7, NULL,
CMD_SUBFUNCS(
{"add", cmd_vcard_add},
{"remove", cmd_vcard_remove},
{"get", cmd_vcard_get},
{"set", cmd_vcard_set},
{"photo", cmd_vcard_photo},
{"refresh", cmd_vcard_refresh},
{"save", cmd_vcard_save})
CMD_MAINFUNC(cmd_vcard)
CMD_TAGS(
CMD_TAG_CHAT,
CMD_TAG_GROUPCHAT)
CMD_SYN(
"/vcard get [<nick|contact>]",
"/vcard photo open <nick|contact> [<index>]",
"/vcard photo save <nick|contact> [output <filepath>] [index <index>]",
"/vcard set fullname <fullname>",
"/vcard set name family <family>",
"/vcard set name given <given>",
"/vcard set name middle <middle>",
"/vcard set name prefix <prefix>",
"/vcard set name suffix <suffix>",
"/vcard set <index> [<value>]",
"/vcard set <index> pobox <value>",
"/vcard set <index> extaddr <value>",
"/vcard set <index> street <value>",
"/vcard set <index> locality <value>",
"/vcard set <index> region <value>",
"/vcard set <index> pocode <value>",
"/vcard set <index> country <value>",
"/vcard set <index> type domestic|international",
"/vcard set <index> home on|off",
"/vcard set <index> work on|off",
"/vcard set <index> voice on|off",
"/vcard set <index> fax on|off",
"/vcard set <index> pager on|off",
"/vcard set <index> msg on|off",
"/vcard set <index> cell on|off",
"/vcard set <index> video on|off",
"/vcard set <index> bbs on|off",
"/vcard set <index> modem on|off",
"/vcard set <index> isdn on|off",
"/vcard set <index> pcs on|off",
"/vcard set <index> preferred on|off",
"/vcard set <index> parcel on|off",
"/vcard set <index> postal on|off",
"/vcard set <index> internet on|off",
"/vcard set <index> x400 on|off",
"/vcard add nickname <nickname>",
"/vcard add birthday <date>",
"/vcard add address",
"/vcard add tel <number>",
"/vcard add email <userid>",
"/vcard add jid <jid>",
"/vcard add title <title>",
"/vcard add role <role>",
"/vcard add note <note>",
"/vcard add url <url>",
"/vcard remove <index>",
"/vcard refresh",
"/vcard save")
CMD_DESC(
"Read your vCard or a user's vCard, get a user's avatar via their vCard, or modify your vCard. If no arguments are given, your vCard will be displayed in a new window, or an existing vCard window.")
CMD_ARGS(
{ "get [<nick|contact>]", "Get your vCard, if a nickname/contact is provided, get that user's vCard" },
{ "photo open <nick|contact> [<index>]", "Download a user's photo from their vCard to a file, and open it. If index is not specified, download the first photo (usually avatar) from their vCard" },
{ "photo save <nick|contact>", "Download a user's photo from their vCard to a file. If index is not specified, download the first photo (usually avatar) from their vCard. If output is not specified, download the photo to profanity's photos directory." },
{ "photo open-self [<index>]", "Download a photo from your vCard to a file, and open it. If index is not specified, download the first photo (usually avatar) from your vCard" },
{ "photo save-self", "Download a photo from your vCard to a file. If index is not specified, download the first photo (usually avatar) from your vCard. If output is not specified, download the photo to profanity's photos directory. Same arguments as `photo open`" },
{ "set fullname <fullname>", "Set your vCard's fullname to the specified value" },
{ "set name family <family>", "Set your vCard's family name to the specified value" },
{ "set name given <given>", "Set your vCard's given name to the specified value" },
{ "set name middle <middle>", "Set your vCard's middle name to the specified value" },
{ "set name prefix <prefix>", "Set your vCard's prefix name to the specified value" },
{ "set name suffix <suffix>", "Set your vCard's suffix name to the specified value" },
{ "set <index> [<value>]", "Set the main field in a element in your vCard to the specified value, or if no value was specified, modify the field in an editor, This only works in elements that have one field." },
{ "set <index> pobox <value>", "Set the P.O. box in an address element in your vCard to the specified value." },
{ "set <index> extaddr <value>", "Set the extended address in an address element in your vCard to the specified value." },
{ "set <index> street <value>", "Set the street in an address element in your vCard to the specified value." },
{ "set <index> locality <value>", "Set the locality in an address element in your vCard to the specified value." },
{ "set <index> region <value>", "Set the region in an address element in your vCard to the specified value." },
{ "set <index> pocode <value>", "Set the P.O. code in an address element in your vCard to the specified value." },
{ "set <index> type domestic|international", "Set the type in an address element in your vCard to either domestic or international." },
{ "set <index> home on|off", "Set the home option in an element in your vCard. (address, telephone, e-mail only)" },
{ "set <index> work on|off", "Set the work option in an element in your vCard. (address, telephone, e-mail only)" },
{ "set <index> voice on|off", "Set the voice option in a telephone element in your vCard." },
{ "set <index> fax on|off", "Set the fax option in a telephone element in your vCard." },
{ "set <index> pager on|off", "Set the pager option in a telephone element in your vCard." },
{ "set <index> msg on|off", "Set the message option in a telephone element in your vCard." },
{ "set <index> cell on|off", "Set the cellphone option in a telephone element in your vCard." },
{ "set <index> video on|off", "Set the video option in a telephone element in your vCard." },
{ "set <index> bbs on|off", "Set the BBS option in a telephone element in your vCard." },
{ "set <index> modem on|off", "Set the modem option in a telephone element in your vCard." },
{ "set <index> isdn on|off", "Set the ISDN option in a telephone element in your vCard." },
{ "set <index> pcs on|off", "Set the PCS option in a telephone element in your vCard." },
{ "set <index> preferred on|off", "Set the preferred option in an element in your vCard. (address, telephone, e-mail only)" },
{ "set <index> parcel on|off", "Set the parcel option in an address element in your vCard." },
{ "set <index> postal on|off", "Set the postal option in an address element in your vCard." },
{ "set <index> internet on|off", "Set the internet option in an e-mail address in your vCard." },
{ "set <index> x400 on|off", "Set the X400 option in an e-mail address in your vCard." },
{ "add nickname <nickname>", "Add a nickname to your vCard" },
{ "add birthday <date>", "Add a birthday date to your vCard" },
{ "add address", "Add an address to your vCard" },
{ "add tel <number>", "Add a telephone number to your vCard" },
{ "add email <userid>", "Add an e-mail address to your vCard" },
{ "add jid <jid>", "Add a Jabber ID to your vCard" },
{ "add title <title>", "Add a title to your vCard" },
{ "add role <role>", "Add a role to your vCard" },
{ "add note <note>", "Add a note to your vCard" },
{ "add url <url>", "Add a URL to your vCard" },
{ "remove <index>", "Remove a element in your vCard by index" },
{ "refresh", "Refreshes the local copy of the current account's vCard (undoes all your unpublished modifications)" },
{ "save", "Save changes to the server" })
CMD_NOEXAMPLES
},
{ "/vercheck",
parse_args, 0, 1, NULL,
CMD_NOSUBFUNCS
@@ -2559,7 +2679,8 @@ static struct cmd_t command_defs[] = {
{ "avatar", cmd_executable_avatar },
{ "urlopen", cmd_executable_urlopen },
{ "urlsave", cmd_executable_urlsave },
{ "editor", cmd_executable_editor })
{ "editor", cmd_executable_editor },
{ "vcard_photo", cmd_executable_vcard_photo })
CMD_NOMAINFUNC
CMD_TAGS(
CMD_TAG_DISCOVERY)
@@ -2568,7 +2689,9 @@ static struct cmd_t command_defs[] = {
"/executable urlopen set <cmdtemplate>",
"/executable urlopen default",
"/executable urlsave set <cmdtemplate>",
"/executable urlsave default")
"/executable urlsave default",
"/executable vcard_photo set <cmdtemplate>",
"/executable vcard_photo default")
CMD_DESC(
"Configure executable that should be called upon a certain command.")
CMD_ARGS(
@@ -2577,7 +2700,9 @@ static struct cmd_t command_defs[] = {
{ "urlopen default", "Restore to default settings." },
{ "urlsave set", "Set executable that is run by /url save. Takes a command template that replaces %u and %p with the URL and path respectively." },
{ "urlsave default", "Use the built-in download method for saving." },
{ "editor set", "Set editor to be used with /editor. Needs a terminal editor or a script to run a graphical editor." })
{ "editor set", "Set editor to be used with /editor. Needs a terminal editor or a script to run a graphical editor." },
{ "vcard_photo set", "Set executable that is run by /vcard photo open. Takes a command template that replaces %p with the path" },
{ "vcard_photo default", "Restore to default settings." })
CMD_EXAMPLES(
"/executable avatar xdg-open",
"/executable urlopen set \"xdg-open %u\"",
@@ -2586,6 +2711,7 @@ static struct cmd_t command_defs[] = {
"/executable urlsave set \"wget %u -O %p\"",
"/executable urlsave set \"curl %u -o %p\"",
"/executable urlsave default",
"/executable vcard_photo set \"feh %p\"",
"/executable editor set vim")
},

File diff suppressed because it is too large Load Diff

View File

@@ -246,6 +246,7 @@ gboolean cmd_executable_avatar(ProfWin* window, const char* const command, gchar
gboolean cmd_executable_urlopen(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_executable_urlsave(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_executable_editor(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_executable_vcard_photo(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_mam(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_editor(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_correct_editor(ProfWin* window, const char* const command, gchar** args);
@@ -253,5 +254,13 @@ gboolean cmd_silence(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_register(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_mood(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_stamp(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_vcard(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_vcard_add(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_vcard_remove(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_vcard_get(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_vcard_photo(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_vcard_refresh(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_vcard_set(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_vcard_save(ProfWin* window, const char* const command, gchar** args);
#endif