circapi.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /******************************************************************************
  2. * The Unofficial IRC API for C *
  3. * Started 12-5-2005 *
  4. * Not Completed (PRE-ALPHA WORK IN PROGRESS) *
  5. * Executive Author: Dustin Molieri dmolieri@users.sourceforge.net *
  6. * E-Mail the author if you would like to contribute your expertise. *
  7. * *
  8. * DESCRIPTION *
  9. * *
  10. * Unofficial C API for RFC1459 client to server communication using IRC *
  11. * protocol. Designed for developers who would like to integrate IRC *
  12. * communication into their application without knowledge of the IRC *
  13. * protocol. This API was designed to be as easy to use as the MySQL API *
  14. * would be for connecting to a SQL server. This API gives developers the *
  15. * ability to build a complete IRC client with little to no understanding of *
  16. * the IRC protocol. *
  17. * *
  18. * This project is licensed using the GNU Public License Agreement (GPL) *
  19. ******************************************************************************/
  20. #define CIRCAPI_H
  21. #define NO_TEXT -1
  22. #define HUGEBUFF 262144
  23. #define BIGBUFF 65536
  24. #define SMALLBUFF 8192
  25. #define TINYBUFF 32
  26. #define NICKLEN 32
  27. #define USERLEN 32
  28. #define CHANLEN 32
  29. #define TOPICLEN 128
  30. /************************
  31. * Structures & Classes *
  32. ************************/
  33. // Uses dynamic string allocation
  34. // IRC ORGANIZED FORMATTED STRUCTURE
  35. struct ircofs
  36. {
  37. // Basic Stringlist Section
  38. int line_ct;
  39. char **ln;
  40. };
  41. struct ircuser
  42. {
  43. char nick[NICKLEN]; // user nick
  44. int active; // user field active?
  45. unsigned char op; // op?
  46. unsigned char voice; // voice?
  47. };
  48. struct ircchannel
  49. {
  50. char name[CHANLEN];
  51. unsigned char active;
  52. int user_field_ct;
  53. int user_active_ct;
  54. struct ircuser *users;
  55. };
  56. struct t_channels
  57. {
  58. int field_ct;
  59. int active_ct;
  60. struct ircchannel *channels;
  61. };
  62. #define PING_MSG 10
  63. #define CHANNEL_PRIVMSG_MSG 20
  64. #define USER_PRIVMSG_MSG 30
  65. #define MODE_CHANGE_MSG 40
  66. #define DISCONNECT_MSG 50
  67. #define USER_JOIN_MSG 60
  68. #define USER_PART_MSG 70
  69. #define USER_QUIT_MSG 80
  70. #define CHANNEL_NAMES_MSG 90
  71. #define CHANNEL_NAMES_COMPLETE_MSG 100
  72. #define CHANNEL_JOIN_REJECT_MSG 110
  73. #define USER_NICK_CHANGE_MSG 120
  74. #define MOTD_MSG 130
  75. #define MOTD_END_MSG 140
  76. #define NOTICE_MSG 150
  77. #define WELCOME_MSG 160
  78. #define YOURHOST_MSG 170
  79. #define CREATED_MSG 180
  80. #define MYINFO_MSG 190
  81. #define BOUNCE_MSG 200
  82. #define NOTICEAUTH_MSG 210
  83. #define UNKNOWN_MSG 400
  84. /******************
  85. * Root Functions *
  86. ******************/
  87. // connect to the irc server
  88. int irc_connect(int *sockfd, char *srv, char *port, char *nick, char *user, unsigned char invisible, char *name, int options, ...);
  89. // disconnect from the irc server
  90. int irc_disconnect(int *sockfd);
  91. // send text to server exactly as it is
  92. int irc_send_server_text(int *sockfd, char *text);
  93. // get raw text from server exactly as it is (raw)
  94. int irc_get_server_text(int *sockfd, char *buff, int buff_len);
  95. // send a private message (undisciplined)
  96. int irc_privmsg(int *sockfd, char *dest, char *message);
  97. /*********************************
  98. * Disciplined Message Functions *
  99. *********************************/
  100. int irc_send_channel_text(int *sockfd, char *channel, char *text); // sends text to a channel
  101. int irc_send_user_text(int *sockfd, char *nick, char *text); // sends text to a user
  102. /******************************************************
  103. * These functions use root functions for interaction *
  104. ******************************************************/
  105. int irc_join_channel(int *sockfd, char *channel); // join a channel on the irc server, returns 1 on success, 0 on failure (/join)
  106. int irc_leave_channel(int *sockfd, char *channel); // leave a channel on the irc server, returns 1 on success, returns 0 on failure (/leave)
  107. int irc_change_nick(int *sockfd, char *nick); // change nickname on the irc server, returns 1 on success, 0 on failure (/nick)
  108. int irc_message_user(int *sockfd, char *nick, char *msg); // Message a user on the IRC server, returns 1 on success, 0 on failure (/msg)
  109. int irc_message_channel(int *sockfd, char *channel, char *msg); // Message a channel (/me)
  110. int irc_set_topic(int *sockfd, char *channel, char *topic); // Change the topic of a channel
  111. int irc_give_voice(int *sockfd, char *nick, char *channel); // Gives user voice (must be channel op)
  112. int irc_give_op(int *sockfd, char *nick, char *channel); // Gives user op status (must be channel op)
  113. int irc_nick_list(int *sockfd, char *channel); // Summons a nick list
  114. /************************
  115. * IRC Channel Handling *
  116. ************************/
  117. int irc_channels_cleanup(struct t_channels *ircch); // sorts channel names alphabetically
  118. int irc_t_channels_init(struct t_channels *ircch); // initializes t_channels structure
  119. int irc_ircchannel_init(struct ircchannel *ch); // initializes ircchannel structure
  120. int irc_clear_ircchannel(struct ircchannel *channel); // clears ircchannel structure
  121. int irc_clear_t_channels(struct t_channels *ircch); // clear t_ircchannel structure
  122. int irc_create_first_channel(struct t_channels *ircch, char *name); // create the first channel in the channel list
  123. int irc_append_channel(struct t_channels *ircch, char *name); // append a new channel or use an unused one
  124. int irc_recycle_channel(struct t_channels *ircch, char *name); // use an unused channel
  125. int irc_channel_deactivate(struct t_channels *ircch, char *name);// deactivate a channel (usually on part or join reject)
  126. /*********************
  127. * IRC User Handling *
  128. *********************/
  129. int irc_create_first_user(struct ircchannel *ch, char *nick); // create first user field in a channel using nick
  130. int irc_append_user(struct ircchannel *ch, char *nick); // append a user field to a channel using nick
  131. int irc_recycle_user(struct ircchannel *ch, char *nick); // recycle an inactivue user field using nick
  132. int irc_user_deactivate(struct ircchannel *ch, char *nick); // deactivate user <nick> from channel <ch>
  133. int irc_adduser(char *joinmsg, struct t_channels *ircch); // adds a user to channel
  134. int irc_adduser2(char *nick, char *channel, struct t_channels *ircch); // same as adduser but uses nick/channel instead of join message
  135. int irc_remuser(char *partmsg, struct t_channels *ircch); // deactivates based on a part message
  136. int irc_remuser2(char *nick, char *channel, struct t_channels *ircch); // same as remuser but uses nick/channel instead
  137. int irc_remuser3(char *quitmsg, struct t_channels *ircch); // deactivates based on a quit message (from every channel)
  138. int irc_addusers(char *nickmsg, struct t_channels *ircch); // adds to irc channel using a user list
  139. int irc_addircuser(struct ircchannel *dest, struct ircuser src); // adds given ircuser structure to channel structure
  140. int irc_remircuser(char *nick_name, char *channel_name, struct t_channels *ircch); // removes given ircuser by evaluating nick & channel instead of partmsg
  141. int irc_ircuser_init(struct ircuser *ircusr); // initializes ircuser structure
  142. int irc_clear_ircuser(struct ircuser *user); // clears ircuser structure
  143. int irc_user_nickchange(char *nickchangemsg, struct t_channels *ircch); // changes the nick of a user in all channels
  144. /**************************************************************************
  145. * This group of functions can tell you what a line of text is (or isn't) *
  146. **************************************************************************/
  147. unsigned char irc_is_ping(char *ln); // is it a ping?
  148. unsigned char irc_is_channel_message(char *ln); // is it a message to a channel?
  149. unsigned char irc_is_user_message(char *ln); // is it a private message from a user?
  150. unsigned char irc_is_mode_message(char *ln); // is it a mode change message?
  151. unsigned char irc_is_disconnect_message(char *ln); // is it a disconnect message?
  152. unsigned char irc_is_join_message(char *ln); // is it a user joined message?
  153. unsigned char irc_is_part_message(char *ln); // is it a user departed channel message?
  154. unsigned char irc_is_quit_message(char *ln); // is it a user disconnected from the irc network message?
  155. unsigned char irc_is_nick_list(char *ln); // is it a numeric 353 channel list message?
  156. unsigned char irc_is_nick_list_complete(char *ln); // is it a numeric 366 end of /NAMES list
  157. unsigned char irc_is_join_reject(char *ln); // is it a channel join rejection message?
  158. unsigned char irc_is_nick_change(char *ln); // did someone change their nick?
  159. unsigned char irc_is_welcome_message(char *ln); // is it a welcome message 001?
  160. unsigned char irc_is_yourhost_message(char *ln); // is it a yourhost message 002?
  161. unsigned char irc_is_created_message(char *ln); // is it a created message 003?
  162. unsigned char irc_is_myinfo_message(char *ln); // is it a myinfo message 004?
  163. unsigned char irc_is_bounce_message(char *ln); // is it a bounce message 005?
  164. unsigned char irc_is_notice_auth_message(char *ln); // is it a NOTICE AUTH message? (non-numeric)
  165. unsigned int irc_translate(char *ln); // return a translated message type
  166. /************************************************************************************
  167. * Extract info related to the text recieved & store it into a \0 terminated buffer *
  168. ************************************************************************************/
  169. char *irc_numeric(char *ln, char *dest); // stores & returns numeric
  170. char *irc_sender(char *ln, char *dest); // stores & returns nick!ip@server info
  171. char *irc_sender_nick(char *ln, char *dest); // stores & returns just the nick of the user
  172. char *irc_sender_ip(char *ln, char *dest); // stores & returns the sender's ip address
  173. char *irc_sender_channel(char *ln, char *dest); // stores & returns the sender's channel
  174. char *irc_numeric_channel(char *ln, char *dest); // stores & returns the channel referring the numeric message
  175. char *irc_sender_message(char *ln, char *dest); // stores & returns the sender's message
  176. /*****************************************************
  177. * These functions are for dealing with server input *
  178. *****************************************************/
  179. int irc_ircofs_init(struct ircofs *ofs); // Initializes ircofs structure
  180. int irc_buff2ircofs(struct ircofs *ofs, char *buff); // Converts a buffer into an ircofs structure
  181. int irc_clear_ircofs(struct ircofs *ofs); // frees memory used for dynamic string allocation
  182. /******************************************************
  183. * Other Functions *
  184. ******************************************************/
  185. void nonblocking(int *sockfd);
  186. void irc_precall(); // called internally by irc api functions to prepare
  187. char *irc_error(); // get error message (if any)
  188. char *irc_ping_responce(char *ln, char *dest); // takes a ping line and formats a ping responce
  189. char *channel_index_text(char *str); // returns a pointer to the first #<channel> or &<channel> in char *str
  190. int compare(char *str1, char *str2); // case insensitive strcmp
  191. int comparen(char *str1, char *str2, int len); // case insensitive strncmp
  192. char *index2string(char *dest, char *src); // takes the first word (all letters prior to first space) and transforms to \0 string
  193. /***********************************
  194. * Functions to index your pointer *
  195. ***********************************/
  196. char *ping_message_index_ping(char *pingmsg); // returns pointer to the ping id in the ping message, NULL if not pingmsg
  197. char *channel_message_index_channel(char *chanmsg); // returns pointer to the channel in the channel message, NULL if not chanmsg
  198. char *user_message_index_nick(char *usermsg); // returns pointer to the user in the user message, NULL if not usermsg
  199. char *mode_message_index_mode(char *modemsg); // return pointer to the mode change in mode message, NULL if not modemsg
  200. char *disconnect_message_index_reason(char *discmsg); // return pointer to the disconnect message reason, NULL if not discmsg
  201. char *join_message_index_nick(char *joinmsg); // return pointer to the nick of the joining user, NULL if not joinmsg
  202. char *join_message_index_channel(char *joinmsg); // return pointer to the channel of the joining user, NULL if not joinmsg
  203. char *part_message_index_nick(char *partmsg); // return pointer to the nick of the parting user, NULL if not partmsg
  204. char *quit_message_index_nick(char *quitmsg); // return pointer to the nick of the person that quit, NULL if not quitmsg
  205. char *quit_message_index_reason(char *quitmsg); // return pointer to the reason the person quit, NULL if not quitmsg
  206. char *nick_list_index_first_nick(char *nicklistmsg); // return pointer to first nick in the nick list, NULL if not nicklistmsg
  207. char *nick_list_index_channel(char *nicklistmsg); // return a pointer to the channel in the nick list, NULL if not nicklistmsg
  208. char *join_reject_index_channel(char *joinrejectmsg); // return pointer to channel name of channel reject, NULL if not joinrejectmsg
  209. char *nick_change_index_oldnick(char *nickchangemsg); // return a pointer to the person's old nick
  210. char *nick_change_index_newnick(char *nickchangemsg); // return a pointer to the person's new nick
  211. char *motd_index_message(char *motdmsg); // return a pointer to the motd message, NULL if not noticemsg
  212. char *motd_end_index_message(char *motdmsg); // return a pointer to the end motd message, NULL if not noticemsg
  213. char *notice_index_message(char *noticemsg); // return a pointer to the notice message, NULL if not noticemsg
  214. char *welcome_message_index_message(char *welcomemsg); // return a pointer to 001 welcome message, NULL if not 001
  215. char *yourhost_message_index_message(char *yourhostmsg); // reutrn a pointer to 002 yourhost message, NULL if not 002
  216. char *created_message_index_message(char *createdmsg); // return a pointer to 003 server created message, NULL if not 003
  217. char *myinfo_message_index_message(char *myinfomsg); // return a pointer to 004 my info message, NULL if not 004
  218. char *bounce_message_index_message(char *bouncemsg); // return a pointer to 005 bounce message, NULL if not 005
  219. char *notice_auth_message_index_message(char *noticemsg); // return a pointer to NOTICE AUTH message
  220. char *rpl_index_message(char *numeric, char *message); // return a pointer to standard rpl numeric message :some.host #NUMERIC#--->: <message>