aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarius Halden <marius.h@lden.org>2015-11-06 20:46:36 +0100
committerMarius Halden <marius.h@lden.org>2016-05-07 14:31:03 +0200
commit6c016f46c9415c72321ec92c40e9af91d566276d (patch)
treeabffd89f23d6ec4337d1217263f94dc137501766
parente693e93e1fb3a9b2cc6fdfe1b2f2076a8585deb4 (diff)
Add support for multiple accounts in set account
The set account for control channels is now a comma separeted list of accounts instead of just one. If the user changes the tag of an accounts trough `account <id> set tag <new_tag>`, the account set will be updated to reflect this change for all relevant channels. If an account is removed trough `account <id> delete` it will be removed from the account set for all relevant channels.
-rw-r--r--irc.h2
-rw-r--r--irc_channel.c40
-rw-r--r--protocols/account.c90
3 files changed, 125 insertions, 7 deletions
diff --git a/irc.h b/irc.h
index c96cdad3..85fab4f2 100644
--- a/irc.h
+++ b/irc.h
@@ -241,7 +241,7 @@ typedef enum {
struct irc_control_channel {
irc_control_channel_type_t type;
struct bee_group *group;
- struct account *account;
+ GSList *account;
struct prpl *protocol;
char modes[5];
};
diff --git a/irc_channel.c b/irc_channel.c
index 34ed54a9..097476dd 100644
--- a/irc_channel.c
+++ b/irc_channel.c
@@ -866,17 +866,39 @@ static char *set_eval_by_account(set_t *set, char *value)
struct irc_channel *ic = set->data;
struct irc_control_channel *icc = ic->data;
account_t *acc;
+ GSList *accl, *new_acc = NULL;
+ char **accounts, **account;
- if (!(acc = account_get(ic->irc->b, value))) {
- return SET_INVALID;
+ accounts = g_strsplit(value, ",", 0);
+ if (accounts == NULL) {
+ goto fail;
}
- icc->account = acc;
+ for (account = accounts; *account; account++) {
+ if (!(acc = account_get(ic->irc->b, *account))) {
+ goto fail;
+ } else {
+ new_acc = g_slist_append(new_acc, acc);
+ }
+ }
+
+ accl = icc->account;
+ g_slist_free(accl);
+
+ icc->account = new_acc;
if ((icc->type & IRC_CC_TYPE_MASK) == IRC_CC_TYPE_ACCOUNT) {
bee_irc_channel_update(ic->irc, ic, NULL);
}
- return g_strdup(acc->tag);
+ g_strfreev(accounts);
+
+ return g_strdup(value);
+
+fail:
+ g_slist_free(new_acc);
+ g_strfreev(accounts);
+
+ return SET_INVALID;
}
static char *set_eval_fill_by(set_t *set, char *value)
@@ -1000,6 +1022,7 @@ fail:
gboolean irc_channel_wants_user(irc_channel_t *ic, irc_user_t *iu)
{
struct irc_control_channel *icc = ic->data;
+ GSList *accl;
gboolean ret = FALSE;
if (iu->bu == NULL) {
@@ -1011,7 +1034,13 @@ gboolean irc_channel_wants_user(irc_channel_t *ic, irc_user_t *iu)
ret = iu->bu->group == icc->group;
break;
case IRC_CC_TYPE_ACCOUNT:
- ret = iu->bu->ic->acc == icc->account;
+ for (accl = icc->account; accl; accl = accl->next) {
+ account_t *acc = accl->data;
+ if (iu->bu->ic->acc == acc) {
+ ret = TRUE;
+ break;
+ }
+ }
break;
case IRC_CC_TYPE_PROTOCOL:
ret = iu->bu->ic->acc->prpl == icc->protocol;
@@ -1039,6 +1068,7 @@ static gboolean control_channel_free(irc_channel_t *ic)
set_del(&ic->set, "protocol");
set_del(&ic->set, "show_users");
+ g_slist_free(icc->account);
g_free(icc);
ic->data = NULL;
diff --git a/protocols/account.c b/protocols/account.c
index e25e40c7..685d15ec 100644
--- a/protocols/account.c
+++ b/protocols/account.c
@@ -122,6 +122,36 @@ account_t *account_add(bee_t *bee, struct prpl *prpl, char *user, char *pass)
return a;
}
+void account_update_channel_set(irc_channel_t *ic, char *old_tag, char *new_tag)
+{
+ gboolean found = FALSE;
+ char **account, **accounts;
+ char *saccount = set_getstr(&ic->set, "account");
+
+ if (saccount == NULL || *saccount == '\0') {
+ return;
+ }
+
+ accounts = g_strsplit(saccount, ",", 0);
+ for (account = accounts; *account; account++) {
+ if (g_strcasecmp(*account, old_tag) == 0) {
+ found = TRUE;
+ break;
+ }
+ }
+
+ if (found) {
+ g_free(*account);
+ *account = g_strdup(new_tag);
+
+ saccount = g_strjoinv(",", accounts);
+ set_setstr(&ic->set, "account", saccount);
+ g_free(saccount);
+ }
+
+ g_strfreev(accounts);
+}
+
char *set_eval_account(set_t *set, char *value)
{
account_t *acc = set->data;
@@ -163,14 +193,28 @@ char *set_eval_account(set_t *set, char *value)
return NULL; /* password shouldn't be visible in plaintext! */
} else if (strcmp(set->key, "tag") == 0) {
account_t *oa;
+ irc_t *irc;
+ GSList *l;
+ char *old;
/* Enforce uniqueness. */
if ((oa = account_by_tag(acc->bee, value)) && oa != acc) {
return SET_INVALID;
}
- g_free(acc->tag);
+ old = acc->tag;
acc->tag = g_strdup(value);
+
+ irc = acc->bee->ui_data;
+ for (l = irc->channels; l; l = l->next) {
+ irc_channel_t *ic = l->data;
+
+ if (g_strcasecmp(set_getstr(&ic->set, "type"), "control") == 0) {
+ account_update_channel_set(ic, old, value);
+ }
+ }
+
+ g_free(old);
return value;
} else if (strcmp(set->key, "auto_connect") == 0) {
if (!is_bool(value)) {
@@ -291,9 +335,43 @@ account_t *account_by_tag(bee_t *bee, const char *tag)
return NULL;
}
+void account_remove_from_channel_set(irc_channel_t *ic, char *tag)
+{
+ gboolean found = FALSE;
+ char **account, **accounts;
+ char *saccount = set_getstr(&ic->set, "account");
+
+ if (saccount == NULL || *saccount == '\0') {
+ return;
+ }
+
+ accounts = g_strsplit(saccount, ",", 0);
+ for (account = accounts; *account; account++) {
+ if (g_strcasecmp(*account, tag) == 0) {
+ found = TRUE;
+ break;
+ }
+ }
+
+ if (found) {
+ g_free(*account);
+
+ do {
+ *account = *(account + 1);
+ } while (*(++account) != NULL);
+
+ saccount = g_strjoinv(",", accounts);
+ set_setstr(&ic->set, "account", saccount);
+ }
+
+ g_strfreev(accounts);
+}
+
void account_del(bee_t *bee, account_t *acc)
{
account_t *a, *l = NULL;
+ GSList *accl;
+ irc_t *irc;
if (acc->ic) {
/* Caller should have checked, accounts still in use can't be deleted. */
@@ -317,6 +395,16 @@ void account_del(bee_t *bee, account_t *acc)
}
*/
+ /* Remove from channel set account */
+ irc = acc->bee->ui_data;
+ for (accl = irc->channels; accl; accl = accl->next) {
+ irc_channel_t *ic = accl->data;
+
+ if (g_strcasecmp(set_getstr(&ic->set, "type"), "control") == 0) {
+ account_remove_from_channel_set(ic, acc->tag);
+ }
+ }
+
while (a->set) {
set_del(&a->set, a->set->key);
}