-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNewsReader.txt
388 lines (341 loc) · 11.5 KB
/
NewsReader.txt
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "\lib\Ribose\Common.txt"
#include "\lib\Ribose\BncsConstants.txt"
#include "\lib\Ribose\Settings.txt"
Option Explicit
' script information
Script("Name") = "NewsReader"
Script("Author") = "Ribose"
Script("Major") = 1
Script("Minor") = 3
Script("Revision") = 0
Private News, NewsRequested, FormVisible, SaveFormDimensions, Product
' script events
' - load
Sub Event_Load()
Dim Motd
' create menu item
CreateObj "Menu", "mnuNews"
mnuNews.Caption = "Show Battle.net News"
' create form
CreateObj "Form", "NewsForm"
' variables
Set News = CreateObject("Scripting.Dictionary")
NewsRequested = False
FormVisible = False
SaveFormDimensions = True
If BotVars.Product = "3RAW" Or BotVars.Product = "PX3W" Then
Product = "WARCRAFT3"
ElseIf BotVars.Product = "VD2D" Or BotVars.Product = "PX2D" Then
Product = "DIABLO2"
Else
Product = "OTHER"
End If
' populate news dictionary
RetrieveNewsItems
If IsOnline() Then
If Not NewsRequested Then
If NewsForm.Caption = "Battle.net News" Then NewsForm.GetObjByName("rtbNews").Text = vbNullString
DisplayStoredItems
Motd = Replace(GetConfigEntry(Product, "Motd", GetWorkingDirectory() & "BnetNews.ini"), "\n", vbCrLf)
If Len(Motd) > 0 Then ShowNewsItem 0, Motd, True
End If
End If
End Sub
' - packet received
Sub Event_PacketReceived(Protocol, ID, Length, Data)
If Protocol = "BNCS" Then
Select Case ID
Case SID_AUTH_INFO
NewsRequested = False
If BotVars.Product = "3RAW" Or BotVars.Product = "PX3W" Then
Product = "WARCRAFT3"
ElseIf BotVars.Product = "VD2D" Or BotVars.Product = "PX2D" Then
Product = "DIABLO2"
Else
Product = StrReverse(BotVars.Product)
End If
Case SID_ENTERCHAT
If Not NewsRequested Then
NewsRequested = True
If NewsForm.Caption = "Battle.net News" Then NewsForm.GetObjByName("rtbNews").Text = vbNullString
DisplayStoredItems
RequestNews
End If
Case SID_NEWS_INFO
HandleNews Mid(Data, 5)
End Select
End If
End Sub
' object events
' - mnuNews clicked
Sub mnuNews_Click()
ToggleNewsForm
End Sub
' - NewsForm resized
Sub NewsForm_Resize()
Dim Width, Height
With NewsForm
Width = .ScaleWidth
Height = .ScaleHeight
With .GetObjByName("rtbNews")
.Width = Width
If Height > 300 Then .Height = Height - 300
End With
With .GetObjByName("btnClose")
.Top = Height - 300
.Width = Width
End With
If SaveFormDimensions Then
WriteSettingsEntry "FormWidth", .Width
WriteSettingsEntry "FormHeight", .Height
End If
End With
End Sub
' - NewsForm hidden or unloaded
Sub NewsForm_Unload(Cancel)
FormHidden
End Sub
' - NewsForm form events
' - - btnClose clicked
Sub NewsForm_btnClose_Click()
HideNewsForm
End Sub
' custom functions
' - show form
Sub ShowNewsForm()
CheckAndLoadNewsForm
NewsForm.Show
FormVisible = True
mnuNews.Caption = "Hide Battle.net News"
End Sub
' - loads the form if it is not loaded
Sub CheckAndLoadNewsForm()
With NewsForm
If .Caption <> "Battle.net News" Then
LoadNewsForm
End If
End With
End Sub
' - loads the form
Sub LoadNewsForm()
With NewsForm
With .CreateObj("RichTextBox", "rtbNews")
.BackColor = &H000000
.Font = "Verdana"
.Locked = True
.TabIndex = 0
.Text = vbNullString
.Left = 0
.Top = 0
End With
With .CreateObj("Button", "btnClose")
.Cancel = True
.Caption = "Close"
.TabIndex = 1
.Left = 0
.Height = 300
End With
SaveFormDimensions = False
.Width = GetNumericSettingsEntry("FormWidth", 8000)
.Height = GetNumericSettingsEntry("FormHeight", 5000)
SaveFormDimensions = True
.Caption = "Battle.net News"
End With
End Sub
' - hide form
Sub HideNewsForm()
NewsForm.Hide
FormHidden
End Sub
' - show form if not visible; hide form if visible
Sub ToggleNewsForm()
If FormVisible Then
HideNewsForm
Else
ShowNewsForm
End If
End Sub
' - form is hidden
Sub FormHidden()
FormVisible = False
mnuNews.Caption = "Show Battle.net News"
End Sub
' - store news item
Sub StoreNewsItem(ByVal ItemStamp, ByVal ItemText)
Dim Count, LastNews, EndLoop, I, Num, Eli
Count = GetConfigEntry(Product, "Count", GetWorkingDirectory() & "BnetNews.ini")
If IsNumeric(Count) Then
Count = CInt(Count) + 1
Else
Count = 1
End If
WriteConfigEntry Product, "Count", Count, GetWorkingDirectory() & "BnetNews.ini"
WriteConfigEntry Product, "News" & Count & "Stamp", ItemStamp, GetWorkingDirectory() & "BnetNews.ini"
If Len(ItemText) > 200 Then
EndLoop = Len(ItemText) \ 200
For I = 0 To EndLoop
If I = 0 Then
Num = vbNullString
Eli = Chr(133)
ElseIf I = EndLoop Then
Num = CStr(I + 1)
Eli = vbNullString
Else
Num = CStr(I + 1)
Eli = Chr(133)
End If
WriteConfigEntry Product, "News" & Count & "Text" & Num, Mid(ItemText, (200 * I) + 1, 200) & Eli, GetWorkingDirectory() & "BnetNews.ini"
Next
Else
WriteConfigEntry Product, "News" & Count & "Text", ItemText, GetWorkingDirectory() & "BnetNews.ini"
End If
LastNews = GetConfigEntry(Product, "LastNews", GetWorkingDirectory() & "BnetNews.ini")
If IsNumeric(LastNews) Then
LastNews = Clng(LastNews)
Else
LastNews = 0
End If
If LastNews < ItemStamp Then WriteConfigEntry Product, "LastNews", ItemStamp, GetWorkingDirectory() & "BnetNews.ini"
End Sub
' - gets the news stored in file
Sub RetrieveNewsItems()
Dim Count, I, ItemStamp, ItemText, J
Count = GetConfigEntry(Product, "Count", GetWorkingDirectory() & "BnetNews.ini")
If IsNumeric(Count) Then
If Count > 0 Then
For I = 1 To Count
ItemStamp = GetConfigEntry(Product, "News" & I & "Stamp", GetWorkingDirectory() & "BnetNews.ini")
ItemText = GetConfigEntry(Product, "News" & I & "Text", GetWorkingDirectory() & "BnetNews.ini")
J = 1
Do While Right(ItemText, 1) = Chr(133)
J = J + 1
ItemText = Left(ItemText, Len(ItemText) - Len(Chr(133)))
ItemText = ItemText & GetConfigEntry(Product, "News" & I & "Text" & J, GetWorkingDirectory() & "BnetNews.ini")
Loop
If IsNumeric(ItemStamp) And Len(ItemText) > 0 Then
News(ItemStamp) = ItemText
End If
Next
End If
End If
End Sub
' - displays the news stored in file
Sub DisplayStoredItems()
Dim ItemStamp, ItemText
For Each ItemStamp In News.Keys()
ItemText = News(ItemStamp)
ShowNewsItem ItemStamp, ItemText, True
Next
End Sub
' - prints a news item -- this is a custom AddChat-like method
Sub PrintNewsItem(Header, Body, UnixTimestamp, IsNew)
Dim ItemStart
With NewsForm.GetObjByName("rtbNews")
If UnixTimestamp > 0 Then
ItemStart = FindLocationBefore(UnixTimestamp)
Else
ItemStart = Len(.Text)
End If
.SelStart = ItemStart
.SelLength = 0
.SelBold = True
.SelColor = vbWhite
.SelIndent = 0
.SelHangingIndent = 200
.SelText = vbCrLf & Header
If IsNew Then
.SelColor = vbRed
.SelText = " [NEW]"
End If
.SelColor = 0
.SelText = " TS=" & UnixTimestamp & vbCrLf
.SelBold = False
.SelColor = 57599
.SelIndent = 200
.SelHangingIndent = 0
.SelText = Replace(Replace(Body, vbLf, vbCrLf), vbCr & vbCrLf, vbCrLf)
If UnixTimestamp <> 0 Then .SelText = vbCrLf
.SelStart = Len(.Text)
End With
End Sub
' - returns the location where a news item should be added to the form based on its UNIX timestamp
Function FindLocationBefore(UnixTimestamp)
Dim Pos, TSStart, TSEnd, TSLen, TS
With NewsForm.GetObjByName("rtbNews")
Pos = 1
Do
Pos = InStr(Pos + 1, .Text, " TS=")
If Pos > 0 Then
TSStart = Pos + 4
TSEnd = InStr(Pos + 1, .Text, vbCrLf)
TSLen = TSEnd - TSStart
TS = Mid(.Text, TSStart, TSLen)
If IsNumeric(TS) Then
TS = CLng(TS)
If Clng(UnixTimestamp) < TS Then
FindLocationBefore = InStrRev(.Text, vbCrLf, Pos - 30)
If FindLocationBefore > 0 Then FindLocationBefore = FindLocationBefore - 1
Exit Function
End If
End If
End If
Loop Until Pos = 0
FindLocationBefore = Len(.Text)
End With
End Function
' - request SID_NEWS_INFO
Sub RequestNews()
Dim LastNews, Packet
LastNews = GetConfigEntry(Product, "LastNews", GetWorkingDirectory() & "BnetNews.ini")
If IsNumeric(LastNews) Then
LastNews = CLng(LastNews)
Else
LastNews = 0
End If
Set Packet = DataBufferEx()
With Packet
.InsertDWORD LastNews ' (DWORD) Last news timestamp
.SendPacket SID_NEWS_INFO
.Clear
End With
Set Packet = Nothing
End Sub
' - parse SID_NEWS_INFO
Sub HandleNews(Data)
Dim Packet, Count, LastLogon, Oldest, Newest, I, ItemStamp, ItemText
Set Packet = DataBufferEx()
With Packet
.Data = Data
Count = .GetBYTE() ' (BYTE) Number of entries
LastLogon = .GetDWORD() ' (DWORD) Last logon timestamp
Oldest = .GetDWORD() ' (DWORD) Oldest news timestamp
Newest = .GetDWORD() ' (DWORD) Newest news timestamp
For I = 1 To Count
ItemStamp = .GetDWORD() ' (DWORD) News item timestamp
ItemText = .GetString() ' (STRING) News item text
ItemText = Replace(ItemText, "â", "'")
ItemText = Replace(ItemText, "â", "")
ItemText = Replace(ItemText, "â", "")
News(ItemStamp) = ItemText
ShowNewsItem ItemStamp, ItemText, False
Next
End With
Set Packet = Nothing
End Sub
' - shows an item in the news form
Sub ShowNewsItem(ItemStamp, ItemText, Exists)
CheckAndLoadNewsForm
If ItemStamp = 0 Then
PrintNewsItem "Message of the Day", ItemText, 0, False
If Not Exists Then
WriteConfigEntry Product, "Motd", Replace(ItemText, vbLf, "\n"), GetWorkingDirectory() & "BnetNews.ini"
If GetBooleanSettingsEntry("ShowNewsOnConnect", False) Then ShowNewsForm
End If
Else
PrintNewsItem GetDateString(UtcTimeToLocalTime(DateAdd("s", ItemStamp, #1/1/1970#))), ItemText, ItemStamp, Not Exists
If Not Exists Then
StoreNewsItem ItemStamp, ItemText
If GetBooleanSettingsEntry("ShowNewsOnNewItem", True) Then ShowNewsForm
End If
End If
End Sub