-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
117 lines (111 loc) · 3.79 KB
/
main.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
#include "db.h"
#include "err.h"
#include "mirai.h"
#include "queue.h"
#include "request.h"
#include "tcp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
extern Queue M_msgRecvQueue;
const char dbName[] = "db/philosophy.db";
const char tableName[] = "FIRST";
int insertCallback(void *data, int argc, char **argv, char **azColName);
void makePhilosophyText(char *retText, size_t size, sqlite3 *db, const char *tableName, size_t id);
void sendARandomPhilosophy(Message message);
int main(void)
{
const char servIP[] = "127.0.0.1";
const int servPort = 8080;
const char verifyKey[] = "YQ666";
const char QQ[][szQQ] = {"1748879992", "2942575858"};
const char mode[] = "default";
int sockfd = TCP_connect(servIP, servPort);
M_customInit(sockfd, verifyKey, QQ[0], "127.0.0.1:8080", mode, mode); // be eauql to M_defaultInit(verifyKey, QQ);
M_checkVerifyKey(echoNone);
M_bindSessionKey(echoNone);
size_t num;
Message message;
for (;;) {
num = M_getUnreadMsgCount(echoNone);
if (num != 0) {
M_getUnreadMsg(num, echoRecv);
M_parseGroupMessage();
for (; !isEmpty(M_msgRecvQueue);) {
pop(M_msgRecvQueue, &message);
if (strcmp(message.type, "GroupMessage") != 0)
continue;
if (strcmp(message.msgs[0].key, "Plain") != 0)
continue;
if (strcasecmp(message.msgs[0].value, "/philosophy") != 0)
continue;
sendARandomPhilosophy(message);
}
}
}
return 0;
}
/*repeatGroupMemberMessage should be called after binding sessionKey*/
void repeatGroupMemberMessage()
{
size_t i, k, num;
Message message;
for (i = 0;; ++i) {
num = M_getUnreadMsgCount(echoNone);
if (num != 0) {
M_getUnreadMsg(num, echoNone);
M_parseGroupMessage();
for (; !isEmpty(M_msgRecvQueue);) {
pop(M_msgRecvQueue, &message);
if (strcmp(message.type, "GroupMessage") != 0)
continue;
print("[msgNum]:%ld\n\n", message.msgNum);
for (k = 0; k < message.msgNum; ++k) {
if (strcmp(message.msgs[k].key, "Plain") == 0)
M_msgAddText(1, message.msgs[k].value);
else if (strcmp(message.msgs[k].key, "Image") == 0)
M_msgAddImg(1, message.msgs[k].value);
else if (strcmp(message.msgs[k].key, "Face") == 0)
M_msgAddFace(1, message.msgs[k].value);
}
M_sendGroupMessage(message.group.groupID, echoSend);
print("%ld", getElemNum(M_msgRecvQueue));
}
}
}
}
void sendARandomPhilosophy(Message message)
{
char text[sizeof(Philosophy)];
sqlite3 *db = DB_open(dbName);
makePhilosophyText(text, sizeof(text), db, tableName, -1);
DB_close(db);
M_msgAddText(1, text);
M_sendGroupMessage(message.group.groupID, echoSend);
}
void makePhilosophyText(char *retText, size_t size, sqlite3 *db, const char *tableName, size_t id)
{
size_t maxID = DB_getMaxID(db, tableName);
if (id < 0 || id > maxID) {
srand(time(NULL));
id = rand() % (maxID + 1);
}
db = DB_open(dbName);
Philosophy phi;
DB_getPhilosophy(id, db, tableName, &phi);
DB_close(db);
snprintf(retText, size, " %s\n\n"
" "
"——%s",
phi.text, phi.author);
}
int insertCallback(void *data, int argc, char **argv, char **azColName)
{
size_t i;
for (i = 0; i < argc; ++i) {
print("%s = %s\n", argv[i], azColName[i]);
}
return 0;
}