summaryrefslogtreecommitdiff
path: root/src/caja-dropbox-hooks.c
blob: 6a13ef84d0800bf58294f3d605f0e03f6defe983 (plain)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * Copyright 2008 Evenflow, Inc.
 *
 * caja-dropbox-hooks.c
 * Implements connection handling and C interface for the Dropbox hook socket.
 *
 * This file is part of caja-dropbox.
 *
 * caja-dropbox 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 3 of the License, or
 * (at your option) any later version.
 *
 * caja-dropbox 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 caja-dropbox.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

#include <string.h>

#include <glib.h>

#include "g-util.h"
#include "async-io-coroutine.h"
#include "dropbox-client-util.h"
#include "caja-dropbox-hooks.h"

typedef struct {
  DropboxUpdateHook hook;
  gpointer ud;
} HookData;

static gboolean
try_to_connect(CajaDropboxHookserv *hookserv);

static gboolean
handle_hook_server_input(GIOChannel *chan,
			 GIOCondition cond,
			 CajaDropboxHookserv *hookserv) {
  /*debug_enter(); */

  /* we have some sweet macros defined that allow us to write this
     async event handler like a microthread yeahh, watch out for context */
  CRBEGIN(hookserv->hhsi.line);
  while (1) {
    hookserv->hhsi.command_args =
      g_hash_table_new_full((GHashFunc) g_str_hash,
			    (GEqualFunc) g_str_equal,
			    (GDestroyNotify) g_free,
			    (GDestroyNotify) g_strfreev);
    hookserv->hhsi.numargs = 0;

    /* read the command name */
    {
      gchar *line;
      CRREADLINE(hookserv->hhsi.line, chan, line);
      hookserv->hhsi.command_name = dropbox_client_util_desanitize(line);
      g_free(line);
    }

    /*debug("got a hook name: %s", hookserv->hhsi.command_name); */

    /* now read each arg line (until a certain limit) until we receive "done" */
    while (1) {
      gchar *line;

      /* if too many arguments, this connection seems malicious */
      if (hookserv->hhsi.numargs >= 20) {
	CRHALT;
      }

      CRREADLINE(hookserv->hhsi.line, chan, line);

      if (strcmp("done", line) == 0) {
	g_free(line);
	break;
      }
      else {
	gboolean parse_result;

	parse_result =
	  dropbox_client_util_command_parse_arg(line,
						hookserv->hhsi.command_args);
	g_free(line);

	if (FALSE == parse_result) {
	  debug("bad parse");
	  CRHALT;
	}
      }

      hookserv->hhsi.numargs += 1;
    }

    {
      HookData *hd;
      hd = (HookData *)
	g_hash_table_lookup(hookserv->dispatch_table,
			    hookserv->hhsi.command_name);
      if (hd != NULL) {
	(hd->hook)(hookserv->hhsi.command_args, hd->ud);
      }
    }

    g_free(hookserv->hhsi.command_name);
    g_hash_table_unref(hookserv->hhsi.command_args);
    hookserv->hhsi.command_name = NULL;
    hookserv->hhsi.command_args = NULL;
  }
  CREND;
}

static void
watch_killer(CajaDropboxHookserv *hookserv) {
  debug("hook client disconnected");

  hookserv->connected = FALSE;

  g_hook_list_invoke(&(hookserv->ondisconnect_hooklist), FALSE);

  /* we basically just have to free the memory allocated in the
     handle_hook_server_init ctx */
  if (hookserv->hhsi.command_name != NULL) {
    g_free(hookserv->hhsi.command_name);
    hookserv->hhsi.command_name = NULL;
  }

  if (hookserv->hhsi.command_args != NULL) {
    g_hash_table_unref(hookserv->hhsi.command_args);
    hookserv->hhsi.command_args = NULL;
  }

  g_io_channel_unref(hookserv->chan);
  hookserv->chan = NULL;
  hookserv->event_source = 0;
  hookserv->socket = 0;

  /* lol we also have to start a new connection */
  try_to_connect(hookserv);
}

