forked from fchat-pidgin/fchat-pidgin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
f-list_ignore.c
172 lines (145 loc) · 6.19 KB
/
f-list_ignore.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "f-list_ignore.h"
void flist_ignore_list_request_add(FListAccount *fla, const gchar *character)
{
JsonObject *json = json_object_new();
json_object_set_string_member(json, "action", "add");
json_object_set_string_member(json, "character", character);
flist_request(fla, FLIST_IGNORE, json);
json_object_unref(json);
}
void flist_ignore_list_request_remove(FListAccount *fla, const gchar *character)
{
JsonObject *json = json_object_new();
json_object_set_string_member(json, "action", "delete");
json_object_set_string_member(json, "character", character);
flist_request(fla, FLIST_IGNORE, json);
json_object_unref(json);
}
void flist_ignore_list_request_list(FListAccount *fla)
{
JsonObject *json = json_object_new();
json_object_set_string_member(json, "action", "list");
flist_request(fla, FLIST_IGNORE, json);
json_object_unref(json);
}
void flist_ignore_list_action(PurplePluginAction *action) {
PurpleConnection *pc = action->context;
g_return_if_fail(pc);
FListAccount *fla = purple_connection_get_protocol_data(pc);
flist_ignore_list_request_list(fla);
}
static void ignore_list_remove_cb(PurpleConnection *pc, GList *row, gpointer user_data)
{
FListAccount *fla = purple_connection_get_protocol_data(pc);
flist_ignore_list_request_remove(fla, g_list_nth_data(row, 0));
}
gboolean flist_ignore_character_is_ignored(FListAccount *fla, const gchar *character)
{
return g_list_find_custom(fla->ignore_list, character, (GCompareFunc)g_ascii_strcasecmp) != NULL;
}
void flist_blist_node_ignore_action(PurpleBlistNode *node, gpointer data) {
PurpleBuddy *b = (PurpleBuddy*) node;
PurpleAccount *pa = purple_buddy_get_account(b);
PurpleConnection *pc;
FListAccount *fla;
FListIgnoreActionType type = GPOINTER_TO_INT(data);
const gchar *name = purple_buddy_get_name(b);
g_return_if_fail((pc = purple_account_get_connection(pa)));
g_return_if_fail((fla = purple_connection_get_protocol_data(pc)));
if (type == FLIST_NODE_IGNORE)
{
flist_ignore_list_request_add(fla, name);
}
else if (type == FLIST_NODE_UNIGNORE)
{
flist_ignore_list_request_remove(fla, name);
}
}
gboolean flist_process_IGN(FListAccount *fla, JsonObject *root) {
const gchar *action;
const gchar *character;
action = json_object_get_string_member(root, "action");
if (g_ascii_strncasecmp(action, "add", 3) == 0)
{
char msg[128];
character = json_object_get_string_member(root, "character");
fla->ignore_list = g_list_append(fla->ignore_list, g_strdup(character));
g_snprintf( msg, sizeof( msg ), _( "'%s' has been added to your ignore list." ), character );
purple_notify_info(fla->pc, "User ignored!", msg, NULL);
}
else if (g_ascii_strncasecmp(action, "delete", 6) == 0)
{
char msg[128];
character = json_object_get_string_member(root, "character");
GList *ignored_char = g_list_find_custom(fla->ignore_list, character, (GCompareFunc)g_ascii_strcasecmp);
if (ignored_char != NULL)
{
g_free(ignored_char->data);
fla->ignore_list = g_list_delete_link(fla->ignore_list, ignored_char);
}
g_snprintf( msg, sizeof( msg ), _( "'%s' has been removed from your ignore list." ), character );
purple_notify_info(fla->pc, "User removed!", msg, NULL);
}
else if (g_ascii_strncasecmp(action, "list", 4) == 0)
{
// Response looks like
// IGN {"characters":["asdasdasdasdasdf343ddfsd","testytest","blablabla"],"action":"list"}
JsonArray *chars = json_object_get_array_member(root, "characters");
PurpleNotifySearchResults *results = purple_notify_searchresults_new();
PurpleNotifySearchColumn *column = purple_notify_searchresults_column_new("Character");
purple_notify_searchresults_column_add(results, column);
for (size_t i = 0; i < json_array_get_length(chars); i++)
{
GList *row = NULL;
row = g_list_append(row, (gpointer) g_strdup(json_array_get_string_element(chars, i)));
purple_notify_searchresults_row_add(results, row);
}
// XXX This button is ignored, might be a bug in libpurple/pidgin
purple_notify_searchresults_button_add_labeled(results, "Remove", ignore_list_remove_cb);
purple_notify_searchresults(fla->pc, NULL, NULL, _("Ignore List"), results, NULL, NULL);
}
else if (g_ascii_strncasecmp(action, "init", 4) == 0)
{
flist_g_list_free_full(fla->ignore_list, g_free);
fla->ignore_list = NULL;
JsonArray *chars = json_object_get_array_member(root, "characters");
for (size_t i = 0; i < json_array_get_length(chars); i++)
{
fla->ignore_list = g_list_append(fla->ignore_list, g_strdup(json_array_get_string_element(chars, i)));
}
}
return TRUE;
}
PurpleCmdRet flist_ignore_cmd(PurpleConversation *convo, const gchar *cmd, gchar **args, gchar **error, void *data) {
FListAccount *fla = purple_connection_get_protocol_data(purple_conversation_get_gc(convo));
g_return_val_if_fail(fla, PURPLE_CMD_RET_FAILED);
if (args[0] == NULL) {
*error = g_strdup("Please specify an action : /ignore [add|delete|list] [character]");
return PURPLE_CMD_RET_FAILED;
}
gchar *subcmd = args[0];
if (g_ascii_strncasecmp(subcmd, "add", 3) == 0)
{
if (args[1] == NULL) {
*error = g_strdup("You must provide a character name to ignore");
return PURPLE_CMD_RET_FAILED;
}
flist_ignore_list_request_add(fla, args[1]);
}
else if (g_ascii_strncasecmp(subcmd, "delete", 6) == 0)
{
if (args[1] == NULL) {
*error = g_strdup("You must provide a nickname to unignore");
return PURPLE_CMD_RET_FAILED;
}
flist_ignore_list_request_remove(fla, args[1]);
}
else if (g_ascii_strncasecmp(subcmd, "list", 4) == 0)
{
flist_ignore_list_request_list(fla);
} else {
*error = g_strdup("Unrecognized action, please use : /ignore [add|delete|list] [character]");
return PURPLE_CMD_RET_FAILED;
}
return PURPLE_CMD_RET_OK;
}