Exemplo n.º 1
0
void win_chat_append(const gchar *id, const gchar *msg, gboolean me, const gchar *str_time)
{
	Chat *c;
	User *u;
	gchar *m = NULL;
	gchar *name = NULL;
	gchar *tim;
	time_t prev_time;

	c = win_chat_get(id, TRUE, TRUE);
	g_assert(c != NULL);

	if (str_time == NULL) {
		tim = (gchar *)get_time(NULL);
	} else {
		tim = (gchar *)str_time;
	}
	
	/* XXX: dodawanie usera do listy jako brak autoryzacji */
	u = user_get(id);
	if (u == NULL) {
		name = (gchar *)id;
	} else {
		name = u->name;
	}

	prev_time = u->last_chat;

	/* Zapisz czas ostatniej wypowiedzi */
	time(&u->last_chat);

	/* Sprawdz czy tekst nie pojawil sie po dlugiej przerwie. Jesli tak, traktujemy
	   to jako rozpoczecie nowej rozmowy */

	if (prev_time == 0 || u->last_chat - prev_time >= 60 * 60) {	/* Godzina, XXX: #define */
		arch_add(me ? ARCH_TYPE_CHAT_START_BY_ME : ARCH_TYPE_CHAT_START, u->last_chat, u, NULL);
	}

	/* Dodaj do archiwum */
	arch_add(me ? ARCH_TYPE_CHAT_FROM_ME : ARCH_TYPE_CHAT_TO_ME, u->last_chat, u, msg);

	m = markup_text(msg);

	if (me) {
		gtk_html_stream_printf(c->stream, append_fmt, my_bg, tim, pref_chat_my_name, pref_tlen_id, m);
	} else {
		gtk_html_stream_printf(c->stream, append_fmt, their_bg, tim, name, id, m);
	}

	/* scroll */
	gtk_html_flush(c->output);
	gtk_html_command(c->output, "scroll-eod");
	
	if (c->need_blinking == TRUE && c->blink_state == BlinkOff) {
		g_timeout_add(750, window_icon_blink, c);
	}

	g_free(m);
}
Exemplo n.º 2
0
void user_info(int id, char *name) {
	_RTS_USER _rts_user = user_get(id, name);
	printf("****************\n");
	printf("id : %d \n", _rts_user.id);
	printf("name : %s \n", _rts_user.name);
	printf("password : %s \n", _rts_user.password);
	printf("salt : %s \n", _rts_user.salt);
	printf("datetime : %s \n", _rts_user.datetime);
	printf("status : %d \n", _rts_user.status);
	printf("****************\n\n");
}
Exemplo n.º 3
0
int auth_session(http_request_t *hr, char *login, char *password)
{
    user_t *user;

    if(login && password &&
       strcmp(login, password) == 0) {
        user = user_get(login);
        http_request_set_data(hr, user, NULL);
        return 0;
    }
    return -1;
}
Exemplo n.º 4
0
int main()
{
    valerooncom_t * myNetwork = valerooncom_new();
    user_t * Valera = user_new(8, "valerik", "Valera", "Babenko", 18, "Ukraine", myNetwork);
    user_t * Igor = user_new(7, "igormet", "Igor", "Babenko", 18, "Lvov", myNetwork);
    user_t * myUser = user_get(myNetwork, 7);
    user_view(myUser);

    user_edit(7, myNetwork, "igoralpha", "Igor", "Babenko", 19, "Russia");
    post_t * postAboutMonster = post_new("My name is Igor and i'm a big monster!", Igor);

    //comment_new(Valera, postAboutMonster, "F**k, it's great!");

    user_view(Igor);
    return 0;
}
Exemplo n.º 5
0
void obs_vector_user_get(const obs_vector_type * obs_vector , const char * index_key , int report_step , double * value , double * std , bool * valid) {
  void * obs_node = obs_vector_iget_node( obs_vector , report_step );
  obs_vector->user_get(obs_node , index_key , value , std , valid);
}
Exemplo n.º 6
0
Arquivo: s725get.c Projeto: ra1fh/s725
int
main(int argc, char **argv)
{
	struct			  stat s;
	char			  path[PATH_MAX];
	char			  inipath[PATH_MAX];
	const char		 *opt_directory_name = NULL;
	const char		 *opt_driver_name = NULL;
	int				  opt_driver_type = DRIVER_SERIAL;
	const char		 *opt_device_name = NULL;
	int				  opt_format_list[5] = { 0 /* FORMAT_UNKNOWN */ };
	int				  opt_format_index = 0;
	BUF				 *files;
	int				  opt_time = 0;
	int				  opt_user = 0;
	int				  opt_listen = 0;
	int				  ch;
	int				  ok;
	char			 *ap;
	int				  format;
	int				  ret;
	int				  i;

	snprintf(inipath, PATH_MAX, "%s/.s725rc", getenv("HOME"));
	yyin = fopen(inipath, "r");
	if (yyin != NULL) {
		conf_filename = inipath;
		ret = yyparse();
		if (ret != 0)
			exit(1);
		if (conf_driver_type != DRIVER_UNKNOWN)
			opt_driver_type = conf_driver_type;
		if (conf_format_type != FORMAT_UNKNOWN)
			opt_format_list[opt_format_index] = conf_format_type;
		if (conf_device_name != NULL)
			opt_device_name = conf_device_name;
		if (conf_directory_name != NULL)
			opt_directory_name = conf_directory_name;
	}

	while ((ch = getopt(argc, argv, "d:D:f:hlo:rtuv")) != -1) {
		switch (ch) {
		case 'd':
			opt_driver_name = optarg;
			opt_driver_type = driver_name_to_type(opt_driver_name);
			if (opt_driver_type == DRIVER_UNKNOWN)
				fatalx("unknown driver type: %s", opt_driver_name);
			break;
		case 'D':
			opt_device_name = optarg;
			break;
		case 'f':
			opt_directory_name = optarg;
			break;
		case 'l':
			opt_listen = 1;
			break;
		case 'o':
			ap = optarg;
			if (opt_format_index < (sizeof(opt_format_list) /
									sizeof(opt_format_list[0]))) {
				if ((format = format_from_str(ap)) != FORMAT_UNKNOWN)
					opt_format_list[opt_format_index++] = format;
				else
					fatalx("unknown output format: %s", ap);
			}
			break;
		case 't':
			opt_time = 1;
			break;
		case 'u':
			opt_user = 1;
			break;
		case 'v':
			log_add_level();
			break;
		case 'h':
			usage();
			return 0;
			break;
		default:
			usage();
			return 1;
		}
	}

	if (opt_driver_type == DRIVER_SERIAL) {
		if (!opt_device_name)
			fatalx("device name required for %s driver",
				   driver_type_to_name(opt_driver_type));
	}

	if (! (opt_time || opt_user)) {
		if (opt_format_list[0] == FORMAT_UNKNOWN)
			fatalx("no output format specified");
	}

	for (i = 0; i < sizeof(opt_format_list) / sizeof(opt_format_list[0]); ++i) {
		log_info("format: %s", format_to_str(opt_format_list[i]));
	}

	log_info("driver name: %s", driver_type_to_name(opt_driver_type));
	log_info("driver type: %d", opt_driver_type);
	log_info("device name: %s", opt_device_name ? opt_device_name : "");
	log_info("directory name: %s", opt_directory_name ? opt_directory_name : "");

	ok = driver_init(opt_driver_type, opt_device_name);
	if (ok != 1)
		fatalx("driver_init failed");

	if (opt_directory_name) {
		opt_directory_name = realpath(opt_directory_name, path);
	} else {
		getcwd(path, sizeof(path));
		opt_directory_name = path;
	}

	if (!opt_directory_name)
		fatalx("could not resolve path. check -f");

	if (access(opt_directory_name, R_OK | W_OK | X_OK) != 0)
		fatalx("unable to access directory: %s", opt_directory_name);

	if (stat(opt_directory_name, &s) != 0)
		fatalx("unable to stat directory: %s", opt_directory_name);

	if (! S_ISDIR(s.st_mode))
		fatalx("not a directory directory: %s", opt_directory_name);

	if (driver_open() < 0)
		fatalx("unable to open port: %s", strerror(errno));

	if (opt_time) {
		time_get();
		driver_close();
		return 0;
	}

	if (opt_user) {
		user_get();
		driver_close();
		return 0;
	}

	files = buf_alloc(0);

	if (opt_listen) {
		ret = files_listen(files);
	} else {
		ret = files_get(files);
	}

	if (ret) {
		for (i = 0; i < sizeof(opt_format_list) / sizeof(opt_format_list[0]); ++i) {
			if (opt_format_list[i] != FORMAT_UNKNOWN) {
				write_hrm_data(files, opt_directory_name, opt_format_list[i]);
			}
		}
	}

	buf_free(files);
	driver_close();
	return 0;
}
Exemplo n.º 7
0
Chat * win_chat_create(const gchar *id)
{
	GtkWidget *scroll;
	Chat *c;
	GtkWidget *b;
	gchar *s;
	
	c = (Chat *)g_malloc(sizeof(Chat));
	c->id = g_strdup(id);
	c->enter_sends = pref_chat_enter_sends;
	
	c->blink_state = BlinkOff;
	c->need_blinking = FALSE;

	c->notify_sent = FALSE;

	c->user = user_get(id);

	c->win = GTK_WINDOW(create_win_chat());
	g_assert(c->win != NULL);
	
	scroll = lookup_widget(GTK_WIDGET(c->win), "scroll_output");
	g_assert(scroll != NULL);
	c->input = GTK_TEXT_VIEW(lookup_widget(GTK_WIDGET(c->win), "input"));
	g_assert(c->input != NULL);

	c->output = GTK_HTML(glen_html_new());
	g_assert(c->output != NULL);
	gtk_widget_set_name(GTK_WIDGET(c->output), "output");
	
	gtk_widget_set(GTK_WIDGET(c->output), "can-focus", FALSE, NULL);

	gtk_container_add(GTK_CONTAINER(scroll), GTK_WIDGET(c->output));

	gtk_widget_show(GTK_WIDGET(c->output));

	c->stream = glen_html_begin(c->output);
//	gtk_html_write(c->output, c->stream, header, strlen(header));

/* ustaw pixbuf dla 'lampki' */
	b = lookup_widget(GTK_WIDGET(c->win), "image_typing");
	g_assert(b != NULL);
	gtk_image_set_from_pixbuf(GTK_IMAGE(b), typing_off_pixbuf);

/* callbacki do przyciskow */
	b = lookup_widget(GTK_WIDGET(c->win), "clear_btn");
	g_assert(b != NULL);
	g_signal_connect(G_OBJECT(b), "clicked",
		G_CALLBACK(clear_btn_clicked_cb), (gpointer)c);

	b = lookup_widget(GTK_WIDGET(c->win), "send_btn");
	g_assert(b != NULL);
	g_signal_connect(G_OBJECT(b), "clicked",
		G_CALLBACK(send_btn_clicked_cb), (gpointer)c);

	b = lookup_widget(GTK_WIDGET(c->win), "close_btn");
	g_assert(b != NULL);
	g_signal_connect(G_OBJECT(b), "clicked",
		G_CALLBACK(close_btn_clicked_cb), (gpointer)c);

	b = lookup_widget(GTK_WIDGET(c->win), "enter_sends_btn");
	g_assert(b != NULL);
	g_signal_connect(G_OBJECT(b), "toggled",
		G_CALLBACK(enter_sends_btn_cb), (gpointer)c);
	
/* reszta */
	b = lookup_widget(GTK_WIDGET(c->win), "input");
	g_assert(b != NULL);
	g_signal_connect(G_OBJECT(b), "key_press_event",
		G_CALLBACK(input_key_press_cb), (gpointer)c);
	g_signal_connect(G_OBJECT(b), "copy_clipboard",
		G_CALLBACK(input_copy_clipboard_cb), (gpointer)c);
	
	gtk_widget_grab_focus(b);

	g_signal_connect(G_OBJECT(c->win), "delete_event",
		G_CALLBACK(delete_win_cb), c);
//	g_signal_connect(G_OBJECT(c->win), "window_state_event",
//		G_CALLBACK(window_state_cb), c);
	g_signal_connect_after(G_OBJECT(c->win), "visibility_notify_event",
		G_CALLBACK(visibility_changed_cb), c);
	g_signal_connect(G_OBJECT(c->win), "focus_in_event",
		G_CALLBACK(focus_in_cb), c);

	/* ustaw tooltip dla 'lampki */
	b = lookup_widget(GTK_WIDGET(c->win), "eventbox_typing");
	g_assert(b != NULL);
	s = toutf("Je¶li lampka zapali siê oznacza to, ¿e Twój "
		"rozmówca w³a¶nie pisze wiadomo¶æ");
	gtk_tooltips_set_tip(tooltips, b, s, NULL);
	g_free(s);
	
	return c;
}
Exemplo n.º 8
0
/**
 * 和客户交互
 * @param  sockfd [description]
 * @param  i      [description]
 * @return        [description]
 */
