Exemple #1
0
/***********************************************
 *  从内存中的buf生成Doc文档                   *
 *  成功返回Doc文档                            *
 *  失败则返回NULL                             *
 *  doc用完后请手动free                        *
 ***********************************************/
xmlDocPtr loadDocFromBuf(byte * buf)
{
	xmlDocPtr doc = NULL;		/* document pointer */
	xmlNodePtr curNode = NULL;	/* node pointer */
	byte * tmp = g2u(buf);
	
	//printf("*********** gb2312: %s ************\n", buf);
	//printf("*********** utf8: %s ************\n", tmp);
	memset(buf, 0, strlen(buf)*2-1);
	memcpy(buf, tmp, strlen(tmp));	
	free(tmp);
	doc = xmlReadMemory(buf, strlen(buf), NULL, "UTF-8", XML_PARSE_RECOVER);	
	if (doc == NULL) {
		ErrorLog(ERROR, "load doc fail, from buffer");
		printf("load doc fail, from buffer");
		return NULL;
	}

	curNode = xmlDocGetRootElement(doc);
	if (curNode == NULL) {
                //xmlFreeDoc(doc);
		ErrorLog(ERROR, "open doc fail, doc content is empty");
		printf("open doc fail, doc content is empty");
		return NULL;
	}
	
	return doc;	
}
Exemple #2
0
string getCEinfo(string runid) {
	FILE * fp=fopen(tfilename,"w+");
	curl = curl_easy_init();
	if(curl) {
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
		curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "hdu.cookie");
		string url=(string)"http://acm.hdu.edu.cn/viewerror.php?rid="+runid;
		//cout<<url;
		curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
		res = curl_easy_perform(curl);
		curl_easy_cleanup(curl);
	}
	fclose(fp);
	string ts=getAllFromFile(tfilename);

	int pos=ts.find("<pre>");
	while (ts[pos]!='>') pos++;
	pos++;
	int st=pos;
	while (ts[pos]!='<') pos++;
	string info=ts.substr(st,pos-st);
	strcpy(tempce,info.c_str());
	g2u(tempce,strlen(tempce),outs,MAX_DATA_SIZE);
	decode_html_entities_utf8(outs, 0);
	info=outs;
	int position = info.find( "\\" );
	while ( position != string::npos ) {
		info.replace( position, 1, "\\\\" );
		position = info.find( "\\", position + 2 );
	}
	return info;
}
Exemple #3
0
static const char *hex2utf8( const char *src, char *dest )
{
	char sz[256] = {0} ;
	int nsrc = hex2str( src, sz ) ;

	int len = 1024 ;
	if( g2u( (char *)sz , nsrc , dest , len ) == -1 ){
		OUT_ERROR( NULL, 0, NULL , "locale2utf8 query %s failed" , sz ) ;
		return NULL ;
	}
	dest[len] = 0 ;
	return dest ;
}
//接收文件夹时分析,头部
int parseHeader(filenode *pfn, char * recvs)
{
  char *strhead, *strtmp, gMsg[FILENAME];
  int i=0;

  strhead = recvs;
  while (i<3) //分析前3段
  {
    strtmp = strchr(strhead, ':');
    if (strtmp == NULL)
      return -1;
    *strtmp = '\0';

    switch (i)
    {
    case 0:

      strncpy(gMsg, strhead, sizeof(gMsg));
      delColon(gMsg, sizeof(gMsg));
      if (utf8)
        g2u(gMsg, sizeof(gMsg),
            pfn->fileName, sizeof(pfn->fileName));
      else strncpy(pfn->fileName, gMsg, sizeof(pfn->fileName));
      break;
    case 1:
      //sscanf(strhead, "%x", &pfn->fileSize);
      strncpy(pfn->fileSize, strhead, sizeof(pfn->fileSize));
      break;
    case 2:
      sscanf(strhead, "%x", &pfn->fileType);
      break;
      //case 3: //进行了简化
      // strncpy(pfh->h14, strhead+3, sizeof(pfh->h14));
      //break;
      //case 4:
      //strncpy(pfh->h16, strhead+3, sizeof(pfh->h16));
      //break;
    default:
      break;
    }

    strhead = strtmp+1;
    i++;
  }

  strncpy(pfn->otherAttrs, strhead, sizeof(pfn->otherAttrs));

  return 0;
}
void get_news()
{
    char *re = (char *)malloc(MAX);
    memset(re, 0, MAX);
    char *out = (char *)malloc(MAX);
    memset(out, 0, MAX);
    re = http_request("rollnews_ch_out_interface.php","/interface/",
                      "roll.news.sina.com.cn", 80);

    g2u(re, strlen(re), out, MAX);
    //printf("%s\n", out);
    char *new_json = extract_news_json(out, 19);
    printf("%s\n", new_json);

    free(re);
    free(out);
}
Exemple #6
0
int GBKToUTF8(unsigned char * lpGBKStr,unsigned char ** lpUTF8Str)
{
#ifdef _WIN32
	int nRetLen = 0;
	nRetLen = CTW(lpGBKStr,NULL,0);
	(* lpUTF8Str) = (unsigned char *)malloc((nRetLen + 1)*sizeof(char));
	if((* lpUTF8Str) == NULL)
		return 0;
	nRetLen = CTW(lpGBKStr,(* lpUTF8Str),nRetLen);
	return nRetLen;
#endif

#ifdef __LINUX__
	int nRetLen = strlen((char *)lpGBKStr) * 2 + 1;
	* lpUTF8Str = (unsigned char *)malloc(nRetLen);
	return g2u((char *)lpGBKStr,strlen((char *)lpGBKStr),(char *)*lpUTF8Str,nRetLen);
#endif

}
Exemple #7
0
int main(int argc, char **argv)
{

	if(argc<3)
	{
		printf("conv3 1/0 xxx\n");
		printf("argv[2] 0 = u2g \n");
		printf("argv[2] 1 = g2u \n");
		return -1;
	}
	//char *in_utf8[6] =   {"01 26涓","02 瀹跺涵鎴愬憳", "03 姘存灉","04 鏁板瓧1鍒","05 浜斿畼" , "06 中文"};
	
	char out[OUTLEN];
	int rec ;

	if(atoi(argv[1])==0)
	{
		//unicode码转为gb2312码
		for(int i=2;i<argc;i++)
		{
			//rec = u2g(in_utf8[i],strlen(in_utf8[i]),out,OUTLEN);
			rec = u2g(argv[i],255,out,OUTLEN);			
			printf("unicode-->gb2312 in=%s out=%s\n",argv[i],out);
		}
	}
	else if(atoi(argv[1])==1)
	{
		//gb2312码转为unicode码
		for(int i=2;i<argc;i++)
		{
			//rec = u2g(in_utf8[i],strlen(in_utf8[i]),out,OUTLEN);
	
			rec = g2u(argv[i],255,out,OUTLEN);
			printf("gb2312-->unicode in=%s out=%s\n",argv[i],out);
		}
	}
	
	return 0;
}
Exemple #8
0
/***********************************************
 *  向Doc新增指定父结点的结点信息              *
 *  成功返回0和Doc文档                         *
 *  失败则返回-1和NULL                         *
 *  doc用完后请手动free                        *
 ***********************************************/
