char *wiki_head(char *url) { curl_data_t *curl; xmlDoc *doc = NULL; xmlXPathContext *ctx = NULL; xmlXPathObject *xpathObj = NULL; char *text = NULL; curl = curl_data_new(); if(curl_download_text(url, curl)) return NULL; doc = (xmlDoc *) htmlReadMemory(curl->data, strlen(curl->data), "/", "utf-8", HTML_PARSE_NOERROR); /* creating xpath request */ ctx = xmlXPathNewContext(doc); xpathObj = xmlXPathEvalExpression((const xmlChar *) "//div[@id='mw-content-text']/p", ctx); if(!xmlXPathNodeSetIsEmpty(xpathObj->nodesetval)) { if(xmlNodeGetContent(xpathObj->nodesetval->nodeTab[0])) text = strdup((char *) xmlNodeGetContent(xpathObj->nodesetval->nodeTab[0])); } xmlXPathFreeObject(xpathObj); xmlXPathFreeContext(ctx); xmlFreeDoc(doc); curl_data_free(curl); return text; }
static ullong get_friend_number(ullong uin) { str_t str_cookie = empty_str; curl_data_t data_uin = empty_curl_data; char str_uin[128] = {0}; char* url; cJSON* cjson_uin; ullong ret = 0; sprintf(str_uin, "%llu", uin); url = calloc(sizeof("http://s.web2.qq.com/api/get_friend_uin2?tuin=&verifysession=&type=1&code=&vfwebqq=&t=1401621019") + strlen(str_uin) + robot.vfwebqq.len, 1); strcpy(url, "http://s.web2.qq.com/api/get_friend_uin2?tuin="); strcat(url, str_uin); strcat(url, "&verifysession=&type=1&code=&vfwebqq="); strncat(url, robot.vfwebqq.ptr, robot.vfwebqq.len); strcat(url, "&t=1401621019"); str_cookie = cookie_to_str(&robot.cookie); ret = get_request_with_cookie(url, 1, "./pems/s.web2.qq.com.pem", str_cookie.ptr, &data_uin, NULL); if (!ret) goto end; cjson_uin = cJSON_Parse(data_uin.data.ptr); if (cJSON_GetObjectItem(cjson_uin, "retcode")->valueint == 0) { cJSON* cjson_result = cJSON_GetObjectItem(cjson_uin, "result"); ret = cJSON_GetObjectItem(cjson_result, "account")->valuedouble; } cJSON_Delete(cjson_uin); end: str_free(str_cookie); curl_data_free(&data_uin); free(url); return ret; }
static int login_step1(const unsigned char password[MD5_DIGEST_LENGTH << 1]) { curl_data_t data_login = empty_curl_data; curl_header_t header_login = empty_curl_header; str_t number = conf_lookup(&robot.conf, str_from("QQ")).string; str_t cookie_str; char* url = malloc(sizeof("https://ssl.ptlogin2.qq.com/login?u=&p=&verifycode=&webqq_type=10&remember_uin=1&login2qq=1&aid=1003903&u1=http%3A%2F%2Fweb2.qq.com%2Floginproxy.html%3Flogin2qq%3D1%26webqq_type%3D10&h=1&ptredirect=0&ptlang=2052&daid=164&from_ui=1&pttype=1&dumy=&fp=loginerroralert&action=6-53-32873&mibao_css=m_webqq&t=4&g=1&js_type=0&js_ver=10077&login_sig=qBpuWCs9dlR9awKKmzdRhV8TZ8MfupdXF6zyHmnGUaEzun0bobwOhMh6m7FQjvWA") + (MD5_DIGEST_LENGTH << 1) + VERIFY_LEN + number.len); str_t* login_response = NULL; int rc = 1; size_t login_response_count = 0; url[0] = 0; strcpy(url, "https://ssl.ptlogin2.qq.com/login?u="); strncat(url, number.ptr, number.len); strcat(url, "&p="); strncat(url, (char*)password, MD5_DIGEST_LENGTH << 1); strcat(url, "&verifycode="); strncat(url, robot.verify_code, VERIFY_LEN); strcat(url, "&webqq_type=10&remember_uin=1&login2qq=1&aid=1003903&u1=http%3A%2F%2Fweb2.qq.com%2Floginproxy.html%3Flogin2qq%3D1%26webqq_type%3D10&h=1&ptredirect=0&ptlang=2052&daid=164&from_ui=1&pttype=1&dumy=&fp=loginerroralert&action=6-53-32873&mibao_css=m_webqq&t=4&g=1&js_type=0&js_ver=10077&login_sig=qBpuWCs9dlR9awKKmzdRhV8TZ8MfupdXF6zyHmnGUaEzun0bobwOhMh6m7FQjvWA"); cookie_str = cookie_to_str(&robot.cookie); rc = get_request_with_cookie(url, 1, "./pems/ssl.ptlogin2.qq.com.pem", cookie_str.ptr, &data_login, &header_login); if (!rc) { fprintf(stderr, "Call login error!!!!\n"); goto end; } merge_cookie_to_robot(&header_login); robot.ptwebqq = pair_array_lookup(&robot.cookie, str_from("ptwebqq")); login_response = fetch_response(data_login.data, &login_response_count); if (strcmp(login_response[4].ptr, "登录成功!") != 0) { rc = 0; goto end; } rc = login_proxy(login_response[2].ptr); end: curl_data_free(&data_login); pair_array_free(&header_login); str_free(cookie_str); free(url); str_array_free(login_response, login_response_count); return rc; }
google_search_t * google_search(char *keywords) { curl_data_t *curl; google_search_t *search; xmlDoc *doc = NULL; xmlXPathContext *ctx = NULL; xmlXPathObject *xpathObj = NULL; xmlNode *node = NULL; char url[2048]; int i; curl = curl_data_new(); snprintf(url, sizeof(url), "%s%s", baseurlen, space_encode(keywords)); if(curl_download_text(url, curl)) return NULL; doc = (xmlDoc *) htmlReadMemory(curl->data, strlen(curl->data), "/", "utf-8", HTML_PARSE_NOERROR); /* creating xpath request */ ctx = xmlXPathNewContext(doc); xpathObj = xmlXPathEvalExpression((const xmlChar *) "//li/div/h3/a", ctx); search = (google_search_t *) calloc(1, sizeof(google_search_t)); if(!xmlXPathNodeSetIsEmpty(xpathObj->nodesetval)) { search->length = xpathObj->nodesetval->nodeNr; search->result = (google_result_t *) calloc(1, sizeof(google_result_t) * search->length); for(i = 0; i < xpathObj->nodesetval->nodeNr; i++) { node = xpathObj->nodesetval->nodeTab[i]; if(xmlNodeGetContent(node)) search->result[i].title = strdup((char *) xmlNodeGetContent(node)); if(xmlGetProp(node, (unsigned char *) "href")) search->result[i].url = strdup((char *) xmlGetProp(node, (unsigned char *) "href")); } } xmlXPathFreeObject(xpathObj); xmlXPathFreeContext(ctx); xmlFreeDoc(doc); curl_data_free(curl); return search; }
static int want_image(int* want) { curl_data_t data_check = empty_curl_data; curl_header_t header_check = empty_curl_header; str_t number = conf_lookup(&robot.conf, str_from("QQ")).string; char* url = malloc(sizeof("https://ssl.ptlogin2.qq.com/check?uin=&appid=1003903&r=0.14233942252344134") + number.len); str_t* check_response = NULL; int rc = 1; size_t check_response_count = 0; url[0] = 0; strcpy(url, "https://ssl.ptlogin2.qq.com/check?uin="); strncat(url, number.ptr, number.len); strcat(url, "&appid=1003903&r=0.14233942252344134"); rc = get_request(url, 1, "./pems/ssl.ptlogin2.qq.com.pem", &data_check, &header_check); if (!rc) { fprintf(stderr, "Call check error!!!!\n"); goto end; } merge_cookie_to_robot(&header_check); check_response = fetch_response(data_check.data, &check_response_count); if (strcmp(check_response[0].ptr, "0") == 0) *want = 0; else if (strcmp(check_response[0].ptr, "2") == 0) { fprintf(stderr, "Invalid QQ Number!!!!\n"); goto end; } else *want = 1; memcpy(robot.verify_code, check_response[1].ptr, VERIFY_LEN); bits_from_str(check_response[2], robot.bits); end: curl_data_free(&data_check); pair_array_free(&header_check); free(url); str_array_free(check_response, check_response_count); return rc; }
static int download_image(const str_t captcha_path) { curl_data_t data_getimage = empty_curl_data; curl_header_t header_getimage = empty_curl_header; str_t number = conf_lookup(&robot.conf, str_from("QQ")).string; char* url = malloc(sizeof("https://ssl.captcha.qq.com/getimage?aid=1003903&r=0.577911190026398&uin=") + number.len); int rc = 1; FILE* fp; url[0] = 0; strcpy(url, "https://ssl.captcha.qq.com/getimage?aid=1003903&r=0.577911190026398&uin="); strncat(url, number.ptr, number.len); rc = get_request(url, 1, "./pems/ssl.captcha.qq.com.pem", &data_getimage, &header_getimage); if (!rc) { fprintf(stderr, "Call getimage error!!!!\n"); goto end; } merge_cookie_to_robot(&header_getimage); fp = fopen(captcha_path.ptr, "wb"); if (fp == NULL) { rc = 0; fprintf(stderr, "Can not open captcha file!!!!\n"); goto end; } fwrite(data_getimage.data.ptr, sizeof(char), data_getimage.data.len, fp); fclose(fp); end: curl_data_free(&data_getimage); pair_array_free(&header_getimage); free(url); return rc; }
static void run() { str_t post_data = empty_str, cookie_str; curl_data_t data_poll = empty_curl_data; curl_header_t header_poll = empty_curl_header; size_t i; for (i = 0; modules[i]; ++i) { if (modules[i]->module_begin && !modules[i]->module_begin()) return; } if (!config_check()) return; if (!init()) return; if (!login()) return; make_poll_post_data(&post_data); cookie_str = cookie_to_str(&robot.cookie); while (robot.run) { if (!post_request_with_cookie("https://d.web2.qq.com/channel/poll2", 1, "./pems/d.web2.qq.com.pem", post_data.ptr, cookie_str.ptr, &data_poll, &header_poll)) continue; route(&cookie_str, &data_poll, &header_poll); curl_data_free(&data_poll); pair_array_free(&header_poll); } for (i = 0; modules[i]; ++i) { if (modules[i] == &conf_module) continue; if (modules[i]->module_exit) modules[i]->module_exit(); } conf_module.module_exit(); }
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; }