-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathslack_messages.c
184 lines (155 loc) · 3.76 KB
/
slack_messages.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
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "slack_messages.h"
#include "json.h"
#include <curl/curl.h>
#include <stdlib.h>
static size_t
writefunc(void *ptr, size_t size, size_t nmemb, string *s)
{
size_t new_len = s->len + size*nmemb;
s->ptr = realloc(s->ptr, new_len+1);
if (s->ptr == NULL)
{
fprintf(stderr, "realloc() failed\n");
exit(EXIT_FAILURE);
}
memcpy(s->ptr+s->len, ptr, size*nmemb);
s->ptr[new_len] = '\0';
s->len = new_len;
return size*nmemb;
}
static void
slack_process_channels_json(PurpleRoomlist *roomlist, json_value *jobj)
{
// iterate through channels list
PurpleRoomlistRoom *room = NULL;
json_value *channels = json_get_value(jobj, "channels");
int length = channels->u.array.length;
for (int i = 0; i < length; i++)
{
json_value *channel = channels->u.array.values[i];
json_value *id = json_get_value(channel, "id");
json_value *name = json_get_value(channel, "name");
room = purple_roomlist_room_new(
PURPLE_ROOMLIST_ROOMTYPE_ROOM,
name->u.str.ptr,
NULL
);
purple_roomlist_room_add_field(
roomlist,
room,
channel->u.str.ptr
);
purple_roomlist_room_add_field(
roomlist,
room,
id->u.str.ptr
);
purple_roomlist_room_add(roomlist, room);
}
}
void
slack_channel_query(SlackConnection * conn)
{
UNUSED(conn);
purple_debug_info(PROTOCOL_CODE, "Channels data update\n");
CURLcode res;
CURL *curl = curl_easy_init();
if (curl)
{
string s = NULL_STRING;
create_string(&s, 0);
curl_easy_setopt(curl, CURLOPT_URL, "https://slack.com/api/channels.list?token=xoxp-37479727381-37710578004-73740828260-ff3474037e");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
#ifdef SKIP_PEER_VERIFICATION
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
#ifdef SKIP_HOSTNAME_VERIFICATION
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
res = curl_easy_perform(curl);
if(res != CURLE_OK)
purple_debug_error(PROTOCOL_CODE, "Error on connection\n");
else
{ // parse json
purple_debug_info(PROTOCOL_CODE, "Channels data accured\n");
json_value* json = json_parse(s.ptr, s.len);
if (json != NULL)
{
json_value *ok = json_get_value(json, "ok");
if (ok->u.boolean)
{
slack_process_channels_json(conn->roomlist, json);
}
else
purple_debug_info(PROTOCOL_CODE, "No data available\n");
json_value_free(json);
}
else
purple_debug_error(PROTOCOL_CODE, "Error on reading json\n");
}
}
curl_easy_cleanup(curl);
}
void
slack_message_send(
SlackConnection *slack,
int id,
const char *message,
int msg_type
)
{
UNUSED(slack);
UNUSED(id);
UNUSED(message);
UNUSED(msg_type);
}
static void
slack_channel_update(
SlackConnection* conn,
gint id,
gchar * channel
)
{
UNUSED(conn);
UNUSED(id);
UNUSED(channel);
}
PurpleCmdRet
slack_parse_cmd(
PurpleConversation * conv,
const gchar * cmd,
gchar ** args,
G_GNUC_UNUSED gchar ** error,
void *data
)
{
PurpleConnection *gc = purple_conversation_get_gc(conv);
PurpleConvChat *chat = PURPLE_CONV_CHAT(conv);
GString *message = NULL;
if (!gc)
return PURPLE_CMD_RET_FAILED;
purple_debug_info("slack", "cmd %s: args[0]: %s\n", cmd, args[0]);
if (g_strcmp0(cmd, SLACK_CMD_MESSAGE) == 0) {
/* send a message */
message = g_string_new("*");
g_string_append(message, args[0]);
g_string_append(message, "*");
slack_message_send(
data,
chat->id,
message->str,
MESSAGE_TEXT
);
} else if (g_strcmp0(cmd, SLACK_CMD_CHANNEL) == 0) {
/* do a room request */
if (args[0])
slack_channel_update(data, chat->id, args[0]);
else
slack_channel_update(data, chat->id, "");
}/* else if (g_strcmp0(cmd, CAMPFIRE_CMD_ROOM) == 0) {
// do a room request
campfire_room_update(data, chat->id, NULL, args[0]);
}*/
return PURPLE_CMD_RET_OK;
}