functions.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "main.h"
  2. #include "functions.h"
  3. #include "irc.h"
  4. /* Remove carriage return from string */
  5. const char *remove_creturn(char *str) {
  6. str = strtok(str, "\r");
  7. return str;
  8. }
  9. /* str_replace function - from stakoverflow */
  10. char *str_replace(char *orig, char *rep, char *with) {
  11. char *result, *buf, *ins;
  12. int replen, withlen, frontlen, count;
  13. // check arguments
  14. if (!orig) {
  15. return NULL;
  16. }
  17. if (!rep) {
  18. rep = "";
  19. }
  20. replen = strlen(rep);
  21. if (!with) {
  22. with = "";
  23. }
  24. withlen = strlen(with);
  25. ins = orig;
  26. for (count = 0; buf = strstr(ins, rep); ++count) {
  27. ins = buf + replen;
  28. }
  29. buf = result = malloc(strlen(orig) + (withlen - replen) * count + 1);
  30. if (!result) {
  31. return NULL;
  32. }
  33. while (count--) {
  34. ins = strstr(orig, rep);
  35. frontlen = ins - orig;
  36. buf = strncpy(buf, orig, frontlen) + frontlen;
  37. buf = strcpy(buf, with) + withlen;
  38. orig += frontlen + replen; // move to next "end of rep"
  39. }
  40. strcpy(buf, orig);
  41. return result;
  42. }
  43. char *bot_nick_exists(char *str, char *nick) {
  44. char *searchstr = strstr(str, nick);
  45. if(searchstr) {
  46. return "0";
  47. }
  48. return "1";
  49. }
  50. int rand_int() {
  51. int r = rand() % 50;
  52. return r+1;
  53. }
  54. int join_channels(int fd, char *filefd) {
  55. FILE *filename = fopen(filefd, "r");
  56. char *search = " ";
  57. if(filename != NULL) {
  58. /* buffer for each topic line */
  59. char line[2048];
  60. char channel[256];
  61. char sendbuf[2305];
  62. /* read the file */
  63. while(fgets(line, sizeof(line), filename) != NULL) {
  64. if(strncmp("\n", line, 1)==0) {
  65. continue;
  66. }
  67. char *linetok;
  68. linetok = strtok(line, search);
  69. /* join the channels!*/
  70. sprintf(sendbuf, "JOIN %s\r\nMODE %s :+tn\r\nMODE %s +o %s\r\n", linetok, linetok, linetok, nick);
  71. irc_send(fd, sendbuf);
  72. sleep(1);
  73. }
  74. }
  75. else
  76. {
  77. return 1;
  78. }
  79. return 0;
  80. }
  81. int set_channel_owner(char *owner, const char *channel, char *filefd) {
  82. FILE *file;
  83. char line[1024];
  84. if((file = fopen(filefd, "r"))) {
  85. while(fgets(line, sizeof(line), file)!=NULL) {
  86. char *dotok = strtok(line, " ");
  87. if(strncmp(channel, dotok, strlen(channel))==0) {
  88. fclose(file);
  89. return 1;
  90. }
  91. }
  92. fclose(file);
  93. }
  94. if((file = fopen(filefd, "a"))) {
  95. fprintf(file, "%s %s\n", channel, owner);
  96. fclose(file);
  97. return 0;
  98. }
  99. return 1;
  100. }
  101. int check_user_passwd(char *nick, const char *pass, char *filefd) {
  102. FILE *file;
  103. char line[1024];
  104. if((file = fopen(filefd, "r"))) {
  105. while(fgets(line, sizeof(line), file)!=NULL) {
  106. /* check if user exists */
  107. char *dotok = strtok(line, " ");
  108. if(strncmp(nick, dotok, strlen(nick))==0) {
  109. dotok = strtok(NULL, " ");
  110. if(strncmp(pass, dotok, strlen(pass))==0) {
  111. /* password correct */
  112. fclose(file);
  113. return 0;
  114. }
  115. else
  116. {
  117. /* password incorrect */
  118. fclose(file);
  119. return 1;
  120. }
  121. }
  122. }
  123. fclose(file);
  124. }
  125. /* user does not exist */
  126. return 2;
  127. }
  128. int check_channel_owner(char *nick, const char *channel, char *filefd) {
  129. FILE *file;
  130. char line[1024];
  131. if((file = fopen(filefd, "r"))) {
  132. while(fgets(line, sizeof(line), file)!=NULL) {
  133. /* check if user exists */
  134. char *dotok = strtok(line, " ");
  135. if(strncmp(channel, dotok, strlen(channel))==0) {
  136. dotok = strtok(NULL, " ");
  137. if(strncmp(nick, dotok, strlen(nick))==0) {
  138. /* owns channel */
  139. fclose(file);
  140. return 0;
  141. }
  142. else
  143. {
  144. /* doesn't */
  145. fclose(file);
  146. return 1;
  147. }
  148. }
  149. }
  150. fclose(file);
  151. }
  152. /* channel not registered */
  153. return 2;
  154. }
  155. // Not using this anymore
  156. /*
  157. int set_topic(char *channel, char *topic, char *filefd) {
  158. FILE *file;
  159. char line[1024];
  160. if((file = fopen(filefd, "r"))) {
  161. while(fgets(line, sizeof(line), file)!=NULL) {
  162. char *dotok = strtok(line, " ");
  163. if(strncmp(channel, dotok, strlen(channel))==0) {
  164. fclose(file);
  165. return 1;
  166. }
  167. }
  168. fclose(file);
  169. }
  170. if((file = fopen(filefd, "a"))) {
  171. fprintf(file, "%s¤%s\n", channel, topic);
  172. fclose(file);
  173. return 0;
  174. }
  175. return 1;
  176. }
  177. */
  178. int register_nick(char *nick, const char *pass, char *filefd) {
  179. FILE *file;
  180. if((file = fopen(filefd, "a"))) {
  181. fprintf(file, "%s %s\n", nick, pass);
  182. fclose(file);
  183. return 0;
  184. }
  185. return 1;
  186. }