-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAnswerToChat.bas
176 lines (146 loc) · 10.9 KB
/
AnswerToChat.bas
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include once "AnswerToChat.bi"
#include once "IntegerToWString.bi"
#include once "Settings.bi"
' Таблица ключевых фраз и ответов
Type QuestionAnswer
' Ключевая фраза
Dim Question As WString * 512
' Количество ответов
Dim AnswersCount As Integer
' Список из 50 ответов
Dim Answers(49) As WString * 512
End Type
Sub AnswerToChat(ByVal pBot As IrcBot Ptr, ByVal User As WString Ptr, ByVal MessageText As WString Ptr)
Dim hFile As HANDLE = CreateFile(@pBot->AnswersDatabaseFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)
If hFile <> INVALID_HANDLE_VALUE Then
' Вопрос‐ответ
Dim qa As QuestionAnswer = Any
Dim BytesCount As DWORD = Any
Dim result As Integer = ReadFile(hFile, @qa, SizeOf(QuestionAnswer), @BytesCount, 0)
Do While result <> 0
If BytesCount <> SizeOf(QuestionAnswer) Then
Exit Do
End If
' Найти ключевую фразу
If StrStrI(MessageText, qa.Question) <> 0 Then
' Получить ответ
If qa.AnswersCount > 0 Then
' Отправить пользователю случайную
Dim AnswerIndex As Integer = pBot->ReceivedRawMessagesCounter Mod qa.AnswersCount
pBot->Say(User, @qa.Answers(AnswerIndex))
End If
Exit Do
End If
result = ReadFile(hFile, @qa, SizeOf(QuestionAnswer), @BytesCount, 0)
Loop
CloseHandle(hFile)
End If
End Sub
Sub GetQuestionList(ByVal pBot As IrcBot Ptr, ByVal User As WString Ptr)
Dim hFile As HANDLE = CreateFile(@pBot->AnswersDatabaseFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)
If hFile <> INVALID_HANDLE_VALUE Then
' Вопрос‐ответ
Dim qa As QuestionAnswer = Any
Dim BytesCount As DWORD = Any
Dim result As Integer = ReadFile(hFile, @qa, SizeOf(QuestionAnswer), @BytesCount, 0)
Dim QuestionCount As Integer = 0
Do While result <> 0
If BytesCount <> SizeOf(QuestionAnswer) Then
Exit Do
End If
If lstrlen(qa.Question) = 0 Then
Exit Do
End If
Dim buf As WString * 100 = Any
itow(QuestionCount, @buf, 10)
pBot->Say(User, @buf)
pBot->Say(User, @qa.Question)
SleepEx(MessageTimeWait, 0)
QuestionCount += 1
result = ReadFile(hFile, @qa, SizeOf(QuestionAnswer), @BytesCount, 0)
Loop
CloseHandle(hFile)
End If
End Sub
Sub GetAnswerList(ByVal pBot As IrcBot Ptr, ByVal User As WString Ptr, ByVal QuestionIndex As Integer)
Dim hFile As HANDLE = CreateFile(@pBot->AnswersDatabaseFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)
If hFile <> INVALID_HANDLE_VALUE Then
' Вопрос‐ответ
Dim qa As QuestionAnswer = Any
Dim BytesCount As DWORD = Any
Dim result As Integer = ReadFile(hFile, @qa, SizeOf(QuestionAnswer), @BytesCount, 0)
Dim QuestionCount As Integer = 0
Do While result <> 0
If BytesCount <> SizeOf(QuestionAnswer) Then
Exit Do
End If
' Найти ключевую фразу
If lstrlen(qa.Question) = 0 Then
Exit Do
End If
If QuestionIndex = QuestionCount Then
Scope
Dim buf As WString * 100 = Any
lstrcpy(@buf, @"Фраза ")
itow(QuestionCount, @buf + lstrlen(@buf), 10)
pBot->Say(User, @buf)
pBot->Say(User, @qa.Question)
SleepEx(MessageTimeWait, 0)
End Scope
For i As Integer = 0 To qa.AnswersCount - 1
Dim buf As WString * 100 = Any
lstrcpy(@buf, @"Ответ ")
itow(i, @buf + lstrlen(@buf), 10)
pBot->Say(User, @buf)
pBot->Say(User, @qa.Answers(i))
SleepEx(MessageTimeWait, 0)
Next
Exit Do
End If
QuestionCount += 1
result = ReadFile(hFile, @qa, SizeOf(QuestionAnswer), @BytesCount, 0)
Loop
CloseHandle(hFile)
End If
End Sub
Sub AddQuestion(ByVal pBot As IrcBot Ptr, ByVal User As WString Ptr, ByVal Question As WString Ptr)
Dim hFile As HANDLE = CreateFile(@pBot->AnswersDatabaseFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)
If hFile <> INVALID_HANDLE_VALUE Then
SetFilePointer(hFile, 0, NULL, FILE_END)
' Вопрос‐ответ
Dim qa As QuestionAnswer = Any
memset(@qa, 0, SizeOf(QuestionAnswer))
lstrcpy(@qa.Question, Question)
Dim BytesCount As DWORD = Any
Dim result As Integer = WriteFile(hFile, @qa, SizeOf(QuestionAnswer), @BytesCount, 0)
CloseHandle(hFile)
End If
End Sub
Sub AddAnswer(ByVal pBot As IrcBot Ptr, ByVal User As WString Ptr, ByVal QuestionIndex As Integer, ByVal Answer As WString Ptr)
Dim hFile As HANDLE = CreateFile(@pBot->AnswersDatabaseFileName, GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)
If hFile <> INVALID_HANDLE_VALUE Then
Dim qa As QuestionAnswer = Any
Dim BytesCount As DWORD = Any
Dim result As Integer = ReadFile(hFile, @qa, SizeOf(QuestionAnswer), @BytesCount, 0)
Dim QuestionCount As Integer = 0
Do While result <> 0
If BytesCount <> SizeOf(QuestionAnswer) Then
Exit Do
End If
' Найти ключевую фразу
If lstrlen(qa.Question) = 0 Then
Exit Do
End If
If QuestionIndex = QuestionCount Then
lstrcpy(@qa.Answers(qa.AnswersCount), Answer)
qa.AnswersCount += 1
SetFilePointer(hFile, -SizeOf(QuestionAnswer), NULL, FILE_CURRENT)
result = WriteFile(hFile, @qa, SizeOf(QuestionAnswer), @BytesCount, 0)
Exit Do
End If
QuestionCount += 1
result = ReadFile(hFile, @qa, SizeOf(QuestionAnswer), @BytesCount, 0)
Loop
CloseHandle(hFile)
End If
End Sub