forked from fchat-pidgin/fchat-pidgin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
f-list_status.c
124 lines (107 loc) · 4.92 KB
/
f-list_status.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
/*
* F-List Pidgin - a libpurple protocol plugin for F-Chat
*
* Copyright 2011 F-List Pidgin developers.
*
* This file is part of F-List Pidgin.
*
* F-List Pidgin 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
* (at your option) any later version.
*
* F-List Pidgin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with F-List Pidgin. If not, see <http://www.gnu.org/licenses/>.
*/
#include "f-list_status.h"
//This file is currently mostly unnecessary, but more will likely be added later.
void flist_update_server_status(FListAccount *fla) {
JsonObject *json = json_object_new();
const gchar *status = purple_account_get_string(fla->pa, "_status", "online");
const gchar *status_message = purple_account_get_string(fla->pa, "_status_message", "");
// If _status_message was stored as NULL, purple_account_get_string will
// return a NULL instead of the default value. We do not want this, as NULL
// values are encoded as (null) by recent versions of libjson-glib, we send
// an empty string instead, as the F-Chat protocol mandates the statusmsg
// parameter
if (status_message == NULL) {
status_message = "";
}
json_object_set_string_member(json, "status", status);
json_object_set_string_member(json, "statusmsg", status_message);
flist_request(fla, FLIST_SET_STATUS, json);
json_object_unref(json);
}
void flist_set_status(FListAccount *fla, FListStatus status, const gchar *status_message) {
purple_account_set_string(fla->pa, "_status_message", status_message);
purple_account_set_string(fla->pa, "_status", flist_internal_status(status));
}
FListStatus flist_get_status(FListAccount *fla) {
return flist_parse_status(purple_account_get_string(fla->pa, "_status", "online"));
}
const gchar *flist_get_status_message(FListAccount *fla) {
return purple_account_get_string(fla->pa, "_status_message", "");
}
void flist_purple_set_status(PurpleAccount *account, PurpleStatus *status) {
g_return_if_fail(account);
g_return_if_fail(status);
PurpleConnection *pc = purple_account_get_connection(account);
FListAccount *fla = purple_connection_get_protocol_data(pc);
g_return_if_fail(fla);
if (fla->sync_status) {
purple_debug_info(FLIST_DEBUG, "Processing purple status change to %s with message %s\n", purple_status_get_id(status), purple_status_get_attr_string(status, FLIST_STATUS_MESSAGE_KEY));
flist_set_internal_status_from_purple_status(fla, status);
flist_update_server_status(fla);
}
}
void flist_set_internal_status_from_purple_status(FListAccount *fla, PurpleStatus *status) {
PurpleStatusType *statusType = purple_status_get_type(status);
GList *statusTypes = flist_status_types(fla->pa);
GList *cur = statusTypes;
FListStatus fStatus = FLIST_STATUS_UNKNOWN;
gchar *message = NULL;
// The status isn't active! bail!
if (!purple_status_is_active(status))
return;
message = flist_html_unescape_utf8(purple_status_get_attr_string(status, FLIST_STATUS_MESSAGE_KEY));
// First, get the presence. If it's idle, we default to idle.
PurplePresence *presence = purple_status_get_presence(status);
if(purple_presence_is_idle(presence)) {
flist_set_status(fla, FLIST_STATUS_IDLE, message);
}
// Alright, not idle. Next, compare StatusType IDs. If it's a match, use that.
while(cur) {
PurpleStatusType *type = cur->data;
if(strcmp(purple_status_type_get_id(type), purple_status_type_get_id(statusType)) == 0){
fStatus = flist_parse_status(purple_status_type_get_id(statusType));
break;
} else {
cur = g_list_next(cur);
}
}
// Found a matching F-list Status. Use it!
if(fStatus != FLIST_STATUS_UNKNOWN) {
flist_set_status(fla, fStatus, message);
} else {
// Alright, seems the status we chose isn't an F-list one. Let's convert to the next best primitive.
switch (purple_status_type_get_primitive(statusType)) {
case PURPLE_STATUS_AWAY:
case PURPLE_STATUS_EXTENDED_AWAY:
flist_set_status(fla, FLIST_STATUS_AWAY, message);
break;
case PURPLE_STATUS_UNAVAILABLE:
flist_set_status(fla, FLIST_STATUS_DND, message);
break;
// Assume AVAILABLE by default if it's not an AWAY/DND status
default:
flist_set_status(fla, FLIST_STATUS_AVAILABLE, message);
}
}
g_list_free(statusTypes);
g_free(message);
}