IRC client framework (wrapper around libircclient library).
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

IRCClientSession.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //
  2. // IRCClientSession.h
  3. // IRCClient
  4. /*! \mainpage IRCClient - a Cocoa IRC Framework to create IRC clients
  5. *
  6. * \section intro_sec Introduction
  7. *
  8. * IRCClient is a Cocoa Framework that uses the excellent libircclient library
  9. * written by Georgy Yunaev.
  10. *
  11. * \section usage Basic Usage
  12. *
  13. * To use this framework, you will need to write an IRCClientSessionDelegate to
  14. * handle all of the events generated by the server, and an IRCClientChannelDelegate
  15. * to handle all of the events generated by channels on that server.
  16. *
  17. * You then create an IRCClientSession object in your code, assign the required
  18. * properties, and call connect: to connect to the server and run: to create
  19. * the new thread and start receiving events. For example:
  20. *
  21. * \code
  22. * IRCClientSession *session = [[IRCClientSession alloc] init];
  23. * MyIRCClientSessionDelegate *controller = [[MyIRCClientSessionDelegate alloc] init];
  24. *
  25. * session.delegate = controller;
  26. * controller.session = session;
  27. *
  28. * NSData *server = [NSData dataWithBytes:@"chat.freenode.net".UTF8String length:strlen(@"chat.freenode.net".UTF8String.length) + 1];
  29. * NSUinteger port = 6665;
  30. * NSData *nickname = [NSData dataWithBytes:@"test".UTF8String length:strlen(@"test".UTF8String) + 1];
  31. * NSData *username = [NSData dataWithBytes:@"test".UTF8String length:strlen(@"test".UTF8String) + 1];
  32. * NSData *realname = [NSData dataWithBytes:@"test".UTF8String length:strlen(@"test".UTF8String) + 1];
  33. *
  34. * [session setServer:server];
  35. * [session setPort:port];
  36. * [session setNickname:nickname username:username realname:realname];
  37. * [session connect];
  38. *
  39. * [session run]; //starts the thread
  40. * \endcode
  41. *
  42. * \section author Author, copyright, support.
  43. *
  44. * If you have questions, bug reports, or suggestions regarding IRCClient,
  45. * find Obormot on the Freenode IRC network.
  46. *
  47. * If you have any questions, bug reports, suggestions regarding libircclient,
  48. * please visit http://libircclient.sourceforge.net
  49. *
  50. * <PRE>
  51. * libircclient Copyright (C) 2004-2009 Georgy Yunaev gyunaev@ulduzsoft.com
  52. * Original IRCClient Copyright (C) 2009 Nathan Ollerenshaw chrome@stupendous.net
  53. * Modified IRCClient Copyright 2015 Said Achmiz (www.saidachmiz.net)
  54. *
  55. * This library is free software; you can redistribute it and/or modify it
  56. * under the terms of the GNU Lesser General Public License as published by
  57. * the Free Software Foundation; either version 2 of the License, or (at your
  58. * option) any later version.
  59. *
  60. * This library is distributed in the hope that it will be useful, but WITHOUT
  61. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  62. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  63. * License for more details.
  64. * </PRE>
  65. */
  66. #import <Foundation/Foundation.h>
  67. #import "IRCClientSessionDelegate.h"
  68. /** @class IRCClientSession
  69. * @brief Represents a connected IRC Session.
  70. *
  71. * IRCClientSession represents a single connection to an IRC server. After initialising
  72. * the object, setting the delegate, server, port, and password (if required)
  73. * properties, and setting the nickname, username and realname using the
  74. * setNickname:username:realname: method, you call the connect: and run: methods
  75. * to connect to the IRC server and start a new thread.
  76. *
  77. * This thread then sends messages to the IRC server delegate,
  78. * or to the IRCClientChannel delegate, as required.
  79. */
  80. /**********************************************/
  81. #pragma mark IRCClientSession class declaration
  82. /**********************************************/
  83. @interface IRCClientSession : NSObject
  84. /************************/
  85. #pragma mark - Properties
  86. /************************/
  87. /** delegate to send events to. */
  88. @property (assign) id <IRCClientSessionDelegate> delegate;
  89. /** The version string for the client to send back on CTCP VERSION requests.
  90. There is usually no reason to set this, as IRCClient correctly sets its
  91. own version string automatically, but this can be any string you like.
  92. */
  93. @property (copy) NSData *version;
  94. /** IRC server to connect to */
  95. @property (copy) NSData *server;
  96. /** IRC port to connect to */
  97. @property (assign) NSUInteger port;
  98. /** Server password to provide on connect (may be left empty or nil) */
  99. @property (copy) NSData *password;
  100. /** Nickname of the connected client.
  101. */
  102. @property (readonly) NSData *nickname;
  103. /** Username of the connected client. Also known as the ident.
  104. */
  105. @property (readonly) NSData *username;
  106. /** Realname of the connected client.
  107. */
  108. @property (readonly) NSData *realname;
  109. /** The suggested text encoding for messages on this server.
  110. This is almost entirely irrelevant (except for CTCP TIME replies), as
  111. all messages and other strings are taken and returned as C strings
  112. encapsulated in NSData objects. This property is for your convenience.
  113. You may change this at any time.
  114. */
  115. @property (assign) NSStringEncoding encoding;
  116. /** An NSDictionary of channels that the client is currently connected to.
  117. Keys are channel names (NSData objects containing C strings), values are
  118. IRCClientChannel objects.
  119. */
  120. @property (readonly) NSDictionary *channels;
  121. /** Returns YES if the server is currently connected successfully, and NO if
  122. it is not. */
  123. @property (readonly, getter=isConnected) bool connected;
  124. /** Stores arbitrary user info. */
  125. @property (strong) NSDictionary *userInfo;
  126. /***************************/
  127. #pragma mark - Class methods
  128. /***************************/
  129. /** Returns a dictionary of IRC numeric codes.
  130. The dictionary contains entries for all known IRC numeric codes (as keys).
  131. (The list is taken from https://www.alien.net.au/irc/irc2numerics.html .)
  132. The value for each key is an NSArray with the known numeric reply names for
  133. which the numeric code is used.
  134. Note that there is no gaurantee whatsoever that any given numeric reply
  135. name will, in fact, describe the contents of the message; most IRC numeric
  136. messages have implementation-specific uses. See the various RFCs, and other
  137. info sources, for details.
  138. */
  139. + (NSDictionary *)ircNumericCodes;
  140. /** Set the nickname, username, and realname for the session.
  141. Returns 1 if successfully set, 0 otherwise.
  142. (0 is returned if you try to call this method after the session has
  143. already connected; use the nick: method to attempt a nick change while
  144. connected.)
  145. */
  146. - (int)setNickname:(NSData *)nickname username:(NSData *)username realname:(NSData *)realname;
  147. /** Connect to the IRC server.
  148. Note that this performs the initial DNS lookup and the TCP connection, so if
  149. there are any problems you will be notified via the return code of the message.
  150. Look at the libircclient documentation for the different return codes.
  151. */
  152. - (int)connect;
  153. /** Disconnect from the IRC server.
  154. This always works, as it simply shuts down the socket. If you want to disconnect
  155. in a friendly way, you should use the quit: message.
  156. */
  157. - (void)disconnect;
  158. /** Starts a new thread and starts the libircclient runloop, processing events and
  159. firing messages back to the main runloop as required. Calling this again will
  160. do nothing other than raise a warning in your logs.
  161. */
  162. - (void)run;
  163. /**************************/
  164. #pragma mark - IRC commands
  165. /**************************/
  166. /** Sends a raw message to the IRC server. Please consult rfc1459 for the format
  167. of IRC commands. */
  168. - (int)sendRaw:(NSData *)message;
  169. /** quits the IRC server with the given reason.
  170. On success, a userQuit:withReason: event will be sent to the
  171. IRCClientSessionDelegate with the nickname of the IRC client and the reason
  172. provided by the user (or nil if no reason was provided).
  173. */
  174. - (int)quit:(NSData *)reason;
  175. /** Joins a channel with a given name and key
  176. On success, a userJoined:channel: event will be sent to the
  177. IRCClientSessionDelegate with the nickname of the IRC client and the name of
  178. the channel that was joined.
  179. @param channel the channel to join
  180. @param key they key for the channel (may be nil)
  181. */
  182. - (int)join:(NSData *)channel key:(NSData *)key;
  183. /** lists channels on the IRC server.
  184. @param channel a channel name or string to pass to the LIST command.
  185. Implementation specific.
  186. */
  187. - (int)list:(NSData *)channel;
  188. /** sets the user mode for the IRC client
  189. @param mode string to set
  190. */
  191. - (int)userMode:(NSData *)mode;
  192. /** sets the IRC client nickname.
  193. On success, a nickChangedFrom:to: event will be sent to the
  194. IRCClientSessionDelegate with our old nick and the new nick that we now
  195. have.
  196. @param newnick new nickname to set.
  197. */
  198. - (int)nick:(NSData *)newnick;
  199. /** sends a WHOIS request to the IRC server
  200. @param nick nickname of the irc client to whois.
  201. */
  202. - (int)whois:(NSData *)nick;
  203. /** send a PRIVMSG to another IRC client
  204. @param message message to send
  205. @param target the other IRC client to send the message to.
  206. */
  207. - (int)message:(NSData *)message to:(NSData *)target;
  208. /** send a CTCP ACTION to another IRC client
  209. @param action the action message to send
  210. @param target the nickname of the irc client to send the message to.
  211. */
  212. - (int)action:(NSData *)action to:(NSData *)target;
  213. /** send a NOTICE to another IRC client
  214. @param notice the message text to send
  215. @param target the nickname of the irc client to send the notice to.
  216. */
  217. - (int)notice:(NSData *)notice to:(NSData *)target;
  218. /** send a CTCP request to another IRC client
  219. @param request the CTCP request string to send
  220. @param target the nickname of the IRC client to send the request to.
  221. */
  222. - (int)ctcpRequest:(NSData *)request target:(NSData *)target;
  223. /** send a CTCP reply to another IRC client
  224. @param reply the CTCP reply string to send
  225. @param target the nickname of the IRC client to send the reply to.
  226. */
  227. - (int)ctcpReply:(NSData *)reply target:(NSData *)target;
  228. @end
  229. /*************************************/
  230. #pragma mark - Useful helper functions
  231. /*************************************/
  232. /** Returns the nick part of a nick!user@host string.
  233. */
  234. NSData* getNickFromNickUserHost(NSData *nickUserHost);
  235. /** Returns the user part of a nick!user@host string.
  236. May be blank if the user component can't be found
  237. (i.e. if the passed string is not, in fact, in nick!user@host format).
  238. */
  239. NSData* getUserFromNickUserHost(NSData *nickUserHost);
  240. /** Returns the host part of a nick!user@host string.
  241. May be blank if the host component can't be found
  242. (i.e. if the passed string is not, in fact, in nick!user@host format).
  243. */
  244. NSData* getHostFromNickUserHost(NSData *nickUserHost);