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);
427 if(is_numeric(hostname))
429 // if ((hp = gethostbyaddr(hostname)) == NULL)
430 cerr<<"*** Can't use numeric IP ***\n";
435 if ((hp = gethostbyname(hostname)) == NULL)
439 cout<<"HP->H_ADDR >";
440 for(int k=0; k<hp->h_length; k++) cout<<"["<<hp->h_addr[k]<<"]";
443 memset(&sa,0,sizeof(sa));
444 memcpy((char *)&sa.sin_addr,hp->h_addr,hp->h_length);
445 sa.sin_family= hp->h_addrtype;
446 sa.sin_port= htons((u_short)portnum);
449 if ((s= socket(AF_INET, SOCK_STREAM, 0)) < 0) /* get socket */
452 if (connect(s, (struct sockaddr *) &sa, sizeof sa) < 0) /* connect */
460 //-----------------------------------------------------------------------------
462 int establish(unsigned short portnum)
464 char myname[MAXHOSTNAME+1];
466 struct sockaddr_in sa;
469 memset(&sa, 0, sizeof(struct sockaddr_in)); /* clear our address */
470 gethostname(myname, MAXHOSTNAME); /* who are we? */
471 hp= gethostbyname(myname); /* get our address info */
472 if (hp == NULL) /* we don't exist !? */
474 sa.sin_family= hp->h_addrtype; /* this is our host address */
475 sa.sin_port= htons(portnum); /* this is our port number */
477 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) /* create socket */
480 if (bind(s,(struct sockaddr *)&sa,sizeof(struct sockaddr_in)) < 0)
483 return(-1); /* bind address to socket */
486 listen(s, NB_QUEUE_CONNECTS); /* max # of queued connects */
490 //-----------------------------------------------------------------------------
492 int get_connection(int s)
494 int t; /* socket of connection */
495 if ((t = accept(s,NULL,NULL)) < 0) /* accept connection if there is one */
500 //-----------------------------------------------------------------------------
502 int slice_buffer(char *&src, char *endsrc,
505 char **slice_cmd, int &n_cmd,
507 char **slice_ctcp, int &n_ctcp)
515 while((*s != '\r') && (*s != '\n') && (s<endsrc)) s++;
530 while((src<endsrc) && (*src != ' '))
541 while((src < endsrc) && !end)
543 if((*src == ' ') || (*src == '\r') || (*src == '\n')
549 slice_ctcp[n_ctcp++] = dest_ctcp;
550 while((src<endsrc) && (*src != '\r') && (*src != '\001'))
551 *dest_ctcp++ = *src++;
556 end = (*src == '\r') || (*src == '\n');
565 end = (*src == '\r') || (*src == '\n');
571 if(!ctcp) slice_cmd[n_cmd++] = dest_cmd;
578 while((src<endsrc) &&
579 ((*src != ' ') || queue) && (*src != '\r') && (*src != '\n')
581 *dest_cmd++ = *src++;
582 ctcp = (*src == '\001');
585 if(src < endsrc) if(*src == '\n') src++; // Loose the '\n'
591 //-----------------------------------------------------------------------------
593 void ListChar::Print()
595 NodeList<char *> *node;
596 for(node = first; node != NULL; node = node->next)
597 cout<<node->body<<"\n";
600 void ListChar::Clear()
602 NodeList<char *> *node, *next;
614 void ListChar::Add(char *s)
622 void ListChar::Remove(char *s)
624 NodeList<char *> *node, *pred, *next;
631 if(strcmp(node->body, s) == 0)
633 if(pred == NULL) first = next;
634 else pred->next = next;
643 int ListChar::Matches(char *s)
645 NodeList<char *> *node;
648 for(node = first; (node != NULL) && !yep; node = node->next)
649 yep = match_pattern(node->body, s);
653 int ListChar::IsMatchedBy(char *s)
655 NodeList<char *> *node;
658 for(node = first; (node != NULL) && !yep; node = node->next)
659 yep = match_pattern(s, node->body);
663 int ListChar::Contains(char *s)
665 NodeList<char *> *node;
668 for(node = first; (node != NULL) && !yep; node = node->next)
669 yep = (strcmp(node->body, s) == 0);