-
Notifications
You must be signed in to change notification settings - Fork 15
/
MudDatabase.xml
676 lines (520 loc) · 13.2 KB
/
MudDatabase.xml
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE muclient>
<!-- Saved on Thursday, November 07, 2002, 12:57 PM -->
<!-- MuClient version 3.31 -->
<!-- Plugin "MudDatabase" generated by Plugin Wizard -->
<!--
Amend the start of the script to change the database name or location.
Version 1.1 - added 'setdatabase filename'
Version 1.2 - a) added error handling for errors on queries and sql statements
b) Sort mud list by mud name
Version 1.3 - changed error handling to show exact error reason
Version 1.4 - a) improved error handling (eg. on database open)
b) detect if database exists on 'setdatabase'
c) a bit more modular
-->
<muclient>
<plugin
name="MudDatabase"
author="Nick Gammon"
id="464461cbb3a282dc839f1e5d"
language="VBscript"
purpose="Maintains a database of MUDs, demonstrates using SQL"
date_written="2002-11-07 12:51:24"
date_modified="2002-11-10 14:30"
requires="3.24"
save_state="y"
version="1.4"
>
<description trim="y">
<![CDATA[
This plugin demonstrates accessing a Database from within a plugin.
It uses the Microsoft.Jet.OLEDB.4.0 database provider, which should be installed with default Windows 98 and upwards installations. If it doesn't work, try installing the Jet engine.
Functions provided are:
addmud name ip port description <-- adds a MUD
eg. addmud realms_of_despair game.org 4000 Realms of Despair MUD
deletemud name <-- deletes a MUD from the database by name
eg. deletemud realms_of_despair
listmuds [searchstring] <-- lists MUDs with optional search
eg. listmuds
listmuds realms
sql command <-- issues arbitrary SQL command to the database
eg. sql DELETE FROM muds WHERE port = 4000
query command <-- issues SQL query, displays results
eg. query SELECT * FROM muds WHERE port > 1000 ORDER BY mud_name
setdatabase filename <-- changes to different database file
eg. setdatabase c:\mydatabase.mdb
The plugin attempts to create the database file, and then the muds table, 5 seconds after it is installed. It checks to see if the database is there so it doesn't get created twice.
]]>
</description>
</plugin>
<!-- Aliases -->
<aliases>
<alias
script="AddMud"
match="addmud * * * *"
enabled="y"
>
</alias>
<alias
script="DeleteMud"
match="deletemud *"
enabled="y"
>
</alias>
<alias
script="ListMuds"
match="listmuds"
enabled="y"
>
</alias>
<alias
script="ListMuds"
match="listmuds *"
enabled="y"
>
</alias>
<alias
script="SQLalias"
match="sql *"
enabled="y"
>
</alias>
<alias
script="QueryAlias"
match="query *"
enabled="y"
>
</alias>
<alias
script="SetDatabase"
match="setdatabase *"
enabled="y"
>
</alias>
</aliases>
<!-- Script -->
<script>
<![CDATA[
'
' Author: Nick Gammon <nick@gammon.com.au>
'
' Written: 7th November 2002
'
option explicit
'
' Amend this to change the location or name of the database.
'
' Default is world file directory, mushclient_db.mdb
'
function GetDatabaseFileName
GetDatabaseFileName = _
world.GetVariable ("database")
end function
'
' Central spot for showing errors, so we can easily customise colours
'
sub ShowError (sMessage)
world.ColourNote "white", "red", sMessage
end sub
'
' Central spot for showing information, so we can easily customise colours
'
sub ShowInfo (sMessage)
world.ColourNote "lightblue", "midnightblue", sMessage
end sub
'
' We need the provider (engine, database name) in various
' spots so we make a function to return it.
'
function GetProvider
GetProvider = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & _
GetDatabaseFileName & _
";" & _
"Jet OLEDB:Engine Type=5;"
end function
'
' Helper function to see if a file exists
'
function DoesFileExist (sFileName)
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
DoesFileExist = FSO.FileExists (sFileName)
Set FSO = Nothing
end function
'
' Helper function to see if a table exists in the database
'
function DoesTableExist (sTableName)
dim db, oTable
On Error Resume Next
Set db = CreateObject ("ADOX.Catalog")
If Err.Number <> 0 Then
ShowError Err.Description
Exit Function
End If
db.ActiveConnection = GetProvider
If Err.Number <> 0 Then
ShowError Err.Description
Set db = Nothing
Exit Function
End If
On Error GoTo 0
DoesTableExist = vbFalse
For Each oTable In db.Tables
If UCase(oTable.Name) = UCase(sTableName) Then
DoesTableExist = vbTrue
Exit For
End If
Next
Set db = Nothing
end function
'
' Create database in MUSHclient world file directory
'
sub CreateDatabase
Dim db
'
' Don't create the database twice - so check if file exists
'
if DoesFileExist (GetDatabaseFileName) then
exit sub
end if
'
' Doesn't exist? Create it.
'
Set db = CreateObject ("ADOX.Catalog")
db.Create GetProvider
Set db = Nothing
ShowInfo "Database '" & GetDatabaseFileName & "' created."
end sub
'
' Execute some arbitrary SQL
'
Function DoSQL (sSQL)
dim db
DoSQL = vbTrue ' error return
On Error Resume Next
Set db = CreateObject ("ADODB.Connection")
If Err.Number <> 0 Then
ShowError Err.Description
Exit Function
End If
' Open the connection
db.Open GetProvider
If Err.Number <> 0 Then
ShowError Err.Description
Set db = Nothing
Exit Function
End If
' Execute it
db.Execute sSQL
If Err.Number <> 0 Then
ShowError Err.Description
Set db = Nothing
Exit Function
End If
On Error GoTo 0
' Wrap up
db.Close
Set db = Nothing
DoSQL = vbFalse ' OK return
end Function
'
' Create the table we want
'
sub CreateTable
if DoesTableExist ("muds") then
exit sub
end if
If DoSQL _
("CREATE TABLE muds (" & _
" mud_id int NOT NULL IDENTITY," & _
" mud_name varchar(64) NOT NULL," & _
" ip_address varchar(64) NOT NULL," & _
" port int NOT NULL default '4000'," & _
" description text," & _
" PRIMARY KEY (mud_id)" & _
")") Then Exit Sub
ShowInfo "Table 'muds' created."
end sub
'
' Called 5 seconds after plugin installation to create the
' database and its table, if necessary
'
sub OnSetup (sTimerName)
ShowInfo "Plugin " & world.GetPluginName & " installed."
'
' Don't create databases everywhere once they change the name
'
if world.GetVariable ("database_changed") <> "Y" then
CreateDatabase
CreateTable
end if
ShowInfo "Database is: " & GetDatabaseFileName
end sub
'
' When the plugin is installed we will wait 5 seconds
' and then create the database and table.
'
sub OnPluginInstall
' timer: enabled, one-shot, active-if-not-connected
world.addtimer "", 0, 0, 5, "", 1 + 4 + 32, "OnSetup"
'
' Set up default database name if variable does not exist
'
if IsEmpty (world.GetVariable ("database")) Then
world.SetVariable "database", _
world.GetInfo (57) & "mushclient_db.mdb"
end if
end sub
'
' Since we are doing queries in a few places, we will do the main
' part here ...
' A "true" result means the query failed.
' A "false" (zero) result means the query succeeded
'
Function ExecuteQuery (db, rst, sQuery)
ExecuteQuery = vbTrue ' assume bad result
On Error Resume Next
Set db = CreateObject ("ADODB.Connection")
If Err.Number <> 0 Then
ShowError Err.Description
Exit Function
End If
Set rst = CreateObject ("ADODB.Recordset")
If Err.Number <> 0 Then
ShowError Err.Description
set db = Nothing
Exit Function
End If
' Open the connection
db.Open GetProvider
If Err.Number <> 0 Then
ShowError Err.Description
Set rst = Nothing
Set db = Nothing
Exit Function
End If
' Open the Recordset
rst.Open sQuery, db
If Err.Number <> 0 Then
ShowError Err.Description
Set rst = Nothing
Set db = Nothing
Exit Function
End If
On Error GoTo 0
ExecuteQuery = vbFalse ' good result
End Function
'
' Do some arbitrary query, display the results
'
sub DoQuery (sQuery)
dim db, rst, count, fld
if ExecuteQuery (db, rst, sQuery) Then Exit Sub
count = 0
' display each record
Do Until rst.EOF
count = count + 1
' display each field name
if count = 1 then
For Each fld In rst.Fields
world.ColourTell "white", "darkblue", _
fld.Name & chr(9)
Next
world.note "" ' newline
end if
' display each field
For Each fld In rst.Fields
world.tell fld.Value & chr(9)
Next
world.note "" ' newline
rst.MoveNext
Loop
db.Close
Set rst = Nothing
Set db = Nothing
world.note count & " record(s)"
end sub
'
' Does a query, and returns the first field returned
' eg. select count(*) from muds where mud_name = "foo"
'
function GetOneValue (sQuery)
dim db, rst
if ExecuteQuery (db, rst, sQuery) Then Exit Function
If Not rst.EOF Then
GetOneValue = rst.Fields (0).Value
End If
db.Close
Set rst = Nothing
Set db = Nothing
end function
'
' called from an alias to add a mud to the list
'
sub AddMud (sName, sLine, wildcards)
dim mud_name, ip_address, port, description
mud_name = wildcards (1)
ip_address = wildcards (2)
port = wildcards (3)
description = wildcards (4)
'
' Quotes will throw us out (because the SQL uses them)
'
if Instr (mud_name, """") > 0 or _
Instr (ip_address, """") > 0 or _
Instr (port, """") > 0 or _
Instr (description, """") > 0 Then
ShowError "You cannot use quotes in the mud name/port/ip/description"
exit sub
end if
'
' Check not already there
'
if GetOneValue (_
"select count(*) from muds where mud_name = """ & _
mud_name & _
"""") > 0 Then
ShowError "MUD '" & mud_name & "' is already in the database"
exit sub
end if
'
' Insert it
'
If DoSQL _
("INSERT INTO muds (mud_name, ip_address," & _
"port, description) VALUES (" & _
"""" & mud_name & """, " & _
"""" & ip_address & """, " & _
"""" & port & """, " & _
"""" & description & """ );") Then Exit Sub
world.ColourNote "white", "green", "MUD '" & mud_name & _
"' added to the database"
end sub
'
' called from an alias to delete a mud from the list
'
sub DeleteMud (sName, sLine, wildcards)
dim mud_name
mud_name = wildcards (1)
'
' Quotes will throw us out (because the SQL uses them)
'
if Instr (mud_name, """") > 0 Then
ShowError "You cannot use quotes in the mud name"
exit sub
end if
'
' Check already there
'
if not GetOneValue (_
"select count(*) from muds where mud_name = """ & _
mud_name & _
"""") > 0 Then
ShowError "MUD '" & mud_name & "' is not in the database"
exit sub
end if
'
' Delete it
'
If DoSQL _
("DELETE FROM muds WHERE mud_name = " & _
"""" & mud_name & """ ") Then Exit Sub
world.ColourNote "white", "green", "MUD '" & mud_name & _
"' deleted from the database"
end sub
'
' List the muds in a nice way
'
sub ListMuds (sName, sLine, wildcards)
dim db, rst, count, sQuery
dim mud_name, ip_address, port, description
'
' a wildcard means to match on a subset
'
if wildcards (1) = "" then
sQuery = "SELECT * FROM muds ORDER BY mud_name"
else
sQuery = "SELECT * FROM muds WHERE " & _
"mud_name like ""%" & wildcards (1) & "%"" " & _
"OR ip_address like ""%" & wildcards (1) & "%"" " & _
"OR port like ""%" & wildcards (1) & "%"" " & _
"OR description like ""%" & wildcards (1) & "%"" " & _
"ORDER BY mud_name"
end if
if ExecuteQuery (db, rst, sQuery) Then Exit Sub
count = 0
' display each record
Do Until rst.EOF
count = count + 1
mud_name = rst.Fields ("mud_name").Value
ip_address = rst.Fields ("ip_address").Value
port = rst.Fields ("port").Value
description = rst.Fields ("description").Value
world.ColourTell "white", "darkred", mud_name
world.ColourTell "white", "black", " IP: " & ip_address
world.ColourTell "white", "black", " Port: " & port
world.Note ""
world.ColourNote "silver", "black", description
world.Note ""
rst.MoveNext
Loop
db.Close
Set rst = Nothing
Set db = Nothing
world.note count & " MUD(s)"
end sub
'
' Alias to execute arbitrary SQL
'
' eq. sql drop table muds
'
sub SQLalias (sName, sLine, wildcards)
If DoSQL (wildcards (1)) Then Exit Sub
ShowInfo "SQL statement processed OK."
end sub
'
' Alias to execute arbitrary query
'
' eq. query select * from muds order by port
'
sub QueryAlias (sName, sLine, wildcards)
DoQuery wildcards (1)
end sub
'
' Change to some other database so we can do queries on it
'
sub SetDatabase (sName, sLine, wildcards)
'
' Check database is there
'
if not DoesFileExist (wildcards (1)) then
ShowError "File '" & wildcards (1) & "' does not exist."
exit sub
end if
world.SetVariable "database", wildcards (1)
world.SetVariable "database_changed", "Y"
ShowInfo "Database changed to: " & GetDatabaseFileName
end sub
]]>
</script>
<!-- Plugin help -->
<aliases>
<alias
script="OnHelp"
match="MudDatabase:help"
enabled="y"
>
</alias>
</aliases>
<script>
<![CDATA[
Sub OnHelp (sName, sLine, wildcards)
World.Note World.GetPluginInfo (World.GetPluginID, 3)
End Sub
]]>
</script>
</muclient>