Mercurial > ~mikael > mcabber > hg
annotate mcabber/mcabber/carbons.c @ 2118:51fde9c25401
Reset carbons when the connection is closed
Then, carbons are re-enabled when mcabber reconnects.
Thanks to sezuan for the report.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Mon, 09 Jun 2014 10:01:48 +0200 |
parents | 9023a6f2bf6c |
children | 2ce6dd6861ea |
rev | line source |
---|---|
2111 | 1 /* |
2 * carbons.c -- Support for Message Carbons (XEP 0280) | |
3 * | |
4 * Copyright (C) 2013 Roeland Jago Douma <roeland@famdouma.nl> | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or (at | |
9 * your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
19 * USA | |
20 */ | |
21 | |
2104
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
22 #include "carbons.h" |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
23 #include "settings.h" |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
24 #include "xmpp_helper.h" |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
25 #include "xmpp_defines.h" |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
26 #include "logprint.h" |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
27 #include "xmpp.h" |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
28 |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
29 static int _carbons_available = 0; |
2106
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
30 static int _carbons_enabled = 0; |
2104
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
31 |
2108
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
32 static LmHandlerResult cb_carbons(LmMessageHandler *h, LmConnection *c, |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
33 LmMessage *m, gpointer user_data); |
2104
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
34 |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
35 |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
36 void carbons_available() |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
37 { |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
38 int enable = 0; |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
39 _carbons_available = 1; |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
40 |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
41 enable = settings_opt_get_int("carbons"); |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
42 |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
43 if (enable) { |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
44 carbons_enable(); |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
45 } |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
46 } |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
47 |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
48 void carbons_enable() |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
49 { |
2111 | 50 LmMessage *iq; |
51 LmMessageNode *enable; | |
52 LmMessageHandler *handler; | |
53 GError *error = NULL; | |
54 | |
2108
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
55 //We cannot enable carbons if there is no carbons support |
2106
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
56 if (_carbons_available == 0) { |
2111 | 57 scr_log_print(LPRINT_NORMAL, "Carbons not available on this server!"); |
2108
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
58 return; |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
59 } |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
60 |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
61 //We only have to enable carbons once |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
62 if (_carbons_enabled == 1) { |
2106
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
63 return; |
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
64 } |
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
65 |
2104
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
66 iq = lm_message_new_with_sub_type(NULL, LM_MESSAGE_TYPE_IQ, |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
67 LM_MESSAGE_SUB_TYPE_SET); |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
68 |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
69 enable = lm_message_node_add_child(iq->node, "enable", NULL); |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
70 lm_message_node_set_attribute(enable, "xmlns", NS_CARBONS_2); |
2108
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
71 handler = lm_message_handler_new(cb_carbons, NULL, NULL); |
2104
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
72 |
2106
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
73 if (!lm_connection_send_with_reply(lconnection, iq, handler, &error)) { |
2111 | 74 scr_log_print(LPRINT_DEBUG, "Error sending IQ request: %s.", |
75 error->message); | |
2106
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
76 g_error_free(error); |
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
77 } |
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
78 |
2104
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
79 lm_message_handler_unref(handler); |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
80 lm_message_unref(iq); |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
81 } |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
82 |
2118
51fde9c25401
Reset carbons when the connection is closed
Mikael Berthe <mikael@lilotux.net>
parents:
2111
diff
changeset
|
83 // Mark carbons as disabled (e.g. when a connection terminates) |
51fde9c25401
Reset carbons when the connection is closed
Mikael Berthe <mikael@lilotux.net>
parents:
2111
diff
changeset
|
84 void carbons_reset() |
51fde9c25401
Reset carbons when the connection is closed
Mikael Berthe <mikael@lilotux.net>
parents:
2111
diff
changeset
|
85 { |
51fde9c25401
Reset carbons when the connection is closed
Mikael Berthe <mikael@lilotux.net>
parents:
2111
diff
changeset
|
86 _carbons_enabled = 0; |
51fde9c25401
Reset carbons when the connection is closed
Mikael Berthe <mikael@lilotux.net>
parents:
2111
diff
changeset
|
87 } |
51fde9c25401
Reset carbons when the connection is closed
Mikael Berthe <mikael@lilotux.net>
parents:
2111
diff
changeset
|
88 |
2104
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
89 void carbons_disable() |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
90 { |
2111 | 91 LmMessage *iq; |
92 LmMessageNode *disable; | |
93 LmMessageHandler *handler; | |
94 GError *error = NULL; | |
95 | |
2108
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
96 //We cannot disable carbons if there is no carbon support on the server |
2106
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
97 if (_carbons_available == 0) { |
2111 | 98 scr_log_print(LPRINT_NORMAL, "Carbons not available on this server!"); |
2106
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
99 return; |
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
100 } |
2108
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
101 |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
102 //We can only disable carbons if they are disabled |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
103 if (_carbons_enabled == 0) { |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
104 return; |
2111 | 105 } |
2108
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
106 |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
107 iq = lm_message_new_with_sub_type(NULL, LM_MESSAGE_TYPE_IQ, |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
108 LM_MESSAGE_SUB_TYPE_SET); |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
109 |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
110 disable = lm_message_node_add_child(iq->node, "disable", NULL); |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
111 lm_message_node_set_attribute(disable, "xmlns", NS_CARBONS_2); |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
112 handler = lm_message_handler_new(cb_carbons, NULL, NULL); |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
113 |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
114 if (!lm_connection_send_with_reply(lconnection, iq, handler, &error)) { |
2111 | 115 scr_log_print(LPRINT_DEBUG, "Error sending IQ request: %s.", |
116 error->message); | |
2108
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
117 g_error_free(error); |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
118 } |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
119 |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
120 lm_message_handler_unref(handler); |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
121 lm_message_unref(iq); |
2111 | 122 |
2106
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
123 } |
2104
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
124 |
2106
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
125 void carbons_info() |
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
126 { |
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
127 if (_carbons_enabled) { |
2111 | 128 scr_log_print(LPRINT_NORMAL, "Carbons enabled."); |
2106
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
129 } else { |
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
130 if (_carbons_available) { |
2111 | 131 scr_log_print(LPRINT_NORMAL, "Carbons available, but not enabled."); |
2106
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
132 } else { |
2111 | 133 scr_log_print(LPRINT_NORMAL, "Carbons not available."); |
2106
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
134 } |
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
135 } |
2104
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
136 } |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
137 |
2108
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
138 static LmHandlerResult cb_carbons(LmMessageHandler *h, LmConnection *c, |
adfd962e1343
Allow the user to enable/disable carbons from the command line
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2106
diff
changeset
|
139 LmMessage *m, gpointer user_data) |
2104
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
140 { |
2106
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
141 if (lm_message_get_sub_type(m) == LM_MESSAGE_SUB_TYPE_RESULT) { |
2111 | 142 _carbons_enabled = (_carbons_enabled == 0 ? 1 : 0); |
143 if (_carbons_enabled) { | |
144 scr_log_print(LPRINT_NORMAL, "Carbons enabled."); | |
145 } else { | |
146 scr_log_print(LPRINT_NORMAL, "Carbons disabled."); | |
147 } | |
2106
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
148 } else { |
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
149 //Handle error cases |
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
150 } |
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
151 |
2104
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
152 return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
c7e9950fa741
Added option to enable carbons (set carbons = 1 in mcabberrc)
Roeland Jago Douma <roeland@famdouma.nl>
parents:
diff
changeset
|
153 } |
2106
72876dcf9094
Bit more checks for enabling carbons
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2105
diff
changeset
|
154 |
2111 | 155 /* vim: set et cindent cinoptions=>2\:2(0 ts=2 sw=2: For Vim users... */ |