--- src/main.c.orig	Thu Aug 31 10:21:21 2006
+++ src/main.c	Thu Nov  9 01:20:40 2006
@@ -113,6 +113,7 @@
 	GPtrArray *status_full_folders;
 	gboolean configdir;
 	gboolean exit;
+	const char *open_resource;
 } cmd;
 
 static void parse_cmd_opt		(int		 argc,
@@ -307,6 +308,8 @@
 {
 	gint i;
 
+	cmd.open_resource = NULL;
+
 	for (i = 1; i < argc; i++) {
 		if (!strncmp(argv[i], "--debug", 7)) {
 			init_console();
@@ -427,7 +430,8 @@
 
 			cleanup_console();
 			exit(1);
-		}
+		} else
+			cmd.open_resource = argv[i];
 	}
 
 	if (cmd.attach_files && cmd.compose == FALSE) {
@@ -958,6 +962,12 @@
 		}
 	} else if (cmd.exit) {
 		fd_write_all(sock, "exit\n", 5);
+	} else if (cmd.open_resource) {
+		char *str;
+
+		str = g_strdup_printf("open_resource %s\n", cmd.open_resource);
+		fd_write_all(sock, str, strlen(str));
+		g_free(str);
 	} else {
 #ifdef G_OS_WIN32
 		HWND hwnd;
@@ -1019,6 +1029,78 @@
 	return folders;
 }
 
+static gboolean find_folder_item_cb(GNode *node, gpointer data)
+{
+	FolderItem *item = node->data;
+	gpointer *d = data;
+	char *path;
+	gboolean found;
+
+	path = folder_item_get_path(item);
+	found = ! strcmp(path, d[0]);
+	g_free(path);
+
+	if (found) {
+		d[1] = item;
+		return TRUE;	/* stop traversal */
+	}
+
+	return FALSE;
+}
+
+static FolderItem *find_folder_item(const char *path)
+{
+	GList *l;
+
+	for (l = folder_get_list(); l != NULL; l = l->next) {
+		Folder *folder = l->data;
+		gpointer d[2];
+
+		d[0] = (gpointer) path;
+		d[1] = NULL;
+
+		g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, find_folder_item_cb, d);
+		if (d[1] != NULL)
+			return d[1];
+	}
+
+	return NULL;
+}
+
+/* resource: folder or message */
+static void open_resource(const char *resource)
+{
+	FolderItem *item;
+	int msgnum = -1;
+
+	item = find_folder_item(resource);
+	if (item == NULL) {
+		char *dirname;
+
+		dirname = g_path_get_dirname(resource);
+		item = find_folder_item(dirname);
+		g_free(dirname);
+
+		if (item != NULL) {
+			char *basename;
+
+			basename = g_path_get_basename(resource);
+			msgnum = atoi(basename);
+			g_free(basename);
+		}
+	}
+
+	if (item != NULL) {
+		MainWindow *mainwin = main_window_get();
+
+		folderview_select(mainwin->folderview, item);
+		if (msgnum != -1) {
+			summary_select_by_msgnum(mainwin->summaryview, msgnum);
+			summary_display_msg_selected(mainwin->summaryview, FALSE, FALSE);
+		}
+	}
+}
+
 static gboolean lock_socket_input_cb(GIOChannel *source, GIOCondition condition,
 				     gpointer data)
 {
@@ -1030,7 +1112,7 @@
 	sock = fd_accept(fd);
 	fd_gets(sock, buf, sizeof(buf));
 
-	if (!strncmp(buf, "popup", 5)) {
+	if (!strncmp(buf, "popup", 5) || g_str_has_prefix(buf, "open_resource")) {
 #ifdef G_OS_WIN32
 		HWND hwnd;
 
@@ -1042,6 +1124,16 @@
 #else
 		main_window_popup(mainwin);
 #endif
+
+		if (g_str_has_prefix(buf, "open_resource")) {
+			int resource_start;
+			char *resource;
+
+			resource_start = strlen("open_resource") + 1;
+			resource = g_strndup(buf + resource_start, strlen(buf) - resource_start - 1);
+			open_resource(resource);
+			g_free(resource);
+		}
 	} else if (!strncmp(buf, "receive_all", 11)) {
 		main_window_popup(mainwin);
 		inc_all_account_mail(mainwin, FALSE);
@@ -1122,6 +1214,8 @@
 		app_will_exit(TRUE);
 		exit(0);
 	}
+	if (cmd.open_resource)
+		open_resource(cmd.open_resource);
 }
 
 static void migrate_old_config(void)
