1 //-----------------------------------------------------------------------------
5 //-----------------------------------------------------------------------------
7 char *cut_nick_from_prefix(char *name)
11 while((*s != '!') && (*s != '\0')) s++;
12 nick = new char[s-name+1];
13 strncpy(nick, name, s-name);
14 *(nick+(s-name)) = '\0';
18 //-----------------------------------------------------------------------------
20 int is_weird_login(char *login)
24 while((*s != '\0') && (*s != '!') &&
25 ((*s & 128) == 0) && (*s >= 32) && (*s != 127)) s++;
26 return (*s != '\0') || (*s == '!');
29 //-----------------------------------------------------------------------------
31 int is_pattern(char *s)
33 while((*s != '\0') && (*s != '!') && (*s != '*')) s++;
34 return (*s == '*') || (*s == '!');
37 //-----------------------------------------------------------------------------
39 char *clean_banid(char *banid)
42 int joker_mode, min_car, k;
44 result = new char[strlen(banid)+1];
54 if(!joker_mode) min_car = 0;
61 if(joker_mode) min_car++;
69 for(k=0; k<min_car; k++) *t++ = '?';
73 if(((*s & 128) != 0) || (*s < 32) || (*s == 127)) *t++ = '?';
83 for(k=0; k<min_car; k++) *t++ = '?';
91 //-----------------------------------------------------------------------------
93 char *next_word(char *buffer, char *r, int buffer_size)
99 while((*r != '\0') && (*r != ' ') && (s<buffer+buffer_size-1))
101 while(*r == ' ') r++;
102 if(*r == '\0') r = NULL;
108 //-----------------------------------------------------------------------------
110 int is_hostname(char *host)
114 while((*s != '!') && (*s != '\0')) s++;
118 int is_numeric(char *host)
124 for(c = host; *c != '\0'; c++)
126 r &= (((*c >='0') && (*c <= '9')) || (*c == '.'));
133 int are_same_site(char *u1, char *u2)
144 while(*c1 != '\0') c1++; while((c1 > u1) && (*c1 != '.')) c1--;
145 while(*c2 != '\0') c2++; while((c2 > u2) && (*c2 != '.')) c2--;
146 if((c1-u1) == (c2-u2)) return strncmp(u1, u2, c1-u1) == 0;
152 while(*u1 != '.') u1++;
153 while(*u2 != '.') u2++;
154 return strcmp(u1, u2) == 0;
160 // :CISMhp.Univ-Lyon1.FR 311 chose Johnkee gponcet@bi bi@192.93.80.208 * :Gael
161 // :sil.polytechnique.fr 311 chose Johnkee gponcet bi@192.93.80.208 * :Gael
163 void concat_pattern_from_host(char *result, char *host)
169 while((*host != '\0') && (r<3))
172 if(*host == '.') r++;
174 concat(result, ".*");
179 while((*host != '.') && (*host != '\0')) host++;
180 concat(result, host);
185 char *pattern_from_prefix(char *prefix, int site_pattern)
187 char small_buffer[SMALL_BUFFER_SIZE];
189 char *c, *d, *pattern;
196 while((*d != '!') && (*d != '\0')) d++;
201 while((*d != '@') && (*d != '\0')) d++;
207 if((*d == '~') || (*d == '+') ||
208 (*d == '-') || (*d=='^')) d++;
210 while((*d != '@') && (*d != '\0') &&
211 ((l<7) || *(d+1) == '\0')) { *c++ = *d++; l++; }
212 if((*d != '\0') && (*d != '@')) *c++ = '*';
213 while(*d != '@') d++;
217 concat_pattern_from_host(c, d);
219 pattern = new char[strlen(small_buffer)+1];
220 strcpy(pattern, small_buffer);
225 //-----------------------------------------------------------------------------
227 int Contains(char *string, char c)
229 while((*string != '\0') && (*string != c)) string++;
233 int find_substring(char *&sub, char *&str)
239 if(*u == '\0') while(*s != '\0') s++;
241 while((*u != '\0') && (*u != '*') && (*s != '\0') &&
242 ((*u == *s) || (*u == '?'))) { u++; s++; };
244 if((*u == '\0') || (*u == '*'))
257 int match_pattern(char *pattern, char *string)
262 if(pattern == NULL) return -1;
263 if(string == NULL) return 0;
265 p = pattern; s = string;
267 if(*p != '*') ok = find_substring(p, s);
270 while((*s != '\0') && (*p != '\0') && ok)
272 while(*p == '*') p++;
274 while((*s != '\0') && !ok) ok = find_substring(p, s);
277 if((*p == '\0') && (*s != '\0') && ok)
279 while(*s != '\0') s++;
280 while((p>=pattern) && (s>=string) && ok && (*p != '*'))
282 ok = (*p == '?') || (*p == *s);
285 return (*p == '*') || (p == pattern-1);
287 else return (*p == '\0') && (*s == '\0') && ok;
290 void uncap(char *string)
296 for(s = string; *s != '\0'; s++)
297 if((*s >= 'A') && (*s <= 'Z')) *s -= 'A'-'a';
301 char *strdup(char *s)
304 if(s == NULL) return NULL;
307 t = new char[strlen(s)+1];
313 int eq(char *s, char *t)
315 return strcmp(s, t) == 0;
318 //-----------------------------------------------------------------------------
320 char *seconds_to_string(int time)
323 char *t, *u, *result;
340 if(s/10>0) *t++ = (s/10)+'0';
345 if(before) *t++ = ' ';
349 if(m/10>0) *t++ = (m/10)+'0';
354 if(before) *t++ = ' ';
358 if(h/10>0) *t++ = (h/10)+'0';
363 if(before) *t++ = ' ';
373 result = new char[(t-buffer)+1];
377 while(t>=buffer) *u++ = *t--;
383 int string_to_seconds(char *string)
388 while(*string != '\0')
390 if((*string >= '0') && (*string <= '9')) acc = 10*acc + (*string - '0');
391 if(*string == 'd') { total = total+86400*acc; acc = 0; };
392 if(*string == 'h') { total = total+3600*acc; acc = 0; };
393 if(*string == 'm') { total = total+60*acc; acc = 0; }
394 if((*string == 's') || (*string == '\0')) { total = total+acc; acc = 0; }
400 //-----------------------------------------------------------------------------
402 int call_socket(char *hostname, unsigned short portnum)
404 struct sockaddr_in sa;
408 if ((sa.sin_addr.s_addr = inet_addr(hostname)) == -1)
410 if ((hp = gethostbyname(hostname)) != NULL)
412 bzero((char *) &sa, sizeof(sa));
413 bcopy(hp->h_addr, (char *) &sa.sin_addr,
415 sa.sin_family = hp->h_addrtype;
420 else sa.sin_family = AF_INET;
422 sa.sin_port = (unsigned short) htons(portnum);
424 if ((s= socket(AF_INET, SOCK_STREAM, 0)) < 0) /* get socket */
427 if (connect(s, (struct sockaddr *) &sa, sizeof sa) < 0) /* connect */
435 //-----------------------------------------------------------------------------
437 int establish(unsigned short portnum)
439 char myname[MAXHOSTNAME+1];
441 struct sockaddr_in sa;
444 memset(&sa, 0, sizeof(struct sockaddr_in)); /* clear our address */
445 gethostname(myname, MAXHOSTNAME); /* who are we? */
446 hp= gethostbyname(myname); /* get our address info */
447 if (hp == NULL) /* we don't exist !? */
449 sa.sin_family= hp->h_addrtype; /* this is our host address */
450 sa.sin_port= htons(portnum); /* this is our port number */
452 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) /* create socket */
455 if (bind(s,(struct sockaddr *) &sa,sizeof(struct sockaddr_in)) < 0)
458 return(-1); /* bind address to socket */
461 listen(s, NB_QUEUE_CONNECTS); /* max # of queued connects */
465 //-----------------------------------------------------------------------------
467 int get_connection(int s)
469 int t; /* socket of connection */
470 if ((t = accept(s,NULL,NULL)) < 0) /* accept connection if there is one */
475 //-----------------------------------------------------------------------------
477 int slice_buffer(char *&src, char *endsrc,
480 char **slice_cmd, int &n_cmd,
482 char **slice_ctcp, int &n_ctcp)
489 s = src; while((*s != '\r') && (*s != '\n') && (s<endsrc)) s++;
491 if(s == endsrc) return 1;
504 while((src<endsrc) && (*src != ' ')) *dest_ctcp++ = *src++;
509 while((src < endsrc) && !end)
511 if((*src == ' ') || (*src == '\r') || (*src == '\n') || (*src == '\001'))
516 slice_ctcp[n_ctcp++] = dest_ctcp;
518 while((src<endsrc) && (*src != '\r') &&
519 (*src != '\n') && (*src != '\001')) *dest_ctcp++ = *src++;
525 end = (*src == '\r') || (*src == '\n');
533 end = (*src == '\r') || (*src == '\n');
539 if(!ctcp) slice_cmd[n_cmd++] = dest_cmd;
540 if(*src == ':') { queue = 1; src++; };
543 while((src<endsrc) &&
544 ((*src != ' ') || queue) && (*src != '\r') && (*src != '\n')
546 *dest_cmd++ = *src++;
548 ctcp = (*src == '\001');
552 if(src < endsrc) if(*src == '\n') src++;
557 //-----------------------------------------------------------------------------
559 void ListChar::Clear()
561 NodeList<char *> *node, *next;
573 void ListChar::Add(char *s)
581 void ListChar::Remove(char *s)
583 NodeList<char *> *node, *pred, *next;
591 if(strcmp(node->body, s) == 0)
593 if(pred == NULL) first = next;
594 else pred->next = next;
605 int ListChar::Matches(char *s)
607 NodeList<char *> *node;
610 for(node = first; (node != NULL) && !yep; node = node->next)
611 yep = match_pattern(node->body, s);
615 int ListChar::IsMatchedBy(char *s)
617 NodeList<char *> *node;
620 for(node = first; (node != NULL) && !yep; node = node->next)
621 yep = match_pattern(s, node->body);
625 int ListChar::Contains(char *s)
627 NodeList<char *> *node;
630 for(node = first; (node != NULL) && !yep; node = node->next)
631 yep = (strcmp(node->body, s) == 0);