Update to version 2.9.3a
[tropbot.git] / tblib.h
1 /*-----------------------------------------------------------------------------
2   Thanks to http://world.std.com/~jimf/sockets.html
3 -----------------------------------------------------------------------------*/
4
5 #ifndef TBLIB_H
6 #define TBLIB_H
7
8 #include <sys/time.h>
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <netdb.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <signal.h>
16
17 #ifdef _AIX
18 #include <unistd.h>
19 #include <strings.h>
20 #include <sys/select.h>
21 #include <sys/socketvar.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #endif
25
26 #include <iostream.h>
27 #include <string.h>
28
29 #include "list.cc"
30
31 //-----------------------------------------------------------------------------
32
33 /*
34 extern "C" int close(int);
35 extern "C" int read(int, char *, int);
36 extern "C" int gethostname();
37 extern "C" size_t write(int, char *, int);
38 extern "C" int socket(int, int, int);
39 extern "C" int connect(int, struct sockaddr *, int);
40 extern "C" int bind(int, struct sockaddr *, int);
41 extern "C" int listen(int, int);
42 extern "C" int accept(int, struct sockaddr *, int *);
43 */
44
45 //-----------------------------------------------------------------------------
46
47 #define BUFFER_SIZE 4096
48 #define SMALL_BUFFER_SIZE 128
49
50 #define NB_QUEUE_CONNECTS 3
51 #define MAXHOSTNAME 64
52 #define NB_SLICES_MAX 32
53
54 //-----------------------------------------------------------------------------
55
56 // This routine gets the nickame from the nick!user@host stuff
57
58 char *cut_nick_from_prefix(char *name);
59
60 int is_weird_login(char *login);
61
62 int is_pattern(char *s);
63
64 char *clean_banid(char *banid);
65
66 //-----------------------------------------------------------------------------
67
68 // This routine copy r in s, and move r to the next ' ', or sets it to
69 // NULL if no more ' ' is find
70
71 char *next_word(char *buffer, char *r, int buffer_size);
72
73 //-----------------------------------------------------------------------------
74
75 // This routine add the d string at the c car, and modify c
76 //                       ** USE WITH CARE **
77
78 inline void concat(char *&c, char *d)
79 {
80   while(*d != '\0') *c++ = *d++;
81 };
82
83 //-----------------------------------------------------------------------------
84
85 int is_hostname(char *host);
86
87 // Evaluate if an IP is numerical or not
88
89 int is_numeric(char *host);
90
91 int are_same_site(char *u1, char *u2);
92
93 //-----------------------------------------------------------------------------
94
95 void concat_pattern_from_host(char *result, char *host);
96 char *pattern_from_prefix(char *prefix, int site_ban);
97
98 //-----------------------------------------------------------------------------
99
100 int Contains(char *string, char c);
101 int find_substring(char *string, char *substring,
102                    int *debut, int *debut_sub);
103
104 // Send back if the string match pattern or not (pattern with '?' and
105 // '*')
106
107 int match_pattern(char *pattern, char *string);
108
109 //-----------------------------------------------------------------------------
110
111 // Uncap string
112 void uncap(char *string);
113
114 // Duplicate a string
115 char *strdup(char *s);
116
117 // Evaluate if strings are equal
118 int eq(char *s, char *t);
119
120 //-----------------------------------------------------------------------------
121
122 char *seconds_to_string(int time);
123 int string_to_seconds(char *string);
124
125 //-----------------------------------------------------------------------------
126
127 // Socket stuff
128
129 int call_socket_ip(unsigned short host, unsigned short portnum);
130 int call_socket(char *hostname, unsigned short portnum);
131 int establish(unsigned short portnum);
132 int get_connection(int s);
133
134 //-----------------------------------------------------------------------------
135
136 int slice_buffer(char *&src, char *endsrc,
137                  char *&dest_cmd,
138                  char *&prefix,
139                  char **slice_cmd, int &n_cmd,
140                  char *&dest_ctcp,
141                  char **slice_ctcp, int &n_ctcp);
142
143 //-----------------------------------------------------------------------------
144
145 class ListChar : public List<char *>
146 {
147 public:
148   void Clear();
149   void Add(char *s);
150   void Remove(char *s);
151   int Matches(char *s);
152   int IsMatchedBy(char *s);
153   int Contains(char *s);
154 };
155
156 //-----------------------------------------------------------------------------
157
158 #endif