static gboolean
try_to_connect(CajaDropboxHookserv *hookserv) {
  /* create socket */
  hookserv->socket = socket(PF_UNIX, SOCK_STREAM, 0);

  /* set native non-blocking, for connect timeout */
  {
    unsigned int flags;

    if ((flags = fcntl(hookserv->socket, F_GETFL, 0)) < 0) {
      goto FAIL_CLEANUP;
    }

    if (fcntl(hookserv->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
      goto FAIL_CLEANUP;
    }
  }

  /* connect to server, might fail of course */
  {
    struct sockaddr_un addr;
    socklen_t addr_len;

    /* intialize address structure */
    addr.sun_family = AF_UNIX;
    g_snprintf(addr.sun_path,
	       sizeof(addr.sun_path),
	       "%s/.dropbox/iface_socket",
	       g_get_home_dir());
    addr_len = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path);

    /* if there was an error we have to try again later */
    if (connect(hookserv->socket, (struct sockaddr *) &addr, addr_len) < 0) {
      if (errno == EINPROGRESS) {
	fd_set writers;
	struct timeval tv = {1, 0};
        FD_ZERO(&writers);
	FD_SET(hookserv->socket, &writers);

	/* if nothing was ready after 3 seconds, fail out homie */
	if (select(hookserv->socket+1, NULL, &writers, NULL, &tv) == 0) {
	  goto FAIL_CLEANUP;
	}

	if (connect(hookserv->socket, (struct sockaddr *) &addr, addr_len) < 0) {
	  debug("couldn't connect to hook server after 1 second");
	  goto FAIL_CLEANUP;
	}
      }
      else {
	goto FAIL_CLEANUP;
      }
    }
  }

  /* lol sometimes i write funny codez */
  if (FALSE) {
  FAIL_CLEANUP:
    close(hookserv->socket);
    g_timeout_add_seconds(1, (GSourceFunc) try_to_connect, hookserv);
    return FALSE;
  }

  /* great we connected!, let's create the channel and wait on it */
  hookserv->chan = g_io_channel_unix_new(hookserv->socket);
  g_io_channel_set_line_term(hookserv->chan, "\n", -1);
  g_io_channel_set_close_on_unref(hookserv->chan, TRUE);

  /*debug("create channel"); */

  /* Set non-blocking ;) (again just in case) */
  {
    GIOFlags flags;
    GIOStatus iostat;

    flags = g_io_channel_get_flags(hookserv->chan);
    iostat = g_io_channel_set_flags(hookserv->chan, flags | G_IO_FLAG_NONBLOCK,
				    NULL);
    if (iostat == G_IO_STATUS_ERROR) {
      g_io_channel_unref(hookserv->chan);
      g_timeout_add_seconds(1, (GSourceFunc) try_to_connect, hookserv);
      return FALSE;
    }
  }

  /*debug("set non blocking"); */

  /* this is fun, async io watcher */
  hookserv->hhsi.line = 0;
  hookserv->hhsi.command_args = NULL;
  hookserv->hhsi.command_name = NULL;
  hookserv->event_source =
    g_io_add_watch_full(hookserv->chan, G_PRIORITY_DEFAULT,
			G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
			(GIOFunc) handle_hook_server_input, hookserv,
			(GDestroyNotify) watch_killer);

  debug("hook client connected");
  hookserv->connected = TRUE;
  g_hook_list_invoke(&(hookserv->onconnect_hooklist), FALSE);

  /*debug("added watch");*/
  return FALSE;
}

/* should only be called in glib main loop */
/* returns a gboolean because it is a GSourceFunc */
gboolean caja_dropbox_hooks_force_reconnect(CajaDropboxHookserv *hookserv) {
  debug_enter();

  if (hookserv->connected == FALSE) {
    return FALSE;
  }

  debug("forcing hook to reconnect");

  g_assert(hookserv->event_source >= 0);

  if (hookserv->event_source > 0) {
    g_source_remove(hookserv->event_source);
  }
  else if (hookserv->event_source == 0) {
    debug("event source was zero!!!!!");
  }

  return FALSE;
}

gboolean
caja_dropbox_hooks_is_connected(CajaDropboxHookserv *hookserv) {
  return hookserv->connected;
}

void
caja_dropbox_hooks_setup(CajaDropboxHookserv *hookserv) {
  hookserv->dispatch_table = g_hash_table_new_full((GHashFunc) g_str_hash,
						   (GEqualFunc) g_str_equal,
						   g_free, g_free);
  hookserv->connected = FALSE;

  g_hook_list_init(&(hookserv->ondisconnect_hooklist), sizeof(GHook));
  g_hook_list_init(&(hookserv->onconnect_hooklist), sizeof(GHook));
}

void
caja_dropbox_hooks_add_on_disconnect_hook(CajaDropboxHookserv *hookserv,
					      DropboxHookClientConnectHook dhcch,
					      gpointer ud) {
  GHook *newhook;

  newhook = g_hook_alloc(&(hookserv->ondisconnect_hooklist));
  newhook->func = dhcch;
  newhook->data = ud;

  g_hook_append(&(hookserv->ondisconnect_hooklist), newhook);
}

void
caja_dropbox_hooks_add_on_connect_hook(CajaDropboxHookserv *hookserv,
					   DropboxHookClientConnectHook dhcch,
					   gpointer ud) {
  GHook *newhook;

  newhook = g_hook_alloc(&(hookserv->onconnect_hooklist));
  newhook->func = dhcch;
  newhook->data = ud;

  g_hook_append(&(hookserv->onconnect_hooklist), newhook);
}

void caja_dropbox_hooks_add(CajaDropboxHookserv *ndhs,
				const gchar *hook_name,
				DropboxUpdateHook hook, gpointer ud) {
  HookData *hd;
  hd = g_new(HookData, 1);
  hd->hook = hook;
  hd->ud = ud;
  g_hash_table_insert(ndhs->dispatch_table, g_strdup(hook_name), hd);
}

void
caja_dropbox_hooks_start(CajaDropboxHookserv *hookserv) {
  try_to_connect(hookserv);
}