circapi.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  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. #include <stdio.h>
  21. #include <netinet/in.h>
  22. #include <arpa/inet.h>
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <netdb.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <fcntl.h>
  29. #include <stdarg.h>
  30. #include <unistd.h>
  31. #include <errno.h>
  32. #include <err.h>
  33. #include <circapi.h>
  34. char err_buff[SMALLBUFF]; // buffer for storing error messages (if any)
  35. /******************
  36. * Root Functions *
  37. ******************/
  38. int irc_connect(int *sockfd, char *srv, char *port, char *nick, char *user, unsigned char invisible, char *name, int options, ...)
  39. {
  40. struct hostent *h;
  41. struct sockaddr_in server_address;
  42. va_list ap;
  43. int ct, x;
  44. char nick_msg[SMALLBUFF], user_msg[SMALLBUFF];
  45. char buff[BIGBUFF];
  46. int flag=0;
  47. irc_precall();
  48. sprintf(nick_msg, "NICK %s\r\n", nick);
  49. sprintf(user_msg, "USER %s %c * :%s\r\n", user, invisible ? '8' : '0', name);
  50. va_start(ap, options);
  51. *sockfd = socket(PF_INET, SOCK_STREAM, 0);
  52. h=gethostbyname(srv);
  53. server_address.sin_family = AF_INET; // host byte order
  54. server_address.sin_port = htons(atoi(port)); // short, network byte order
  55. server_address.sin_addr = *((struct in_addr *)h->h_addr);
  56. memset(&(server_address.sin_zero), '\0', 8); // zero the rest of the struct
  57. if(connect(*sockfd, (struct sockaddr *)&server_address, sizeof(server_address)) == -1)
  58. {
  59. switch(errno)
  60. {
  61. case EBADF:
  62. strcpy(err_buff, "EBADF");
  63. break;
  64. case EFAULT:
  65. strcpy(err_buff, "EFAULT");
  66. break;
  67. case ENOTSOCK:
  68. strcpy(err_buff, "ENOTSOCK");
  69. break;
  70. case EISCONN:
  71. strcpy(err_buff, "EISCONN");
  72. break;
  73. case ECONNREFUSED:
  74. strcpy(err_buff, "ECONNREFUSED");
  75. break;
  76. case ETIMEDOUT:
  77. strcpy(err_buff, "ETIMEDOUT");
  78. break;
  79. case ENETUNREACH:
  80. strcpy(err_buff, "ENETREACH");
  81. break;
  82. case EADDRINUSE:
  83. strcpy(err_buff, "EADDRINUSE");
  84. break;
  85. case EINPROGRESS:
  86. strcpy(err_buff, "EINPROGRESS");
  87. break;
  88. case EALREADY:
  89. strcpy(err_buff, "EALREADY");
  90. break;
  91. case EAGAIN:
  92. strcpy(err_buff, "EAGAIN");
  93. break;
  94. case EAFNOSUPPORT:
  95. strcpy(err_buff, "EAFNOSUPPORT");
  96. break;
  97. case EACCES:
  98. strcpy(err_buff, "EACCES");
  99. break;
  100. case EPERM:
  101. strcpy(err_buff, "EPERM");
  102. break;
  103. default:
  104. strcpy(err_buff, "Unknown");
  105. break;
  106. }
  107. return 0;
  108. }
  109. nonblocking(sockfd);
  110. if(irc_send_server_text(sockfd, nick_msg) <= 0)
  111. {
  112. close(*sockfd);
  113. return 0;
  114. }
  115. if(irc_send_server_text(sockfd, user_msg) <= 0)
  116. {
  117. close(*sockfd);
  118. return 0;
  119. }
  120. return 1;
  121. }
  122. int irc_disconnect(int *sockfd)
  123. {
  124. irc_precall();
  125. close(*sockfd);
  126. }
  127. int irc_send_server_text(int *sockfd, char *text)
  128. {
  129. int bytes=0;
  130. irc_precall();
  131. if((bytes = send(*sockfd, text, strlen(text), 0)) <= 0)
  132. {
  133. if(bytes == 0) return 0;
  134. switch(errno)
  135. {
  136. case EAGAIN:
  137. strcpy(err_buff, "EAGAIN");
  138. break;
  139. case EBADF:
  140. strcpy(err_buff, "EBADF");
  141. break;
  142. case ECONNRESET:
  143. strcpy(err_buff, "ECONNRESET");
  144. break;
  145. case EDESTADDRREQ:
  146. strcpy(err_buff, "EDESTADDRREQ");
  147. break;
  148. case EFAULT:
  149. strcpy(err_buff, "ECONNRESET");
  150. break;
  151. case EINTR:
  152. strcpy(err_buff, "EINTR");
  153. break;
  154. case EINVAL:
  155. strcpy(err_buff, "EINVAL");
  156. break;
  157. case EISCONN:
  158. strcpy(err_buff, "EISCONN");
  159. break;
  160. case EMSGSIZE:
  161. strcpy(err_buff, "EMSGSIZE");
  162. break;
  163. case ENOBUFS:
  164. strcpy(err_buff, "ENOBUFS");
  165. break;
  166. case ENOMEM:
  167. strcpy(err_buff, "ENOMEM");
  168. break;
  169. case ENOTCONN:
  170. strcpy(err_buff, "ENOTCONN");
  171. break;
  172. case ENOTSOCK:
  173. strcpy(err_buff, "ENOTSOCK");
  174. break;
  175. case EOPNOTSUPP:
  176. strcpy(err_buff, "EOPNOTSUPP");
  177. break;
  178. case EPIPE:
  179. strcpy(err_buff, "EPIPE");
  180. break;
  181. default:
  182. strcpy(err_buff, "UNKNOWNSENDERR");
  183. break;
  184. }
  185. return 0;
  186. }
  187. return bytes;
  188. }
  189. int irc_get_server_text(int *sockfd, char *buff, int buff_len)
  190. {
  191. int bytes=0;
  192. irc_precall();
  193. if((bytes = recv(*sockfd, buff, buff_len, 0)) <= 0)
  194. {
  195. buff[0] = '\0';
  196. if(bytes == 0) return 0;
  197. switch(errno)
  198. {
  199. case EAGAIN:
  200. strcpy(err_buff, "EAGAIN");
  201. break;
  202. case EBADF:
  203. strcpy(err_buff, "EBADF");
  204. break;
  205. case EFAULT:
  206. strcpy(err_buff, "EFAULT");
  207. break;
  208. case EINTR:
  209. strcpy(err_buff, "EINTR");
  210. break;
  211. case EINVAL:
  212. strcpy(err_buff, "EINVAL");
  213. break;
  214. case ENOMEM:
  215. strcpy(err_buff, "ENOMEM");
  216. break;
  217. case ENOTCONN:
  218. strcpy(err_buff, "ENOTCONN");
  219. break;
  220. case ENOTSOCK:
  221. strcpy(err_buff, "ENOTSOCK");
  222. break;
  223. default:
  224. strcpy(err_buff, "UNKNOWNSENDERR");
  225. break;
  226. }
  227. return NO_TEXT;
  228. }
  229. return bytes;
  230. }
  231. /*********************
  232. * SubRoot Functions *
  233. *********************/
  234. int irc_privmsg(int *sockfd, char *dest, char *message)
  235. {
  236. char buff[SMALLBUFF];
  237. memset(buff, '\0', sizeof(buff));
  238. if(strlen(dest)+strlen(message) > SMALLBUFF+16) return 0;
  239. sprintf(buff, "PRIVMSG %s :%s\r\n", dest, message);
  240. return irc_send_server_text(sockfd, buff);
  241. }
  242. int irc_send_user_text(int *sockfd, char *nick, char *text)
  243. {
  244. if(nick[0] == '#' || nick[0] == '&') return 0;
  245. return irc_privmsg(sockfd, nick, text);
  246. }
  247. int irc_send_channel_text(int *sockfd, char *channel, char *text)
  248. {
  249. if(channel[0] != '#' && *channel != '&') return 0;
  250. return irc_privmsg(sockfd, channel, text);
  251. }
  252. /******************************************************
  253. * These functions use root functions for interaction *
  254. ******************************************************/
  255. int irc_join_channel(int *sockfd, char *channel)
  256. {
  257. char buff[SMALLBUFF];
  258. irc_precall();
  259. sprintf(buff, "JOIN %s\r\n", channel);
  260. return irc_send_server_text(sockfd, buff);
  261. }
  262. int irc_leave_channel(int *sockfd, char *channel)
  263. {
  264. char buff[SMALLBUFF];
  265. irc_precall();
  266. sprintf(buff, "PART %s\r\n", channel);
  267. return irc_send_server_text(sockfd, buff);
  268. }
  269. int irc_change_nick(int *sockfd, char *nick)
  270. {
  271. char buff[SMALLBUFF];
  272. irc_precall();
  273. sprintf(buff, "NICK %s\r\n", nick);
  274. return irc_send_server_text(sockfd, buff);
  275. }
  276. int irc_message_user(int *sockfd, char *nick, char *msg)
  277. {
  278. char buff[SMALLBUFF];
  279. irc_precall();
  280. sprintf(buff, "PRIVMSG %s :%s\r\n", nick, msg);
  281. return irc_send_server_text(sockfd, buff);
  282. }
  283. int irc_message_channel(int *sockfd, char *channel, char *msg)
  284. {
  285. irc_precall();
  286. char buff[SMALLBUFF];
  287. sprintf(buff, "PRIVMSG %s :%s\r\n", channel, msg);
  288. return irc_send_server_text(sockfd, buff);
  289. }
  290. int irc_set_topic(int *sockfd, char *channel, char *topic)
  291. {
  292. irc_precall();
  293. char buff[SMALLBUFF];
  294. sprintf(buff, "TOPIC %s :%s\r\n", channel, topic);
  295. return irc_send_server_text(sockfd, buff);
  296. }
  297. int irc_give_voice(int *sockfd, char *nick, char *channel)
  298. {
  299. irc_precall();
  300. char buff[SMALLBUFF];
  301. sprintf(buff, "MODE %s +v %s\r\n", channel, nick);
  302. return irc_send_server_text(sockfd, buff);
  303. }
  304. int irc_give_op(int *sockfd, char *nick, char *channel)
  305. {
  306. irc_precall();
  307. char buff[SMALLBUFF];
  308. sprintf(buff, "MODE %s +o %s\r\n", channel, nick);
  309. return irc_send_server_text(sockfd, buff);
  310. }
  311. int irc_nick_list(int *sockfd, char *channel)
  312. {
  313. irc_precall();
  314. char buff[SMALLBUFF];
  315. sprintf(buff, "NAMES %s\r\n", channel);
  316. return irc_send_server_text(sockfd, buff);
  317. }
  318. int irc_ircofs_init(struct ircofs *ofs)
  319. {
  320. memset(ofs, '\0', sizeof(struct ircofs));
  321. return 1;
  322. }
  323. int irc_buff2ircofs(struct ircofs *ofs, char *buff)
  324. {
  325. int x, y, z;
  326. // count how many lines are in a buffer
  327. for(x=y=0; buff[x] != '\0'; ++x)
  328. {
  329. if(buff[x] == '\n' || buff[x] == '\0') ++y;
  330. }
  331. if((ofs->line_ct = y) < 1) return 0;
  332. #ifdef DEBUG
  333. printf("WE HAVE %d LINES OF TEXT!\n", ofs->line_ct);
  334. #endif
  335. // allocate memory for each line
  336. ofs->ln = malloc(ofs->line_ct * sizeof(char *)+1);
  337. for(y=0; y < ofs->line_ct; ++y)
  338. {
  339. ofs->ln[y] = (char *)malloc(SMALLBUFF);
  340. memset(ofs->ln[y], '\0', SMALLBUFF);
  341. #ifdef DEBUG
  342. printf("ALLOCATED MEMORY FOR LINE %d\n", y);
  343. #endif
  344. }
  345. // store lines from buff into ofs as individual '\0' terminated strings
  346. // since I memset the strings to '\0' I shouldn't have to null terminate right? ok...
  347. for(x=y=z=0; buff[z] != '\0' && x < ofs->line_ct;)
  348. {
  349. if(buff[z] != '\n' && buff[z] != '\r' && buff[z] != '\0')
  350. {
  351. ofs->ln[x][y] = buff[z];
  352. ++y;
  353. // concatinate destination buffer, no line should be >= SMALLBUFF
  354. if(y >= SMALLBUFF-1) while(buff[z] != '\n' && buff[z] != '\0') ++z;
  355. else ++z;
  356. }
  357. else
  358. {
  359. ++x;
  360. // concatinate lines to be stored if more than MAX_LINES (safeguard)
  361. if(x >= ofs->line_ct) while(buff[z] != '\0') ++z;
  362. else
  363. {
  364. while(buff[z] == '\r' || buff[z] == '\n') ++z;
  365. y=0;
  366. }
  367. }
  368. }
  369. #ifdef DEBUG
  370. for(x=0; x<ofs->line_ct; ++x)
  371. {
  372. printf("ofs->ln[%d][0]: [%c]\nofs->ln[%d]: [%s]\n", x, ofs->ln[x][0], x, ofs->ln[x]);
  373. }
  374. #endif
  375. return ofs->line_ct;
  376. }
  377. int irc_clear_ircofs(struct ircofs *ofs)
  378. {
  379. int x;
  380. // frees memory allocated for this structure
  381. for(x=0; x<ofs->line_ct; ++x)
  382. {
  383. free(ofs->ln[x]);
  384. }
  385. // frees memory allocated for pointers
  386. free(ofs->ln);
  387. // reinitializes the structure
  388. irc_ircofs_init(ofs);
  389. return 1;
  390. }
  391. /***************************************
  392. * Set Nonblocking *
  393. ***************************************/
  394. void nonblocking(int *sockfd)
  395. {
  396. irc_precall();
  397. fcntl(*sockfd, F_SETFL, O_NONBLOCK);
  398. return;
  399. }
  400. void irc_precall()
  401. {
  402. memset(err_buff, '\0', sizeof(err_buff));
  403. return;
  404. }
  405. char *irc_error()
  406. {
  407. if(err_buff[0] != '\0') return err_buff;
  408. strcpy(err_buff, "Unknown Error");
  409. return err_buff;
  410. }
  411. char *ping_message_index_ping(char *pingmsg)
  412. {
  413. char *p=pingmsg;
  414. if(comparen(p, "PING :", 6))
  415. {
  416. return p + 6;
  417. }
  418. return NULL;
  419. }
  420. unsigned char irc_is_ping(char *ln)
  421. {
  422. if(ping_message_index_ping(ln) != NULL) return 1;
  423. return 0;
  424. }
  425. char *channel_message_index_channel(char *chanmsg)
  426. {
  427. char *p;
  428. int flag=0;
  429. p = chanmsg;
  430. if(*p != ':') return NULL;
  431. while(*p != ' ' && *p != '\0')
  432. {
  433. if(*p == '!') ++flag;
  434. if(*p == '@') ++flag;
  435. ++p;
  436. }
  437. if(flag < 2) return NULL;
  438. while(*p != ' ' && *p != '\0') ++p;
  439. while(*p != ' ' && *p != '\0') ++p;
  440. if(*p == ' ') ++p;
  441. if(!comparen(p, "PRIVMSG", 7)) return NULL;
  442. while(*p != ' ' && *p != '\0') ++p;
  443. if(*p == ' ') ++p;
  444. if(*p == '#' || *p == '&') return p;
  445. return NULL;
  446. }
  447. unsigned char irc_is_channel_message(char *ln)
  448. {
  449. if(channel_message_index_channel(ln) != NULL) return 1;
  450. return 0;
  451. }
  452. char *user_message_index_nick(char *usermsg)
  453. {
  454. char *p;
  455. int flag=0;
  456. p = usermsg;
  457. if(*p != ':') return NULL;
  458. while(*p != ' ' && *p != '\0')
  459. {
  460. if(*p == '!') ++flag;
  461. if(*p == '@') ++flag;
  462. ++p;
  463. }
  464. if(flag < 2) return NULL;
  465. while(*p != ' ' && *p != '\0') ++p;
  466. if(*p == ' ') ++p;
  467. if(!comparen(p, "PRIVMSG", 7)) return NULL;
  468. while(*p != ' ' && *p != '\0') ++p;
  469. if(*p == ' ') ++p;
  470. if(*p != '#' && *p != '&' && *p != '\0' && *p != ':') return p;
  471. return NULL;
  472. }
  473. unsigned char irc_is_user_message(char *ln)
  474. {
  475. if(user_message_index_nick(ln) != NULL) return 1;
  476. return 0;
  477. }
  478. char *irc_mode_message_index_mode(char *modemsg)
  479. {
  480. char *p;
  481. int flag=0;
  482. p = modemsg;
  483. if(*p != ':') return NULL;
  484. while(*p != ' ' && *p != '\0')
  485. {
  486. if(*p == '!') ++flag;
  487. if(*p == '@') ++flag;
  488. ++p;
  489. }
  490. if(flag < 2) return NULL;
  491. while(*p != ' ' && *p != '\0') ++p;
  492. if(*p == ' ') ++p;
  493. if(comparen(p, "MODE ", 5)) return p + 5;
  494. return NULL;
  495. }
  496. unsigned char irc_is_mode_message(char *ln)
  497. {
  498. if(irc_mode_message_index_mode(ln) != NULL) return 1;
  499. return 0;
  500. }
  501. char *disconnect_message_index_reason(char *discmsg)
  502. {
  503. char *p;
  504. p=discmsg;
  505. if(comparen(p, "ERROR", 5))
  506. {
  507. while(*p != ':' && *p != '\0') ++p;
  508. if(*p == '\0') return NULL;
  509. if(comparen(p, ":Closing Link:", 14)) return p+14;
  510. }
  511. return NULL;
  512. }
  513. unsigned char irc_is_disconnect_message(char *ln)
  514. {
  515. if(disconnect_message_index_reason(ln) != NULL) return 1;
  516. return 0;
  517. }
  518. char *join_message_index_nick(char *joinmsg)
  519. {
  520. char *p;
  521. p=joinmsg;
  522. if(*p != ':') return NULL;
  523. while(*p != ' ' && *p != '\0') ++p;
  524. if(*p == ' ') ++p;
  525. else return NULL;
  526. if(*p == '\0') return NULL;
  527. if(comparen("JOIN", p, 4)) return joinmsg+1;
  528. return NULL;
  529. }
  530. char *join_message_index_channel(char *joinmsg)
  531. {
  532. char *p;
  533. p=joinmsg;
  534. if(*p != ':') return NULL;
  535. while(*p != ' ' && *p != '\0') ++p;
  536. if(*p == ' ') ++p;
  537. else return NULL;
  538. if(*p == '\0') return NULL;
  539. if(comparen("JOIN ", p, 5)) return p+5;
  540. return NULL;
  541. }
  542. unsigned char irc_is_join_message(char *ln)
  543. {
  544. if(join_message_index_nick(ln) != NULL) return 1;
  545. return 0;
  546. }
  547. char *part_message_index_nick(char *partmsg)
  548. {
  549. char *p;
  550. p=partmsg;
  551. if(*p != ':') return NULL;
  552. while(*p != ' ' && *p != '\0') ++p;
  553. if(*p == ' ') ++p;
  554. else return NULL;
  555. if(*p == '\0') return NULL;
  556. if(comparen(p, "PART ", 5)) return partmsg+1;
  557. return NULL;
  558. }
  559. char *part_message_index_channel(char *partmsg)
  560. {
  561. char *p;
  562. p=partmsg;
  563. if(*p != ':') return NULL;
  564. while(*p != ' ' && *p != '\0') ++p;
  565. if(*p == ' ') ++p;
  566. else return NULL;
  567. if(*p == '\0') return NULL;
  568. if(comparen(p, "PART ", 5)) return p+6;
  569. return NULL;
  570. }
  571. unsigned char irc_is_part_message(char *ln)
  572. {
  573. if(part_message_index_nick(ln) != NULL) return 1;
  574. return 0;
  575. }
  576. char *quit_message_index_nick(char *quitmsg)
  577. {
  578. char *p;
  579. p=quitmsg;
  580. if(*p != ':') return NULL;
  581. while(*p != ' ' && *p != '\0') ++p;
  582. if(*p == ' ') ++p;
  583. else return NULL;
  584. if(*p == '\0') return NULL;
  585. if(comparen(p, "QUIT", 4)) return quitmsg+1;
  586. return NULL;
  587. }
  588. char *quit_message_index_reason(char *quitmsg)
  589. {
  590. char *p;
  591. p=quitmsg;
  592. if(*p != ':') return NULL;
  593. while(*p != ' ' && *p != '\0') ++p;
  594. if(*p == ' ') ++p;
  595. else return NULL;
  596. if(*p == '\0') return NULL;
  597. if(comparen(p, "QUIT ", 5)) return p+5;
  598. return NULL;
  599. }
  600. unsigned char irc_is_quit_message(char *ln)
  601. {
  602. if(quit_message_index_nick(ln) != NULL) return 1;
  603. return 0;
  604. }
  605. char *nick_list_index_first_nick(char *nicklistmsg)
  606. {
  607. char *p;
  608. p=nicklistmsg;
  609. if(*p != ':') return NULL;
  610. while(*p != ' ' && *p != '\0') ++p;
  611. if(*p == ' ') ++p;
  612. else return NULL;
  613. if(*p == '\0') return 0;
  614. if(comparen(p, "353 ", 4))
  615. {
  616. while(*p != ':' && *p != '\0') ++p;
  617. while(*p == ' ') ++p;
  618. if(*p != '\0' && *(++p) != '\0') return p;
  619. else return NULL;
  620. }
  621. return NULL;
  622. }
  623. char *nick_list_index_channel(char *nicklistmsg)
  624. {
  625. char *p;
  626. p=nicklistmsg;
  627. if(*p != ':') return NULL;
  628. while(*p != ' ' && *p != '\0') ++p;
  629. if(*p != ' ') return NULL;
  630. ++p;
  631. if(*p == '\0') return NULL;
  632. if(comparen(p, "353 ", 4))
  633. {
  634. while(*p != '#' && *p != '&' && *p != '\0') ++p;
  635. if(*p != '#' && *p != '&') return NULL;
  636. return p;
  637. }
  638. return NULL;
  639. }
  640. unsigned char irc_is_nick_list(char *ln)
  641. {
  642. if(nick_list_index_first_nick(ln) != NULL) return 1;
  643. return 0;
  644. }
  645. unsigned char irc_is_nick_list_complete(char *ln)
  646. {
  647. if(rpl_index_message("366", ln) == NULL) return 0;
  648. else return 1;
  649. }
  650. char *join_reject_index_channel(char *joinrejectmsg)
  651. {
  652. int chan=0;
  653. int ref=0;
  654. if(joinrejectmsg[0] != ':') return NULL;
  655. ++ref;
  656. while(joinrejectmsg[ref] != ':' && joinrejectmsg[ref] != '\0')
  657. {
  658. if(joinrejectmsg[ref] == '#' || joinrejectmsg[ref] == '&') chan=ref;
  659. ++ref;
  660. }
  661. if(joinrejectmsg[ref] == '\0' || joinrejectmsg[ref] == '\0') return NULL;
  662. if(comparen(joinrejectmsg+ref, ":Cannot join channel ", 21))
  663. {
  664. return joinrejectmsg+chan;
  665. }
  666. return NULL;
  667. }
  668. unsigned char irc_is_join_reject(char *ln)
  669. {
  670. if(join_reject_index_channel(ln) != NULL) return 1;
  671. return 0;
  672. }
  673. int irc_user_nickchange(char *nickchangemsg, struct t_channels *ircch)
  674. {
  675. char oldnick[48], newnick[48], *d, *p;
  676. unsigned int x, y, retval;
  677. if((p=nick_change_index_newnick(nickchangemsg)) == NULL) return 0;
  678. d=newnick;
  679. while(*p != ' ' && *p != '\0') *(d++) = *(p++);
  680. *d = '\0';
  681. if((p=nick_change_index_oldnick(nickchangemsg)) == NULL) return 0;
  682. d=oldnick;
  683. while(*p != '!' && *p != '\0') *(d++) = *(p++);
  684. *d = '\0';
  685. retval=0;
  686. for(x=0; x<ircch->field_ct; ++x)
  687. {
  688. for(y=0; y<ircch->channels[x].user_field_ct; ++y)
  689. {
  690. if(compare(ircch->channels[x].users[y].nick, oldnick))
  691. {
  692. strcpy(ircch->channels[x].users[y].nick, newnick);
  693. ++retval;
  694. }
  695. }
  696. }
  697. if(retval == 0)
  698. {
  699. sprintf(err_buff, "irc_user_nickchange: didn't change any nicks from [%s] to [%s]",
  700. oldnick, newnick);
  701. return 0;
  702. }
  703. return 1;
  704. }
  705. unsigned char irc_is_nick_change(char *ln)
  706. {
  707. if(nick_change_index_newnick(ln) != NULL) return 1;
  708. return 0;
  709. }
  710. char *motd_index_message(char *motdmsg)
  711. {
  712. return rpl_index_message("372", motdmsg);
  713. }
  714. int irc_is_motd_message(char *ln)
  715. {
  716. if(motd_index_message(ln) != NULL) return 1;
  717. else return 0;
  718. }
  719. char *motd_end_index_message(char *motdmsg)
  720. {
  721. return rpl_index_message("376", motdmsg);
  722. }
  723. int irc_is_motd_end_message(char *ln)
  724. {
  725. if(motd_end_index_message(ln) != NULL) return 1;
  726. else return 0;
  727. }
  728. char *notice_index_message(char *noticemsg)
  729. {
  730. char *p=noticemsg;
  731. if(*p != ':') return NULL;
  732. while(*p != ' ' && *p != '\0') ++p;
  733. if(*p != ' ') return NULL;
  734. if(!comparen(p, " NOTICE ", 8)) return NULL;
  735. while(*p != ':' && *p != '\0') ++p;
  736. if(*p != ':') return NULL;
  737. return ++p;
  738. }
  739. int irc_is_notice_message(char *ln)
  740. {
  741. if(notice_index_message(ln) != NULL) return 1;
  742. else return 0;
  743. }
  744. char *welcome_message_index_message(char *welcomemsg)
  745. {
  746. return rpl_index_message("001", welcomemsg);
  747. }
  748. unsigned char irc_is_welcome_message(char *ln)
  749. {
  750. if(welcome_message_index_message(ln) != NULL) return 1;
  751. return 0;
  752. }
  753. char *yourhost_message_index_message(char *yourhostmsg)
  754. {
  755. return rpl_index_message("002", yourhostmsg);
  756. }
  757. unsigned char irc_is_yourhost_message(char *ln)
  758. {
  759. if(yourhost_message_index_message(ln) != NULL) return 1;
  760. return 0;
  761. }
  762. char *created_message_index_message(char *createdmsg)
  763. {
  764. return rpl_index_message("003", createdmsg);
  765. }
  766. unsigned char irc_is_created_message(char *ln)
  767. {
  768. if(created_message_index_message(ln) != NULL) return 1;
  769. return 0;
  770. }
  771. char *myinfo_message_index_message(char *myinfomsg)
  772. {
  773. return rpl_index_message("004", myinfomsg);
  774. }
  775. unsigned char irc_is_myinfo_message(char *ln)
  776. {
  777. if(myinfo_message_index_message(ln) != NULL) return 1;
  778. return 0;
  779. }
  780. char *bounce_message_index_message(char *bouncemsg)
  781. {
  782. return rpl_index_message("005", bouncemsg);
  783. }
  784. unsigned char irc_is_bounce_message(char *ln)
  785. {
  786. if(bounce_message_index_message(ln) != NULL) return 1;
  787. return 0;
  788. }
  789. char *notice_auth_message_index_message(char *noticemsg)
  790. {
  791. if(!comparen(noticemsg, "NOTICE AUTH :", 13)) return NULL;
  792. return noticemsg+13;
  793. }
  794. unsigned char irc_is_notice_auth_message(char *ln)
  795. {
  796. if(notice_auth_message_index_message(ln) != NULL) return 1;
  797. return 0;
  798. }
  799. unsigned int irc_translate(char *ln)
  800. {
  801. if(irc_is_ping(ln)) return PING_MSG;
  802. else if(irc_is_channel_message(ln)) return CHANNEL_PRIVMSG_MSG;
  803. else if(irc_is_user_message(ln)) return USER_PRIVMSG_MSG;
  804. else if(irc_is_mode_message(ln)) return MODE_CHANGE_MSG;
  805. else if(irc_is_disconnect_message(ln)) return DISCONNECT_MSG;
  806. else if(irc_is_join_message(ln)) return USER_JOIN_MSG;
  807. else if(irc_is_part_message(ln)) return USER_PART_MSG;
  808. else if(irc_is_quit_message(ln)) return USER_QUIT_MSG;
  809. else if(irc_is_nick_list(ln)) return CHANNEL_NAMES_MSG;
  810. else if(irc_is_nick_list_complete(ln)) return CHANNEL_NAMES_COMPLETE_MSG;
  811. else if(irc_is_join_reject(ln)) return CHANNEL_JOIN_REJECT_MSG;
  812. else if(irc_is_nick_change(ln)) return USER_NICK_CHANGE_MSG;
  813. else if(irc_is_motd_message(ln)) return MOTD_MSG;
  814. else if(irc_is_motd_end_message(ln)) return MOTD_END_MSG;
  815. else if(irc_is_notice_message(ln)) return NOTICE_MSG;
  816. else if(irc_is_welcome_message(ln)) return WELCOME_MSG;
  817. else if(irc_is_yourhost_message(ln)) return YOURHOST_MSG;
  818. else if(irc_is_created_message(ln)) return CREATED_MSG;
  819. else if(irc_is_myinfo_message(ln)) return MYINFO_MSG;
  820. else if(irc_is_bounce_message(ln)) return BOUNCE_MSG;
  821. else if(irc_is_notice_auth_message(ln)) return NOTICEAUTH_MSG;
  822. return UNKNOWN_MSG;
  823. }
  824. char *rpl_index_message(char *numeric, char *message)
  825. {
  826. char *p=message;
  827. char buf[12];
  828. if(strlen(numeric) > 3)
  829. {
  830. strcpy(err_buff, "not a numeric");
  831. return NULL;
  832. }
  833. sprintf(buf, " %s ", numeric);
  834. if(*p != ':') return NULL;
  835. while(*p != ' ' && *p != '\0') ++p;
  836. if(*p != ' ') return NULL;
  837. if(!comparen(p, buf, 5)) return NULL;
  838. while(*p != ':' && *p != '\0') ++p;
  839. if(*p != ':') return NULL;
  840. return ++p;
  841. }
  842. char *irc_numeric(char *ln, char *dest)
  843. {
  844. char *p=ln;
  845. if(*p != ':') return dest=NULL;
  846. ++p;
  847. while(*p != ' ' && *p != '\0') ++p;
  848. if(*p == '\0' || *(p+1) == '\0') return 0;
  849. ++p;
  850. if(*p >= '0' && *p <= '9' && *(p+1) >= '0' && *(p+1) <= '9' && *(p+2) >= '0' && *(p+2) <= '9')
  851. {
  852. *dest = *p;
  853. *(dest+1) = *(p+1);
  854. *(dest+2) = *(p+2);
  855. *(p+3) = '\0';
  856. return dest;
  857. }
  858. else return NULL;
  859. }
  860. char *irc_sender(char *ln, char *dest)
  861. {
  862. char *p, *d;
  863. p=ln;
  864. if(*p != ':') return NULL;
  865. ++p;
  866. d=dest;
  867. while(*p != ' ' && *p != '\0')
  868. {
  869. *d = *p;
  870. ++p;
  871. ++d;
  872. }
  873. *d = '\0';
  874. return dest;
  875. }
  876. char *irc_sender_nick(char *ln, char *dest)
  877. {
  878. char *p, *d;
  879. p=ln;
  880. if(*p != ':') return NULL;
  881. ++p;
  882. d=dest;
  883. while(*p != '!' && *p != '\0')
  884. {
  885. *d = *p;
  886. ++p;
  887. ++d;
  888. }
  889. *d = '\0';
  890. return dest;
  891. }
  892. char *irc_sender_ip(char *ln, char *dest)
  893. {
  894. char *p, *d;
  895. int flag=0;
  896. p=ln;
  897. if(*p != ':') return NULL;
  898. ++p;
  899. d=dest;
  900. while(*p != '@' && *p != '\0') ++p;
  901. if(*p == '@') ++p;
  902. while(*p != ' ' && *p != '\0' && flag <= 3)
  903. {
  904. if(*p == '-' || *p == '.')
  905. {
  906. if(flag < 3) *d = '.';
  907. ++flag;
  908. }
  909. else *d = *p;
  910. ++p;
  911. ++d;
  912. }
  913. *d = '\0';
  914. return dest;
  915. }
  916. char *irc_sender_channel(char *ln, char *dest)
  917. {
  918. char *p, *d;
  919. p=ln;
  920. if(*p != ':') return NULL;
  921. ++p;
  922. d=dest;
  923. while(*p != ' ' && *p != '\0') ++p;
  924. if(*p == ' ') ++p;
  925. if(*p != '\0') while(*p != ' ' && *p != '\0') ++p;
  926. if(*p == ' ') ++p;
  927. if(*p == '#' || *p == '&')
  928. {
  929. while(*p != ' ' && *p != '\0')
  930. {
  931. *d = *p;
  932. ++p;
  933. ++d;
  934. }
  935. *d = '\0';
  936. }
  937. return dest;
  938. }
  939. inline char *irc_numeric_channel(char *ln, char *dest)
  940. {
  941. char *p, *d;
  942. p=ln;
  943. if(*p != ':') return NULL;
  944. ++p;
  945. d=dest;
  946. while(*p != ' ' && *p != '\0') ++p;
  947. if(*p == ' ') ++p;
  948. if(*p != '\0') while(*p != ' ' && *p != '\0') ++p;
  949. if(*p == ' ') ++p;
  950. if(*p != '\0') while(*p != ' ' && *p != '\0') ++p;
  951. if(*p == ' ') ++p;
  952. if(*p == '#' || *p == '&')
  953. {
  954. while(*p != ' ' && *p != '\0')
  955. {
  956. *d = *p;
  957. ++p;
  958. ++d;
  959. }
  960. *d = '\0';
  961. }
  962. return dest;
  963. }
  964. char *irc_sender_message(char *ln, char *dest)
  965. {
  966. char *p;
  967. char *d;
  968. p=ln;
  969. if(*p != ':') return NULL;
  970. ++p;
  971. while(*p != ':' && *p != '\0') ++p;
  972. if(*p != ':') return NULL;
  973. ++p;
  974. if(*p == '\0') return NULL;
  975. d=dest;
  976. while(*p != '\0')
  977. {
  978. *d = *p;
  979. ++p;
  980. ++d;
  981. }
  982. *d = '\0';
  983. return dest;
  984. }
  985. char *irc_ping_responce(char *ln, char *dest)
  986. {
  987. char *p;
  988. if((p=ping_message_index_ping(ln)) != NULL)
  989. {
  990. sprintf(dest, "PONG :%s\r\n", p);
  991. return dest;
  992. }
  993. return NULL;
  994. }
  995. int irc_create_first_user(struct ircchannel *ch, char *nick)
  996. {
  997. if(ch->user_field_ct >= 1)
  998. {
  999. strcpy(err_buff, "irc_create_first_user: user_field_ct != 0, use recycle/append!");
  1000. return 0;
  1001. }
  1002. ch->users = malloc(sizeof(struct ircuser));
  1003. if(ch->users == NULL)
  1004. {
  1005. strcpy(err_buff, "irc_create_first_user: Unable to allocate memory");
  1006. return 0;
  1007. }
  1008. strcpy(ch->users[0].nick, nick);
  1009. ch->users[0].active = 1;
  1010. ch->user_field_ct = 1;
  1011. ch->user_active_ct = 1;
  1012. return 1;
  1013. }
  1014. int irc_append_user(struct ircchannel *ch, char *nick)
  1015. {
  1016. int x;
  1017. for(x=0; x<ch->user_field_ct; ++x)
  1018. {
  1019. if(ch->users[x].active && compare(ch->users[x].nick,nick))
  1020. {
  1021. sprintf(err_buff, "irc_append_user: %s is already active in the list, cannot activate this user again!", nick);
  1022. return 0;
  1023. }
  1024. }
  1025. if(ch->users == NULL || ch->user_field_ct <= 0)
  1026. {
  1027. strcpy(err_buff, "irc_append_user: Use irc_create_first_user first!");
  1028. return 0;
  1029. }
  1030. ch->users = realloc(ch->users, sizeof(struct ircuser) * (ch->user_field_ct+1));
  1031. if(ch->users == NULL)
  1032. {
  1033. strcpy(err_buff, "irc_append_user: Unable to reallocate user buffer");
  1034. return 0;
  1035. }
  1036. strcpy(ch->users[ch->user_field_ct].nick, nick);
  1037. ch->users[ch->user_field_ct].active = 1;
  1038. ch->user_field_ct++;
  1039. ch->user_active_ct++;
  1040. return ch->user_field_ct;
  1041. }
  1042. int irc_recycle_user(struct ircchannel *ch, char *nick)
  1043. {
  1044. int x;
  1045. for(x=0; x<ch->user_field_ct; ++x)
  1046. {
  1047. if(ch->users[x].active && compare(ch->users[x].nick,nick))
  1048. {
  1049. strcpy(err_buff, "irc_recycle_user: this user is active in the list, use irc_user_deactivate first");
  1050. return 0;
  1051. }
  1052. }
  1053. if(ch->users == NULL || ch->user_field_ct == 0)
  1054. {
  1055. strcpy(err_buff, "irc_recycle_user: Cannot recycle, use irc_create_first_user");
  1056. return 0;
  1057. }
  1058. for(x=0; x<ch->user_field_ct; ++x)
  1059. {
  1060. if(ch->users[x].active == 0)
  1061. {
  1062. ch->users[x].active = 1;
  1063. strcpy(ch->users[x].nick, nick);
  1064. ++ch->user_active_ct;
  1065. return x+1;
  1066. }
  1067. }
  1068. return 0;
  1069. }
  1070. int irc_user_deactivate(struct ircchannel *ch, char *nick)
  1071. {
  1072. int x;
  1073. if(ch->users == NULL || ch->user_field_ct <= 0 || ch->user_active_ct <= 0)
  1074. {
  1075. strcpy(err_buff, "irc_user_deactivate: no users to scan");
  1076. return 0;
  1077. }
  1078. for(x=0; x<ch->user_field_ct; ++x)
  1079. {
  1080. if(ch->users[x].active == 1)
  1081. {
  1082. if(compare(ch->users[x].nick,nick))
  1083. {
  1084. ch->users[x].active = 0;
  1085. --ch->user_active_ct;
  1086. return 1;
  1087. }
  1088. }
  1089. }
  1090. sprintf(err_buff, "irc_user_deactivate: no users in [%s] with nick [%s]", ch->name, nick);
  1091. return 0;
  1092. }
  1093. int irc_adduser(char *joinmsg, struct t_channels *ircch)
  1094. {
  1095. char *c, *n, *d, nick[32];
  1096. c=join_message_index_channel(joinmsg);
  1097. n=join_message_index_nick(joinmsg);
  1098. if(c == NULL || n == NULL)
  1099. {
  1100. strcpy(err_buff, "irc_adduser: invalid joinmsg");
  1101. return 0;
  1102. }
  1103. d=nick;
  1104. while(*n != '!' && *n != '\0')
  1105. {
  1106. *d = *n;
  1107. ++d;
  1108. ++n;
  1109. }
  1110. *d = '\0';
  1111. return irc_adduser2(nick, c, ircch);
  1112. }
  1113. int irc_adduser2(char *nick, char *channel, struct t_channels *ircch)
  1114. {
  1115. int x, y;
  1116. unsigned char set_op=0, set_voice=0;
  1117. if(nick[0] == '+')
  1118. {
  1119. ++nick;
  1120. set_voice=1;
  1121. }
  1122. if(nick[0] == '@')
  1123. {
  1124. ++nick;
  1125. set_op=1;
  1126. }
  1127. for(x=0; x<ircch->field_ct; ++x)
  1128. {
  1129. if(compare(ircch->channels[x].name, channel))
  1130. {
  1131. if(ircch->channels[x].user_field_ct == 0)
  1132. {
  1133. if(!irc_create_first_user(&ircch->channels[x], nick)) return 0;
  1134. goto success;
  1135. }
  1136. else
  1137. {
  1138. if(!irc_recycle_user(&ircch->channels[x], nick))
  1139. {
  1140. if(!irc_append_user(&ircch->channels[x], nick)) return 0;
  1141. }
  1142. goto success;
  1143. }
  1144. }
  1145. }
  1146. sprintf(err_buff, "irc_adduser: Could not find channel %s in given channel list", channel);
  1147. return 0;
  1148. success:
  1149. for(y=0; y<ircch->channels[x].user_field_ct; ++y)
  1150. {
  1151. if(compare(ircch->channels[x].users[y].nick,nick))
  1152. {
  1153. // set new user attributes
  1154. ircch->channels[x].users[y].voice=set_voice;
  1155. ircch->channels[x].users[y].op=set_op;
  1156. }
  1157. }
  1158. }
  1159. int irc_remuser(char *partmsg, struct t_channels *ircch)
  1160. {
  1161. char *c, *n, *d, nick[32];
  1162. c=part_message_index_channel(partmsg);
  1163. n=part_message_index_nick(partmsg);
  1164. if(c == NULL || n == NULL)
  1165. {
  1166. strcpy(err_buff, "irc_remuser: invalid partmsg");
  1167. return 0;
  1168. }
  1169. d=nick;
  1170. while(*n != '!' && *n != '\0')
  1171. {
  1172. *d = *n;
  1173. ++n;
  1174. ++d;
  1175. }
  1176. *d = '\0';
  1177. return irc_remuser2(nick, c, ircch);
  1178. }
  1179. int irc_remuser2(char *nick, char *channel, struct t_channels *ircch)
  1180. {
  1181. int x;
  1182. for(x=0; x<ircch->field_ct; ++x)
  1183. {
  1184. if(compare(ircch->channels[x].name, channel))
  1185. {
  1186. return irc_user_deactivate(&ircch->channels[x], nick);
  1187. }
  1188. }
  1189. sprintf(err_buff, "irc_remuser: unable to find channel %s in channel list", channel);
  1190. return 0;
  1191. }
  1192. int irc_remuser3(char *partmsg, struct t_channels *ircch)
  1193. {
  1194. char *n, *d, nick[32];
  1195. int x, retval=0;
  1196. n=quit_message_index_nick(partmsg);
  1197. if(n == NULL)
  1198. {
  1199. strcpy(err_buff, "irc_remuser: invalid partmsg");
  1200. return 0;
  1201. }
  1202. d=nick;
  1203. while(*n != '!' && *n != '\0')
  1204. {
  1205. *d = *n;
  1206. ++n;
  1207. ++d;
  1208. }
  1209. *d = '\0';
  1210. for(x=0; x<ircch->field_ct; ++x)
  1211. {
  1212. if(irc_remuser2(nick, ircch->channels[x].name, ircch)) ++retval;
  1213. }
  1214. if(!retval) strcpy(err_buff, "irc_remuser3: user does not exist in any channels");
  1215. return retval;
  1216. }
  1217. int irc_addusers(char *nickmsg, struct t_channels *ircch)
  1218. {
  1219. char *p, *c, buff[32], buff2[32];
  1220. int x;
  1221. p=nickmsg;
  1222. if(*p != ':') goto invalid_nickmsg;
  1223. while(*p != ' ' && *p != '\0') ++p;
  1224. if(*p != ' ') goto invalid_nickmsg;
  1225. ++p;
  1226. if(!comparen("353 ", p, 4)) goto invalid_nickmsg;
  1227. while(*p != '@' && *p != '=' && *p != '\0') ++p;
  1228. if(*p != '@' && *p != '=') goto invalid_nickmsg;
  1229. ++p;
  1230. if(*p != ' ') goto invalid_nickmsg;
  1231. ++p;
  1232. if(*p != '#' && *p != '&') goto invalid_nickmsg;
  1233. c=buff;
  1234. while(*p != ' ' && *p != '\0')
  1235. {
  1236. *c = *p;
  1237. ++c;
  1238. ++p;
  1239. }
  1240. *c = '\0';
  1241. if(*p != ' ') goto invalid_nickmsg;
  1242. while(*p != ':' && *p != '\0') ++p;
  1243. if(*p != ':') goto invalid_nickmsg;
  1244. ++p;
  1245. while(*p != '\0')
  1246. {
  1247. c = buff2;
  1248. if(*p == ' ') ++p;
  1249. while(*p != ' ' && *p != '\0')
  1250. {
  1251. *c = *p;
  1252. ++c;
  1253. ++p;
  1254. }
  1255. *c = '\0';
  1256. if(strlen(buff2) > 0 && strlen(buff) > 0)
  1257. {
  1258. if(!irc_adduser2(buff2, buff, ircch))
  1259. {
  1260. printf("#### UNABLE TO ADD A USER FROM NICKLIST: chan[%s] user[%s] ###\n### %s ###\n",
  1261. buff, buff2, irc_error());
  1262. }
  1263. }
  1264. }
  1265. if(*p == '\0') return 1;
  1266. sprintf(err_buff, "irc_addusers: Channel %s is not in the channel list", buff);
  1267. return 0;
  1268. invalid_nickmsg:
  1269. strcat(err_buff, "irc_addusers: invalid nickmsg");
  1270. return 0;
  1271. }
  1272. int irc_addircuser(struct ircchannel *dest, struct ircuser src)
  1273. {
  1274. if(dest->user_field_ct == 0)
  1275. {
  1276. dest->users = malloc(sizeof(struct ircuser));
  1277. if(dest->users == NULL)
  1278. {
  1279. strcpy(err_buff, "irc_addircuser: unable to allocate memory");
  1280. return 0;
  1281. }
  1282. }
  1283. else
  1284. {
  1285. dest->users = realloc(dest->users, sizeof(struct ircuser) * (dest->user_field_ct + 1));
  1286. if(dest->users == NULL)
  1287. {
  1288. strcpy(err_buff, "irc_addircuser: unable to reallocate memory");
  1289. return 0;
  1290. }
  1291. }
  1292. ++dest->user_field_ct;
  1293. memcpy(&dest->users[dest->user_field_ct-1], &src, sizeof(struct ircuser));
  1294. return dest->user_field_ct;
  1295. }
  1296. int irc_remircuser(char *nick_name, char *channel_name, struct t_channels *ircch)
  1297. {
  1298. int x;
  1299. for(x=0; x<ircch->field_ct; ++x)
  1300. {
  1301. if(compare(ircch->channels[x].name,channel_name))
  1302. {
  1303. return irc_user_deactivate(&ircch->channels[x], nick_name);
  1304. }
  1305. }
  1306. sprintf(err_buff, "irc_remircuser: unable to find channel %s in channel list", channel_name);
  1307. return 0;
  1308. }
  1309. int irc_channels_cleanup(struct t_channels *ircch)
  1310. {
  1311. struct t_channels chbuff;
  1312. int x, y;
  1313. memcpy(&chbuff, ircch, sizeof(struct t_channels));
  1314. memset(ircch, '\0', sizeof(struct t_channels));
  1315. for(x=0; x<chbuff.field_ct; ++x)
  1316. {
  1317. if(chbuff.channels[x].active)
  1318. {
  1319. if(!irc_create_first_channel(ircch, chbuff.channels[x].name))
  1320. {
  1321. if(!irc_recycle_channel(ircch, chbuff.channels[x].name))
  1322. {
  1323. if(!irc_append_channel(ircch, chbuff.channels[x].name))
  1324. {
  1325. strcpy(err_buff, "irc_channel_sort: couldn't create/recycle/append an active channel");
  1326. // free up unsuccessful t_channels structure and set everything back to how it was
  1327. irc_clear_t_channels(ircch);
  1328. memcpy(ircch, &chbuff, sizeof(struct t_channels));
  1329. return 0;
  1330. }
  1331. }
  1332. }
  1333. }
  1334. printf("chbuff.channels[x].user_field_ct = %d\n", chbuff.channels[x].user_field_ct);
  1335. for(y=0; y<chbuff.channels[x].user_field_ct; ++y)
  1336. {
  1337. printf("chbuff.channels[%d].users[%d].acvtive = %d\n", x, y, chbuff.channels[x].users[y].active);
  1338. if(chbuff.channels[x].users[y].active)
  1339. {
  1340. puts("preparing to copy...");
  1341. if(!irc_create_first_user(ircch->channels, chbuff.channels[x].users[y].nick))
  1342. {
  1343. puts("couldn't create_first, trying to recycle...");
  1344. if(!irc_recycle_user(ircch->channels, chbuff.channels[x].users[y].nick))
  1345. {
  1346. puts("couldn't recycle, trying append...");
  1347. if(!irc_append_user(ircch->channels, chbuff.channels[x].users[y].nick))
  1348. {
  1349. puts("couldn't append, exiting...");
  1350. strcpy(err_buff, "irc_channel_sort: couldn't create/recycle/append an active user");
  1351. // free up unsuccessful t_channels structure and set everything back to how it was
  1352. irc_clear_t_channels(ircch);
  1353. memcpy(ircch, &chbuff, sizeof(struct t_channels));
  1354. return 0;
  1355. }
  1356. }
  1357. }
  1358. }
  1359. }
  1360. }
  1361. return 1;
  1362. }
  1363. int irc_t_channels_init(struct t_channels *ircch)
  1364. {
  1365. memset(ircch, 0, sizeof(struct t_channels));
  1366. return 1;
  1367. }
  1368. int irc_ircchannel_init(struct ircchannel *ch)
  1369. {
  1370. memset(ch, 0, sizeof(struct ircchannel));
  1371. return 1;
  1372. }
  1373. int irc_ircuser_init(struct ircuser *ircusr)
  1374. {
  1375. memset(ircusr, 0, sizeof(struct ircuser));
  1376. return 1;
  1377. }
  1378. inline int irc_clear_ircuser(struct ircuser *user)
  1379. {
  1380. return irc_ircuser_init(user);
  1381. }
  1382. int irc_clear_ircchannel(struct ircchannel *channel)
  1383. {
  1384. int x;
  1385. free(channel);
  1386. irc_ircchannel_init(channel);
  1387. return 1;
  1388. }
  1389. int irc_clear_t_channels(struct t_channels *ircch)
  1390. {
  1391. irc_clear_ircchannel(ircch->channels);
  1392. irc_t_channels_init(ircch);
  1393. return 0;
  1394. }
  1395. int irc_channel_deactivate(struct t_channels *ircch, char *name)
  1396. {
  1397. int x;
  1398. if(ircch == NULL || ircch->field_ct <= 0 || ircch->active_ct <= 0)
  1399. {
  1400. strcpy(err_buff, "irc_channel_dactivate: no channels to scan");
  1401. return 0;
  1402. }
  1403. for(x=0; x<ircch->field_ct; ++x)
  1404. {
  1405. if(ircch->channels[x].active)
  1406. {
  1407. if(compare(ircch->channels[x].name,name))
  1408. {
  1409. --ircch->active_ct;
  1410. free(ircch->channels[x].users);
  1411. irc_ircchannel_init(&ircch->channels[x]);
  1412. return 1;
  1413. }
  1414. }
  1415. }
  1416. strcpy(err_buff, "irc_channel_deactivate: no active channels with that name");
  1417. return 0;
  1418. }
  1419. int irc_create_first_channel(struct t_channels *ircch, char *name)
  1420. {
  1421. ircch->channels = malloc(sizeof(struct ircchannel));
  1422. if(ircch->channels == NULL)
  1423. {
  1424. strcpy(err_buff, "irc_create_first_channel: Unable to allocate memory");
  1425. return 0;
  1426. }
  1427. irc_ircchannel_init(&ircch->channels[0]);
  1428. strcpy(ircch->channels[0].name, name);
  1429. ircch->channels[0].active = 1;
  1430. ircch->field_ct = 1;
  1431. ircch->active_ct = 1;
  1432. return 1;
  1433. }
  1434. int irc_recycle_channel(struct t_channels *ircch, char *name)
  1435. {
  1436. int x;
  1437. for(x=0; x<ircch->field_ct; ++x)
  1438. {
  1439. if(ircch->channels[x].active && compare(ircch->channels[x].name,name))
  1440. {
  1441. strcpy(err_buff, "irc_recycle_channel: this channel is active in the list, use irc_channel_deactivate first");
  1442. return 0;
  1443. }
  1444. }
  1445. if(ircch->channels == NULL || ircch->field_ct == 0)
  1446. {
  1447. strcpy(err_buff, "irc_recycle_channel: Cannot recycle, use irc_create_first_channel");
  1448. return 0;
  1449. }
  1450. for(x=0; x<ircch->field_ct; ++x)
  1451. {
  1452. if(ircch->channels[x].active == 0)
  1453. {
  1454. irc_ircchannel_init(&ircch->channels[x]);
  1455. ircch->channels[x].active = 1;
  1456. strcpy(ircch->channels[x].name, name);
  1457. ++ircch->active_ct;
  1458. return x+1;
  1459. }
  1460. }
  1461. return 0;
  1462. }
  1463. int irc_append_channel(struct t_channels *ircch, char *name)
  1464. {
  1465. int x;
  1466. for(x=0; x<ircch->field_ct; ++x)
  1467. {
  1468. if(ircch->channels[x].active && compare(ircch->channels[x].name,name))
  1469. {
  1470. strcpy(err_buff, "irc_append_channel: this channel is active in the list, use irc_channel_deactivate first");
  1471. return 0;
  1472. }
  1473. }
  1474. if(ircch->channels == NULL || ircch->field_ct <= 0)
  1475. {
  1476. strcpy(err_buff, "irc_append_channel: Cannot append, use irc_create_first_channel");
  1477. return 0;
  1478. }
  1479. ircch->channels = realloc(ircch->channels, sizeof(struct ircchannel) * (ircch->field_ct+1));
  1480. if(ircch->channels == NULL)
  1481. {
  1482. strcpy(err_buff, "irc_append_channel: Unable to reallocate channel buffer");
  1483. return 0;
  1484. }
  1485. irc_ircchannel_init(&ircch->channels[ircch->field_ct]);
  1486. strcpy(ircch->channels[ircch->field_ct].name, name);
  1487. ircch->channels[ircch->field_ct].active = 1;
  1488. ircch->field_ct++;
  1489. ircch->active_ct++;
  1490. return ircch->field_ct;
  1491. }
  1492. char *nick_change_index_oldnick(char *nickchangemsg)
  1493. {
  1494. char *p;
  1495. p=nickchangemsg;
  1496. if(*p != ':') return NULL;
  1497. while(*p != ' ' && *p != '\0') ++p;
  1498. if(!comparen(" NICK ",p, 6)) return NULL;
  1499. return nickchangemsg+1;
  1500. }
  1501. char *nick_change_index_newnick(char *nickchangemsg)
  1502. {
  1503. char *p;
  1504. p=nickchangemsg;
  1505. while(*p != ' ' && *p != '\0') ++p;
  1506. if(!comparen(p, " NICK ", 6)) return NULL;
  1507. while(*p != ':' && *p != '\0') ++p;
  1508. if(*p != ':') return NULL;
  1509. return ++p;
  1510. }
  1511. char *channel_index_text(char *str)
  1512. {
  1513. char *p=str;
  1514. while(*p != '#' && *p != '&' && *p != '\0') ++p;
  1515. if(*p == '\0') return NULL;
  1516. else return p;
  1517. }
  1518. int compare(char *str1, char *str2)
  1519. {
  1520. int x=0;
  1521. while(str1[x] != '\0' && str2[x] != '\0')
  1522. {
  1523. if(str1[x] != str2[x])
  1524. {
  1525. if(str1[x] >= 'a' && str1[x] <= 'z')
  1526. {
  1527. if(str1[x]-32 != str2[x]) return 0;
  1528. }
  1529. else if(str1[x] >= 'A' && str1[x] <= 'Z')
  1530. {
  1531. if(str1[x]+32 != str2[x]) return 0;
  1532. }
  1533. else return 0;
  1534. }
  1535. ++x;
  1536. }
  1537. if(str1[x] == '\0' && str2[x] != '\0' || str1[x] != '\0' && str2[x] == '\0') return 0;
  1538. return 1;
  1539. }
  1540. int comparen(char *str1, char *str2, int len)
  1541. {
  1542. int x=0;
  1543. while(x < len && str1[x] != '\0' && str2[x] != '\0')
  1544. {
  1545. if(str1[x] != str2[x])
  1546. {
  1547. if(str1[x] >= 'a' && str1[x] <= 'z')
  1548. {
  1549. if(str1[x]-32 != str2[x]) return 0;
  1550. }
  1551. else if(str1[x] >= 'A' && str1[x] <= 'Z')
  1552. {
  1553. if(str1[x]+32 != str2[x]) return 0;
  1554. }
  1555. else return 0;
  1556. }
  1557. ++x;
  1558. }
  1559. if(x < len) return 0;
  1560. return 1;
  1561. }
  1562. char *index2string(char *dest, char *src)
  1563. {
  1564. int x=0;
  1565. while(src[x] == ' ') ++x;
  1566. while(src[x] != ' ')
  1567. {
  1568. dest[x] = src[x++];
  1569. }
  1570. dest[x] = '\0';
  1571. }