From 88b7ef1582a2e5c8f9c8645b4a8f0c10bd11b6a7 Mon Sep 17 00:00:00 2001 From: achmizs <1874748+achmizs@users.noreply.github.com> Date: Sun, 5 Dec 2021 15:27:59 -0500 Subject: [PATCH] Some more code cleanup --- IRCClient/IRCClientSession.m | 156 ++++++++++++++++++----------------- README.md | 2 +- 2 files changed, 81 insertions(+), 77 deletions(-) diff --git a/IRCClient/IRCClientSession.m b/IRCClient/IRCClientSession.m index ecfc587..2ac3028 100644 --- a/IRCClient/IRCClientSession.m +++ b/IRCClient/IRCClientSession.m @@ -71,9 +71,78 @@ static NSDictionary* ircNumericCodeList; return [self new]; } -/*************************************/ -#pragma mark - Useful helper functions -/*************************************/ +-(instancetype) init { + if (!(self = [super init])) + return nil; + + _callbacks.event_connect = onEvent; + _callbacks.event_ping = onEvent; + _callbacks.event_nick = onEvent; + _callbacks.event_quit = onEvent; + _callbacks.event_join = onEvent; + _callbacks.event_part = onEvent; + _callbacks.event_mode = onEvent; + _callbacks.event_umode = onEvent; + _callbacks.event_topic = onEvent; + _callbacks.event_kick = onEvent; + _callbacks.event_error = onEvent; + _callbacks.event_channel = onEvent; + _callbacks.event_privmsg = onEvent; + _callbacks.event_server_msg = onEvent; + _callbacks.event_notice = onEvent; + _callbacks.event_channel_notice = onEvent; + _callbacks.event_server_notice = onEvent; + _callbacks.event_invite = onEvent; + _callbacks.event_ctcp_req = onEvent; + _callbacks.event_ctcp_rep = onEvent; + _callbacks.event_ctcp_action = onEvent; + _callbacks.event_unknown = onEvent; + _callbacks.event_numeric = onNumericEvent; + _callbacks.event_dcc_chat_req = onDCCChatRequest; + _callbacks.event_dcc_send_req = onDCCSendRequest; + + _irc_session = irc_create_session(&_callbacks); + + if (!_irc_session) { + NSLog(@"Could not create irc_session."); + return nil; + } + + // Strip server info from nicks. + // irc_option_set(_irc_session, LIBIRC_OPTION_STRIPNICKS); + + // Set debug mode. + // irc_option_set(_irc_session, LIBIRC_OPTION_DEBUG); + + irc_set_ctx(_irc_session, (__bridge void *)(self)); + + unsigned int high, low; + irc_get_version (&high, &low); + + _version = [[NSString stringWithFormat:@"IRCClient Framework v%s (Said Achmiz) - libirc v%d.%d (Georgy Yunaev)", + IRCCLIENTVERSION, + high, + low] dataAsUTF8]; + + _channels = [NSMutableDictionary dictionary]; + _encoding = NSUTF8StringEncoding; + + _userInfo = [NSMutableDictionary dictionary]; + + return self; +} + +-(void) dealloc { + if (irc_is_connected(_irc_session)) { + NSLog(@"Warning: IRC Session is not disconnected on dealloc"); + } + + irc_destroy_session(_irc_session); +} + +/***************************/ +#pragma mark - Class methods +/***************************/ +(NSData *) nickFromNickUserHost:(NSData *)nickUserHost { if (nickUserHost == nil) @@ -121,78 +190,9 @@ static NSDictionary* ircNumericCodeList; nickUserHost.length - (rangeOfUserHostSeparator.location + 1))]); } -/***************************/ -#pragma mark - Class methods -/***************************/ - --(instancetype) init { - if (!(self = [super init])) - return nil; - - _callbacks.event_connect = onEvent; - _callbacks.event_ping = onEvent; - _callbacks.event_nick = onEvent; - _callbacks.event_quit = onEvent; - _callbacks.event_join = onEvent; - _callbacks.event_part = onEvent; - _callbacks.event_mode = onEvent; - _callbacks.event_umode = onEvent; - _callbacks.event_topic = onEvent; - _callbacks.event_kick = onEvent; - _callbacks.event_error = onEvent; - _callbacks.event_channel = onEvent; - _callbacks.event_privmsg = onEvent; - _callbacks.event_server_msg = onEvent; - _callbacks.event_notice = onEvent; - _callbacks.event_channel_notice = onEvent; - _callbacks.event_server_notice = onEvent; - _callbacks.event_invite = onEvent; - _callbacks.event_ctcp_req = onEvent; - _callbacks.event_ctcp_rep = onEvent; - _callbacks.event_ctcp_action = onEvent; - _callbacks.event_unknown = onEvent; - _callbacks.event_numeric = onNumericEvent; - _callbacks.event_dcc_chat_req = onDCCChatRequest; - _callbacks.event_dcc_send_req = onDCCSendRequest; - - _irc_session = irc_create_session(&_callbacks); - - if (!_irc_session) { - NSLog(@"Could not create irc_session."); - return nil; - } - - // Strip server info from nicks. -// irc_option_set(_irc_session, LIBIRC_OPTION_STRIPNICKS); - - // Set debug mode. -// irc_option_set(_irc_session, LIBIRC_OPTION_DEBUG); - - irc_set_ctx(_irc_session, (__bridge void *)(self)); - - unsigned int high, low; - irc_get_version (&high, &low); - - _version = [[NSString stringWithFormat:@"IRCClient Framework v%s (Said Achmiz) - libirc v%d.%d (Georgy Yunaev)", - IRCCLIENTVERSION, - high, - low] dataAsUTF8]; - - _channels = [NSMutableDictionary dictionary]; - _encoding = NSUTF8StringEncoding; - - _userInfo = [NSMutableDictionary dictionary]; - - return self; -} - --(void) dealloc { - if (irc_is_connected(_irc_session)) { - NSLog(@"Warning: IRC Session is not disconnected on dealloc"); - } - - irc_destroy_session(_irc_session); -} +/*************************************/ +#pragma mark - Class methods (private) +/*************************************/ +(void) loadNumericCodes { NSString* numericCodeListPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"IRC_Numerics" @@ -205,6 +205,10 @@ static NSDictionary* ircNumericCodeList; } } +/******************************/ +#pragma mark - Instance methods +/******************************/ + -(int) connect { return irc_connect(_irc_session, _server.terminatedCString, @@ -251,7 +255,7 @@ static NSDictionary* ircNumericCodeList; message.terminatedCString); } -- (int) quit:(NSData *)reason { +-(int) quit:(NSData *)reason { return irc_send_raw(_irc_session, "QUIT :%s", (reason diff --git a/README.md b/README.md index af85b90..c035c99 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ properties, and call `-[connect:]` to connect to the server and `-[run:]` to pla the connection on a new event queue and start receiving events. For example: ``` -IRCClientSession *session = [IRCClientSession new]; +IRCClientSession *session = [IRCClientSession session]; MyIRCClientSessionDelegate *controller = [[MyIRCClientSessionDelegate alloc] init]; session.delegate = controller;