Esempio n. 1
0
void main() {
	char str1[] = "asdf1234";
	char str2[] = "qwer5678qwer5678";

	char *p;
	p = str_ncat(str1, str2, 10);
	printf("%s", p);
}
Esempio n. 2
0
void
str_scat(STR *dstr, register STR *sstr)
{
    if (!(sstr->str_pok))
	str_2ptr(sstr);
    if (sstr)
	str_ncat(dstr,sstr->str_ptr,sstr->str_cur);
}
Esempio n. 3
0
int main(int argc, char** argv) {

    char buffer1[10] = "Soft";
    str_ncat(buffer1, "Uni", 7);
    printf("%s\n", buffer1);

    char buffer2[5] = "Soft";
    str_ncat(buffer2, "ware University", 15);
    printf("%s\n", buffer2);
    
    char buffer3[10] = "C";
    str_ncat(buffer3, " is cool", 8);
    printf("%s\n", buffer3);

    // Cannot modify this string literal
    //    char * buffer4 = "C";
    //    strncat(buffer4, " is cool", 8);
    return (EXIT_SUCCESS);
}
Esempio n. 4
0
static void make_poll_post_data(str_t* post_data)
{
    cJSON* cjson_poll_post = cJSON_CreateObject();
    str_t tmp = empty_str;

    cJSON_AddStringToObject(cjson_poll_post, "clientid", CLIENTID);
    cJSON_AddStringToObject(cjson_poll_post, "psessionid", robot.session.ptr);
    cJSON_AddNumberToObject(cjson_poll_post, "key", 0);
    cJSON_AddItemToObject(cjson_poll_post, "ids", cJSON_CreateArray());
    post_data->ptr = cJSON_PrintUnformatted(cjson_poll_post);
    post_data->len = strlen(post_data->ptr);
    cJSON_Delete(cjson_poll_post);
    str_cpy(&tmp, str_from("r="));
    str_ncat(&tmp, post_data->ptr, post_data->len);
    str_cat(&tmp, "&clientid="CLIENTID"&psessionid=");
    str_ncat(&tmp, robot.session.ptr, robot.session.len);
    str_free(*post_data);
    urlencode(tmp, post_data);
    str_free(tmp);
}
Esempio n. 5
0
static int init()
{
    str_t tmp = empty_str;
    str_t host = conf_lookup(&robot.conf, str_from("DB_HOST")).string;
    str_t name = conf_lookup(&robot.conf, str_from("DB_NAME")).string;
    size_t i;

    int rc = 1;

    str_cat(&tmp, "mongodb://");
    str_ncat(&tmp, host.ptr, host.len);
    robot.mongoc_client = mongoc_client_new(tmp.ptr);
    if (robot.mongoc_client == NULL)
    {
        rc = 0;
        fprintf(stderr, "mongoc_client_new(\"%s\") error!!!!\n", tmp.ptr);
        goto end;
    }

    robot.mongoc_database = mongoc_client_get_database(robot.mongoc_client, name.ptr);
    if (robot.mongoc_database == NULL)
    {
        rc = 0;
        fprintf(stderr, "mongoc_client_get_database(\"%s\") error!!!!\n", name.ptr);
        goto end;
    }

    {
        mongoc_collection_t* message_collection;
        mongoc_index_opt_t opt;
        bson_error_t error;
        bson_t keys;

        message_collection = mongoc_database_get_collection(robot.mongoc_database, "message");
        if (message_collection == NULL)
        {
            rc = 0;
            fprintf(stderr, "mongoc_database_get_collection(\"message\") error!!!!\n");
            goto end;
        }

        mongoc_index_opt_init(&opt);

        // from+type 做联合索引
        bson_init(&keys);
        BSON_APPEND_INT32(&keys, "from", 1);
        BSON_APPEND_INT32(&keys, "type", 1);

        if (!mongoc_collection_create_index(message_collection, &keys, &opt, &error)) MONGOC_WARNING("%s\n", error.message);

        bson_destroy(&keys);

        // time 做逆序索引
        bson_init(&keys);
        BSON_APPEND_INT32(&keys, "time", -1);

        if (!mongoc_collection_create_index(message_collection, &keys, &opt, &error)) MONGOC_WARNING("%s\n", error.message);

        bson_destroy(&keys);

        // content 做全文索引
        bson_init(&keys);
        BSON_APPEND_UTF8(&keys, "content", "text");

        if (!mongoc_collection_create_index(message_collection, &keys, &opt, &error)) MONGOC_WARNING("%s\n", error.message);

        bson_destroy(&keys);
    }

    {
        mongoc_collection_t* unprocessed_collection;
        mongoc_index_opt_t opt;
        bson_error_t error;
        bson_t keys;

        unprocessed_collection = mongoc_database_get_collection(robot.mongoc_database, "unprocessed");
        if (unprocessed_collection == NULL)
        {
            rc = 0;
            fprintf(stderr, "mongoc_database_get_collection(\"unprocessed\") error!!!!\n");
            goto end;
        }

        mongoc_index_opt_init(&opt);

        // time 做逆序索引
        bson_init(&keys);
        BSON_APPEND_INT32(&keys, "time", -1);

        if (!mongoc_collection_create_index(unprocessed_collection, &keys, &opt, &error)) MONGOC_WARNING("%s\n", error.message);

        bson_destroy(&keys);
    }

    for (i = 0; modules[i]; ++i)
    {
        if (modules[i]->module_init)
        {
            rc = modules[i]->module_init();
            if (!rc) goto end;
        }
    }
end:
    str_free(tmp);
    return rc;
}
Esempio n. 6
0
static int login_step2()
{
    curl_data_t data_login2 = empty_curl_data;
    curl_header_t header_login2 = empty_curl_header;
    cJSON* cjson_login2_post = cJSON_CreateObject();
    cJSON *cjson_login2 = NULL, *cjson_result;
    str_t post_data = empty_str, tmp = empty_str;
    str_t cookie_str;
    conf_val_t status_val = conf_lookup(&robot.conf, str_from("STATUS"));
    str_t status;
    int rc = 1;

    if (status_val.type != CONF_VALUE_TYPE_STRING)
    {
        fprintf(stdout, "Warning: Unset STATUS variable, the default value is online!!!!\n");
        fflush(stdout);
        status = str_from("online");
    }
    else status = status_val.string;

    cJSON_AddStringToObject(cjson_login2_post, "status", status.ptr);
    cJSON_AddStringToObject(cjson_login2_post, "ptwebqq", robot.ptwebqq.ptr);
    cJSON_AddStringToObject(cjson_login2_post, "passwd_sig", "");
    cJSON_AddStringToObject(cjson_login2_post, "clientid", CLIENTID);
    cJSON_AddNullToObject(cjson_login2_post, "psessionid");
    post_data.ptr = cJSON_PrintUnformatted(cjson_login2_post);
    post_data.len = strlen(post_data.ptr);
    str_cpy(&tmp, str_from("r="));
    str_ncat(&tmp, post_data.ptr, post_data.len);
    str_cat(&tmp, "&clientid="CLIENTID"&psessionid=null");
    str_free(post_data);
    urlencode(tmp, &post_data);

    cookie_str = cookie_to_str(&robot.cookie);
    rc = post_request_with_cookie("https://d.web2.qq.com/channel/login2", 1, "./pems/d.web2.qq.com.pem", post_data.ptr, cookie_str.ptr, &data_login2, &header_login2);
    if (!rc)
    {
        fprintf(stderr, "Call login2 error!!!!\n");
        goto end;
    }

    merge_cookie_to_robot(&header_login2);

    cjson_login2 = cJSON_Parse(data_login2.data.ptr);
    if (cJSON_GetObjectItem(cjson_login2, "retcode")->valueint != 0)
    {
        rc = 0;
        fprintf(stderr, "login2 faild!!!!\n");
        fprintf(stderr, "%s\n", data_login2.data.ptr);
        goto end;
    }

    cjson_result = cJSON_GetObjectItem(cjson_login2, "result");
    str_free(robot.session);
    robot.session = str_dup(cJSON_GetObjectItem(cjson_result, "psessionid")->valuestring);
    str_free(robot.vfwebqq);
    robot.vfwebqq = str_dup(cJSON_GetObjectItem(cjson_result, "vfwebqq")->valuestring);
end:
    curl_data_free(&data_login2);
    pair_array_free(&header_login2);
    cJSON_Delete(cjson_login2_post);
    cJSON_Delete(cjson_login2);
    str_free(post_data);
    str_free(tmp);
    str_free(cookie_str);
    return rc;
}