Mercurial > pidgin
annotate plugins/autorecon.c @ 9952:8fdf9706c45d
[gaim-migrate @ 10848]
these should be const, and now is as good a time as any to make them that way
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Sat, 04 Sep 2004 17:06:10 +0000 |
parents | f8e395a054e2 |
children | ff704c905239 |
rev | line source |
---|---|
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5607
diff
changeset
|
1 #include "internal.h" |
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5607
diff
changeset
|
2 |
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5607
diff
changeset
|
3 #include "connection.h" |
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5607
diff
changeset
|
4 #include "debug.h" |
8774 | 5 #include "pluginpref.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5607
diff
changeset
|
6 #include "prpl.h" |
6485
70d5122bc3ff
[gaim-migrate @ 6999]
Christian Hammond <chipx86@chipx86.com>
parents:
6371
diff
changeset
|
7 #include "signals.h" |
9943 | 8 #include "version.h" |
4202
59751fe608c5
[gaim-migrate @ 4438]
Christian Hammond <chipx86@chipx86.com>
parents:
4113
diff
changeset
|
9 |
8774 | 10 |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
11 #define AUTORECON_PLUGIN_ID "core-autorecon" |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
12 |
2216
66783ad29e55
[gaim-migrate @ 2226]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1818
diff
changeset
|
13 #define INITIAL 8000 |
4590 | 14 #define MAXTIME 2048000 |
2216
66783ad29e55
[gaim-migrate @ 2226]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1818
diff
changeset
|
15 |
6113 | 16 typedef struct { |
17 int delay; | |
18 guint timeout; | |
19 } GaimAutoRecon; | |
2216
66783ad29e55
[gaim-migrate @ 2226]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1818
diff
changeset
|
20 |
9546 | 21 /* |
22 I use a struct here, but the visible/invisible isn't yet supported | |
9549 | 23 in this plugin, so this is more for future implementation of those |
24 features | |
9546 | 25 */ |
26 typedef struct { | |
27 const char *state; | |
28 const char *message; | |
29 } GaimAwayState; | |
30 | |
6113 | 31 static GHashTable *hash = NULL; |
9546 | 32 static GHashTable *awayStates = NULL; |
9930 | 33 |
8774 | 34 |
35 #define AUTORECON_OPT "/plugins/core/autorecon" | |
9930 | 36 #define OPT_HIDE_CONNECTED AUTORECON_OPT "/hide_connected_error" |
37 #define OPT_HIDE_CONNECTING AUTORECON_OPT "/hide_connecting_error" | |
38 #define OPT_RESTORE_STATE AUTORECON_OPT "/restore_state" | |
8774 | 39 |
40 | |
41 /* storage of original (old_ops) and modified (new_ops) ui ops to allow us to | |
42 intercept calls to report_disconnect */ | |
43 static GaimConnectionUiOps *old_ops = NULL; | |
44 static GaimConnectionUiOps *new_ops = NULL; | |
45 | |
46 | |
47 static void report_disconnect(GaimConnection *gc, const char *text) { | |
48 | |
49 if(old_ops == NULL || old_ops->report_disconnect == NULL) { | |
50 /* there's nothing to call through to, so don't bother | |
51 checking prefs */ | |
52 return; | |
53 | |
54 } else if(gc->state == GAIM_CONNECTED | |
55 && gaim_prefs_get_bool(OPT_HIDE_CONNECTED)) { | |
56 /* this is a connected error, and we're hiding those */ | |
57 gaim_debug(GAIM_DEBUG_INFO, "autorecon", | |
58 "hid disconnect error message\n"); | |
59 return; | |
60 | |
61 } else if(gc->state == GAIM_CONNECTING | |
62 && gaim_prefs_get_bool(OPT_HIDE_CONNECTING)) { | |
63 /* this is a connecting error, and we're hiding those */ | |
64 gaim_debug(GAIM_DEBUG_INFO, "autorecon", | |
65 "hid error message while connecting\n"); | |
66 return; | |
67 } | |
68 | |
69 /* if we haven't returned by now, then let's pass to the real | |
70 function */ | |
71 old_ops->report_disconnect(gc, text); | |
72 } | |
73 | |
74 | |
2216
66783ad29e55
[gaim-migrate @ 2226]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1818
diff
changeset
|
75 static gboolean do_signon(gpointer data) { |
5587
1c55b1540e18
[gaim-migrate @ 5991]
Christian Hammond <chipx86@chipx86.com>
parents:
5227
diff
changeset
|
76 GaimAccount *account = data; |
6113 | 77 GaimAutoRecon *info; |
78 | |
5227
6d1707dc8c3d
[gaim-migrate @ 5597]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
79 gaim_debug(GAIM_DEBUG_INFO, "autorecon", "do_signon called\n"); |
6113 | 80 g_return_val_if_fail(account != NULL, FALSE); |
81 info = g_hash_table_lookup(hash, account); | |
4494
b5a50a6a13b0
[gaim-migrate @ 4769]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4491
diff
changeset
|
82 |
5607 | 83 if (g_list_index(gaim_accounts_get_all(), account) < 0) |
1817
b367beee6448
[gaim-migrate @ 1827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1404
diff
changeset
|
84 return FALSE; |
6113 | 85 |
7372 | 86 if(info) |
87 info->timeout = 0; | |
88 | |
5587
1c55b1540e18
[gaim-migrate @ 5991]
Christian Hammond <chipx86@chipx86.com>
parents:
5227
diff
changeset
|
89 gaim_debug(GAIM_DEBUG_INFO, "autorecon", "calling gaim_account_connect\n"); |
1c55b1540e18
[gaim-migrate @ 5991]
Christian Hammond <chipx86@chipx86.com>
parents:
5227
diff
changeset
|
90 gaim_account_connect(account); |
1c55b1540e18
[gaim-migrate @ 5991]
Christian Hammond <chipx86@chipx86.com>
parents:
5227
diff
changeset
|
91 gaim_debug(GAIM_DEBUG_INFO, "autorecon", "done calling gaim_account_connect\n"); |
6113 | 92 |
1817
b367beee6448
[gaim-migrate @ 1827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1404
diff
changeset
|
93 return FALSE; |
1378
aedeb1218a0a
[gaim-migrate @ 1388]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1165
diff
changeset
|
94 } |
aedeb1218a0a
[gaim-migrate @ 1388]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1165
diff
changeset
|
95 |
8774 | 96 |
5587
1c55b1540e18
[gaim-migrate @ 5991]
Christian Hammond <chipx86@chipx86.com>
parents:
5227
diff
changeset
|
97 static void reconnect(GaimConnection *gc, void *m) { |
6113 | 98 GaimAccount *account; |
99 GaimAutoRecon *info; | |
100 | |
101 g_return_if_fail(gc != NULL); | |
102 account = gaim_connection_get_account(gc); | |
103 info = g_hash_table_lookup(hash, account); | |
104 | |
2216
66783ad29e55
[gaim-migrate @ 2226]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1818
diff
changeset
|
105 if (!gc->wants_to_die) { |
6113 | 106 if (info == NULL) { |
107 info = g_new0(GaimAutoRecon, 1); | |
108 g_hash_table_insert(hash, account, info); | |
109 info->delay = INITIAL; | |
8249 | 110 } else { |
6113 | 111 info->delay = MIN(2 * info->delay, MAXTIME); |
8250 | 112 if (info->timeout != 0) |
113 g_source_remove(info->timeout); | |
8249 | 114 } |
6113 | 115 info->timeout = g_timeout_add(info->delay, do_signon, account); |
116 } else if (info != NULL) { | |
117 g_hash_table_remove(hash, account); | |
2216
66783ad29e55
[gaim-migrate @ 2226]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1818
diff
changeset
|
118 } |
9546 | 119 |
120 if (gc->wants_to_die) | |
121 g_hash_table_remove(awayStates, account); | |
99 | 122 } |
123 | |
9546 | 124 static void save_state(GaimAccount *account, const char *state, const char *message) { |
9549 | 125 /* Saves whether the account is back/away/visible/invisible */ |
9546 | 126 |
9549 | 127 GaimAwayState *info; |
9546 | 128 |
129 if (!strcmp(state,GAIM_AWAY_CUSTOM)) { | |
9549 | 130 info = g_new0(GaimAwayState, 1); |
131 info->state = state; | |
132 info->message = message; | |
9546 | 133 |
9549 | 134 g_hash_table_insert(awayStates, account, info); |
9546 | 135 } else if(!strcmp(state,"Back")) |
136 g_hash_table_remove(awayStates, account); | |
137 } | |
138 | |
139 static void restore_state(GaimConnection *gc, void *m) { | |
9549 | 140 /* Restore the state to what it was before the disconnect */ |
141 GaimAwayState *info; | |
142 GaimAccount *account; | |
9546 | 143 |
9549 | 144 g_return_if_fail(gc != NULL && gaim_prefs_get_bool(OPT_RESTORE_STATE)); |
145 account = gaim_connection_get_account(gc); | |
9546 | 146 |
9549 | 147 info = g_hash_table_lookup(awayStates, account); |
148 if (info) | |
149 serv_set_away(gc, info->state, info->message); | |
9546 | 150 } |
8774 | 151 |
6113 | 152 static void |
153 free_auto_recon(gpointer data) | |
154 { | |
155 GaimAutoRecon *info = data; | |
156 | |
157 if (info->timeout != 0) | |
158 g_source_remove(info->timeout); | |
159 | |
160 g_free(info); | |
161 } | |
162 | |
8774 | 163 |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
164 static gboolean |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
165 plugin_load(GaimPlugin *plugin) |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
166 { |
8774 | 167 |
168 /* this was the suggested way to override a single function of the | |
169 real ui ops. However, there's a mild concern of having more than one | |
170 bit of code making a new ui op call-through copy. If plugins A and B | |
171 both override the ui ops (in that order), B thinks that the | |
172 overridden ui ops A created was the original. If A unloads first, | |
173 and swaps out and frees its overridden version, then B is calling | |
174 through to a free'd ui op. There needs to be a way to "stack up" | |
175 overridden ui ops or something... I have a good idea of how to write | |
176 such a creature if someone wants it done. - siege 2004-04-20 */ | |
177 | |
178 /* get old ops, make a copy with a minor change */ | |
179 old_ops = gaim_connections_get_ui_ops(); | |
180 new_ops = (GaimConnectionUiOps *) g_memdup(old_ops, | |
181 sizeof(GaimConnectionUiOps)); | |
182 new_ops->report_disconnect = report_disconnect; | |
183 gaim_connections_set_ui_ops(new_ops); | |
184 | |
6485
70d5122bc3ff
[gaim-migrate @ 6999]
Christian Hammond <chipx86@chipx86.com>
parents:
6371
diff
changeset
|
185 hash = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, |
8774 | 186 free_auto_recon); |
3630 | 187 |
9546 | 188 awayStates = g_hash_table_new(g_int_hash, g_int_equal); |
189 | |
6485
70d5122bc3ff
[gaim-migrate @ 6999]
Christian Hammond <chipx86@chipx86.com>
parents:
6371
diff
changeset
|
190 gaim_signal_connect(gaim_connections_get_handle(), "signed-off", |
8774 | 191 plugin, GAIM_CALLBACK(reconnect), NULL); |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
192 |
9546 | 193 gaim_signal_connect(gaim_connections_get_handle(), "signed-on", |
194 plugin, GAIM_CALLBACK(restore_state), NULL); | |
195 | |
196 gaim_signal_connect(gaim_accounts_get_handle(), "account-away", | |
197 plugin, GAIM_CALLBACK(save_state), NULL); | |
198 | |
199 | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
200 return TRUE; |
3802 | 201 } |
202 | |
8774 | 203 |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
204 static gboolean |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
205 plugin_unload(GaimPlugin *plugin) |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
206 { |
8243 | 207 gaim_signal_disconnect(gaim_connections_get_handle(), "signed-off", |
208 plugin, GAIM_CALLBACK(reconnect)); | |
9546 | 209 |
210 gaim_signal_disconnect(gaim_connections_get_handle(), "signed-on", | |
211 plugin, GAIM_CALLBACK(restore_state)); | |
212 | |
213 gaim_signal_disconnect(gaim_accounts_get_handle(), "account-away", | |
214 plugin, GAIM_CALLBACK(save_state)); | |
9549 | 215 |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
216 g_hash_table_destroy(hash); |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
217 hash = NULL; |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
218 |
9546 | 219 g_hash_table_destroy(awayStates); |
220 awayStates = NULL; | |
221 | |
8774 | 222 gaim_connections_set_ui_ops(old_ops); |
223 g_free(new_ops); | |
224 old_ops = new_ops = NULL; | |
225 | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
226 return TRUE; |
3630 | 227 } |
228 | |
8774 | 229 |
230 static GaimPluginPrefFrame *get_plugin_pref_frame(GaimPlugin *plugin) { | |
231 GaimPluginPrefFrame *frame = gaim_plugin_pref_frame_new(); | |
232 GaimPluginPref *pref; | |
233 | |
234 pref = gaim_plugin_pref_new_with_label(_("Error Message Suppression")); | |
9549 | 235 gaim_plugin_pref_frame_add(frame, pref); |
8774 | 236 |
237 pref = gaim_plugin_pref_new_with_name_and_label(OPT_HIDE_CONNECTED, | |
238 _("Hide Disconnect Errors")); | |
239 gaim_plugin_pref_frame_add(frame, pref); | |
240 | |
241 pref = gaim_plugin_pref_new_with_name_and_label(OPT_HIDE_CONNECTING, | |
242 _("Hide Login Errors")); | |
243 gaim_plugin_pref_frame_add(frame, pref); | |
244 | |
9546 | 245 pref = gaim_plugin_pref_new_with_name_and_label(OPT_RESTORE_STATE, |
9666 | 246 _("Restore Away State On Reconnect")); |
9546 | 247 gaim_plugin_pref_frame_add(frame, pref); |
248 | |
8774 | 249 return frame; |
250 } | |
251 | |
252 | |
253 static GaimPluginUiInfo pref_info = { | |
254 get_plugin_pref_frame | |
255 }; | |
256 | |
257 | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
258 static GaimPluginInfo info = |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
259 { |
9943 | 260 GAIM_PLUGIN_MAGIC, |
261 GAIM_MAJOR_VERSION, | |
262 GAIM_MINOR_VERSION, | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
263 GAIM_PLUGIN_STANDARD, /**< type */ |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
264 NULL, /**< ui_requirement */ |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
265 0, /**< flags */ |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
266 NULL, /**< dependencies */ |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
267 GAIM_PRIORITY_DEFAULT, /**< priority */ |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
268 |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
269 AUTORECON_PLUGIN_ID, /**< id */ |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
270 N_("Auto-Reconnect"), /**< name */ |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
271 VERSION, /**< version */ |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
272 /** summary */ |
6485
70d5122bc3ff
[gaim-migrate @ 6999]
Christian Hammond <chipx86@chipx86.com>
parents:
6371
diff
changeset
|
273 N_("When you are kicked offline, this reconnects you."), |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
274 /** description */ |
6485
70d5122bc3ff
[gaim-migrate @ 6999]
Christian Hammond <chipx86@chipx86.com>
parents:
6371
diff
changeset
|
275 N_("When you are kicked offline, this reconnects you."), |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
276 "Eric Warmenhoven <eric@warmenhoven.org>", /**< author */ |
6485
70d5122bc3ff
[gaim-migrate @ 6999]
Christian Hammond <chipx86@chipx86.com>
parents:
6371
diff
changeset
|
277 GAIM_WEBSITE, /**< homepage */ |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
278 |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
279 plugin_load, /**< load */ |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
280 plugin_unload, /**< unload */ |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
281 NULL, /**< destroy */ |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
282 |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
283 NULL, /**< ui_info */ |
8774 | 284 NULL, /**< extra_info */ |
8993 | 285 &pref_info, /**< prefs_info */ |
286 NULL | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
287 }; |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
288 |
8774 | 289 |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
290 static void |
5920
7d385de2f9cd
[gaim-migrate @ 6360]
Christian Hammond <chipx86@chipx86.com>
parents:
5873
diff
changeset
|
291 init_plugin(GaimPlugin *plugin) |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
4590
diff
changeset
|
292 { |
8774 | 293 gaim_prefs_add_none(AUTORECON_OPT); |
294 gaim_prefs_add_bool(OPT_HIDE_CONNECTED, FALSE); | |
295 gaim_prefs_add_bool(OPT_HIDE_CONNECTING, FALSE); | |
9546 | 296 gaim_prefs_add_bool(OPT_RESTORE_STATE, TRUE); |
3630 | 297 } |
298 | |
6063 | 299 GAIM_INIT_PLUGIN(autorecon, init_plugin, info) |
8774 | 300 |