Debian Codegraph @

src/fe-common/xep/fe-composing.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 Colin DIDIER
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #include <string.h>
19
20 #include "module.h"
21 #include "settings.h"
22 #include "signals.h"
23 #include "special-vars.h"
24 #include "window-items.h"
25
26 #include "xmpp-servers.h"
27 #include "xmpp-queries.h"
28 #include "tools.h"
29
30 #define KEY_TAB 9
31 #define KEY_RETURN 10
32 #define KEY_ESCAPE 27
33 #define KEYS_PAGE 91
34 #define KEYS_OTHER 126
35 #define KEY_BACKSPACE 127
36
37 #define COMPOSING_TIMEOUT 5
38
39 static gboolean keylog_active;
40 static int last_key;
41
42 #define IS_ALIVE_SERVER(server) \
43 ((server) != NULL \
44 && g_slist_find(servers, (server)) != NULL \
45 && (server)->connected)
46
47 static gboolean
48 stop_composing(gpointer *user_data)
49 {
50 XMPP_QUERY_REC *query = XMPP_QUERY(user_data);
51
52 if (query == NULL || query->composing_time == 0
53 || !IS_ALIVE_SERVER(query->server))
54 return FALSE;
55 /* still composing */
56 if ((time(NULL) - query->composing_time) < COMPOSING_TIMEOUT)
57 return TRUE;
58 signal_emit("xmpp composing stop", 2, query->server, query->name);
59 query->composing_time = 0;
60 return FALSE;
61 }
62
63 static void
64 sig_gui_key_pressed(int key)
65 {
66 XMPP_QUERY_REC *query;
67 time_t current_time;
68 char *str = NULL;
69
70 if (!settings_get_bool("xmpp_send_composing") && keylog_active)
71 return;
72 query = XMPP_QUERY(active_win->active);
73 if (query == NULL || !IS_XMPP_SERVER(query->server))
74 return;
75 /* ignore command or empty line */
76 str = parse_special_string("$L", active_win->active_server,
77 active_win->active, "", NULL, 0);
78 if (str != NULL &&
79 (*str == *settings_get_str("cmdchars") || *str == '\0'))
80 goto out;
81 if (key != KEY_TAB && key != KEY_RETURN && last_key != KEY_ESCAPE
82 && key != KEY_ESCAPE && last_key != KEYS_PAGE && key != KEYS_PAGE
83 && key != KEYS_OTHER && key != KEY_BACKSPACE) {
84 current_time = time(NULL);
85 /* start composing */
86 if (query->composing_time == 0) {
87 query->composing_time = current_time;
88 g_timeout_add(COMPOSING_TIMEOUT * 1000,
89 (GSourceFunc)stop_composing, query);
90 signal_emit("xmpp composing start", 2,
91 query->server, query->name);
92 /* still composing */
93 } else if ((current_time - query->composing_time)
94 < (COMPOSING_TIMEOUT - 1))
95 query->composing_time = current_time;
96 }
97
98 out:
99 /* message sent */
100 if (key == KEY_RETURN)
101 query->composing_time = 0;
102 last_key = key;
103 g_free(str);
104 }
105
106 static void
107 keyloger_enabled(gboolean enable)
108 {
109 if (enable && !keylog_active) {
110 signal_add_last("gui key pressed", sig_gui_key_pressed);
111 keylog_active = TRUE;
112 } else if (!enable && keylog_active) {
113 signal_remove("gui key pressed", sig_gui_key_pressed);
114 keylog_active = FALSE;
115 }
116 }
117
118
119 static void
120 sig_window_changed(WINDOW_REC *new_window, WINDOW_REC *old_window)
121 {
122 XMPP_SERVER_REC *server;
123 XMPP_QUERY_REC *query;
124
125 if (!settings_get_bool("xmpp_send_composing")
126 || (server = XMPP_SERVER(active_win->active_server)) == NULL) {
127 keyloger_enabled(FALSE);
128 return;
129 }
130 query = XMPP_QUERY(active_win->active);
131 if (query == NULL || !xmpp_have_resource(query->name))
132 keyloger_enabled(FALSE);
133 else
134 keyloger_enabled(TRUE);
135 }
136
137 static void
138 sig_query_destroyed(QUERY_REC *query_destroyed)
139 {
140 XMPP_QUERY_REC *query;
141
142 query = XMPP_QUERY(query_destroyed);
143 if (query != NULL && query->composing_time != 0
144 && IS_ALIVE_SERVER(query->server))
145 signal_emit("xmpp composing stop", 2, query->server,
146 query->name);
147 }
148
149 static void
150 sig_disconnected(XMPP_SERVER_REC *server)
151 {
152 GSList *tmp;
153 XMPP_QUERY_REC *query;
154
155 if (!IS_XMPP_SERVER(server))
156 return;
157 for (tmp = queries; tmp != NULL; tmp = tmp->next) {
158 query = XMPP_QUERY(tmp->data);
159 if (query == NULL)
160 continue;
161 if (query->server == server)
162 g_source_remove_by_user_data(query);
163 }
164 }
165
166 void
167 fe_composing_init(void)
168 {
169 signal_add_last("window changed", sig_window_changed);
170 signal_add("query destroyed", sig_query_destroyed);
171 signal_add("server disconnected", sig_disconnected);
172
173 settings_add_bool("xmpp", "xmpp_send_composing", TRUE);
174 keylog_active = FALSE;
175 last_key = 0;
176 }
177
178 void
179 fe_composing_deinit(void)
180 {
181 signal_remove("window changed", sig_window_changed);
182 signal_remove("query destroyed", sig_query_destroyed);
183 signal_remove("server disconnected", sig_disconnected);
184
185 keyloger_enabled(FALSE);
186 }