-
Notifications
You must be signed in to change notification settings - Fork 2
/
logstatus.c
291 lines (237 loc) · 6.19 KB
/
logstatus.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* (C) 2008 by Tomasz bla Fortuna
*
* DESC: Pidgin status logging.
*
* This program 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 2 of the License, or
* any later version.
*
*
*/
#define GETTEXT_PACKAGE "plugin_pack"
#define PURPLE_PLUGINS
#include <string.h>
#include <glib.h>
#include <locale.h>
#include <libintl.h>
#include <glib/gi18n-lib.h>
#include <libpurple/conversation.h>
#include <libpurple/signals.h>
#include <libpurple/plugin.h>
#include <libpurple/version.h>
#define LOGSTATUS_PLUGIN_ID "core-logstatus"
/* Debug */
#define DEBUG 1
/* Set to 1 if you don't want to trace users without conversation */
#define ONLY_OPENED_CONVERSATIONS 0
#define INTRO "logstatus: "
static int try_to_append(PurpleBuddy *buddy, const char *message)
{
int retval = 0;
/* Log file to write to */
PurpleLog *log = NULL;
GList *list;
list = purple_log_get_logs(PURPLE_LOG_IM, buddy->name, buddy->account);
if (!list)
return 0;
log = g_list_first(list)->data;
if (!log) {
if (DEBUG)
printf(INTRO "Unable to append, log list empty\n");
goto cleanup;
}
if (!log->logger) {
if (DEBUG)
printf(INTRO "Unable to append logger is NULL\n");
goto cleanup;
}
if (!log->logger_data) {
if (DEBUG)
printf(INTRO "Unable to append NULL logger_data\n");
goto cleanup;
}
/* We must reopen log file
* This should work with txt logger and html logger.
* No idea about others. This is kind of hack.
*
* We don't know real type of logger_data.
*/
PurpleLogCommonLoggerData *data = log->logger_data;
if (data->file) {
/* Right. This should be NULL really, type might be wrong. */
if (DEBUG)
printf(INTRO "WARNING incompatible logger or logging system updated!\n");
goto cleanup;
}
data->file = fopen(data->path, "a");
if (!data->file) {
if (DEBUG)
printf(INTRO "Unable to append to existing log!\n");
goto cleanup;
}
const char *alias = purple_buddy_get_alias(buddy);
purple_log_write(log, PURPLE_MESSAGE_SYSTEM, alias, time(NULL), message);
fclose(data->file);
data->file = NULL;
/* All fine */
retval = 1;
cleanup:
g_list_foreach(list, (GFunc)purple_log_free, NULL);
g_list_free(list);
return retval;
}
static void
write_message(PurpleBuddy *buddy, const char *message)
{
PurpleConversation *conv;
const char *alias = purple_buddy_get_alias(buddy);
/* Acquire conversion */
conv = purple_find_conversation_with_account(
PURPLE_CONV_TYPE_ANY, buddy->name, buddy->account);
if (conv != NULL) {
/* If conversation is open just place there the message using purple_conversation_write() */
purple_conversation_write(conv, alias, message, PURPLE_MESSAGE_SYSTEM, time(NULL));
} else {
/* No conversation opened. Reopen last or create new */
if (ONLY_OPENED_CONVERSATIONS)
return;
/* Log file to write to */
PurpleLog *log = NULL;
if (!try_to_append(buddy, message)) {
if (DEBUG)
printf(INTRO "Creating new log file for %s\n", buddy->name);
log = purple_log_new(PURPLE_LOG_IM, buddy->name, buddy->account, NULL, time(NULL), NULL);
if (!log) {
printf(INTRO "Log creation for %s failed!\n", buddy->name);
return;
}
purple_log_write(log, PURPLE_MESSAGE_SYSTEM, alias, time(NULL), message);
purple_log_free(log);
}
}
return;
}
static void
write_status(PurpleBuddy *buddy, const char *fmt)
{
const char *message;
const char *status_type;
const char *alias = purple_buddy_get_alias(buddy);
char *log_entry;
PurpleStatus *status = purple_presence_get_active_status(buddy->presence);
/* Collect data */
message = purple_status_get_attr_string(status, "message");
if (message == NULL) {
/* Don't bother with no message only
* if we have something other to say */
if (fmt)
message = _("");
else
return;
}
if (!fmt) {
/* No fmt? Use default */
fmt = _("%s status is: %s (%s)");
}
if (!status) {
status_type = _("Unknown status type");
} else {
status_type = purple_status_get_name(status);
}
log_entry = g_strdup_printf(fmt, alias, status_type, message);
write_message(buddy, log_entry);
g_free(log_entry);
return;
}
static void
buddy_status_changed_cb(PurpleBuddy *buddy, PurpleStatus *old_status,
PurpleStatus *status, void *data)
{
g_return_if_fail(buddy != NULL);
g_return_if_fail(status != NULL);
write_status(buddy, NULL);
}
static void
buddy_idle_changed_cb(PurpleBuddy *buddy, gboolean old_idle, gboolean idle,
void *data)
{
if (idle) {
write_status(buddy, _("%s has become idle: %s (%s)"));
} else {
write_status(buddy, _("%s is no longer idle: %s (%s)"));
}
}
static void
buddy_signon_cb(PurpleBuddy *buddy, void *data)
{
write_status(buddy, _("%s has signed on: %s (%s)"));
}
static void
buddy_signoff_cb(PurpleBuddy *buddy, void *data)
{
write_status(buddy, _("%s has signed off: %s (%s)"));
}
static gboolean
plugin_load(PurplePlugin *plugin)
{
void *blist_handle = purple_blist_get_handle();
purple_signal_connect(blist_handle, "buddy-status-changed", plugin,
PURPLE_CALLBACK(buddy_status_changed_cb), NULL);
purple_signal_connect(blist_handle, "buddy-idle-changed", plugin,
PURPLE_CALLBACK(buddy_idle_changed_cb), NULL);
purple_signal_connect(blist_handle, "buddy-signed-on", plugin,
PURPLE_CALLBACK(buddy_signon_cb), NULL);
purple_signal_connect(blist_handle, "buddy-signed-off", plugin,
PURPLE_CALLBACK(buddy_signoff_cb), NULL);
return TRUE;
}
static PurplePluginUiInfo prefs_info =
{
NULL,
0,
NULL,
/* padding */
NULL,
NULL,
NULL,
NULL
};
static PurplePluginInfo info =
{
PURPLE_PLUGIN_MAGIC,
PURPLE_MAJOR_VERSION,
PURPLE_MINOR_VERSION,
PURPLE_PLUGIN_STANDARD,
NULL,
0,
NULL,
PURPLE_PRIORITY_DEFAULT,
LOGSTATUS_PLUGIN_ID,
N_("Buddy Status history"),
N_("0.7v"),
N_("Stores in the buddy history his status changes"),
N_("Stores in the buddy history his status changes"),
" Tomasz bla Fortuna <bla@thera.be>",
N_("http://bla.thera.be"),
plugin_load,
NULL,
NULL,
NULL,
NULL,
&prefs_info,
NULL,
/* padding */
NULL,
NULL,
NULL,
NULL
};
static void
init_plugin(PurplePlugin *plugin)
{
if (DEBUG)
printf(INTRO "entry\n");
}
PURPLE_INIT_PLUGIN(logstatus, init_plugin, info)