int client_interface(struct bufferevent *bev, client_t *client) {
    /*加锁*/
    if (pthread_mutex_lock(&mutex) != 0) {
        perror("pthread_mutex_lock");
        exit(EXIT_FAILURE);
    }

    char data[4096];
    int nbytes;

    //接收数据
    char buffer[MAX_BUF + 1];
    bzero(buffer, MAX_BUF + 1);
    int n = 0;
    int str_len = 0;
    int last_len;
    while ((nbytes = EVBUFFER_LENGTH(bev->input)) > 0) {
        if (nbytes > 4096) nbytes = 4096;
        n += evbuffer_remove(bev->input, data, nbytes);
        str_len = strlen(data);
        last_len = (MAX_BUF - strlen(buffer));
        if (last_len >= str_len) {
            strncat(buffer, data, str_len);
        } else {
            strncat(buffer, data, last_len);
        }
    }

    if (n <= 0) {
        /*解锁锁*/
        if (pthread_mutex_unlock(&mutex) != 0) {
            perror("pthread_mutex_unlock");
            exit(EXIT_FAILURE);
        }

        return 0; //客户端退出时
    }

    //printf("第%d个客户端 IP:%s\n", i + 1, inet_ntoa(_client[i].addr.sin_addr));

    //解析客户端信息
    int flag = 0;

    int ret;
    char key[20];
    sprintf(key, "%d", client->fd);
    client_elem *c_el;
    ret = hashmap_get(client_map, key, (void **)&c_el);
    assert(ret == HMAP_S_OK);

    _RTS_TRANSPORT_DATA _rts_transport_data;
    _rts_transport_data = RTS_transport_data_init();
    if (RTS_transport_data_parse(buffer, &_rts_transport_data) == 0) {
        client_send(client, "{\"code\":\"0001\",\"message\":\"数据格式非法\"}");

        /*解锁锁*/
        if (pthread_mutex_unlock(&mutex) != 0) {
            perror("pthread_mutex_unlock");
            exit(EXIT_FAILURE);
        }

        return 0;
    }

    if (!_rts_transport_data.action) {
        client_send(client, "{\"code\":\"0005\",\"message\":\"参数非法\"}");

        /*解锁锁*/
        if (pthread_mutex_unlock(&mutex) != 0) {
            perror("pthread_mutex_unlock");
            exit(EXIT_FAILURE);
        }

        return 0;
    }

    if (strcmp(_rts_transport_data.action, "login") == 0) { //登录
        if (strlen(_rts_transport_data.name) == 0 || strlen(_rts_transport_data.password) == 0) {
            client_send(client, "{\"code\":\"0005\",\"message\":\"参数非法\"}");

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 0;
        }

        //验证用户名密码
        _RTS_USER *_rts_user = user_get(0, _rts_transport_data.name);
        if (_rts_user->id <= 0) {
            client_send(client, "{\"code\":\"0003\",\"message\":\"用户名或密码错误\"}");
            CLIENT_FREE(_rts_user);

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 0;
        }

        char *pwdhash = RTS_hash(_rts_transport_data.password, _rts_user->salt);

        if (strcmp(_rts_user->password, pwdhash) != 0) {
            client_send(client, "{\"code\":\"0003\",\"message\":\"用户名或密码错误\"}");
            CLIENT_FREE(pwdhash);
            CLIENT_FREE(_rts_user);

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 0;
        }

        //用户名密码验证通过后,判断当前账号是否已登录
        //这时应该已经拿到了用户的id
        if (c_el->token && strlen(c_el->token) > 0) { //该设备已经登录了账号,不能再做登录操作
            client_send(client, "{\"code\":\"1004\",\"message\":\"该设备已经登录了账号,不能再做登录操作\"}");
            CLIENT_FREE(pwdhash);
            CLIENT_FREE(_rts_user);

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 1;
        }

        if (c_el->id == 0 || (c_el->token && strlen(c_el->token) == 0)) { //未登录,直接执行登录赋值操作
            //登录成功,返回认证标示
            char *unique = RTS_unique();
            strncpy(_rts_transport_data.token, unique, MAX_TOKEN_LENGTH);
            CLIENT_FREE(unique);

            //重新设置client_map
            ret = hashmap_remove(client_map, c_el->key, (void **)&c_el);
            assert(ret == HMAP_S_OK);
            client_elem *new_c_el = (client_elem *)malloc(sizeof(client_elem));
            sprintf(new_c_el->key, "%d", client->fd);
            new_c_el->addr = c_el->addr;
            new_c_el->client = client;
            new_c_el->id = _rts_user->id;
            new_c_el->name = (char *)malloc(MAX_NAME_LENGTH + 1);
            strncpy(new_c_el->name, _rts_transport_data.name, MAX_NAME_LENGTH);
            new_c_el->token = (char *)malloc(MAX_TOKEN_LENGTH + 1);
            strncpy(new_c_el->token, _rts_transport_data.token, MAX_TOKEN_LENGTH);
            free_client_elem(c_el, 0);
            ret = hashmap_put(client_map, new_c_el->key, new_c_el);
            assert(ret == HMAP_S_OK);

            //设置client_id_map
            client_id_elem *cid_el = (client_id_elem *)malloc(sizeof(client_id_elem));
            sprintf(cid_el->key, "%d", _rts_user->id);
            cid_el->fd = client->fd;
            ret = hashmap_put(client_id_map, cid_el->key, cid_el);
            assert(ret == HMAP_S_OK);

            RTS_printf("[++登录成功++]: NAME:%s--IP:%s\n", _rts_transport_data.name, inet_ntoa(c_el->addr.sin_addr));
            bzero(buf, MAX_BUF + 1);
            sprintf(buf, "{\"code\":\"0000\",\"message\":\"登录成功\",\"token\":\"%s\",\"id\":%d}", _rts_transport_data.token, _rts_user->id);
            client_send(client, buf);

            //修改登录成功标识
            _RTS_USER _rts_user2 = user_init();
            _rts_user2.id = _rts_user->id;
            _rts_user2.status = 1;
            user_edit(_rts_user2);

            CLIENT_FREE(pwdhash);
            CLIENT_FREE(_rts_user);

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 1;
        }

        client_send(client, "{\"code\":\"1003\",\"message\":\"该用户已经在其他地方成功登录\"}");
        CLIENT_FREE(pwdhash);
        CLIENT_FREE(_rts_user);

        /*解锁锁*/
        if (pthread_mutex_unlock(&mutex) != 0) {
            perror("pthread_mutex_unlock");
            exit(EXIT_FAILURE);
        }

        return 1;

    }

    if (strcmp(_rts_transport_data.action, "message") == 0) { //聊天
        if (strlen(_rts_transport_data.token) == 0 || !_rts_transport_data.toid || !_rts_transport_data.id || wcslen(_rts_transport_data.content) == 0) {
            client_send(client, "{\"code\":\"0005\",\"message\":\"参数非法\"}");

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 0;
        }

        //认证信息失败,退出客户端
        if (!c_el->token || (strcmp(_rts_transport_data.token, "bsh_test_$%1KP@'") != 0 && strcmp(_rts_transport_data.token, c_el->token) != 0)) {
            client_send(client, "{\"code\":\"0004\",\"message\":\"token非法\"}");

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 0;
        }

        //判断登录者身份
        if (!c_el->id || c_el->id != _rts_transport_data.id) {
            client_send(client, "{\"code\":\"0006\",\"message\":\"用户身份非法\"}");

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 0;
        }

        bzero(key, 20);
        sprintf(key, "%d", _rts_transport_data.toid);
        client_id_elem *cid_el;
        ret = hashmap_get(client_id_map, key, (void **)&cid_el);
        

        printf("发给id:%s\n", key);
        if (ret == 0) {
            printf("get_toid:%s ret:%d fd:%d\n", key, ret, cid_el->fd);
        }
        if (ret == HMAP_E_NOTFOUND || !cid_el || !cid_el->fd) {
            client_send(client, "{\"code\":\"1002\",\"message\":\"对方不在线\"}");

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 1;
        }

        bzero(key, 20);
        sprintf(key, "%d", cid_el->fd);
        client_elem *to_c_el;
        ret = hashmap_get(client_map, key, (void **)&to_c_el);
        assert(ret == HMAP_S_OK);

        if (to_c_el->id == _rts_transport_data.id) { //如果接受者是自己,则发出警告
            client_send(client, "{\"code\":\"1001\",\"message\":\"不能给自己发消息\"}");

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 1;
        }

        bzero(buf, MAX_BUF + 1);
        sprintf(buf, "{\"code\":\"0000\",\"message\":\"发送成功\",\"content\":\"%ls\"}", _rts_transport_data.content);
        client_send(to_c_el->client, buf);

        /*解锁锁*/
        if (pthread_mutex_unlock(&mutex) != 0) {
            perror("pthread_mutex_unlock");
            exit(EXIT_FAILURE);
        }

        return 1;
    }

    if (strcmp(_rts_transport_data.action, "logout") == 0) { //logout
        if (strlen(_rts_transport_data.token) == 0 || !_rts_transport_data.id) {
            client_send(client, "{\"code\":\"0005\",\"message\":\"参数非法\"}");

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 0;
        }

        //判断登录者身份
        if (!c_el->id || c_el->id != _rts_transport_data.id) {
            client_send(client, "{\"code\":\"0006\",\"message\":\"用户身份非法\"}");

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 0;
        }

        //认证信息失败,退出客户端
        if (c_el->token && strcmp(_rts_transport_data.token, c_el->token) != 0) {
            client_send(client, "{\"code\":\"0004\",\"message\":\"token非法\"}");

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 0;
        }

        client_send(client, "{\"code\":\"0000\",\"message\":\"退出成功\"}");

        /*解锁锁*/
        if (pthread_mutex_unlock(&mutex) != 0) {
            perror("pthread_mutex_unlock");
            exit(EXIT_FAILURE);
        }

        return 0;
    }

    if (strcmp(_rts_transport_data.action, "register") == 0) {
        if (strlen(_rts_transport_data.name) == 0 || strlen(_rts_transport_data.password) == 0) {
            client_send(client, "{\"code\":\"0005\",\"message\":\"参数非法\"}");

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 0;
        }

        char *salt = RTS_rand();
        char *pwdhash = RTS_hash(_rts_transport_data.password, salt);
        char *datetime = RTS_current_datetime();
        _rts_transport_data.id = user_add(_rts_transport_data.name, pwdhash, salt, inet_ntoa(c_el->addr.sin_addr), datetime, 0);
        CLIENT_FREE(salt);
        CLIENT_FREE(pwdhash);
        CLIENT_FREE(datetime);

        if (_rts_transport_data.id == 0) {
            client_send(client, "{\"code\":\"1005\",\"message\":\"注册失败,该用户名已注册\"}");

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 1;
        }

        if (_rts_transport_data.id > 0) {
            bzero(buf, MAX_BUF + 1);
            sprintf(buf, "{\"code\":\"0000\",\"message\":\"注册成功\",\"id\":%d}", _rts_transport_data.id);
            client_send(client, buf);

            /*解锁锁*/
            if (pthread_mutex_unlock(&mutex) != 0) {
                perror("pthread_mutex_unlock");
                exit(EXIT_FAILURE);
            }

            return 1;
        }

        client_send(client, "{\"code\":\"0007\",\"message\":\"注册失败,未知错误\"}");

        /*解锁锁*/
        if (pthread_mutex_unlock(&mutex) != 0) {
            perror("pthread_mutex_unlock");
            exit(EXIT_FAILURE);
        }

        return 1;
    }

    client_send(client, "{\"code\":\"0002\",\"message\":\"动作非法\"}");

    /*解锁锁*/
    if (pthread_mutex_unlock(&mutex) != 0) {
        perror("pthread_mutex_unlock");
        exit(EXIT_FAILURE);
    }

    return 0;
}
Exemplo n.º 9
0
int main(int argc, char *argv[])
{
	//forum state
	//forum = *(forum_t**)get_env(argv, US_HANDLER_DATA, 0);

	//HTTP request
	request_t req = {
	.args = parse_args(argc, argv),
	.user = user_get(forum, 1), //user from cookies
	};

	//HTTP response
	xbuf_t *reply = get_reply(argv);

	int http_code = 405; //method not allowed
	switch (get_env(argv, REQUEST_METHOD, 0))
	{
		case HTTP_GET:
		{
			http_code = method_get(forum, &req, reply);
		} break;
		case HTTP_POST:
		{
			http_code = method_post(forum, &req, reply);
		} break;
		case HTTP_PUT:
		{
			http_code = method_put(forum, &req, reply);
		} break;
		case HTTP_DELETE:
		{