Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 【程序名】小涵QQ自动回复机器人 【编辑制作】涵仔 【QQ】747577328
- PS:怕有人盗用,我只段取了副程序的源码,如需要主程序的源码,请联系本人,售价10元,谢谢!本程序为出售程序,30元/1年,50元/2年,100元/5年,300元永久!
- /*
- * buddy.c
- *
- * Buddy management
- *
- * Copyright (C) 2008 Huang Guan
- *
- * 2008-7-12 Created.
- *
- * Description: This file mainly includes the functions about
- *
- */
- #include <time.h>
- #include <stdlib.h>
- #include <string.h>
- #ifdef __WIN32__
- #include <winsock.h>
- #include <wininet.h>
- #else
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #endif
- #include "qqclient.h"
- #include "memory.h"
- #include "debug.h"
- #include "protocol.h"
- #include "list.h"
- #include "buddy.h"
- #include "packetmgr.h"
- //按qq号码进行升序排序
- static int buddy_comp(const void *p, const void *q)
- {
- return ( (*(qqbuddy **)p)->number - (*(qqbuddy **)q)->number );
- }
- static int searcher( const void* p, const void* v )
- {
- return ( ((qqbuddy*)p)->number == (int)v );
- }
- qqbuddy* buddy_get( qqclient* qq, uint number, int create )
- {
- if( !number )
- return NULL;
- // if( create && number != qq->number ) return NULL;
- qqbuddy* b;
- b = list_search( &qq->buddy_list, (void*)number, searcher );
- //if not found, b must be NULL
- if( b==NULL && create ){
- NEW( b, sizeof( qqbuddy ) );
- if( !b ) return b;
- b->number = number;
- sprintf( b->nickname, "%u", number );
- if( list_append( &qq->buddy_list, (void*)b )<0 ){
- DEL( b );
- }
- }
- return b;
- }
- void buddy_remove( qqclient* qq, uint number )
- {
- qqbuddy* b;
- b = list_search( &qq->buddy_list, (void*)number, searcher );
- if( b ){
- list_remove( &qq->buddy_list, b );
- }
- }
- void buddy_sort_list( qqclient* qq )
- {
- list_sort( &qq->buddy_list, buddy_comp );
- }
- static int buddyoff_searcher( const void* p, const void* v )
- {
- ((qqbuddy*)p)->status = QQ_OFFLINE;
- return 0;
- }
- void buddy_set_all_off( struct qqclient* qq )
- {
- list_search( &qq->buddy_list, NULL, buddyoff_searcher );
- }
- void buddy_update_list( qqclient* qq )
- {
- prot_buddy_update_list( qq, 0 );
- prot_buddy_update_online( qq, 0 );
- }
- void buddy_update_info( qqclient* qq, qqbuddy* b )
- {
- prot_buddy_get_info( qq, b->number );
- }
- int buddy_send_message( qqclient* qq, uint number, char* msg )
- {
- prot_im_send_msg( qq, number, msg );
- return 0;
- }
- char* buddy_status_string( int st )
- {
- switch( st ){
- case QQ_ONLINE:
- return "Online";
- case QQ_OFFLINE:
- return "Offline";
- case QQ_AWAY:
- return "Away";
- case QQ_HIDDEN:
- return "Hidden";
- case QQ_BUSY:
- return "Busy";
- case QQ_KILLME:
- return "KillMe";
- case QQ_QUIET:
- return "Quiet";
- default:
- return "Unknown";
- }
- }
- //buddy_put_event的函数在登录之后调用比较频繁,
- //在获取好友列表,获取好友在线列表,获取好友签名,获取好友备注
- void buddy_put_event( qqclient* qq )
- {
- char *temp;
- int i;
- qqbuddy* b;
- NEW( temp, KB(128) );
- if( !temp ) return;
- strcpy( temp, "buddylist^$" );
- pthread_mutex_lock( &qq->buddy_list.mutex );
- struct in_addr addr;
- for( i=0; i<qq->buddy_list.count; i++ ){
- b = (qqbuddy*)qq->buddy_list.items[i];
- addr.s_addr = htonl( b->ip );
- sprintf( temp, "%s%u\t%s\t%s\t%s\t%d\t%s\t%s\t%d^@", temp, b->number, buddy_status_string(b->status), b->nickname,
- b->signature, b->sex, inet_ntoa( addr ), b->alias, b->gid );
- }
- pthread_mutex_unlock( &qq->buddy_list.mutex );
- qqclient_put_event( qq, temp );
- DEL( temp );
- }
- #ifndef _BUDDY_H
- #define _BUDDY_H
- #include "qqdef.h"
- typedef struct qqbuddy{
- uint number;
- char nickname[NICKNAME_LEN];
- uint ip;
- ushort port;
- ushort face;
- uchar age;
- uchar sex;
- uchar gid;
- uchar qqshow;
- uchar flag;
- uchar session_key[16];
- uchar status;
- ushort version;
- uchar verify_flag; //00 允许 01 验证 02 拒绝 03 问题
- uint sign_time;
- uchar account_flag;
- #ifndef NO_BUDDY_DETAIL_INFO
- char signature[SIGNITURE_LEN];
- char account[ACCOUNT_LEN]; //email account
- char alias[ALIAS_LEN];
- char post_code[32];
- char address[64];
- char homephone[32];
- char mobilephone[32];
- char email[32];
- char occupation[32];
- char homepage[64];
- char brief[256];
- char school[32];
- char birth[16];
- char province[16];
- char city[16];
- char country[16];
- #else
- char signiture[1];
- char account[1];
- char alias[1];
- char post_code[1];
- char address[1];
- char homephone[1];
- char mobilephone[1];
- char email[1];
- char occupation[1];
- char homepage[1];
- char brief[1];
- char school[1];
- char birth[1];
- char province[1];
- char city[1];
- char country[1];
- #endif
- }qqbuddy;
- struct qqclient;
- qqbuddy* buddy_get( struct qqclient* qq, uint number, int create );
- void buddy_remove( struct qqclient* qq, uint number );
- void buddy_update_list( struct qqclient* qq );
- void buddy_update_info( struct qqclient* qq, qqbuddy* b );
- void buddy_sort_list( struct qqclient* qq );
- int buddy_send_message( struct qqclient* qq, uint number, char* msg );
- void buddy_set_all_off( struct qqclient* qq );
- void buddy_put_event( struct qqclient* qq );
- char* buddy_status_string( int st );
- #endif
- /*
- * coinfig.c
- *
- * Configuration Procedures
- *
- * Copyright (C) 2008 Huang Guan
- *
- * 2008-7-9 15:40:05 Created.
- * 2009-7-5 23:32:00 Patched a chars memory bug in parse().
- *
- * Description: This file mainly includes the functions about
- * reading configuration file.
- *
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "debug.h"
- #include "qqdef.h"
- #include "memory.h"
- #include "config.h"
- #ifndef __WIN32__
- #define stricmp strcasecmp
- #endif
- typedef config_item item;
- enum PARSING{
- NAME,
- VALUE
- };
- static void add( config* c, char* name, char* value )
- {
- if( c->item_count >= 1024 ){
- DBG("failed to add config item: too many.");
- return;
- }
- int i = c->item_count ++;
- c->items[i] = (config_item*)malloc( sizeof(config_item) );
- strncpy( c->items[i]->name, name, CONFIG_NAME_LEN );
- strncpy( c->items[i]->value, value, CONFIG_VALUE_LEN );
- // printf("added item '%s':'%s'\n", name, value );
- }
- static void parse( config*conf, char* buf, int len )
- {
- enum PARSING state;
- state = NAME;
- int i, j;
- char c;
- char name[CONFIG_NAME_LEN+1], value[CONFIG_VALUE_LEN+1];
- i = j = 0;
- buf[len] = '\0';
- while( i <= len ){
- c = buf[i];
- switch( state ){
- case NAME:
- if( c == '\r' ){
- break;
- }else if( c == '\n' ){ //not found =, but found \n
- j = 0;
- }else if( c == '=' ){
- state = VALUE;
- while( j>0 && name[j-1] == ' ' )
- j--;
- name[j] = '\0'; //add end
- j = 0;
- }else if( c == '\\' ){
- i ++;
- }else{
- if( j==0 && c == ' ' ) //do not add front space
- break;
- if( j<CONFIG_NAME_LEN )
- name[j++] = c;
- }
- break;
- case VALUE:
- if( c == '\r' ){
- break;
- }else if( c == '\n' || c == '\0' ){
- state = NAME;
- while( j>0 && value[j-1] == ' ' )
- j--;
- value[j] = '\0'; //add end
- if( name[0] != '#' ){ //comment?? do not add it.
- add( conf, name, value );
- }
- j = 0;
- }else if( c == '\\' ){
- i ++;
- }else{
- if( j==0 && c == ' ' ) //do not add front space
- break;
- if( j<CONFIG_VALUE_LEN )
- value[j++] = c;
- }
- break;
- default:
- break;
- }
- i ++;
- }
- }
- static char default_configs[]="### Default Configuration ###\n"
- "QQPacketLog = 0\n"
- "QQTerminalLog = 0\n"
- "# 日志目录,删除下面一条将不记录日志。\n"
- "QQLogDir = ./log\n"
- "# 验证码目录\n"
- "QQVerifyDir = ./verify\n"
- "# 登录方式 UDP or TCP or PROXY_HTTP\n"
- "QQNetwork = UDP\n"
- "NO_COLOR = 0\n"
- "下面为QQ服务器列表,每个服务器用|来隔开,IP和端口用:来给开。\n"
- "电信服务器\n"
- "#QQTcpServerList = 219.133.60.173:80|219.133.38.232:80|219.133.40.177:80\n"
- "最多允许添加16个\n"
- "QQTcpServerList = 219.133.60.173:443|219.133.49.125:443|58.60.15.33:443\n"
- "QQUdpServerList = 219.133.49.171:8000|58.60.14.37:8000|219.133.60.36:8000|58.60.15.39:8000|sz6.tencent.com:8000|sz7.tencent.com:8000\n"
- "QQHttpProxyServerList = \n"
- ;
- int config_open( config* c, char* filename )
- {
- FILE* fp;
- fp = fopen ( filename, "r" );
- if( fp == NULL ){
- DBG("failed to open file %s", filename );
- fp = fopen( filename, "w" );
- if( fp == NULL )
- return -1;
- fputs( default_configs, fp );
- fclose( fp );
- fp = fopen( filename, "r" );
- if( fp == NULL )
- return -1;
- }
- char* buf;
- buf = (char*) malloc( KB(32) );
- int len = fread( buf, 1, KB(32), fp );
- if( len > 0 ){
- memset( c, 0, sizeof(config) );
- parse( c, buf, len );
- }else{
- DBG("failed to read file %s", filename );
- fclose( fp );
- return -2;
- }
- free( buf );
- fclose( fp );
- return 0;
- }
- int config_readint( config*c, char* name )
- {
- int i;
- for( i=0; i<c->item_count; i++ )
- {
- if( strcmp( c->items[i]->name, name ) == 0 )
- {
- if( stricmp( c->items[i]->value, "1" ) == 0 )
- return 1;
- else if( stricmp( c->items[i]->value, "0" ) == 0 )
- return 0;
- //else
- return atol( c->items[i]->value );
- }
- }
- // DBG("config item not found.");
- return 0;
- }
- char* config_readstr( config*c, char* name )
- {
- int i;
- for( i=0; i<c->item_count; i++ )
- {
- if( strcmp( c->items[i]->name, name ) == 0 )
- {
- return c->items[i]->value;
- }
- }
- // DBG("config item not found.");
- return NULL;
- }
- void config_close( config* c )
- {
- int i;
- if( !c ) return;
- for( i=0; i<c->item_count; i++ )
- free( c->items[i] );
- c->item_count = 0;
- }
- config *g_conf;
- void config_init()
- {
- NEW( g_conf, sizeof(config) );
- if( !g_conf ) return;
- if( config_open( g_conf, "./qqconfig.txt" ) < 0 ){
- perror("can't not open qqconfig.txt file.");
- exit(-1);
- }
- char* log_dir = config_readstr( g_conf, "QQLogDir" );
- uint log_terminal = config_readint( g_conf, "QQTerminalLog" );
- if( log_terminal ){
- debug_term_on();
- }else{
- debug_term_off();
- }
- if( log_dir == NULL ){
- debug_file_off();
- }else{
- debug_set_dir( log_dir );
- debug_file_on();
- }
- }
- void config_end()
- {
- config_close( g_conf );
- DEL( g_conf );
- }
- //test
- /*
- int main()
- {
- config* c = (config*)malloc( sizeof( config) );
- config_open( c, "./server.txt" );
- MSG("port: %d", config_readint( c, "ServerPort" ) );
- config_close( c );
- free( c );
- system("pause");
- return 0;
- }
- */
- //config.h
- #ifndef _CONFIG_H
- #define _CONFIG_H
- #define CONFIG_NAME_LEN 64
- #define CONFIG_VALUE_LEN 256
- typedef struct config_item{
- char name[CONFIG_NAME_LEN];
- char value[CONFIG_VALUE_LEN];
- }config_item;
- typedef struct cconfig{
- int item_count;
- config_item *items[1024];
- }config;
- int config_open( config* c, char* filename );
- int config_readint( config*c, char* name );
- char* config_readstr( config*c, char* name );
- void config_close( config* c );
- //
- void config_init();
- void config_end();
- extern config *g_conf;
- #endif
- /*
- * crc32.c
- *
- * CRC32
- *
- * Copyright (C) 2008 Huang Guan
- *
- * 2008-7-12 Created.
- *
- * Description: This file mainly includes the functions about
- * Hash
- *
- */
- #include <stdio.h>
- #include "qqdef.h"
- static uint CRC32[256];
- static char init = 0;
- static void init_table()
- {
- int i,j;
- uint crc;
- for(i = 0;i < 256;i++)
- {
- crc = i;
- for(j = 0;j < 8;j++)
- {
- if(crc & 1){
- crc = (crc >> 1) ^ 0xEDB88320;
- }else{
- crc = crc >> 1;
- }
- }
- CRC32[i] = crc;
- }
- }
- uint crc32( uchar *buf, int len)
- {
- uint ret = 0xFFFFFFFF;
- int i;
- if( !init ){
- init_table();
- init = 1;
- }
- for(i = 0; i < len;i++)
- {
- ret = CRC32[((ret & 0xFF) ^ buf[i])] ^ (ret >> 8);
- }
- ret = ~ret;
- return ret;
- }
- # include "db.h" ;
- void db_connect(MYSQL * pDBConn)
- {
- char *db_server = "localhost";
- char *db_user = "**db user name**";
- char *db_password = "**db password**";
- char *db_database = "**db name**";
- if (!mysql_real_connect(pDBConn, db_server, db_user, db_password, db_database, 0, 0, 0))
- {
- fprintf(stderr, "%s\n", mysql_error(pDBConn));
- exit(1);
- }
- mysql_query(pDBConn,"SET NAMES UTF8 ;") ;
- }
- MYSQL * db_init()
- {
- MYSQL * pDBConn = mysql_init(0);
- db_connect(pDBConn) ;
- return pDBConn ;
- }
- int _db_query(char * psSQL,MYSQL * pDBConn,char * psFile,char * psFunc, int nLine)
- {
- int nTry = 0 ;
- while(1)
- {
- int nRes = mysql_query(pDBConn, psSQL) ;
- if(nRes>0)
- {
- int nErr = mysql_errno(pDBConn) ;
- char * psErr = mysql_error(pDBConn) ;
- printf("[mysql error:%d][call: %s:%d %s()]%s\n%s\n]",nErr,psFile,nLine,psFunc,psErr,psSQL) ;
- if( nErr==2013 || nErr==2006 )
- {
- nTry ++ ;
- printf("%d秒后 重新连接 mysql ...\n",nTry) ;
- sleep(nTry) ;
- db_connect(pDBConn) ;
- }
- else
- {
- return nRes ;
- }
- }
- else
- {
- return nRes ;
- }
- }
- }
- /*
- * debug.c
- *
- * MyQQ debugger.
- *
- * Copyright (C) 2008 Huang Guan
- *
- * 2008-01-31 Created.
- *
- */
- #include <stdio.h>
- #include <stdarg.h>
- #include <fcntl.h>
- #include <string.h>
- #ifdef __WIN32__
- #include <io.h>
- #endif
- #include <time.h>
- #include "debug.h"
- #include "util.h"
- #include "qqdef.h"
- #include "utf8.h"
- static int dbg_term = 0, dbg_file = 0, log_day = 0;
- static FILE* fp_log = NULL;
- static char dir[128]={0,}, filename[160];
- void print_error(char* file, char* function, int line, const char *fmt, ...)
- {
- va_list args;
- char printbuf[512];
- int i;
- if( !dbg_term && !dbg_file )
- return;
- va_start(args, fmt);
- i=vsprintf( printbuf, fmt, args );
- printbuf[i] = 0;
- va_end(args);
- #ifdef __WIN32__
- utf8_to_gb( printbuf, printbuf, i );
- #endif
- if( dbg_term ){
- printf("%s(%d): %s\n", function, line, printbuf);
- }
- if( dbg_file ){
- time_t t = time( NULL );
- struct tm* tm1 = localtime(&t);
- if( !tm1 ) return;
- if( tm1->tm_mday != log_day ){
- init_file_path(0);
- }
- char tmp[16];
- strftime( tmp, 15, "%X", tm1 );
- fprintf( fp_log, "%s [%s]%s(%d): %s\n", tmp, file, function, line, printbuf);
- fflush( fp_log );
- }
- }
- static char* hex_str(unsigned char *buf, int len, char* outstr )
- {
- const char *set = "0123456789abcdef";
- char *tmp;
- unsigned char *end;
- if (len > 1024)
- len = 1024;
- end = buf + len;
- tmp = &outstr[0];
- while (buf < end)
- {
- *tmp++ = set[ (*buf) >> 4 ];
- *tmp++ = set[ (*buf) & 0xF ];
- *tmp++ = ' ';
- buf ++;
- }
- *tmp = '\0';
- return outstr;
- }
- void hex_dump( unsigned char * buf, int len )
- {
- char str[KB(4)];
- if( dbg_term )
- puts( hex_str( buf, len, str ) );
- if( dbg_file ){
- fputs( hex_str( buf, len, str ), fp_log );
- fprintf( fp_log, "\n" );
- fflush( fp_log );
- }
- //fprintf( stderr, hex_str( buf, len ) );
- }
- void debug_term_on()
- {
- dbg_term = 1;
- }
- void debug_term_off()
- {
- dbg_term = 0;
- }
- void init_file_path(int nQQAcount)
- {
- char tmp[64] ;
- char tmp2[64] = {0,} ;
- sprintf(tmp2,"/%d-",nQQAcount) ;
- time_t t = time( NULL );
- struct tm* tm1 = localtime(&t);
- if( !tm1 ){
- perror("log.c init_file_path: ERROR GETTING SYSTEM TIME.");
- }
- log_day = tm1->tm_mday;
- strftime( tmp, 64, "%Y.%m.%d.log", tm1 );
- if( access( dir, 0 )!=0 ){
- mkdir_recursive( dir );
- }
- strcpy( filename, dir );
- strcat( filename, tmp2 );
- strcat( filename, tmp );
- if( fp_log )
- {
- fclose( fp_log );
- }
- fp_log = fopen( filename, "aw" );
- // fp_log = fopen( filename, "w" );
- }
- void debug_file_on()
- {
- if( dbg_file )
- return;
- init_file_path(0);
- dbg_file = 1;
- }
- void debug_file_off()
- {
- if( !dbg_file )
- return;
- dbg_file = 0;
- if( fp_log )
- fclose( fp_log );
- }
- void debug_set_dir(char* str)
- {
- strcpy( dir, str );
- }
- /*
- * group.c
- *
- * Group Operations
- *
- * Copyright (C) 2008 Huang Guan
- *
- * 2008-7-12 Created.
- *
- * Description: This file mainly includes the functions about
- *
- */
- #include <time.h>
- #include <stdlib.h>
- #include <string.h>
- #include "qqclient.h"
- #include "memory.h"
- #include "debug.h"
- #include "protocol.h"
- #include "list.h"
- #include "group.h"
- static int searcher( const void* p, const void* v )
- {
- return ( ((qqgroup*)p)->number == (int)v );
- }
- qqgroup* group_get( struct qqclient* qq, uint number, int create )
- {
- if( !number )
- return NULL;
- qqgroup* g;
- g = list_search( &qq->group_list, (void*)number, searcher );
- //if not found, g must be NULL
- if( !g && create ){
- NEW( g, sizeof( qqgroup ) );
- if( !g ){
- DBG("Fatal error: group not allocated");
- return g;
- }
- g->number = number;
- sprintf( g->name, "%u", number );
- if( list_append( &qq->group_list, (void*)g )<0 ){
- DEL( g );
- DBG("group list is full.");
- }
- }
- return g;
- }
- void group_remove( struct qqclient* qq, uint number )
- {
- qqgroup* g;
- g = list_search( &qq->group_list, (void*)number, searcher );
- if( g ){
- list_remove( &qq->group_list, g );
- }
- }
- void group_update_list( struct qqclient* qq )
- {
- // prot_group_download_list( qq, 0 ); 不知道这个09还能不能用。
- prot_group_download_labels( qq, 0 );
- }
- void group_update_info( qqclient* qq, qqgroup* g )
- {
- }
- void group_put_event( qqclient* qq )
- {
- char *temp;
- int i;
- qqgroup* g;
- NEW( temp, KB(1) );
- if( !temp ) return;
- strcpy( temp, "grouplist^$" );
- pthread_mutex_lock( &qq->group_list.mutex );
- for( i=0; i<qq->group_list.count; i++ ){
- g = (qqgroup*)qq->group_list.items[i];
- sprintf( temp, "%s%u\t%s^@", temp, g->number, g->name );
- }
- pthread_mutex_unlock( &qq->group_list.mutex );
- qqclient_put_event( qq, temp );
- DEL( temp );
- }
- /*
- * libqq.c
- *
- * LibQQ Program
- *
- * Copyright (C) 2009 Huang Guan
- *
- * 2009-5-6 23:23:51 Created.
- *
- * Description: This file mainly includes the functions about
- *
- */
- #define LIBQQLIB
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- #ifdef __WIN32__
- #include <winsock.h>
- #include <wininet.h>
- #else
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #endif
- #include "debug.h"
- #include "memory.h"
- #include "qqclient.h"
- #include "libqq.h"
- #include "loop.h"
- #include "buddy.h"
- #include "qun.h"
- #include "group.h"
- #include "config.h"
- #include "qqsocket.h"
- #include "utf8.h"
- #define MAX_USERS 1000
- static int if_init = 0;
- static void* login_ex( void* data )
- {
- qqclient* qq = (qqclient*) data;
- pthread_detach(pthread_self());
- DBG("login: %u", qq->number );
- qqclient_login( qq );
- return NULL;
- }
- EXPORT void libqq_init()
- {
- config_init();
- if_init = 1;
- qqsocket_init();
- }
- EXPORT void libqq_cleanup()
- {
- config_end();
- qqsocket_end();
- }
- EXPORT qqclient* libqq_create( uint number, char* pass )
- {
- qqclient* qq;
- if( !if_init )
- libqq_init();
- NEW( qq, sizeof(qqclient) );
- if( !qq ){
- DEL( qq );
- return NULL;
- }
- qqclient_create( qq, number, pass );
- qq->auto_accept = 1; //temporarily do this
- return qq;
- }
- EXPORT int libqq_login( qqclient* qq )
- {
- int ret;
- pthread_t ptr;
- ret = pthread_create( &ptr, NULL, login_ex, (void*)qq );
- if( ret != 0 ){
- DBG("thread creation failed. ret=%d", ret );
- }
- return ret;
- }
- EXPORT int libqq_logout( qqclient* qq )
- {
- DBG("logout %u", qq->number );
- qqclient_logout( qq );
- return 0;
- }
- EXPORT int libqq_detach( qqclient* qq )
- {
- DBG("detach %u", qq->number );
- qqclient_detach( qq );
- return 0;
- }
- EXPORT int libqq_getmessage( qqclient* qq, char* buf, int size, int wait )
- {
- int ret;
- ret = qqclient_get_event( qq, buf, size, wait );
- utf8_to_gb( buf, buf, size );
- return ret;
- }
- EXPORT int libqq_sendmessage( qqclient* qq, uint to, char* buf, char qun_msg )
- {
- char* tmp;
- int len = strlen(buf);
- if( len<1 ) return -2;
- NEW( tmp, len*2 );
- gb_to_utf8( buf, tmp, len*2-1 );
- if( qun_msg ){
- qqqun* q = qun_get_by_ext( qq, to );
- if( q )
- qun_send_message( qq, q->number, tmp );
- }else{
- buddy_send_message( qq, to, tmp );
- }
- DEL( tmp );
- return 0;
- }
- EXPORT void libqq_updatelist( qqclient* qq )
- {
- buddy_update_list( qq );
- group_update_list( qq );
- }
- // 090622 by Huang Guan. change the type of code from uint to const char*
- EXPORT void libqq_verify( qqclient* qq, const char* code )
- {
- if( code ){
- qqclient_verify( qq, *(uint*)code );
- }else{
- qqclient_verify( qq, 0x00000000 ); //this will make the server change another png
- }
- }
- EXPORT void libqq_remove( qqclient* qq )
- {
- qqclient_cleanup( qq ); //will call qqclient_logout if necessary
- DEL( qq );
- }
- EXPORT void libqq_status( qqclient* qq, int st, uchar has_camera )
- {
- qq->has_camera = has_camera;
- qqclient_change_status( qq, st );
- }
- EXPORT void libqq_addbuddy( qqclient* qq, uint uid, char* msg )
- {
- qqclient_add( qq, uid, msg );
- }
- EXPORT void libqq_delbuddy( qqclient* qq, uint uid )
- {
- qqclient_del( qq, uid );
- }
- void buddy_msg_callback ( qqclient* qq, uint uid, time_t t, char* msg, qqmessage* pMsg )
- {
- char timestr[24];
- struct tm * timeinfo;
- char* str;
- int len;
- timeinfo = localtime ( &t );
- strftime( timestr, 24, "%Y-%m-%d %H:%M:%S", timeinfo );
- len = strlen( msg );
- NEW( str, len+64 );
- if( uid == 10000 ){
- sprintf( str, "broadcast^$System^$%s", msg );
- }else{
- sprintf( str, "buddymsg^$%u^$%s^$%s", uid, timestr, msg );
- }
- qqclient_put_message( qq, str );
- }
- void qun_msg_callback ( qqclient* qq, uint uid, uint int_uid,
- time_t t, char* msg )
- {
- qqqun* q;
- char timestr[24];
- struct tm * timeinfo;
- char* str;
- int len;
- timeinfo = localtime ( &t );
- strftime( timestr, 24, "%Y-%m-%d %H:%M:%S", timeinfo );
- q = qun_get( qq, int_uid, 1 );
- if( !q ){
- DBG("error: q=NULL");
- return;
- }
- len = strlen( msg );
- NEW( str, len+64 );
- sprintf( str, "clustermsg^$%u^$%u^$%s^$%s", q->ext_number, uid, timestr, msg );
- qqclient_put_message( qq, str );
- }
- EXPORT uint libqq_refresh( qqclient* qq )
- {
- char event[16];
- qqclient_set_process( qq, qq->process );
- sprintf( event, "status^$%d", qq->mode );
- qqclient_put_event( qq, event );
- buddy_put_event( qq );
- group_put_event( qq );
- qun_put_event( qq );
- qqclient_set_process( qq, qq->process );
- return qq->number;
- }
- EXPORT void libqq_getqunname( qqclient* qq, uint ext_id, char* buf )
- {
- qqqun* q = qun_get_by_ext( qq, ext_id );
- if( q ){
- strncpy( buf, q->name, 15 );
- }else{
- if( ext_id != 0 ){
- sprintf( buf, "%u" , ext_id );
- }
- }
- }
- EXPORT void libqq_getqunmembername( qqclient* qq, uint ext_id, uint uid, char* buf )
- {
- qqqun* q = qun_get_by_ext( qq, ext_id );
- if( q ){
- qunmember* m = qun_member_get( qq, q, uid, 0 );
- if( m ){
- strncpy( buf, m->nickname, 15 );
- return;
- }
- }
- if( uid != 0 ){
- sprintf( buf, "%u" , uid );
- }
- }
- EXPORT void libqq_getbuddyname( qqclient* qq, uint uid, char* buf )
- {
- qqbuddy* b = buddy_get( qq, uid, 0 );
- if( b ){
- strncpy( buf, b->nickname, 15 );
- }else{
- if( uid != 0 ){
- sprintf( buf, "%u" , uid );
- }
- }
- }
- // 090706 by HG
- EXPORT void libqq_sethttpproxy( struct qqclient* qq, char* ip, ushort port )
- {
- struct sockaddr_in addr;
- qq->network = PROXY_HTTP;
- netaddr_set( ip, &addr );
- qq->proxy_server_ip = ntohl( addr.sin_addr.s_addr );
- qq->proxy_server_port = port;
- }
- EXPORT void libqq_getextrainfo( struct qqclient* qq, uint uid )
- {
- prot_buddy_get_extra_info( qq, uid );
- }
- #ifndef _libQQ_H
- #define _libQQ_H
- #include <time.h>
- #ifdef __WIN32__
- #define STDCALL __stdcall
- #ifdef LIBQQLIB
- #define EXPORT __declspec(dllexport) STDCALL
- #else
- #define EXPORT __declspec(dllimport) STDCALL
- #endif
- #else
- #define EXPORT
- #endif
- struct qqclient;
- EXPORT void libqq_init();
- EXPORT void libqq_cleanup();
- EXPORT struct qqclient* libqq_create( uint number, char* pass );
- EXPORT int libqq_login( struct qqclient* qq );
- EXPORT int libqq_logout( struct qqclient* qq );
- EXPORT int libqq_detach( struct qqclient* qq );
- EXPORT int libqq_getmessage( struct qqclient* qq, char* buf, int size, int wait );
- EXPORT int libqq_sendmessage( struct qqclient* qq, uint to, char* buf, char qun_msg );
- EXPORT void libqq_updatelist( struct qqclient* qq );
- EXPORT void libqq_verify( struct qqclient* qq, const char* code );
- EXPORT void libqq_remove( struct qqclient* qq );
- EXPORT void libqq_status( struct qqclient* qq, int st, uchar has_camera );
- EXPORT uint libqq_refresh( struct qqclient* qq );
- EXPORT void libqq_getqunname( struct qqclient* qq, uint ext_id, char* buf );
- EXPORT void libqq_getqunmembername( struct qqclient* qq, uint ext_id, uint uid, char* buf );
- EXPORT void libqq_getbuddyname( struct qqclient* qq, uint uid, char* buf );
- EXPORT void libqq_getextrainfo( struct qqclient* qq, uint uid );
- EXPORT void libqq_addbuddy( struct qqclient* qq, uint uid, char* msg );
- EXPORT void libqq_delbuddy( struct qqclient* qq, uint uid );
- EXPORT void libqq_sethttpproxy( struct qqclient* qq, char* ip, ushort port );
- #endif
- /*
- * list.c
- *
- * List
- *
- * Copyright (C) 2008 Huang Guan
- *
- * 2008-7-12 Created.
- *
- * Description: This file mainly includes the functions about
- *
- */
- #include <string.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include "qqdef.h"
- #include "debug.h"
- #include "memory.h"
- #include "list.h"
- int list_create( list* l, int size )
- {
- l->size = size;
- l->count = 0;
- pthread_mutex_init( &l->mutex, NULL );
- NEW( l->items, l->size*sizeof(void*) );
- assert( l->items );
- return 0;
- }
- int list_append( list* l, void* data )
- {
- int i, ret = 0;
- pthread_mutex_lock( &l->mutex );
- if( l->count >= l->size ){
- DBG("list is full. count:%d", l->count);
- ret = -1;
- }else{
- i = l->count ++;
- l->items[i] = data;
- }
- pthread_mutex_unlock( &l->mutex );
- return ret;
- }
- int list_remove( list* l, void* data )
- {
- int i;
- pthread_mutex_lock( &l->mutex );
- for( i=0; i<l->count; i++ ){
- if( l->items[i] == data ){
- l->count --;
- if( i != l->count )
- l->items[i] = l->items[l->count];
- break;
- }
- }
- pthread_mutex_unlock( &l->mutex );
- return 0;
- }
- void* list_search( list* l, void* v, search_func search )
- {
- int i;
- pthread_mutex_lock( &l->mutex );
- for( i=0; i<l->count; i++ ){
- if( search( l->items[i], v ) )
- break;
- }
- pthread_mutex_unlock( &l->mutex );
- if( i < l->count )
- return l->items[i];
- return NULL;
- }
- void list_sort( list* l, comp_func comp )
- {
- pthread_mutex_lock( &l->mutex );
- if( l->count == 0 )
- return;
- qsort( l->items, l->count, sizeof(void*), comp );
- pthread_mutex_unlock( &l->mutex );
- }
- void list_cleanup( list* l )
- {
- int i;
- pthread_mutex_lock( &l->mutex );
- if( l->count > 0 ){
- // DBG("list count = %d", l->count );
- for( i=0; i<l->count; i++ ){
- DEL( l->items[i] );
- }
- }
- DEL( l->items );
- pthread_mutex_unlock( &l->mutex );
- pthread_mutex_destroy( &l->mutex );
- }
- /*
- * memory.c
- *
- * Memory Management
- *
- * Copyright (C) 2008 Huang Guan
- *
- * 2008-7-9 15:40:05 Created.
- *
- * Description: This file mainly includes the functions about
- * allocating/releasing memory.
- *
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- #include "debug.h"
- #include "memory.h"
- mem_detail* g_md;
- void memory_init()
- {
- if( !g_md ){
- g_md = (mem_detail*)malloc(sizeof(mem_detail));
- memset( g_md, 0, sizeof(mem_detail) );
- pthread_mutex_init( &g_md->mutex_mem, NULL );
- }
- }
- void memory_new_detail( void** p, int size, char* file, char* function, int line, char* name )
- {
- char temp[128];
- sprintf( temp, "[%s]%s(%d) %s", file, function, line, name );
- memory_new( p, size, temp );
- }
- void memory_new( void** p, int size, char* memo )
- {
- if( !g_md )
- memory_init();
- *p = NULL;
- if( g_md->item_count >= MAX_ALLOCATION )
- {
- DBG("no more allocations."); return;
- }
- *p = malloc(size);
- memset( *p, 0, size );
- if( *p == NULL ){
- DBG("no enough memory.");
- exit(-1);
- }
- pthread_mutex_lock( &g_md->mutex_mem );
- int i = g_md->item_count ++;
- g_md->items[i] = (allocation*)malloc(sizeof(allocation));
- //zero
- g_md->items[i]->pointer = *p;
- g_md->items[i]->size = size;
- strncpy( g_md->items[i]->memo, memo, MEMO_LEN );
- g_md->items[i]->time_alloc = time( NULL );
- pthread_mutex_unlock( &g_md->mutex_mem );
- //ok
- }
- //a piece of very lazy code
- void memory_delete( void* p )
- {
- int i;
- if( p == NULL ) return;
- pthread_mutex_lock( &g_md->mutex_mem );
- for( i=0; i<g_md->item_count; i++ )
- {
- if( g_md->items[i]->pointer == p )
- {
- free( p );
- free( g_md->items[i] );
- g_md->item_count --;
- if( i!=g_md->item_count )
- g_md->items[i] = g_md->items[g_md->item_count];
- g_md->items[g_md->item_count] = NULL;
- pthread_mutex_unlock( &g_md->mutex_mem );
- return;
- }
- }
- DBG("not found pointer %x", p );
- pthread_mutex_unlock( &g_md->mutex_mem );
- }
- void memory_end(){
- if( !g_md ) return;
- DBG("g_md->item_count = %d", g_md->item_count );
- if( g_md->item_count>0 ){
- memory_print();
- }
- pthread_mutex_destroy( &g_md->mutex_mem );
- }
- void memory_print(){
- if( !g_md ) return;
- DBG("#memory info dumping (item_count: %d) ", g_md->item_count );
- int i;
- pthread_mutex_lock( &g_md->mutex_mem );
- for( i=0; i<g_md->item_count; i++ )
- {
- char timestr[10];
- struct tm* t = localtime( & g_md->items[i]->time_alloc);
- strftime( timestr, 10, "%X", t );
- DBG("[%d] 0x%x(%d)\t%s\t%s", i, g_md->items[i]->pointer, g_md->items[i]->size,
- g_md->items[i]->memo, timestr );
- }
- pthread_mutex_unlock( &g_md->mutex_mem );
- }
- //test
- /*
- int main()
- {
- memory_init();
- char* p, *str2;
- NEW( str2, 1024 );
- NEW( p, 256 );
- strcpy( str2, "hello");
- puts( str2 );
- DEL( p );
- memory_end();
- return 0;
- }
- */
Add Comment
Please, Sign In to add comment