int appendRowToDoc(xmlDocPtr doc, byte * parentNodeName, Row row)
{
	int nRet = FAIL;
	xmlNodePtr curNode = NULL;
	xmlNodePtr node = NULL;
	byte * value = g2u(row.value);
	LIBXML_TEST_VERSION;
	
	if(doc == NULL) {
                ErrorLog(ERROR, "doc is null");
                printf("doc is null");
                return FAIL;
        }

	if(row.key == NULL && row.value == NULL) {
		ErrorLog(ERROR, "row can not empty");
		printf("row can not empty");
		return FAIL;
	}

        curNode = xmlDocGetRootElement(doc);
        if (curNode == NULL) {
                xmlFreeDoc(doc);
                ErrorLog(ERROR, "open doc fail, doc content is empty");
                printf("open doc fail, doc content is empty");
                return FAIL;
        }

        if(xmlStrcmp(curNode->name, BAD_CAST "UTILITY_PAYMENT")) {
                xmlFreeDoc(doc);
                ErrorLog(ERROR, "can not find the root node");
                printf("can not find the root node");
                return FAIL;
        }

        curNode = curNode->xmlChildrenNode;
        if (curNode == NULL) {
                xmlFreeDoc(doc);
                ErrorLog(ERROR, "can not find child node");
                printf("can not find child node");
                return FAIL;
        }

        /* if has child node, do while */
        while (curNode != NULL) {
                if(!xmlStrcmp(curNode->name, BAD_CAST parentNodeName)) {
			node = xmlNewTextChild (curNode, NULL, (const xmlChar *)row.key, (const xmlChar *)value);
			if(node == NULL) {
				//xmlFreeDoc(doc);
				ErrorLog(ERROR, "append child text node fail");
				printf("append child text node fail");
				nRet = FAIL;
				break;
			}
			nRet = SUCC;
                	break;
                }
                curNode = curNode->next;
        }
	
	free(value);
	return nRet;
}
Exemple #9
0
int
apiqry_main()
{
	FILE *fp;
	char userid[14], filename[80], buf[512], output[4096],output_utf8[4096];
	struct userec *x;
	struct user_info *u;
	int i, tmp2, num;
	json_header();
//	changemode(QUERY);
	strsncpy(userid, getparm("U"), 13);
	if (!userid[0])
		strsncpy(userid,getparm("userid"), 13);
	if (userid[0] == '\0')
	{
		printf("{\"User\":null}");
		return 0;
	}
	x = getuser(userid);
	if (x == 0)
	{
		printf("{\"User\":null}");
		return 0;
	}
	// ��ʼ����û�����
	sstrcat(output, "{\"User\":{");
	// ��ʾ��������
	sstrcat(output, "\"UserID\":\"%s\",\"UserNickName\":\"%s\",\"LoginCounts\":%d,\"PostCounts\":%d,\"LastLogin\":\"%s\",\"LastHost\":\"%s\",", x->userid, x->username, x->numlogins, x->numposts,Ctime(x->lastlogin), x->lasthost);
	// ��ʾ��������
	if(!strcasecmp(x->userid, currentuser.userid)){
		sstrcat(output, "\"Exp\":%d,\"ExpLevel\":\"%s\",\"Perf\":%d,\"PerfLevel\":\"%s\"",countexp(x), cexp(countexp(x)), countperf(x), cperf(countperf(x)));
	}
	// ��ʾ����վ�����Ϣ
	if(x->userlevel & PERM_BOARDS)
	{
		sstrcat(output, "\"BOARDBM\":[");
		sethomefile(filename, x->userid, "mboard");
		new_apply_record(filename, sizeof(struct boardmanager), (void *)bm_printboardapi, output);
		sstrcat(output, "],");
	}
	if((x->userlevel & PERM_SYSOP) && (x->userlevel&PERM_ARBITRATE))
		sstrcat(output, "\"Job\":\"��վ������\",");
	else if(x->userlevel & PERM_SYSOP)
		sstrcat(output, "\"Job\":\"����վ��\",");
	else if(x->userlevel & PERM_OBOARDS)
		sstrcat(output, "\"Job\":\"ʵϰվ��\",");
	else if(x->userlevel & PERM_ARBITRATE)
		sstrcat(output, "\"Job\":\"���μ�ί\",");
	else if(x->userlevel & PERM_SPECIAL4)
		sstrcat(output, "\"Job\":\"����\",");
	else if(x->userlevel & PERM_WELCOME)
		sstrcat(output, "\"Job\":\"ϵͳ����\",");
	else if(x->userlevel & PERM_SPECIAL7)
	{
		if((x->userlevel & PERM_SPECIAL1) && !(x->userlevel & PERM_CLOAK))
			sstrcat(output, "\"Job\":\"���γ���Ա\",");
		else
			sstrcat(output, "\"Job\":\"�������Ա\",");
	}
	else if(x->userlevel & PERM_ACCOUNTS)
		sstrcat(output, "\"Job\":\"�ʺŹ���Ա\",");
		
	// ��ʾ��ǰ״̬��null ���� array
	num = 0;
	sstrcat(output, "\"States\":");
	for (i=0; i<MAXACTIVE; ++i)
	{
		u = &(shm_utmp->uinfo[i]);
		if(!strcmp(u->userid, x->userid))
		{
			if(u->active == 0 || u->pid ==0 || (u->invisible && !HAS_PERM(PERM_SEECLOAK)))
				continue;
			++num;
			if(num == 1)
				sstrcat(output, "[");
			if(u->mode != USERDF4){
				if(u->invisible)
					sstrcat(output, "\"C%s\"", ModeType(u->mode));
				else
					sstrcat(output, "\"%s\"", ModeType(u->mode));
			}
			else
				// �Զ���״̬
				sstrcat(output, "%s", u->user_state_temp);
		}
		if( (num>0) && (i == MAXACTIVE - 1))
			sstrcat(output, "],");
	}
	if (num == 0 )
	{
		sstrcat(output, "\"null\",");
		if( x->lastlogout != NULL )
			sstrcat(output, "\"LastLogout\":\"%s\",",Ctime(x->lastlogout));
		else
			sstrcat(output, "\"LastLogout\":null,");
	}
		
	
	// ��ʾ˵����
	sethomefile(filename,x->userid,"plans");
	fp = fopen(filename, "r");
	sprintf(filename, "00%s-plan", x->userid);
	if (fp) {
		sstrcat(output, "\"PersonalIntro\":\""); // ��ʼ����˵����
		while(1){
			if(fgets(buf,256,fp) == 0)
				break;
			if (buf[strlen(buf) - 1] == '\n'){
				int currentlength;
				currentlength = strlen(buf);
				buf[currentlength - 1] = '\\';
				buf[currentlength] = 'n';
				buf[currentlength + 1] = 0;
			}
			if(!strncmp(buf,"bigin 644",10)){
				// errlog()
				// fdisplay_attach()
			}
			sstrcat(output, "%s", buf);
		}
		sstrcat(output, "\","); // ��������˵����
		fclose(fp);
	}
	else //û�и���˵����
		sstrcat(output, "\"PersonalIntro\":null,");
		
	// ��ʾ�����ǩ
	show_special_api(x->userid, output);
	// �������
	sstrcat(output, "}}"); 
	g2u(output,sizeof(output), output_utf8,sizeof(output_utf8));
	printf("%s", output_utf8);
	//http_quit();
	return 0;
}
Exemple #10
0
bool get_assignment_detail( CURL* curl_web_handler, char *url, p_assignment_element cur)
{
	extern cache_memory cache;
	if(!copy_web_page_to_memory(curl_web_handler, url, &copy_to_buffer, NULL))
		return FALSE;
	p_list_entity entity_head = NULL;
	// utf8 cache buffer covert it 
	// convert buffer to UTF-8
	// This is a GBK coding page.
	char *utf8_buffer = (char *)malloc(sizeof(cache.size) + CACHE_INIT_SIZE);
	if(g2u(cache.mem, strlen(cache.mem), utf8_buffer, sizeof(cache.size) + CACHE_INIT_SIZE) == 0)
	{
		//printf("Length:%d", strlen(cache.mem));
		//printf("Convert OK:\n");
		//puts(utf8_buffer);
	}
	else
	{
		printf("May be memory broken, check CACHE_INIT_SIZE, GBK code convert to UTF8 code failure!@line %d, file %s\n",__LINE__, __FILE__ );
		return FALSE;
	}
	if(!utf8_buffer)
		return FALSE;
	// get entity
	entity_head = basic_parse_page(utf8_buffer,"<td class=\"tr_2","</td>");
	reset_cache_memory();
	p_list_entity p = entity_head->next;
	int i = 0;

	char buf[BUFFER_MAX_SIZE * 10];

	while(p)
	{
		// convert buffer to UTF-8
		// This is a GBK coding page.

		switch(i){
	  case 0:
		  // homework title
		  break;
	  case 1:
		  //homework_description
		  if(extract_content_between_tags(p->entity, buf, "<textarea","</textarea>"))
		  {
			  remove_and_convert_html(buf);
			  string_trip(buf);
			  cur->assignment_description = strdup(buf);
		  }

		  else
			  cur->assignment_description = strdup("");
		  break;
	  case 2:
		  //
		  //puts("-------------------");
		  //puts(p->entity);
		  if(extract_content_between_fix(p->entity, buf, "href=\"","\">"))
		  {
			  str_add_prefix(tsinghua_prefix, buf);
			  cur->assignment_attachment_url = strdup(buf);
			  //printf("IN URL:%s\nADDR:%x\n",cur->assignment_attachment_url,cur->assignment_attachment_url);
			  if(extract_content_between_tags(p->entity, buf, "<a target=\"_top\"","</a>"))
			  {
				  cur->assignment_attachment_name = strdup(buf);
				  //printf("IN NAME:%s\nADDR:%x\n",cur->assignment_attachment_name,cur->assignment_attachment_name);
			  }
			  else
				  cur->assignment_attachment_name  = strdup("");
		  }
		  else
		  {
			  strcpy(buf,p->entity);
			  remove_and_convert_html(buf);
			  string_trip(buf);
			  cur->assignment_attachment_name = strdup(buf);
			  cur->assignment_attachment_url = NULL;
		  }
		  break;
	  case 3:
		  if(extract_content_between_tags(p->entity, buf, "<textarea","</textarea>"))
		  {
			  remove_and_convert_html(buf);
			  string_trip(buf);
			  cur->my_handin_description = strdup(buf);
		  }
		  else
			  cur->my_handin_description = strdup("");
		  break;
	  case 4:
		  if(extract_content_between_fix(p->entity, buf, "href=\"","\">"))
		  {
			  str_add_prefix(tsinghua_prefix, buf);
			  cur->my_handin_attachment_url = strdup(buf);
			  if(extract_content_between_tags(p->entity, buf, "<a target=\"_top\"","</a>"))
				  cur->my_handin_attachment_name = strdup(buf);
			  else
				  cur->my_handin_attachment_name  = strdup("");
		  }
		  else
		  {
			  strcpy(buf,p->entity);
			  remove_and_convert_html(buf);
			  string_trip(buf);
			  cur->my_handin_attachment_name = strdup(buf);
			  cur->my_handin_attachment_url = NULL;
		  }
		  break;
	  default:
		  break;
		}
		i++;
		p = p->next;
	}
	if(utf8_buffer)
		free(utf8_buffer);

	if(entity_head)
		destroy_all_entity_memory(entity_head);
	return TRUE;
}
Exemple #11
0
static char * parse_mail(char * userid, int filetime, int mode, struct attach_link **attach_link_list)
{
	if(mode != ARTICLE_PARSE_WITHOUT_ANSICOLOR && mode != ARTICLE_PARSE_WITH_ANSICOLOR)
		return NULL;

	if(!userid || filetime<=0)
		return NULL;

	char path[STRLEN];
	snprintf(path, STRLEN, "mail/%c/%s/M.%d.A", mytoupper(userid[0]), userid, filetime);
	FILE *article_stream = fopen(path, "r");
	if(!article_stream)
		return NULL;

	FILE *mem_stream, *html_stream;
	char *mem_buf, *html_buf, buf[512], attach_link[256], *tmp_buf, *attach_filename;
	size_t mem_buf_len, html_buf_len, attach_file_size;
	int attach_no = 0;

	mem_stream = open_memstream(&mem_buf, &mem_buf_len);
	fseek(article_stream, 0, SEEK_SET);
	keepoldheader(article_stream, SKIPHEADER);

	while(1) {
		if(fgets(buf, 500, article_stream) == 0)
			break;

		// RAW 模式下跳过 qmd
		if(mode == ARTICLE_PARSE_WITHOUT_ANSICOLOR && (strncmp(buf, "--\n", 3)==0))
			break;

		// 附件处理
		if(!strncmp(buf, "begin 644", 10)) {
			// TODO 老方式暂不实现
			fflush(mem_stream);
			fclose(mem_stream);
			free(mem_buf);
			return NULL;
		} else if(checkbinaryattach(buf, article_stream, &attach_file_size)) {
			attach_no++;
			attach_filename = buf + 18;
			fprintf(mem_stream, "#attach %s\n", attach_filename);
			memset(attach_link, 0, 256);
			snprintf(attach_link, 256, "/api/attach/get?mid=%d&pos=%d&attname=%s",
					filetime, -4+(int)ftell(article_stream), attach_filename);
			add_attach_link(attach_link_list, attach_link, attach_file_size);
			fseek(article_stream, attach_file_size, SEEK_CUR);
			continue;
		}

		// 常规字符处理
		if(mode == ARTICLE_PARSE_WITHOUT_ANSICOLOR && strchr(buf, '\033')!=NULL) {
			tmp_buf = strdup(buf);

			while(strchr(tmp_buf, '\033') != NULL)
				tmp_buf = string_replace(tmp_buf, "\033", "[ESC]");

			fprintf(mem_stream, "%s", tmp_buf);
			free(tmp_buf);
			tmp_buf = NULL;
		} else {
			fprintf(mem_stream, "%s", buf[0]==0 ? "" : buf);
		}
	}
	fflush(mem_stream);
	fclose(article_stream);

	char *utf_content;
	if(mode == ARTICLE_PARSE_WITHOUT_ANSICOLOR) {
		if(strlen(mem_buf)==0) {
			utf_content = strdup("");
		} else {
			utf_content = (char *)malloc(3*mem_buf_len);
			memset(utf_content, 0, 3*mem_buf_len);
			g2u(mem_buf, mem_buf_len, utf_content, 3*mem_buf_len);
		}
	} else {
		html_stream = open_memstream(&html_buf, &html_buf_len);
		fseek(mem_stream, 0, SEEK_SET);

		fprintf(html_stream, "<article>\n");
		aha_convert(mem_stream, html_stream);
		fprintf(html_stream, "</article>");

		fflush(html_stream);
		fclose(html_stream);

		utf_content = (char*)malloc(3*html_buf_len);
		memset(utf_content, 0, 3*html_buf_len);
		g2u(html_buf, html_buf_len, utf_content, 3*html_buf_len);
		free(html_buf);
	}

	// 释放资源
	fclose(mem_stream);
	free(mem_buf);

	return utf_content;
}
Exemple #12
0
int api_mail_list(ONION_FUNC_PROTO_STR)
{
	const char * str_startnum = onion_request_get_query(req, "startnum");
	const char * str_count    = onion_request_get_query(req, "count");
	const char * userid   = onion_request_get_query(req, "userid");
	const char * appkey   = onion_request_get_query(req, "appkey");
	const char * sessid   = onion_request_get_query(req, "sessid");
	const char * box_type  = onion_request_get_query(req, "box_type");

	if(!userid || !appkey || !sessid)
		return api_error(p, req, res, API_RT_WRONGPARAM);

	struct userec *ue = getuser(userid);
	if(!ue)
		return api_error(p, req, res, API_RT_NOSUCHUSER);

	int r = check_user_session(ue, sessid, appkey);
	if(r != API_RT_SUCCESSFUL) {
		free(ue);
		return api_error(p, req, res, r);
	}

	int startnum = (str_startnum) ? atoi(str_startnum) : 999999;
	int count = (str_count) ? atoi(str_count) : 20;

	char mail_dir[80];

	int box_type_i = (box_type != NULL && box_type[0] == '1') ? API_MAIL_SENT_BOX : API_MAIL_RECIEVE_BOX;
	if(box_type_i == API_MAIL_RECIEVE_BOX)
		setmailfile(mail_dir, ue->userid, ".DIR");
	else
		setsentmailfile(mail_dir, ue->userid, ".DIR");

	int total = file_size_s(mail_dir) / sizeof(struct fileheader);

	if(!total) {
		free(ue);
		return api_error(p, req, res, API_RT_MAILEMPTY);
	}

	FILE *fp = fopen(mail_dir, "r");
	if(fp==0) {
		free(ue);
		return api_error(p, req, res, API_RT_MAILDIRERR);
	}

	if(startnum == 0 || startnum > total-count+1)
		startnum = total - count + 1;
	if(startnum <= 0)
		startnum = 1;

	struct fileheader x;
	int i;
	struct bmy_article mail_list[count];
	memset(mail_list, 0, sizeof(struct bmy_article) * count);
	fseek(fp, (startnum - 1) * sizeof(struct fileheader), SEEK_SET);
	for(i=0; i<count; ++i) {
		if(fread(&x, sizeof(x), 1, fp) <= 0)
			break;

		mail_list[i].sequence_num = i + startnum;
		mail_list[i].mark = x.accessed;
		strncpy(mail_list[i].author, fh2owner(&x), sizeof(mail_list[i].author));
		mail_list[i].filetime = x.filetime;
		g2u(x.title, strlen(x.title), mail_list[i].title, sizeof(mail_list[i].title));
	}

	fclose(fp);

	char *s = bmy_mail_array_to_json_string(mail_list, count, 0, ue);

	api_set_json_header(res);
	onion_response_write0(res, s);
	free(s);
	free(ue);
	return OCS_PROCESSED;
}
Exemple #13
0
static int api_mail_get_content(ONION_FUNC_PROTO_STR, int mode)
{
	const char * userid = onion_request_get_query(req, "userid");
	const char * sessid = onion_request_get_query(req, "sessid");
	const char * appkey = onion_request_get_query(req, "appkey");
	const char * str_num = onion_request_get_query(req, "num");
	const char * box_type  = onion_request_get_query(req, "box_type");

	if(!userid || !sessid || !appkey || !str_num)
		return api_error(p, req, res, API_RT_WRONGPARAM);

	struct userec * ue = getuser(userid);
	if(ue == 0)
		return api_error(p, req, res, API_RT_WRONGPARAM);

	if(check_user_session(ue, sessid, appkey) != API_RT_SUCCESSFUL) {
		free(ue);
		return api_error(p, req, res, API_RT_WRONGSESS);
	}

	char mail_dir[80];
	struct fileheader fh;

	int box_type_i = (box_type != NULL && box_type[0] == '1') ? API_MAIL_SENT_BOX : API_MAIL_RECIEVE_BOX;
	if(box_type_i == API_MAIL_RECIEVE_BOX)
		setmailfile(mail_dir, ue->userid, ".DIR");
	else
		setsentmailfile(mail_dir, ue->userid, ".DIR");

	FILE *fp = fopen(mail_dir, "r");
	if(fp==0) {
		free(ue);
		return api_error(p, req, res, API_RT_MAILINNERR);
	}

	int total = file_size_s(mail_dir) / sizeof(struct fileheader);
	int num = atoi(str_num);

	if(num<=0)
		num = 1;
	if(num>total)
		num = total;

	fseek(fp, (num-1)*sizeof(struct fileheader), SEEK_SET);
	if(fread(&fh, sizeof(fh), 1, fp) <= 0) {
		fclose(fp);
		free(ue);
		return api_error(p, req, res, API_RT_MAILINNERR);
	}

	fclose(fp);

	char title_utf[240];
	g2u(fh.title, strlen(fh.title), title_utf, 240);

	struct attach_link *attach_link_list = NULL;
	char * mail_content_utf8 = parse_mail(ue->userid, fh.filetime, mode, &attach_link_list);

	if(!mail_content_utf8) {
		// 文件不存在
		free(ue);
		free_attach_link_list(attach_link_list);
		return api_error(p, req, res, API_RT_MAILEMPTY);
	}

	char * mail_json_str = (char *)malloc(strlen(mail_content_utf8) + 512);

	if(!mail_json_str) {
		free(ue);
		free(mail_content_utf8);
		free_attach_link_list(attach_link_list);
		return api_error(p, req, res, API_RT_NOTENGMEM);
	}

	memset(mail_json_str, 0, strlen(mail_content_utf8)+512);
	sprintf(mail_json_str, "{\"errcode\": 0, \"attach\":[]}");
	struct json_object * jp = json_tokener_parse(mail_json_str);
	if(!jp) {
		free(ue);
		free(mail_content_utf8);
		free_attach_link_list(attach_link_list);
		free(mail_json_str);
		return api_error(p, req, res, API_RT_NOTENGMEM);
	}

	json_object_object_add(jp, "content", json_object_new_string(mail_content_utf8));
	json_object_object_add(jp, "title", json_object_new_string(title_utf));
	if(attach_link_list) {
		struct json_object * attach_array = json_object_object_get(jp, "attach");
		char at_buf[320];
		struct attach_link * alp = attach_link_list;
		while(alp) {
			memset(at_buf, 0, 320);
			sprintf(at_buf, "{\"link\": \"%s\", \"size\": %d}", alp->link, alp->size);
			json_object_array_add(attach_array, json_tokener_parse(at_buf));
			alp=alp->next;
		}
	}

	char * api_output = strdup(json_object_to_json_string(jp));

	free(ue);
	free(mail_content_utf8);
	free_attach_link_list(attach_link_list);
	free(mail_json_str);
	json_object_put(jp);

	api_set_json_header(res);
	onion_response_write0(res, api_output);
	free(api_output);

	return OCS_PROCESSED;
}
Exemple #14
0
/***********************************************
 *  向Doc新增指定父结点的结点信息              *
 *  成功返回0和Doc文档                         *
 *  失败则返回-1和NULL                         *
 *  doc用完后请手动free                        *
 ***********************************************/
