summaryrefslogtreecommitdiff
path: root/network.h
blob: 136de3d1a11cecb8b453b7a9b153baa9ee799687 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// One of the headers for HaxServ
//
// Written by: Test_User <hax@andrewyu.org>
//
// This is free and unencumbered software released into the public
// domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#pragma once

#include <netinet/in.h>
#include <pthread.h>
#include <limits.h>
#include <errno.h>

#include <stdio.h>

#include "types.h"
#include "table.h"

// ID is the index you got this from
struct server_info {
	uint64_t distance; // gl if you exceed this

	struct string address;
	struct string name;
	struct string via; // netsplit purposes

	// TODO: this v
	struct table user_list;
	// TODO: metadata
};

struct user_info {
	uint64_t nick_ts;
	uint64_t user_ts;

	struct string server;
	struct string nick;
	struct string hostname;
	struct string vhost;
	struct string ident;
	struct string ip;
	struct string realname;
	struct string opertype;

	// TODO: this v
	struct table channel_list;

	struct table metadata;
};

struct channel_info {
	uint64_t ts;

	struct string topic;
	uint64_t topic_ts;

	struct table modes; // TODO: Parse modes properly
	struct table user_list; // points to corresponding user_info struct (if available, currently not)

	struct table metadata;
};

extern struct table server_network_commands;
extern struct table client_network_commands;
extern struct table channel_list;
extern struct table server_list;
extern struct table user_list;

extern pthread_mutex_t send_lock;

extern int server_fd;

extern int client_fd;
extern int client_listen_fd;
extern struct string client_nick;
extern uint8_t client_connected;

extern int resolve(char* address, char* port, struct sockaddr *server);

extern int initservernetwork(void);
extern int initclientnetwork(void);

#define MODE_TYPE_UNKNOWN 0
#define MODE_TYPE_NOARGS 1
#define MODE_TYPE_REPLACE 2
#define MODE_TYPE_MULTIPLE 3
// Mode goes away when the user leaves the channel
#define MODE_TYPE_USERS 4

extern char channel_mode_types[UCHAR_MAX];

#if LOGALL
extern ssize_t SENDCLIENT(struct string msg);
#else
#define SENDCLIENT(x) write(client_fd, x.data, x.len)
#endif

#if LOGALL
extern ssize_t SEND(struct string msg);
#else
#define SEND(x) write(server_fd, x.data, x.len)
#endif

inline size_t RECV(char *buf, size_t buflen, char *timeout) {
	ssize_t len;
	do {
		len = recv(server_fd, buf, buflen, 0);
	} while (len == -1 && errno == EINTR);

	if (len == -1)
		fprintf(stderr, "errno: %d\n", errno);

	if (len == -1)
		*timeout = (errno == EAGAIN || errno == EWOULDBLOCK);
	else
		*timeout = 0;

	if (len < 0)
		return 0;
	else
		return (size_t)len;
}

extern int privmsg(struct string source, struct string target, size_t num_message_parts, struct string message[num_message_parts]);
extern int add_local_client(struct string uid, struct string nick_arg, struct string vhost_arg, struct string ident_arg, struct string realname_arg, time_t timestamp, char fake_cert);
extern int remove_user(struct string uid, struct string reason);