-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.c
145 lines (122 loc) · 3.71 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "g_webqq.h"
#include "gwq_user.h"
static GMainLoop *MainLoop;
static gint64 QQNumForTestMsg;
static void _LogoutCallback(GWQSession* wqs, void* ctx)
{
if (wqs->st != GWQS_ST_OFFLINE) {
GWQ_MSG("Logout failed\n");
g_main_loop_quit(MainLoop);
} else {
GWQ_MSG("Logout success\n");
g_main_loop_quit(MainLoop);
}
}
void _DisplayMsg(gpointer data, gpointer user_data)
{
QQMsgContent *qmc;
qmc = (QQMsgContent*)data;
if (qmc->type == QQ_MSG_CONTENT_STRING_T) {
GWQ_MSG("%s\n", qmc->value.str->str);
}
}
void _MessageRecieved(GWQSession* wqs, QQRecvMsg* msg)
{
GWQ_DBG("==>_MessageRecieved()\n");
switch (msg->msg_type) {
case MSG_STATUS_CHANGED_T:
GWQ_MSG("uin=%"G_GINT64_FORMAT", status:%s\n", msg->uin, msg->status->str);
break;
case MSG_BUDDY_T:
GWQ_MSG("[%"G_GINT64_FORMAT"]\n", msg->from_uin);
g_ptr_array_foreach(msg->contents, _DisplayMsg, wqs);
default:
GWQ_MSG("Unknown message type received\n");
break;
}
}
static void _MessageSent(GWQSession* wqs, QQSendMsg* msg, gint32 retCode)
{
if (retCode) {
GWQ_ERR("Message sent failed\n");
} else {
GWQ_MSG("Message sent OK\n");
}
qq_sendmsg_free(msg);
}
void _usersForeach(GWQSession* wqs, GWQUserInfo* user)
{
GWQ_MSG("qqNum:%"G_GINT64_FORMAT",\t nick:%s,\t markname:%s\n",
user->qqNum, user->nick ,user->markname);
}
static void _UpdateUsersInfoCallback(GWQSession* wqs, void* ctx)
{
QQSendMsg *qsm;
QQMsgContent *qmc;
GWQ_MSG("=== Buddies List ===\n");
GWQSessionUsersForeach(wqs, _usersForeach);
GWQ_MSG("====================\n");
GWQ_DBG("Waiting for message\n");
GWQSessionDoPoll(wqs);
GWQ_DBG("Start sending test message\n");
qsm = qq_sendmsg_new(MSG_BUDDY_T, QQNumForTestMsg); /* qsm should be freed with qq_sendmsg_free!! */
qmc = qq_msgcontent_new(QQ_MSG_CONTENT_STRING_T, "来自<libgwebqq>的测试\n");
qq_sendmsg_add_content(qsm, qmc);
//qmc = qq_msgcontent_new(QQ_MSG_CONTENT_FACE_T, 110);
//qq_sendmsg_add_content(qsm, qmc);
qmc = qq_msgcontent_new(QQ_MSG_CONTENT_FONT_T, "宋体", 12, "000000", 0,0,0);
qq_sendmsg_add_content(qsm, qmc);
/*
if (GWQSessionSendBuddyMsg(wqs, QQNumForTestMsg, -1LL, qsm)) {
GWQ_ERR("Sent failed, BUSY sending message, please try later\n");
}
* */
}
static void _LoginCallback(GWQSession* wqs, void* ctx)
{
GWQ_DBG("==>__LoginCallback()\n");
if (wqs->st != GWQS_ST_IDLE) {
GWQ_MSG("Login failed\n");
g_main_loop_quit(MainLoop);
} else {
GWQ_MSG("Login successfully\n");
GWQ_MSG("Fetching buddies' information, please wait......\n");
GWQSessionUpdateUsersInfo(wqs, _UpdateUsersInfoCallback);
}
}
static GWQSession Wqs;
static void _sig_term_hndl(int sig)
{
GWQSessionLogOut(&Wqs);
}
int main(int argc, char** argv)
{
if (argc != 4) {
GWQ_ERR_OUT(ERR_OUT, "Usage: test <qq number> <password> <qqNUm for sending test message>\n");
}
signal(SIGINT, _sig_term_hndl);
g_type_init();
MainLoop = g_main_loop_new(NULL, TRUE);
GWQ_DBG("\n");
if (GWQSessionInit(&Wqs, argv[1], argv[2], &Wqs)) {
GWQ_ERR_OUT(ERR_OUT, "\n");
}
GWQSessionSetCallBack(&Wqs,
_LoginCallback,
_LogoutCallback,
_MessageRecieved,
_MessageSent,
NULL,
NULL,
NULL,
NULL);
QQNumForTestMsg = g_ascii_strtoll(argv[3], NULL, 10);
GWQSessionLogin(&Wqs, GWQ_CHAT_ST_HIDDEN);
g_main_loop_run(MainLoop);
GWQSessionExit(&Wqs);
return 0;
ERR_EXIT_SESSION:
GWQSessionExit(&Wqs);
ERR_OUT:
return -1;
}