Rewrite of core IRC code in Objective C (NSStream)
[IRCClient.git] / IRCClient / IRCClientSession.h
blob8911d8e8c4f4caad98c4672678f2cbca0490260c
1 //
2 // IRCClientSession.h
3 //
4 // Modified IRCClient Copyright 2015-2021 Said Achmiz.
5 // Original IRCClient Copyright 2009 Nathan Ollerenshaw.
6 // libircclient Copyright 2004-2009 Georgy Yunaev.
7 //
8 // See LICENSE and README.md for more info.
10 #import <Foundation/Foundation.h>
11 #import "IRCClientSessionDelegate.h"
13 /** @class IRCClientSession
14 * @brief Represents a connected IRC Session.
16 * IRCClientSession represents a single connection to an IRC server. After initialising
17 * the object, setting the delegate, server, port, and password (if required)
18 * properties, and setting the nickname, username and realname using the
19 * setNickname:username:realname: method, you call the connect: and run: methods
20 * to connect to the IRC server and place the connection on a new event queue.
22 * This thread then sends messages to the IRC server delegate,
23 * or to the IRCClientChannel delegate, as required.
26 /**********************************************/
27 #pragma mark IRCClientSession class declaration
28 /**********************************************/
30 @interface IRCClientSession : NSObject <NSStreamDelegate>
32 /******************************/
33 #pragma mark - Class properties
34 /******************************/
36 /** Returns a dictionary of IRC numeric codes.
38 The dictionary contains entries for all known IRC numeric codes (as keys).
39 (The list is taken from https://www.alien.net.au/irc/irc2numerics.html .)
41 The value for each key is an NSArray with the known numeric reply names for
42 which the numeric code is used.
44 Note that there is no guarantee whatsoever that any given numeric reply
45 name will, in fact, describe the contents of the message; most IRC numeric
46 messages have implementation-specific uses. See the various RFCs, and other
47 info sources, for details.
49 @property (class, nonatomic, readonly) NSDictionary <NSString *, NSDictionary *> *ircNumericCodes;
51 /************************/
52 #pragma mark - Properties
53 /************************/
55 /** Delegate to send events to. */
56 @property (assign) id <IRCClientSessionDelegate> delegate;
58 /** The version string for the client to send back on CTCP VERSION requests.
59 There is usually no reason to set this, as IRCClient correctly sets its
60 own version string automatically, but this can be any string you like.
62 @property (copy, nonatomic) NSData *version;
64 /** IRC server to connect to. */
65 @property (copy, nonatomic) NSData *server;
67 /** IRC port to connect to. */
68 @property (assign) NSUInteger port;
70 /** Server password to provide on connect (may be left empty or nil). */
71 @property (copy, nonatomic) NSData *password;
73 /** Nickname of the connected client.
75 @property (nonatomic, readonly) NSData *nickname;
77 /** Username of the connected client. Also known as the ident.
79 @property (nonatomic, readonly) NSData *username;
81 /** Realname of the connected client.
83 @property (nonatomic, readonly) NSData *realname;
85 /** The suggested text encoding for messages on this server.
87 This is almost entirely irrelevant (except for CTCP TIME replies), as
88 all messages and other strings are taken and returned as NSData objects.
89 This property is for your convenience.
91 You may change this at any time.
93 @property (assign) NSStringEncoding encoding;
95 /** An NSDictionary of channels that the client is currently connected to.
96 Keys are channel names (NSData), values are IRCClientChannel objects.
98 @property (nonatomic, readonly) NSDictionary <NSData *, IRCClientChannel *> *channels;
100 /** Returns YES if the server is currently connected successfully, and NO if
101 it is not. */
102 @property (readonly, getter=isConnected) BOOL connected;
104 /** Stores arbitrary user info. */
105 @property (nonatomic, readonly) NSMutableDictionary *userInfo;
107 /********************************************/
108 #pragma mark - Initializers & factory methods
109 /********************************************/
111 +(instancetype) session;
113 /***************************/
114 #pragma mark - Class methods
115 /***************************/
117 /** Returns the nick part of a nick!user@host string.
119 +(NSData *) nickFromNickUserHost:(NSData *)nickUserHost;
121 /** Returns the user part of a nick!user@host string.
122 May be blank if the user component can’t be found
123 (i.e. if the passed string is not, in fact, in nick!user@host format).
125 +(NSData *) userFromNickUserHost:(NSData *)nickUserHost;
127 /** Returns the host part of a nick!user@host string.
128 May be blank if the host component can’t be found
129 (i.e. if the passed string is not, in fact, in nick!user@host format).
131 +(NSData *) hostFromNickUserHost:(NSData *)nickUserHost;
133 /******************************/
134 #pragma mark - Instance methods
135 /******************************/
137 /** Set the nickname, username, and realname for the session.
139 Returns YES if successfully set, NO otherwise.
140 (NO is returned if you try to call this method after the session has
141 already connected; use the nick: method to attempt a nick change while
142 connected.)
144 -(BOOL) setNickname:(NSData *)nickname
145 username:(NSData *)username
146 realname:(NSData *)realname;
148 /** Connect to the IRC server.
150 Note that this performs the initial DNS lookup and the TCP connection, so if
151 there are any problems you will be notified via the return code of the message.
153 Look at the libircclient documentation for the different return codes.
155 -(int) connect;
157 /** Disconnect from the IRC server.
159 This always works, as it simply shuts down the socket. If you want to disconnect
160 in a friendly way, you should use the quit: message.
162 -(void) disconnect;
164 /** Convert libircclient markup in a message to mIRC format codes.
166 -(NSData *) colorConvertToMIRC:(NSData *)message;
168 /** Convert mIRC format codes in a message to libircclient markup.
170 -(NSData *) colorConvertFromMIRC:(NSData *)message;
172 /** Remove mIRC format codes from a message.
174 -(NSData *) colorStripFromMIRC:(NSData *)message;
176 /**************************/
177 #pragma mark - IRC commands
178 /**************************/
180 /** Sends a raw message to the IRC server. Please consult RFC 1459 for the
181 format of IRC commands.
183 -(int) sendRaw:(NSData *)message;
185 /** Quits the IRC server with the given reason.
187 On success, a -[userQuit:withReason:session:] event will be sent to the
188 IRCClientSessionDelegate with the nickname of the IRC client and the reason
189 provided by the user (or nil if no reason was provided).
191 @param reason The quit reason.
193 -(int) quit:(NSData *)reason;
195 /** Joins a channel with a given name and key.
197 On success, a -[joinedNewChannel:session:] event will be sent to the
198 IRCClientSessionDelegate with the IRCClientChannel object representing the
199 newly-joined channel.
201 @param channel The name of the channel to join.
202 @param key The key for the channel (may be nil).
204 -(int) join:(NSData *)channel
205 key:(NSData *)key;
207 /** Lists users in an IRC channel (or channels).
209 @param channel A channel name or string to pass to the NAMES command.
210 Implementation specific.
212 -(int) names:(NSData *)channel;
214 /** Lists channels on the IRC server.
216 @param channel A channel name or string to pass to the LIST command.
217 Implementation specific.
219 -(int) list:(NSData *)channel;
221 /** Sets the user mode for the IRC client.
223 @param mode The mode string to set.
225 -(int) userMode:(NSData *)mode;
227 /** Sets the IRC client nickname.
229 On success, a nickChangedFrom:to: event will be sent to the
230 IRCClientSessionDelegate with our old nick and the new nick that we now
231 have.
233 @param newnick The new nickname to set.
235 -(int) nick:(NSData *)newnick;
237 /** Sends a WHO query to the IRC server.
239 @param nickmask Nickname mask of the IRC client to WHO.
241 -(int) who:(NSData *)nickmask;
243 /** Sends a WHOIS query to the IRC server.
245 @param nick Nickname of the IRC client to WHOIS.
247 -(int) whois:(NSData *)nick;
249 /** Send a PRIVMSG to another IRC client.
251 @param message Message to send.
252 @param target The other IRC client to send the message to.
254 -(int) message:(NSData *)message
255 to:(NSData *)target;
257 /** Sends a CTCP ACTION to another IRC client.
259 @param action The action message to send.
260 @param target The nickname of the irc client to send the message to.
262 -(int) action:(NSData *)action
263 to:(NSData *)target;
265 /** Send a NOTICE to another IRC client.
267 @param notice The message text to send.
268 @param target The nickname of the irc client to send the notice to.
270 -(int) notice:(NSData *)notice
271 to:(NSData *)target;
273 /** Send a CTCP request to another IRC client.
275 @param request The CTCP request string to send.
276 @param target The nickname of the IRC client to send the request to.
278 -(int) ctcpRequest:(NSData *)request
279 target:(NSData *)target;
281 /** Send a CTCP reply to another IRC client.
283 @param reply The CTCP reply string to send.
284 @param target The nickname of the IRC client to send the reply to.
286 -(int) ctcpReply:(NSData *)reply
287 target:(NSData *)target;
289 @end