int __appendRowToDoc(xmlDocPtr doc, byte * parentNodeName, Row row)
{
	int nRet = FAIL;
	int i,j;
	xmlNodePtr curNode = NULL;
	xmlNodePtr node = NULL;
	xmlNodePtr childnode;
	byte * value = g2u(row.value);
	LIBXML_TEST_VERSION;
	
	if(doc == NULL) {
                ErrorLog(ERROR, "doc is null");
                printf("doc is null");
                return FAIL;
        }

	if(row.key == NULL && row.value == NULL) {
		ErrorLog(ERROR, "row can not empty");
		printf("row can not empty");
		return FAIL;
	}

        curNode = xmlDocGetRootElement(doc);
        if (curNode == NULL) {
                xmlFreeDoc(doc);
                ErrorLog(ERROR, "open doc fail, doc content is empty");
                printf("open doc fail, doc content is empty");
                return FAIL;
        }

        curNode = curNode->xmlChildrenNode;
        if (curNode == NULL) {
                xmlFreeDoc(doc);
                ErrorLog(ERROR, "can not find child node");
                printf("can not find child node");
                return FAIL;
        }

        /* if has child node, do while */
        while (curNode != NULL) {
                if(!xmlStrcmp(curNode->name, BAD_CAST parentNodeName)) {
			if(row.row_type == ONLY_ATTRIB) {
				if(row.column == NULL || row.column_num == 0) {
					xmlFreeDoc(doc);
					ErrorLog(ERROR, "row 's attrib is not exist");
					printf("row 's attrib is not exist");
					return FAIL;
				}
				
				node = xmlNewTextChild (curNode, NULL, (const xmlChar *)row.key, NULL);	
				for(i=0; i<row.column_num; i++) {
					childnode = xmlNewTextChild(node, NULL,(const xmlChar *)row.column[i].attrib, (const xmlChar *)g2u(row.column[i].value));
				}
				nRet = SUCC;
				break;
			}
			if(row.row_type == ONLY_VALUE) {
				node = xmlNewTextChild (curNode, NULL, (const xmlChar *)row.key, (const xmlChar *)value);
				if(node == NULL) {
					//xmlFreeDoc(doc);
					ErrorLog(ERROR, "append child text node fail");
					printf("append child text node fail");
					nRet = FAIL;
					break;
				}
				nRet = SUCC;
                		break;
			}	
			if(row.row_type == HAS_ATTR_VALUE) {
				if(row.column == NULL || row.column_num == 0) {
					xmlFreeDoc(doc);
					ErrorLog(ERROR, "row 's attrib is not exist");
					printf("row 's attrib is not exist");
					return FAIL;
				}
				
				node = xmlNewTextChild (curNode, NULL, (const xmlChar *)row.key, (const xmlChar *)value);	
				for(j=0; j<row.column_num; j++) {
					childnode = xmlNewTextChild(node, NULL, (const xmlChar *)row.column[j].attrib, (const xmlChar *)g2u(row.column[j].value));
				}
				nRet = SUCC;
				break;
			}		
                }
                curNode = curNode->next;
        }
	
	free(value);
	return nRet;
}