-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
executable file
·1273 lines (1084 loc) · 58.1 KB
/
main.py
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
"""
Copyright 2011, Dipesh Amin <[email protected]>
Copyright 2011, Stefan Beller <[email protected]>
This file is part of tradey, a trading bot in The Mana World
see www.themanaworld.org
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
Additionally to the GPL, you are *strongly* encouraged to share any modifications
you do on these sources.
"""
import logging
import logging.handlers
import socket
import sys
import time
import string
try:
import config
except:
print "no config file found. please move config.py.template to config.py and edit to your needs!"
sys.exit(0);
from being import *
from net.packet import *
from net.protocol import *
from net.packet_out import *
from player import *
import tradey
import utils
import eliza
from onlineusers import SqliteDbManager
from ircbot import IRCBot
from sdnotify import SystemdNotifier
chatbot = eliza.eliza()
shop_broadcaster = utils.Broadcast()
trader_state = utils.TraderState()
ItemDB = utils.ItemDB()
player_node = Player('')
beingManager = BeingManager()
user_tree = tradey.UserTree()
sale_tree = tradey.ItemTree()
ItemLog = utils.ItemLog()
logger = logging.getLogger('ManaLogger')
db_manager = SqliteDbManager(config.sqlite3_dbfile)
ircbot = IRCBot()
sd = SystemdNotifier()
# How long to wait between WATCHDOG=1 systemd keepalives, in seconds
sd_min_keepalive_rate = 5
def process_whisper(nick, msg, mapserv):
msg = filter(lambda x: x in utils.allowed_chars, msg)
if len(msg) == 0:
return
# Infinite chat loop anyone?
if nick == "guild":
return
user = user_tree.get_user(nick)
broken_string = msg.split()
if len(broken_string) == 0:
return
if user != -10:
if int(user.get("accesslevel")) == -1: # A user who has been blocked for abuse.
if int(user.get("used_stalls")) == 0 and int(user.get("money")) == 0:
mapserv.sendall(whisper(nick, "You can no longer use the bot. If you feel this is in error, please contact" + config.admin))
return
allowed_commands = ['!money', '!help', '!getback', '!info' ]
if not broken_string[0] in allowed_commands:
mapserv.sendall(whisper(nick, "Your access level has been set to blocked! If you feel this is in error, please contact" + config.admin))
mapserv.sendall(whisper(nick, "Though, you still can do the following: "+str(allowed_commands)))
return
if msg == "!list":
# Sends the list of items for sale.
if len(sale_tree.root) != 0:
mapserv.sendall(whisper(nick, "The following items are on sale:"))
else:
mapserv.sendall(whisper(nick, "No items for sale."))
for elem in sale_tree.root:
if time.time() - float(elem.get('add_time')) < config.relist_time: # Check if an items time is up.
msg = "[selling] [" + elem.get("uid") + "] " + elem.get("amount") + " [@@" + \
elem.get("itemId") + "|" + ItemDB.getItem(int(elem.get("itemId"))).name + "@@] for " + elem.get("price") + "gp each"
mapserv.sendall(whisper(nick, msg))
elif broken_string[0] == '!selllist':
# Support for 4144's shop (Sell list)
data = '\302\202B1'
for elem in sale_tree.root:
if time.time() - float(elem.get('add_time')) < config.relist_time:
data += utils.encode_str(int(elem.get("itemId")), 2)
data += utils.encode_str(int(elem.get("price")), 4)
data += utils.encode_str(int(elem.get("amount")), 3)
mapserv.sendall(whisper(nick, data))
elif broken_string[0] == '!buyitem':
# 4144 buy command
if len(broken_string) == 4:
if broken_string[1].isdigit() and broken_string[2].isdigit() and broken_string[3].isdigit():
# Traditional 4144 shop.
item_id = int(broken_string[1])
price = int(broken_string[2])
amount = int(broken_string[3])
for elem in sale_tree.root:
if int(elem.get('amount')) >= amount and int(elem.get('price')) == price and int(elem.get('itemId')) == item_id:
process_whisper(nick, '!buy ' + str(amount) + " " + elem.get('uid'), mapserv)
return
mapserv.sendall(whisper(nick, "Item not found. Please check and try again."))
else:
mapserv.sendall(whisper(nick, "Syntax incorrect"))
elif msg == "!info":
# Send information related to a player.
if user == -10:
mapserv.sendall(whisper(nick, "Your current access level is 0. Request access in [@@https://forums.themanaworld.org/viewtopic.php?f=14&t=14010|ManaMarket's forum thread@@]"))
elif int(user.get('accesslevel')) > 0:
mapserv.sendall(whisper(nick, "Your current access level is " + user.get('accesslevel') + "."))
items_for_sale = False
for elem in sale_tree.root:
if elem.get('name') == nick:
if time.time() - float(elem.get('add_time')) > config.relist_time:
msg = "[expired] ["
else:
msg = "[selling] ["
msg += elem.get("uid") + "] " + elem.get("amount") + " [@@" + elem.get("itemId") + "|" + \
ItemDB.getItem(int(elem.get("itemId"))).name + "@@] for " + elem.get("price") + "gp each"
if items_for_sale == False:
mapserv.sendall(whisper(nick, "Your have the following items for sale:"))
items_for_sale = True
mapserv.sendall(whisper(nick, msg))
if items_for_sale == False:
mapserv.sendall(whisper(nick, "You have no items for sale."))
money = int(user.get('money'))
mapserv.sendall(whisper(nick, "You have " + str(money) + "gp to collect."))
stall_msg = "You have " + str(int(user.get('stalls')) - int(user.get('used_stalls'))) + " free slots."
mapserv.sendall(whisper(nick, stall_msg))
elif broken_string[0] == "!help":
# Sends help information
if len(broken_string) == 1:
mapserv.sendall(whisper(nick, "Welcome to ManaMarket!"))
mapserv.sendall(whisper(nick, "The basic commands for the bot are: !list, !find <id> or <Item Name>, !buy <amount> <uid>, !add <amount> <price> <Item Name>, !money, !relist <uid>, !info, !getback <uid> "))
mapserv.sendall(whisper(nick, "For a detailed description of each command, type !help <command> e.g. !help !buy"))
mapserv.sendall(whisper(nick, "For example to purchase an item shown in the list as:"))
mapserv.sendall(whisper(nick, "[selling] [6] 5 [@@640|Iron Ore@@] for 1000gp each"))
mapserv.sendall(whisper(nick, "you would type /whisper ManaMarket !buy 1 6" ))
mapserv.sendall(whisper(nick, "This will purchase one of item 6 (Iron Ore)."))
if user != -10:
if int(user.get('accesslevel')) >= 5:
mapserv.sendall(whisper(nick,"---"))
mapserv.sendall(whisper(nick, "Ah, you have sellers access level. How lovely!")) # the first words the ticket seller told me when i was in london for the first time. How lovely!
mapserv.sendall(whisper(nick, "Use !add to tell me which items I should trade for you:"))
mapserv.sendall(whisper(nick, "For example !add 10 1000 Iron Ore would tell me to sell 10 [@@640|Iron Ore@@] for a price of 1000 gp"))
mapserv.sendall(whisper(nick, "Later you can whisper me !money to get back your money. In the example given, I'd give you 10*1000 = 10000gp"))
mapserv.sendall(whisper(nick, "When you just want to know, which items you have given me or how much money I have for you can whisper me !info"))
mapserv.sendall(whisper(nick,"If you want to get back an unsold item, whisper me !getback <uid>"))
if int(user.get('accesslevel')) == 20:
mapserv.sendall(whisper(nick, "You're my master! How should I serve you?"))
mapserv.sendall(whisper(nick, "You also have access to the following commands: !listusers, !setslots <slots> <name>, !setaccess <access level > <name>, !adduser <access level> <slot> <name>"))
elif len(broken_string) == 2:
if broken_string[1] == '!buy':
mapserv.sendall(whisper(nick, "!buy <amount> <uid> - Request the purchase of an item or items."))
elif broken_string[1] == '!list':
mapserv.sendall(whisper(nick, "!list - Displays a list of all items for sale."))
elif broken_string[1] == '!find':
mapserv.sendall(whisper(nick, "!find <id> or <Item Name> - Simple search to locate an item."))
elif broken_string[1] == '!add':
mapserv.sendall(whisper(nick, "!add <amount> <price> <Item Name> - Add an item to the sell list (requires that you have an account)."))
elif broken_string[1] == '!money':
mapserv.sendall(whisper(nick, "!money - Allows you to collect money for any sales made on your behalf."))
elif broken_string[1] == '!relist':
mapserv.sendall(whisper(nick, "!relist <uid> - Allows you to relist an item which has expired."))
elif broken_string[1] == '!info':
mapserv.sendall(whisper(nick, "!info - Displays basic information about your account."))
elif broken_string[1] == '!getback':
mapserv.sendall(whisper(nick, "!getback <uid> - Allows you to retrieve an item that has expired or you no longer wish to sell."))
elif broken_string[1] == '!lastseen':
mapserv.sendall(whisper(nick, "!lastseen <nick> - Show when <nick> was online the last time."))
elif broken_string[1] == '!mail':
mapserv.sendall(whisper(nick, "!mail <nick> <message> - Send a message to <nick>."))
elif broken_string[1] == '!irc':
mapserv.sendall(whisper(nick, "!irc <on|off> - Enable/disable IRC mode (the channel is also bridged to Discord)."))
elif user != -10:
if int(user.get('accesslevel')) >= 10 and broken_string[1] == '!listusers':
mapserv.sendall(whisper(nick, "!listusers - Lists all users which have a special accesslevel, e.g. they are blocked, seller or admin"))
elif int(user.get('accesslevel')) >= 10 and broken_string[1] == '!adduser':
mapserv.sendall(whisper(nick, "!adduser <access level> <slots> <name> - Add a user to the bot, a seller should be added with access level 5."))
elif int(user.get('accesslevel')) == 20 and broken_string[1] == '!setslots':
mapserv.sendall(whisper(nick, "!setslots <slots> <name> - Sets the number of slots available to a given user."))
elif int(user.get('accesslevel')) == 20 and broken_string[1] == '!setaccess':
mapserv.sendall(whisper(nick, "!setaccess <access level> <name> - Sets access level for the player: -1 is blocked, 5 is seller and 20 is admin" ))
elif msg == "!money":
# Trades any money earned through item sales.
if user == -10:
mapserv.sendall(whisper(nick, "You don't have the correct permissions."))
return
amount = int(user.get('money'))
if amount == 0:
mapserv.sendall(whisper(nick, "You have no money to collect."))
else:
if not trader_state.Trading.testandset():
mapserv.sendall(whisper(nick, "I'm currently busy with a trade. Try again shortly"))
return
trader_state.money = nick
player_id = beingManager.findId(nick)
if player_id != -10:
mapserv.sendall(trade_request(player_id))
trader_state.timer = time.time()
else:
mapserv.sendall(whisper(nick, "Where are you?!? I can't trade with somebody who isn't here!"))
trader_state.reset()
elif broken_string[0] == "!find":
# Returns a list of items, with the corresponding Item Id - !find <id> or <item name>.
if len(broken_string) < 2:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
return
items_found = False
item = " ".join(broken_string[1:]) # could be an id or an item name
if item.isdigit(): # an id
for elem in sale_tree.root:
if ((time.time() - float(elem.get('add_time'))) < config.relist_time) \
and int(elem.get("itemId")) == int(item): # Check if an items time is up.
msg = "[selling] [" + elem.get("uid") + "] " + elem.get("amount") + " [@@" + elem.get("itemId") + "|" \
+ ItemDB.getItem(int(elem.get("itemId"))).name + "@@] for " + elem.get("price") + "gp each"
mapserv.sendall(whisper(nick, msg))
items_found = True
else: # an item name
for elem in sale_tree.root:
if ((time.time() - float(elem.get('add_time'))) < config.relist_time) \
and item.lower() in ItemDB.getItem(int(elem.get("itemId"))).name.lower(): # Check if an items time is up.
msg = "[selling] [" + elem.get("uid") + "] " + elem.get("amount") + " [@@" + elem.get("itemId") + "|" \
+ ItemDB.getItem(int(elem.get("itemId"))).name + "@@] for " + elem.get("price") + "gp each"
mapserv.sendall(whisper(nick, msg))
items_found = True
if not items_found:
mapserv.sendall(whisper(nick, "Item not found."))
elif msg == '!tradestate':
# Admin command - return trade state.
if user == -10:
return
if int(user.get("accesslevel")) != 20:
mapserv.sendall(whisper(nick, "You don't have the correct permissions."))
return
if trader_state.Trading.test():
mapserv.sendall(whisper(nick, "I'm busy with a trade."))
else:
mapserv.sendall(whisper(nick, "I'm free."))
elif broken_string[0] == '!identify':
if user == -10:
mapserv.sendall(whisper(nick, "You don't have the correct permissions."))
return
elif len(broken_string) != 2:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
return
elif int(user.get("accesslevel")) < 10:
mapserv.sendall(whisper(nick, "You don't have the correct permissions."))
return
if broken_string[1].isdigit():
uid = int(broken_string[1])
item_info = sale_tree.get_uid(uid)
if item_info == -10:
mapserv.sendall(whisper(nick, "Item not found. Please check the uid number and try again."))
return
weight = ItemDB.item_names[int(item_info.get('itemId'))].weight*int(item_info.get("amount"))
mapserv.sendall(whisper(nick, "That item/s belongs to: "+item_info.get("name")))
mapserv.sendall(whisper(nick, "The weight used is: "+str(weight)+"/"+str(player_node.MaxWEIGHT)))
elif msg == '!listusers':
# Admin command - shows a list of all user.
if user == -10:
return
if int(user.get("accesslevel")) < 10:
mapserv.sendall(whisper(nick, "You don't have the correct permissions."))
return
data = ''
total_money = 0
total_slots_reserved = 0
total_slots_used = 0
no_users = 0
for user in user_tree.root:
no_users += 1
name = user.get('name')
accesslevel = user.get('accesslevel')
slots = user.get('stalls')
total_slots_reserved += int(slots)
used_slots = user.get('used_stalls')
total_slots_used += int(used_slots)
money = user.get('money')
total_money += int(money)
data += name+" ("+accesslevel+") "+used_slots+"/"+slots+" "+money+'gp, '
# Format ManaMarket (20) 2/5 100000gp,
if len(data) > 400:
mapserv.sendall(whisper(nick, data[0:len(data)-2]+"."))
data = ''
if len(data) > 0:
mapserv.sendall(whisper(nick, data[0:len(data)-2]+"."))
mapserv.sendall(whisper(nick,"Number of users:"+str(no_users)+ ", Sale slots used: "+ \
str(total_slots_used)+"/"+str(total_slots_reserved)+ ", Total Money: "+str(total_money)+\
", Char slots used: "+str(len(player_node.inventory))+", Weight Used: "+\
str(player_node.WEIGHT)+"/"+str(player_node.MaxWEIGHT)))
elif broken_string[0] == '!setslots':
# Change the number of slots a user has - !setslots <slots> <name>
if user == -10:
return
if int(user.get("accesslevel")) != 20:
mapserv.sendall(whisper(nick, "You don't have the correct permissions."))
return
if len(broken_string) < 3:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
return
if broken_string[1].isdigit():
slot = int(broken_string[1])
name = " ".join(broken_string[2:])
user_info = user_tree.get_user(name)
if user_info == -10:
mapserv.sendall(whisper(nick, "User not found, check and try again."))
return
user_tree.get_user(name).set('stalls', str(slot))
mapserv.sendall(whisper(nick, "Slots changed: "+name+" "+str(slot)))
tradey.saveData("User: "+name+", Slots changed: "+str(slot))
user_tree.save()
else:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
elif broken_string[0] == '!setaccess':
# Change someones access level - !setaccess <access level> <name>
if user == -10:
return
if int(user.get("accesslevel")) != 20:
mapserv.sendall(whisper(nick, "You don't have the correct permissions."))
return
if len(broken_string) < 3:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
return
if (broken_string[1][0] == '-' and broken_string[1][1:].isdigit()) or broken_string[1].isdigit():
accesslevel = int(broken_string[1])
name = " ".join(broken_string[2:])
user_info = user_tree.get_user(name)
if user_info == -10:
mapserv.sendall(whisper(nick, "User not found, check and try again."))
return
if int(user_info.get('accesslevel')) < int(user.get("accesslevel")) and accesslevel <= int(user.get("accesslevel")):
user_tree.get_user(name).set('accesslevel', str(accesslevel))
mapserv.sendall(whisper(nick, "Access level changed:"+name+ " ("+str(accesslevel)+")."))
user_tree.save()
tradey.saveData("User: "+name+", Set Access Level: "+str(accesslevel))
else:
mapserv.sendall(whisper(nick, "You don't have the correct permissions."))
return
else:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
elif broken_string[0] == "!adduser":
# A command to give a user access to the bot - !adduser <access level> <stall> <player name>.
if user == -10:
return
if int(user.get("accesslevel")) < 10:
mapserv.sendall(whisper(nick, "You don't have the correct permissions."))
return
if len(broken_string) < 3:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
return
if broken_string[1].isdigit() and broken_string[2].isdigit():
if int(broken_string[1]) > user.get("accesslevel"):
mapserv.sendall(whisper(nick, "You can't give someone a higher accesslevel than your own."))
return
al = int(broken_string[1])
stalls = int(broken_string[2])
player_name = " ".join(broken_string[3:])
pl_user = user_tree.get_user(player_name)
if pl_user == -10:
user_tree.add_user(player_name, stalls, al)
mapserv.sendall(whisper(nick, "User Added with " + str(stalls) + " slots."))
tradey.saveData("User Added: "+player_name+", Slots: "+str(stalls)+", Access Level: "+str(al))
else:
pl_user.set("accesslevel", str(al))
pl_user.set("stalls", str(stalls))
mapserv.sendall(whisper(nick, "User Added with " + str(stalls) + " slots."))
tradey.saveData("User Added: "+player_name+", Slots: "+str(stalls)+", Access Level: "+str(al))
else:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
elif broken_string[0] == "!add":
# Allows a player with the correct permissions to add an item for sale - !add <amount> <price> <item name>
if user == -10:
mapserv.sendall(whisper(nick, "You are unable to add items. Request access in [@@https://forums.themanaworld.org/viewtopic.php?f=14&t=14010|ManaMarket's forum thread@@]"))
return
if len(broken_string) < 3:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
return
if int(user.get("accesslevel")) < 5:
mapserv.sendall(whisper(nick, "You are unable to add items."))
return
if int(user.get("used_stalls")) >= int(user.get("stalls")):
mapserv.sendall(whisper(nick, "You have no free slots. You may remove an item or wait for something to be sold."))
return
if broken_string[1].isdigit() and broken_string[2].isdigit():
amount = int(broken_string[1])
price = int(broken_string[2])
item_name = " ".join(broken_string[3:])
item_id = ItemDB.findId(item_name)
weight = ItemDB.item_names[item_id].weight*amount
if item_id == -10:
mapserv.sendall(whisper(nick, "Item not found, check spelling."))
return
elif item_id in config.nosell:
mapserv.sendall(whisper(nick, "That item can't be added to ManaMarket, as its too heavy."))
return
elif int(weight) + player_node.WEIGHT > player_node.MaxWEIGHT:
mapserv.sendall(whisper(nick, "I've not got enough room left to carry those. Please try again later. "))
return
elif ItemDB.item_names[item_id].weight > 10 and amount > 150:
mapserv.sendall(whisper(nick, "Sorry, as each of those items weighs more than 10g you can only add a maximum quantity of 150."))
return
if amount > 1 and ItemDB.getItem(item_id).type != 'equip-ammo' and 'equip' in ItemDB.getItem(item_id).type:
mapserv.sendall(whisper(nick, "You can only add one piece of equipment per slot."))
return
elif price == 0 or price > 50000000:
mapserv.sendall(whisper(nick, "Please use a valid price between 1-50000000gp."))
return
elif amount == 0:
mapserv.sendall(whisper(nick, "You can't add 0 of an item."))
return
item = Item()
item.player = nick
item.get = 1 # 1 = get, 0 = give
item.id = item_id
item.amount = amount
item.price = price
if not trader_state.Trading.testandset():
mapserv.sendall(whisper(nick, "I'm currently busy with a trade. Try again shortly"))
return
trader_state.item = item
player_id = beingManager.findId(nick)
if player_id != -10:
mapserv.sendall(trade_request(player_id))
trader_state.timer = time.time()
else:
mapserv.sendall(whisper(nick, "Where are you?!? I can't trade with somebody who isn't here!"))
trader_state.reset()
else:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
elif broken_string[0] == "!buy":
# Buy a given quantity of an item - !buy <amount> <uid>
if len(broken_string) != 3:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
return
if broken_string[1].isdigit() and broken_string[2].isdigit():
amount = int(broken_string[1])
uid = int(broken_string[2])
item_info = sale_tree.get_uid(uid)
if item_info == -10:
mapserv.sendall(whisper(nick, "Item not found. Please check the uid number and try again."))
return
if amount > int(item_info.get("amount")):
mapserv.sendall(whisper(nick, "I do not have that many."))
return
if item_info.get("name") == nick:
mapserv.sendall(whisper(nick, "You can not buy your own items. To get back the item whisper me !getback "+broken_string[2]))
return
item = Item()
item.get = 0 # 1 = get, 0 = give
item.player = nick
item.id = int(item_info.get("itemId"))
item.uid = uid
item.amount = amount
item.price = int(item_info.get("price"))
if not trader_state.Trading.testandset():
mapserv.sendall(whisper(nick, "I'm currently busy with a trade. Try again shortly"))
return
trader_state.item = item
player_id = beingManager.findId(nick)
if player_id != -10:
mapserv.sendall(trade_request(player_id))
trader_state.timer = time.time()
mapserv.sendall(whisper(nick, "That will be " + str(item.price * item.amount) + "gp."))
else:
mapserv.sendall(whisper(nick, "Where are you?!? I can't trade with somebody who isn't here!"))
trader_state.reset()
else:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
elif broken_string[0] == "!removeuser":
# Remove a user, for whatever reason - !removeuser <player name>
if user == -10:
return
if len(broken_string) < 2:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
return
if int(user.get("accesslevel")) != 20:
mapserv.sendall(whisper(nick, "You don't have the correct permissions."))
return
player_name = " ".join(broken_string[1:])
check = user_tree.remove_user(player_name)
if check == 1:
mapserv.sendall(whisper(nick, "User Removed."))
tradey.saveData("User Removed: "+player_name)
elif check == -10:
mapserv.sendall(whisper(nick, "User removal failed. Please check spelling."))
elif broken_string[0] == "!relist":
# Relist an item which has expired - !relist <uid>.
if user == -10 or len(broken_string) != 2:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
return
if int(user.get("accesslevel")) < 5:
mapserv.sendall(whisper(nick, "You don't have the correct permissions."))
return
if broken_string[1].isdigit():
uid = int(broken_string[1])
item_info = sale_tree.get_uid(uid)
if item_info == -10:
mapserv.sendall(whisper(nick, "Item not found. Please check the uid number and try again."))
return
if item_info.get('name') != nick:
mapserv.sendall(whisper(nick, "That doesn't belong to you!"))
return
time_relisted = int(item_info.get('relisted'))
if int(item_info.get('relisted')) < 3:
sale_tree.get_uid(uid).set('add_time', str(time.time()))
sale_tree.get_uid(uid).set('relisted', str(time_relisted + 1))
sale_tree.save()
mapserv.sendall(whisper(nick, "The item has been successfully relisted."))
user_tree.get_user(nick).set('last_use', str(time.time()))
user_tree.save()
else:
mapserv.sendall(whisper(nick, "This item can no longer be relisted. Please collect it using !getback "+str(uid)+"."))
return
else:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
elif broken_string[0] == "!getback":
# Trade the player back uid, remove from sale_items if trade successful - !getback <uid>.
if user == -10 or len(broken_string) != 2:
mapserv.sendall(whisper(nick, "Syntax incorrect."))
return
if int(user.get("accesslevel")) < 5 and int(user.get("accesslevel")) > 0:
mapserv.sendall(whisper(nick, "You don't have the correct permissions."))
return
if broken_string[1].isdigit():
uid = int(broken_string[1])
item_info = sale_tree.get_uid(uid)
if item_info == -10:
mapserv.sendall(whisper(nick, "Item not found. Please check the uid number and try again."))
return
if item_info.get('name') != nick:
mapserv.sendall(whisper(nick, "That doesn't belong to you!"))
return
item = Item()
item.get = 0
item.player = nick
item.id = int(item_info.get("itemId"))
item.uid = uid
item.amount = int(item_info.get("amount"))
item.price = 0
if not trader_state.Trading.testandset():
mapserv.sendall(whisper(nick, "I'm currently busy with a trade. Try again shortly"))
return
trader_state.item = item
player_id = beingManager.findId(nick)
if player_id != -10:
mapserv.sendall(trade_request(player_id))
trader_state.timer = time.time()
else:
mapserv.sendall(whisper(nick, "Where are you?!? I can't trade with somebody who isn't here!"))
trader_state.reset()
elif broken_string[0] == "!lastseen":
who = msg[10:].strip()
if len(who) == 0:
mapserv.sendall(whisper(nick, "Usage: !lastseen <nick>"))
else:
ls_info = db_manager.get_lastseen_info(who)
mapserv.sendall(whisper(nick, ls_info))
elif broken_string[0] == "!mail":
if user == -10:
mapserv.sendall(whisper(nick, "Your current access level is 0. Request access in [@@https://forums.themanaworld.org/viewtopic.php?f=14&t=14010|ManaMarket's forum thread@@]"))
return
to_, msg_ = utils.parse_mail_cmdargs(msg[6:].strip())
if to_ == "" or msg_ == "":
mapserv.sendall(whisper(nick, "Usage: !mail <nick> <message> OR !mail \"nick with spaces\" <message>"))
else:
db_manager.send_mail(nick, to_, msg_)
mapserv.sendall(whisper(nick, "Message to \"%s\" sent" % (to_)))
elif broken_string[0] == "!irc":
if len(broken_string) < 2:
mapserv.sendall(whisper(nick, "Incorrect syntax."))
return
if broken_string[1] == "on":
if user == -10:
user_tree.add_user(nick, 0, 0)
tradey.saveData("Stub User Added: "+nick+", Slots: 0, Access Level: 0")
user = user_tree.get_user(nick)
user.set("irc", "on")
mapserv.sendall(whisper(nick, "IRC relay mode is now enabled (the channel is also bridged to Discord)."))
elif broken_string[1] == "off":
if user != -10:
user.set("irc", "off")
if int(user.get("accesslevel")) == 0 and int(user.get("stalls")) == 0 and int(user.get("money")) == 0:
user_tree.remove_user(nick)
tradey.saveData("Stub User Removed: "+nick)
mapserv.sendall(whisper(nick, "IRC relay mode is now disabled."))
elif user != -10 and user.get("irc") == "on":
if not ircbot.isAFK(msg): # if not an AFK message
ircbot.send(nick, msg)
db_manager.forEachOnline(broadcast_if_irc_on, nick, "TMW.%s: %s" % (nick, msg))
else:
response = chatbot.respond(msg)
logger.info("Bot Response: "+response)
mapserv.sendall(whisper(nick, response))
#mapserv.sendall(whisper(nick, "Command not recognised, please whisper me !help for a full list of commands."))
def broadcast_from_irc(nick, msg):
db_manager.forEachOnline(broadcast_if_irc_on, "IRC", u"IRC.%s: %s" % (nick, msg))
def broadcast_if_irc_on(pl, sender_nick, msg):
if sender_nick == pl:
return
pl_user = user_tree.get_user(pl)
if pl_user != -10 and pl_user.get("irc") == "on":
mapserv.sendall(whisper(pl, msg.encode("utf-8", "ignore")))
def main():
# Use rotating log files.
log_handler = logging.handlers.RotatingFileHandler('data/logs/activity.log', maxBytes=1048576*3, backupCount=5)
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
log_handler.setFormatter(formatter)
logger.addHandler(log_handler)
logger.info("Bot Started.")
account = config.account
password = config.password
character = config.character
login = socket.socket()
login.connect((config.server, config.port))
logger.info("Login connected")
login_packet = PacketOut(0x0064)
login_packet.write_int32(6) # <= CLIENT VERSION
login_packet.write_string(account, 24)
login_packet.write_string(password, 24)
login_packet.write_int8(0x03); # <= FLAGS
login.sendall(str(login_packet))
pb = PacketBuffer()
id1 = accid = id2 = 0
charip = ""
charport = 0
# Login server packet loop.
while True:
data = login.recv(1500)
if not data:
break
pb.feed(data)
for packet in pb:
if packet.is_type(SMSG_LOGIN_DATA): # login succeeded
packet.skip(2)
id1 = packet.read_int32()
accid = packet.read_int32()
id2 = packet.read_int32()
packet.skip(30)
player_node.sex = packet.read_int8()
charip = utils.parse_ip(packet.read_int32())
charport = packet.read_int16()
login.close()
break
if charip:
break
assert charport
if charip == "127.0.0.1" and config.server != "127.0.0.1":
charip = config.server
char = socket.socket()
char.connect((charip, charport))
logger.info("Char connected")
char_serv_packet = PacketOut(CMSG_CHAR_SERVER_CONNECT)
char_serv_packet.write_int32(accid)
char_serv_packet.write_int32(id1)
char_serv_packet.write_int32(id2)
char_serv_packet.write_int16(1) # this should match MIN_CLIENT_VERSION in tmwa/src/char/char.hpp
char_serv_packet.write_int8(player_node.sex)
char.sendall(str(char_serv_packet))
char.recv(4)
pb = PacketBuffer()
mapip = ""
mapport = 0
# Character Server Packet loop.
while True:
data = char.recv(1500)
if not data:
break
pb.feed(data)
for packet in pb:
if packet.is_type(SMSG_CHAR_LOGIN):
packet.skip(2)
slots = packet.read_int16()
packet.skip(18)
count = (len(packet.data)-22) / 106
for i in range(count):
player_node.id = packet.read_int32()
player_node.EXP = packet.read_int32()
player_node.MONEY = packet.read_int32()
packet.skip(62)
player_node.name = packet.read_string(24)
packet.skip(6)
slot = packet.read_int8()
packet.skip(1)
logger.info("Character information recieved:")
logger.info("Name: %s, Id: %s, EXP: %s, MONEY: %s", \
player_node.name, player_node.id, player_node.EXP, player_node.MONEY)
if slot == character:
break
char_select_packet = PacketOut(CMSG_CHAR_SELECT)
char_select_packet.write_int8(character)
char.sendall(str(char_select_packet))
elif packet.is_type(SMSG_CHAR_MAP_INFO):
player_node.id = packet.read_int32()
player_node.map = packet.read_string(16)
mapip = utils.parse_ip(packet.read_int32())
mapport = packet.read_int16()
char.close()
break
if mapip:
break
assert mapport
if mapip == "127.0.0.1" and charip != "127.0.0.1":
mapip = charip
beingManager.container[player_node.id] = Being(player_node.id, 42)
global mapserv
mapserv = socket.socket()
mapserv.connect((mapip, mapport))
logger.info("Map connected")
mapserv_login_packet = PacketOut(CMSG_MAP_SERVER_CONNECT)
mapserv_login_packet.write_int32(accid)
mapserv_login_packet.write_int32(player_node.id)
mapserv_login_packet.write_int32(id1)
mapserv_login_packet.write_int32(id2)
mapserv_login_packet.write_int8(player_node.sex)
mapserv.sendall(str(mapserv_login_packet))
mapserv.recv(4)
pb = PacketBuffer()
shop_broadcaster.mapserv = mapserv
db_manager.mapserv = mapserv
db_manager.start()
ircbot.broadcastFunc = broadcast_from_irc
ircbot.start()
# Functionality for systemd watchdog keepalives
last_notify = None
def notify_systemd():
global last_sd_notify
sd.notify("WATCHDOG=1")
last_sd_notify = time.time()
return True
notify_systemd()
# Map server packet loop
print "Entering map packet loop\n";
while True:
data = mapserv.recv(2048)
if not data:
break
pb.feed(data)
# If it's been more than five seconds since we last notified systemd that we're still alive, do so now.
if time.time() - last_notify > sd_min_keepalive_rate:
notify_systemd()
# For unfinished trades - one way to distrupt service would be leaving a trade active.
if trader_state.Trading.test():
if time.time() - trader_state.timer > 2*60:
logger.info("Trade Cancelled - Timeout.")
trader_state.timer = time.time()
mapserv.sendall(str(PacketOut(CMSG_TRADE_CANCEL_REQUEST)))
for packet in pb:
if packet.is_type(SMSG_MAP_LOGIN_SUCCESS): # connected
logger.info("Map login success.")
packet.skip(4)
coord_data = packet.read_coord_dir()
player_node.x = coord_data[0]
player_node.y = coord_data[1]
player_node.direction = coord_data[2]
logger.info("Starting Postion: %s %s %s", player_node.map, player_node.x, player_node.y)
mapserv.sendall(str(PacketOut(CMSG_MAP_LOADED))) # map loaded
# A Thread to send a shop broadcast: also keeps the network active to prevent timeouts.
shop_broadcaster.start()
elif packet.is_type(SMSG_PVP_SET):
packet.skip(12)
elif packet.is_type(SMSG_PVP_MAP_MODE):
packet.skip(2)
elif packet.is_type(SMSG_QUEST_SET_VAR):
packet.skip(6)
elif packet.is_type(SMSG_QUEST_PLAYER_VARS):
nb = (packet.read_int16() - 4) / 6
for loop in range(nb):
packet.skip(6)
elif packet.is_type(SMSG_NPC_COMMAND):
packet.skip(14)
elif packet.is_type(SMSG_BEING_MOVE3):
nb = (packet.read_int16() - 14) / 1
packet.skip(10)
for loop in range(nb):
packet.skip(1)
elif packet.is_type(SMSG_MAP_MASK):
packet.skip(8)
elif packet.is_type(SMSG_MAP_MUSIC):
nb = (packet.read_int16() - 4) / 1
for loop in range(nb):
packet.skip(1)
elif packet.is_type(SMSG_NPC_CHANGETITLE):
nb = (packet.read_int16() - 10) / 1
packet.skip(6)
for loop in range(nb):
packet.skip(1)
elif packet.is_type(SMSG_SCRIPT_MESSAGE):
nb = (packet.read_int16() - 5) / 1
packet.skip(1)
for loop in range(nb):
packet.skip(1)
elif packet.is_type(SMSG_PLAYER_CLIENT_COMMAND):
nb = (packet.read_int16() - 4) / 1
for loop in range(nb):
packet.skip(1)
elif packet.is_type(SMSG_MAP_SET_TILES_TYPE):
packet.skip(32)
elif packet.is_type(SMSG_PLAYER_HP):
packet.skip(8)
elif packet.is_type(SMSG_PLAYER_HP_FULL):
packet.skip(12)
elif packet.is_type(SMSG_WHISPER):
msg_len = packet.read_int16() - 26
nick = packet.read_string(24)
message = packet.read_raw_string(msg_len)
# Clean up the logs.
if nick != 'AuctionBot':
logger.info("Whisper: " + nick + ": " + message)
process_whisper(nick, utils.remove_colors(message), mapserv)
elif packet.is_type(SMSG_PLAYER_STAT_UPDATE_1):
stat_type = packet.read_int16()
value = packet.read_int32()
if stat_type == 0x0018:
logger.info("Weight changed from %s/%s to %s/%s", \
player_node.WEIGHT, player_node.MaxWEIGHT, value, player_node.MaxWEIGHT)
player_node.WEIGHT = value
elif stat_type == 0x0019:
logger.info("Max Weight: %s", value)
player_node.MaxWEIGHT = value
elif packet.is_type(SMSG_PLAYER_STAT_UPDATE_2):
stat_type = packet.read_int16()
value = packet.read_int32()
if stat_type == 0x0014:
logger.info("Money Changed from %s, to %s", player_node.MONEY, value)
player_node.MONEY = value
elif packet.is_type(SMSG_BEING_MOVE) or packet.is_type(SMSG_BEING_VISIBLE)\
or packet.is_type(SMSG_PLAYER_MOVE) or packet.is_type(SMSG_PLAYER_UPDATE_1)\
or packet.is_type(SMSG_PLAYER_UPDATE_2):
being_id = packet.read_int32()
packet.skip(8)
job = packet.read_int16()
if being_id not in beingManager.container:
if job == 0 and id >= 110000000 and (packet.is_type(SMSG_BEING_MOVE)\
or packet.is_type(SMSG_BEING_VISIBLE)):
continue
# Add the being to the BeingManager, and request name.
beingManager.container[being_id] = Being(being_id, job)
requestName = PacketOut(0x0094)
requestName.write_int32(being_id)