-
Notifications
You must be signed in to change notification settings - Fork 0
/
erklelib.py
2204 lines (1757 loc) · 52.1 KB
/
erklelib.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
# _ _
# | | | |
# ___ _ __| | _| | ___
# / _ \ '__| |/ / |/ _ \
# | __/ | | <| | __/
# \___|_| |_|\_\_|\___|
#
# Erkle IRC Library
# Version 0.0622
#
# https://github.com/nutjob-laboratories/erkle
# MIT License
# Copyright (c) 2019 Dan Hetrick
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import socket
from collections import defaultdict
import string
import sys
import threading
import os
import random
import ipaddress
import urllib.request
SSL_AVAILABLE = True
try:
import ssl
except ImportError:
SSL_AVAILABLE = False
__all__ = ['irc','Erkle','ERKLE_VERSION']
APPLICATION_NAME = "Erkle"
ERKLE_VERSION = "0.0622"
DEFAULT_REALNAME = APPLICATION_NAME + " " + ERKLE_VERSION + " IRC Client"
NO_SSL_ERROR = "SSL/TLS is not available. Please install pyOpenSSL."
DISCONNECTED_ERROR = "Disconnected from the server."
SEND_MESSAGE_ERROR = "Cannot send message: disconnected from the server."
NOT_STARTED_MESSAGE_ERROR = "Connection not started; can't send message '{}'"
NOT_THREADED_ERROR = "Connection is not running in multithread mode! Exiting..."
NOT_STARTED_ERROR = "Connection not started."
WRONG_VARIABLE_TYPE = "Wrong variable type for '{key}' (received {rec}, expected {exp})."
CANNOT_ADD_ASTERIX_TAG = "'*' is not a valid tag name."
CANNOT_REMOVE_ASTERIX_TAG = "Can't remove '*' tag (not a valid tag name)."
CANNOT_BE_CONFIGURED = "Can't call configure() while connected."
CONNECTION_IS_STARTED = "Can't call connect(): already connected to an IRC server."
NO_KEY_ERROR = "Client certificate '{}' is missing its key."
CANNOT_FIND_SSL_FILE = "Can't find {ftype} file '{name}'."
UNKNOWN_ERROR = "Unknown error"
class Event:
def __init__(self,func,tags):
self.function = func
self.tags = tags
class EventHandler:
def __init__(self):
self._handlers = {}
self._disabled = []
def call(self,event,eobj,*args):
if event in self._handlers:
for h in self._handlers[event]:
# h(*args)
# Check to see if the Erkle object has any tags
DOIT = True
if len(eobj.tags)>0:
# See if the hooked function has the Erkle object's tag
DOIT = False
for t in eobj.tags:
# The hooked function has the Erkle object's tag
if t in h.tags: DOIT = True
if '*' in h.tags: DOIT = True
nogo = False
for t in h.tags:
if t in self._disabled: nogo=True
if not nogo:
if DOIT:
retval = h.function(eobj,*args)
# If the event function returns a value, then
# return it here
if retval:
return retval
def event(self, event, *args):
def registerhandler(handler):
tags = []
for t in args:
tags.append(t)
if event in self._handlers:
#self._handlers[event].append(handler)
ef = Event(handler,tags)
self._handlers[event].append(ef)
else:
# self._handlers[event] = [handler]
ef = Event(handler,tags)
self._handlers[event] = [ef]
return handler
return registerhandler
irc = EventHandler()
class Erkle:
# __init__()
# Arguments: string, string, [key words,...]
#
# Initializes an Erkle() object.
def __init__(self,nickname,server,**kwargs):
self.nickname = nickname
self.server = server
self._started = False # Stores if the IRC connection has been started or not
self.port = 6667 # The server's port
self.username = None # User's username
self.alternate = None # User's alternate nick
self.password = None # The server's password
self.usessl = False # Whether to use SSL or not
self.realname = DEFAULT_REALNAME # User's realname
self.encoding = "utf-8" # String encoding to use
self.flood_protection = True # Whether to use flood protection
self.floodrate = 2 # How often to send msgs with flood protection
self._clock_resolution = 1.0 # The tick clock's resolution
self._multithreaded = False # Whether the object is multithreaded or not
self._show_input = False # Whether to display incoming data or not
self._show_output = False # Whether to display outgoing data or not
self._daemon = False # Whether we daemonize the thread or not
self._uptime_resolution = 0.25 # The uptime clock's resolution
self._verify_hostname = False # Whether to verify the server's hostname or not
self._verify_cert = False # Whether to verify the server's certificate or not
self._client_cert = None # The user's client certificate
self._client_key = None # The user's client certificate key
self._cert_authority = None # Certificate authority file
self._fetch_external_ip = False
self.external_ip = "127.0.0.1"
self.encoded_external_ip = int(ipaddress.IPv4Address(self.external_ip))
self._external_ip_source = "http://myexternalip.com/raw"
# Handle configuration options
self.configure(**kwargs)
# Create the socket
self._client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# If SSL isn't available, and SSL is set to
# be used, raise an error
if not SSL_AVAILABLE:
if self.usessl:
self._display_error_and_exit(NO_SSL_ERROR)
self._buffer = "" # Incoming data buffer
self._channels = [] # Channel list buffer
self._thread = None # If multithreaded, stores the object's thread
self._whois = {} # WHOIS data buffer
self._message_queue = [] # Outgoing message queue for flood protection
self._last_message_time = 0 # The time the last message was sent to the server
self._current_nickname = self.nickname # Stores the current nickname (for 433 errors)
self._dcc_chat_sockets = [] # DCC chat socket storage
self._dcc_client_ids = [] # DCC chat client ids in use
self.hostname = "" # The server's hostname
self.software = "" # The server's software
self.options = [] # The server's options
self.network = "" # The IRC server's network
self.commands = [] # Commands the server supports
self.maxchannels = 0 # Maximum number of channels
self.maxnicklen = 0 # Maximum nick length
self.chanlimit = [] # Server channel limit
self.nicklen = 0 # Server nick length
self.chanellen = 0 # Server channel name length
self.topiclen = 0 # Server channel topic length
self.kicklen = 0 # Server kick length
self.awaylen = 0 # Server away length
self.maxtargets = 0 # Server maximum msg targets
self.modes = 0 # Server maximum channel modes
self.chantypes = [] # What channel types the server uses
self.prefix = [] # Server status prefixes
self.chanmodes = [] # What channel modes the server uses
self.casemapping = "" # Server case mapping
self.spoofed = "" # The client's spoofed host
self.users = defaultdict(list) # List of channel users
self.topic = {} # Channel topics
self.channels = [] # Server channel list
self.tags = [] # Object tags
self.uptime = 0 # Object uptime, in seconds
self.multithreaded = self._multithreaded # If the object is multithreaded or not
self.certificate = None # If using SSL/TLS, this is where the cert is stored
# configure()
# Arguments: key words
#
# Configures an Erkle object
def configure(self,**kwargs):
if self._started:
print("ERROR: "+CANNOT_BE_CONFIGURED)
sys.exit(1)
for key, value in kwargs.items():
if key=="external_ip":
self.external_ip = value
self.encoded_external_ip = int(ipaddress.IPv4Address(self.external_ip))
if key=="external_ip_source":
self._external_ip_source = value
if key=="fetch_external_ip":
self._fetch_external_ip = value
if key=="cert_authority":
self._cert_authority = value
if key=="certificate":
self._client_cert = value
if key=="key":
self._client_key = value
if key=="verify_host":
self._verify_hostname = value
if key=="verify_cert":
self._verify_cert = value
if key=="socket":
if value!=None:
global socket
socket = value
if key=='daemon':
self._daemon = value
if key=='debug_output':
self._show_output = value
if key=='debug_input':
self._show_input = value
if key=='multithreaded':
self._multithreaded = value
if key=='tick_frequency':
self._clock_resolution = value
if key=='clock_resolution':
self._uptime_resolution = value
if key=='flood_protection':
self.flood_protection = value
if key=='flood_rate':
self.floodrate = value
if key=='username':
self.username = value
if key=='realname':
self.realname = value
if key=='alternate':
self.alternate = value
if key=='port':
self.port = value
if key=='password':
self.password = value
if key=='ssl':
self.usessl = value
if key=='encoding':
self.encoding = value
if self.alternate==None: self.alternate = self.nickname + "_"
if self.username==None: self.username = self.nickname
# Check to make sure all config input is the right variable type
self._check_configuration_input_type('nickname',self.nickname,str())
self._check_configuration_input_type('username',self.username,str())
self._check_configuration_input_type('realname',self.realname,str())
self._check_configuration_input_type('alternate',self.alternate,str())
self._check_configuration_input_type('server',self.server,str())
self._check_configuration_input_type('port',self.port,int())
self._check_configuration_input_type('ssl',self.usessl,bool())
self._check_configuration_input_type('encoding',self.encoding,str())
self._check_configuration_input_type('flood_protection',self.flood_protection,bool())
self._check_configuration_input_type('multithreaded',self._multithreaded,bool())
self._check_configuration_input_type('debug_input',self._show_input,bool())
self._check_configuration_input_type('debug_output',self._show_output,bool())
self._check_configuration_input_type('daemon',self._daemon,bool())
self._check_configuration_input_type('verify_host',self._verify_hostname,bool())
self._check_configuration_input_type('verify_cert',self._verify_cert,bool())
# Make sure flood_rate is an int or a float
if type(self.floodrate)!=type(int()):
if type(self.floodrate)!=type(float()):
print("ERROR: "+WRONG_VARIABLE_TYPE.format(key='flood_rate',rec=intype,exp='float'))
sys.exit(1)
# Make sure clock_frequency is an int or a float
if type(self._clock_resolution)!=type(int()):
if type(self._clock_resolution)!=type(float()):
print("ERROR: "+WRONG_VARIABLE_TYPE.format(key='tick_frequency',rec=intype,exp='float'))
sys.exit(1)
# Make sure clock_resolution is an int or a float
if type(self._uptime_resolution)!=type(int()):
if type(self._uptime_resolution)!=type(float()):
print("ERROR: "+WRONG_VARIABLE_TYPE.format(key='clock_resolution',rec=intype,exp='float'))
sys.exit(1)
# Check to make sure that the password (if there is one) is a string
if self.password!=None:
if type(self.password)!=type(str):
if type(var)==type(str()):
intype = "string"
elif type(var)==type(int()):
intype = "integer"
elif type(var)==type(float()):
intype = "float"
elif type(var)==type(bool()):
intype = "boolean"
else:
intype = 'unknown'
print("ERROR: "+WRONG_VARIABLE_TYPE.format(key='password',rec=intype,exp='string'))
sys.exit(1)
# Check to make sure that the client_cert (if there is one) is a string
if self._client_cert!=None:
if type(self._client_cert)!=type(str):
if type(var)==type(str()):
intype = "string"
elif type(var)==type(int()):
intype = "integer"
elif type(var)==type(float()):
intype = "float"
elif type(var)==type(bool()):
intype = "boolean"
else:
intype = 'unknown'
print("ERROR: "+WRONG_VARIABLE_TYPE.format(key='certificate',rec=intype,exp='string'))
sys.exit(1)
# Check to make sure the client cert file exists
if not os.path.isfile(self._client_cert):
print("ERROR: "+CANNOT_FIND_SSL_FILE.format(ftype='certificate',name=self._client_cert))
sys.exit(1)
# Check to make sure that the client_key (if there is one) is a string
if self._client_key!=None:
if type(self._client_key)!=type(str):
if type(var)==type(str()):
intype = "string"
elif type(var)==type(int()):
intype = "integer"
elif type(var)==type(float()):
intype = "float"
elif type(var)==type(bool()):
intype = "boolean"
else:
intype = 'unknown'
print("ERROR: "+WRONG_VARIABLE_TYPE.format(key='key',rec=intype,exp='string'))
sys.exit(1)
# Check to make sure the client cert file exists
if not os.path.isfile(self._client_key):
print("ERROR: "+CANNOT_FIND_SSL_FILE.format(ftype='key',name=self._client_key))
sys.exit(1)
# Check to make sure that the load_ca (if there is one) is a string
if self._cert_authority!=None:
if type(self._cert_authority)!=type(str):
if type(var)==type(str()):
intype = "string"
elif type(var)==type(int()):
intype = "integer"
elif type(var)==type(float()):
intype = "float"
elif type(var)==type(bool()):
intype = "boolean"
else:
intype = 'unknown'
print("ERROR: "+WRONG_VARIABLE_TYPE.format(key='cert_authority',rec=intype,exp='string'))
sys.exit(1)
# Check to make sure the client cert file exists
if not os.path.isfile(self._cert_authority):
print("ERROR: "+CANNOT_FIND_SSL_FILE.format(ftype='CA',name=self._cert_authority))
sys.exit(1)
# GET EXTERNAL IP ADDRESS
if self._fetch_external_ip:
data = urllib.request.urlopen(self._external_ip_source).read()
self.external_ip = data.decode()
self.encoded_external_ip = int(ipaddress.IPv4Address(self.external_ip))
# _run()
# Arguments: none
#
# Connects to IRC and starts the loop that receives messages from the server
# and handles them.
def _run(self):
# Create "stop clocks" event
self._stop_clocks = threading.Event()
# Start the uptime clock
self._uptime_clock = Uptimer(self._stop_clocks,self)
self._uptime_clock.start()
# Start the tick clock
self._regular_clock = Clock(self._stop_clocks,self)
self._regular_clock.start()
# Set object state as "started"
self._started = True
# Raise the "start" event
irc.call("connecting",self)
## SSL-ify the connection if needed
if self.usessl:
self._ssl_context = ssl.create_default_context()
if self._verify_hostname:
self._ssl_context.check_hostname = True
else:
self._ssl_context.check_hostname = False
if self._verify_cert:
self._ssl_context.verify_mode = ssl.CERT_REQUIRED
else:
self._ssl_context.verify_mode = ssl.CERT_NONE
if self._client_cert!=None:
if self._client_key!=None:
self._ssl_context.load_cert_chain(certfile=self._client_cert, keyfile=self._client_key)
else:
self._display_error_and_exit(NO_KEY_ERROR.format(self._client_cert))
if self._cert_authority!=None:
self._ssl_context.load_verify_locations(self._cert_authority)
if ssl.HAS_SNI:
self._client = self._ssl_context.wrap_socket(self._client,server_side=False,server_hostname=self.server)
else:
self._client = self._ssl_context.wrap_socket(self._client,server_side=False)
# Create the connection socket and connect to the server
# self._client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self._client.connect((self.server,self.port))
# Store the server's certificate, if we're using SSL
if self.usessl:
self.certificate = self._client.getpeercert()
# Raise the "connect" event
irc.call("connect",self)
# Get the server to send nicks/hostmasks and all status symbols
self._send("PROTOCTL UHNAMES NAMESX")
# Send server password, if necessary
if self.password:
self._send(f"PASS {self.password}")
# Send user information
self._send(f"NICK {self.nickname}")
self._send(f"USER {self.username} 0 0 :{self.realname}")
# Begin the connection loop
self._buffer = ""
while True:
try:
# Get incoming server data
line = self._client.recv(4096)
# Decode incoming server data
try:
# Attempt to decode with the selected encoding
line2 = line.decode(self.encoding)
except UnicodeDecodeError:
try:
# Attempt to decode with "latin1"
line2 = line.decode('iso-8859-1')
except UnicodeDecodeError:
# Finally, if nothing else works, use windows default encoding
line2 = line.decode("CP1252", 'replace')
# Add incoming data to the internal buffer
self._buffer = self._buffer + line2
except socket.error:
print(DISCONNECTED_ERROR)
# Stop timers
self._stop_clocks.set()
# Shutdown the connection
self._client.shutdown(socket.SHUT_RDWR)
self._client.close()
return
# Step through the buffer and look for newlines
while True:
newline = self._buffer.find("\n")
# Newline not found, so we'll break and wait for more incoming data
if newline == -1:
break
# Grab the incoming line
line = self._buffer[:newline]
# Remove the incoming line from the buffer
self._buffer = self._buffer[newline+1:]
# Raise the "line" event
irc.call("line",self,line)
# Handle the line
self._handle(line)
if self._show_input: print("<- "+line)
# _handle()
# Arguments: string
#
# Parses IRC messages from the server, and triggers events
def _handle(self,line):
tokens = line.split()
# Return server ping
if tokens[0].lower()=="ping":
self._send("PONG " + tokens[1])
irc.call("ping",self)
return
# Nick collision
if tokens[1]=="433":
if self._current_nickname==self.nickname:
# Use alternate nick
if self.nickname == self.alternate:
self.nickname = self.nickname + "_"
self._current_nickname = self.nickname
self._send(f"NICK {self.nickname}")
else:
self.nickname = self.alternate
self._current_nickname = self.alternate
self._send(f"NICK {self.alternate}")
irc.call("nick-taken",self,self.nickname)
return
# Server welcome
if tokens[1]=="001":
irc.call("registered",self)
return
# Chat message
if tokens[1].lower()=="privmsg":
userhost = tokens.pop(0)
userhost = userhost[1:]
tokens.pop(0)
target = tokens.pop(0)
message = ' '.join(tokens)
message = message[1:]
p = userhost.split('!')
nickname = p[0]
host = p[1]
# CTCP action
if "\x01ACTION" in message:
message = message.replace("\x01ACTION",'')
message = message[:-1]
message = message.strip()
irc.call("action",self,nickname,host,target,message)
return
# CTCP message
if "\x01" in message:
message = message[1:] # strip delimiter
message = message[:-1] # strip delimiter
parsed = message.split(' ')
if len(parsed)>=1:
command = parsed.pop(0)
data = ' '.join(parsed)
irc.call("ctcp",self,nickname,host,target,command,data)
return
if target.lower()==self.nickname.lower():
# private message
irc.call("private",self,nickname,host,message)
return
else:
# public message
irc.call("public",self,nickname,host,target,message)
return
# Notice messages
if tokens[1].lower()=="notice":
tokens.pop(0) # remove server
tokens.pop(0) # remove message type
parsed = " ".join(tokens).split(":")
sender = parsed[0].strip()
message = parsed[1].strip()
# CTCP reply
if "\x01" in message:
message = message[1:] # strip delimiter
message = message[:-1] # strip delimiter
parsed = message.split(' ')
if len(parsed)>=1:
command = parsed.pop(0)
data = ' '.join(parsed)
irc.call("ctcp-reply",self,sender,command,data)
return
irc.call("notice",self,sender,message)
return
# Error management
if handle_errors(self,line): return
# Server options
if handle_information(self,line): return
# User management
if handle_users(self,line): return
# _regular_clock_tick()
# Arguments: none
#
# Executes every X seconds (set by the clock-frequency setting)
def _regular_clock_tick(self):
irc.call("tick",self)
# _uptime_clock_tick()
# Arguments: none
#
# Increments the uptime by one second, and calls
# _sendqueue() to send queued messages.
def _uptime_clock_tick(self):
self.uptime = self.uptime + self._uptime_resolution
#self.uptime = self.uptime + 1
if self.flood_protection:
# if (self.uptime % self.floodrate)==0:
# self._sendqueue()
if self._last_message_time==0:
self._sendqueue()
elif (self._last_message_time + self.floodrate)<=self.uptime:
self._sendqueue()
# _not_started()
# Arguments: None
#
# Returns false if the object is connected (or has
# started connecting) to an IRC server
def _not_started(self):
if self._started:
return False
else:
return True
# _check_configuration_input_type()
# Arguments: string, variuable, variable type
#
# Checks to make sure a configuration input is
# the proper variable type; display an error and
# exit if it isn't
def _check_configuration_input_type(self,cname,var,typ):
if type(var)!=type(typ):
if type(var)==type(str()):
intype = "string"
elif type(var)==type(int()):
intype = "integer"
elif type(var)==type(float()):
intype = "float"
elif type(var)==type(bool()):
intype = "boolean"
else:
intype = 'unknown'
if type(typ)==type(str()):
reqtype = "string"
elif type(typ)==type(int()):
reqtype = "integer"
elif type(typ)==type(float()):
reqtype = "float"
elif type(typ)==type(bool()):
reqtype = "boolean"
else:
reqtype = 'unknown'
print("ERROR: "+WRONG_VARIABLE_TYPE.format(key=cname,rec=intype,exp=reqtype))
sys.exit(1)
# _display_error_and_exit()
# Arguments: string
#
# Displays an error message and exits
def _display_error_and_exit(self,msg):
print("ERROR: "+msg)
sys.exit(1)
# _raise_runtime_error()
# Arguments: string
#
# Raises a runtime error
def _raise_runtime_error(self,msg):
raise RuntimeError(msg)
# _sendqueue()
# Arguments: none
#
# Removes a single message from the message queue
# and sends it to the IRC server.
def _sendqueue(self):
if len(self._message_queue)>0:
msg = self._message_queue.pop(0)
self._send(msg)
self._last_message_time = self.uptime
# _send()
# Arguments: string
#
# Sends a message to the IRC server
def _send(self,data):
if self._show_output: print("-> "+data)
sender = getattr(self._client, 'write', self._client.send)
try:
sender(bytes(data + "\r\n", self.encoding))
except socket.error:
print(SEND_MESSAGE_ERROR)
# Shutdown the connection and exit
self._client.shutdown(socket.SHUT_RDWR)
self._client.close()
sys.exit(1)
# tag()
# Arguments: string[, string,...]
#
# Adds a tag to the Erkle object
def tag(self,*args):
for t in args:
if t=='*': self._raise_runtime_error(CANNOT_ADD_ASTERIX_TAG)
self.tags.append(t)
irc._disabled = list(dict.fromkeys(irc._disabled))
# untag()
# Arguments: string[, string,...]
#
# Removes a tag from the Erkle object
def untag(self,*args):
for t in args:
if t=='*': self._raise_runtime_error(CANNOT_REMOVE_ASTERIX_TAG)
try:
self.tags.remove(t)
except:
pass
# enable()
# Arguments: string[, string,...]
#
# Enables a disabled event tag
def enable(self,*args):
for tag in args:
try:
irc._disabled.remove(tag)
except:
pass
# disabled()
# Arguments: string[, string,...]
#
# Disables events with the given tag(s)
def disable(self,*args):
for tag in args:
irc._disabled.append(tag)
irc._disabled = list(dict.fromkeys(irc._disabled))
# connect()
# Arguments: none
#
# Calls _run().
def connect(self):
if self._started:
self._raise_runtime_error(CONNECTION_IS_STARTED)
if self._multithreaded:
t = threading.Thread(name=f"{self.server}:{str(self.port)}",target=self._run)
if self._daemon: t.daemon = True
self._thread = t
t.start()
else:
self._run()
def closechat(self,clientid):
dccsock = None
for c in self._dcc_chat_sockets:
if c.id == clientid:
dccsock = c.socket
if dccsock==None: return False
dccsock.shutdown(socket.SHUT_RDWR)
dccsock.close()
clean = []
for c in self._dcc_chat_sockets:
if c.id == clientid: continue
clean.append(c)
self._dcc_chat_sockets = clean
try:
self._dcc_client_ids.remove(clientid)
except:
pass
return True
def chat(self,clientid,data):
dccsock = None
for c in self._dcc_chat_sockets:
if c.id == clientid:
dccsock = c.socket
if dccsock==None: return
sender = getattr(dccsock, 'write', dccsock.send)
try:
sender(bytes(data + "\r\n", self.encoding))
except socket.error:
print(SEND_MESSAGE_ERROR)
# Shutdown the connection and exit
dccsock.shutdown(socket.SHUT_RDWR)
dccsock.close()
clean = []
for c in self._dcc_chat_sockets:
if c.id == clientid: continue
clean.append(c)
self._dcc_chat_sockets = clean
try:
self._dcc_client_ids.remove(clientid)
except:
pass
def _dcc_chat_server(self,nickname,port,clientid):
ds = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ds.bind(("0.0.0.0",port))
ds.listen(5)
connection,address = ds.accept()
chatter = DCC_Chat_Socket(clientid,connection)
self._dcc_chat_sockets.append(chatter)
connection.settimeout(60)
client_address = address[0]
client_port = address[1]
client_nickname = nickname
irc.call("dcc-chat-accept",self,client_nickname,client_address,client_port,clientid)
while True:
try:
line = connection.recv(4096)
try:
# Attempt to decode with the selected encoding
line2 = line.decode(self.encoding)
except UnicodeDecodeError:
try:
# Attempt to decode with "latin1"
line2 = line.decode('iso-8859-1')
except UnicodeDecodeError:
# Finally, if nothing else works, use windows default encoding
line2 = line.decode("CP1252", 'replace')
line = line2
except:
connection.close()
clean = []
for c in self._dcc_chat_sockets:
if c.id == clientid: continue
clean.append(c)
self._dcc_chat_sockets = clean
try:
self._dcc_client_ids.remove(clientid)
except:
pass
sys.exit()
if len(line)==0:
irc.call("dcc-chat-end",self,client_nickname,client_address,client_port,clientid)
connection.close()
clean = []
for c in self._dcc_chat_sockets:
if c.id == clientid: continue
clean.append(c)
self._dcc_chat_sockets = clean
try:
self._dcc_client_ids.remove(clientid)
except:
pass
sys.exit()
break
line = line.replace("\n","")
line = line.replace("\r","")
if len(line)>0:
#print(client_nickname+" DCC-> "+line)
irc.call("dcc-chat",self,client_nickname,clientid,line)
def dccchat(self,nickname,port,host_ip=None):
if host_ip != None:
addy = int(ipaddress.IPv4Address(host_ip))
else:
addy = self.encoded_external_ip
cid = random.randint(1,50000)
while cid in self._dcc_client_ids:
cid = random.randint(1,50000)
self._dcc_client_ids.append(cid)
t = threading.Thread(target=self._dcc_chat_server,args=(nickname,port,cid))
t.start()
self.privmsg(nickname,"\x01DCC CHAT chat "+str(addy)+" "+str(port)+"\x01")
return cid
# thread()
# Arguments: none
#
# Returns the objects thread, or None if the object isn't threaded