summary refs log tree commit diff
path: root/www/text.causal.agency/008-how-irc.7
blob: aba1bbf9c8f90e9e6b13075ecf581bb398fd0486 (plain) (blame)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
.Dd March  8, 2020
.Dt HOW-IRC 7
.Os "Causal Agency"
.
.Sh NAME
.Nm How I Relay Chat
.Nd in code
.
.Sh DESCRIPTION
I've been writing a lot of IRC software lately
.Pq Sx SEE ALSO ,
and developed some nice code patterns
that I've been reusing.
Here they are.
.
.Ss Parsing
I use fixed size buffers almost everywhere,
so it's necessary to know IRC's size limits.
A traditional IRC message is a maximum of 512 bytes,
but the IRCv3 message-tags spec adds
(unreasonably, in my opinion)
8191 bytes for tags.
IRC messages also have a maximum of 15 command parameters.
.Bd -literal -offset indent
enum { MessageCap = 8191 + 512 };
enum { ParamCap = 15 };
.Ed
.
.Pp
If I'm using tags,
I'll use X macros
to declare the set I care about.
X macros are a way of maintaining parallel arrays,
or in this case an enum and an array.
.Bd -literal -offset indent
#define ENUM_TAG \e
	X("msgid", TagMsgid) \e
	X("time", TagTime)

enum Tag {
#define X(name, id) id,
	ENUM_TAG
#undef X
	TagCap,
};

static const char *TagNames[TagCap] = {
#define X(name, id) [id] = name,
	ENUM_TAG
#undef X
};
.Ed
.
.Pp
The TagNames array is used by the parsing function
to assign tag values into the message structure,
which looks like this:
.Bd -literal -offset indent
struct Message {
	char *tags[TagCap];
	char *nick;
	char *user;
	char *host;
	char *cmd;
	char *params[ParamCap];
};
.Ed
.
.Pp
I'm a fan of using
.Xr strsep 3
for simple parsing.
Although it modifies its input
(replacing delimiters with NUL terminators),
since the raw message is in a static buffer,
it is ideal for so-called zero-copy parsing.
I'm not going to include the whole parsing function here,
but I will at least include the part that many get wrong,
which is dealing with the colon-prefixed trailing parameter:
.Bd -literal -offset indent
msg.cmd = strsep(&line, " ");
for (int i = 0; line && i < ParamCap; ++i) {
	if (line[0] == ':') {
		msg.params[i] = &line[1];
		break;
	}
	msg.params[i] = strsep(&line, " ");
}
.Ed
.
.Ss Handling
To handle IRC commands and replies
I add handler functions to a big array.
I usually have some form of helper as well
to check the number of expected parameters.
.Bd -literal -offset indent
typedef void HandlerFn(struct Message *msg);

static const struct Handler {
	const char *cmd;
	HandlerFn *fn;
} Handlers[] = {
	{ "001", handleReplyWelcome },
	{ "PING", handlePing },
	{ "PRIVMSG", handlePrivmsg },
};
.Ed
.
.Pp
Since I keep these arrays sorted anyway,
I started using the standard
.Xr bsearch 3
function,
but a basic for loop probably works just as well.
I do wish I could compile-time assert
that the array really is sorted, though.
.Bd -literal -offset indent
static int compar(const void *cmd, const void *_handler) {
	const struct Handler *handler = _handler;
	return strcmp(cmd, handler->cmd);
}

void handle(struct Message msg) {
	if (!msg.cmd) return;
	const struct Handler *handler = bsearch(
		msg.cmd,
		Handlers, ARRAY_LEN(Handlers),
		sizeof(*handler), compar
	);
	if (handler) handler->fn(&msg);
}
.Ed
.
.Ss Capabilities
For IRCv3 capabilties
I use X macros again,
this time with another handy macro
for declaring bit flag enums.
.Bd -literal -offset indent
#define BIT(x) x##Bit, x = 1 << x##Bit, x##Bit_ = x##Bit

#define ENUM_CAP \e
	X("message-tags", CapMessageTags) \e
	X("sasl", CapSASL) \e
	X("server-time", CapServerTime)

enum Cap {
#define X(name, id) BIT(id),
	ENUM_CAP
#undef X
};

static const char *CapNames[] = {
#define X(name, id) [id##Bit] = name,
	ENUM_CAP
#undef X
};
.Ed
.
.Pp
The
.Fn BIT
macro declares, for example,
.Dv CapSASL
as the bit flag and
.Dv CapSASLBit
as the corresponding index.
The
.Vt "enum Cap"
is used as a set,
for example checking if SASL is enabled with
.Ql caps & CapSASL .
.
.Pp
These patterns are serving my IRC software well,
and my IRC projects are serving me well.
It is immensely satisfying
to be (near) constantly using software
that I wrote myself and am happy with,
regardless of how niche it may be.
.
.Sh SEE ALSO
.Bl -item -compact
.It
.Lk https://git.causal.agency/pounce/about "IRC bouncer"
.It
.Lk https://git.causal.agency/litterbox/about "IRC logger"
.It
.Lk https://git.causal.agency/catgirl/about "IRC client"
.El
.
.Sh AUTHORS
.An June Bug Aq Mt june@causal.agency
'/cgit-pink/commit/Makefile?h=1.3.0&id=dad80d1ff8e065002cdf4e37252164a7f8517a5b&follow=1'>Tag release v0.3Lars Hjemli Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-05-11Update README with submodule build infoLars Hjemli Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-05-11Add submodule links in tree listingLars Hjemli When a submodule occurs in a tree, generate a link to show the module/commit. The link is specified as a sprintf string in /etc/cgitrc, using parameters 'module-link' and 'repo.module-link'. This should probably be extended with repo.module-link.$path. Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-05-11Add submodules.sh and use it during buildsLars Hjemli This adds a shell script which can be be used to initialize, list and update submodules in a git repository. It reads the file .gitmodules to find a mapping between submodule path and repository url for the initial clone of all submodules. The script is used during cgit builds to enable automatic download and checkout of the git git repository. Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-05-11Added git as a submoduleLars Hjemli This commit adds the subdirectory 'git' as a submodule containing the git git repository, but doesn't add support for automatically cloning the submodule. Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-05-09Add support for downloading single blobsLars Hjemli Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-05-08ui-view: show pathname if specified in querystringLars Hjemli Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-05-08Update to libgit 1.5.2-rc2Lars Hjemli Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-02-21Layout updateLars Hjemli 2007-02-08Make snapshot feature configurableLars Hjemli Snapshots can now be enabled/disabled by default for all repositories in cgitrc with param "snapshots". Additionally, any repo can override the default setting with param "repo.snapshots". By default, no snapshotting is enabled. Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-02-08Add support for snapshotsLars Hjemli Make a link from the commit viewer to a snapshot of the corresponding tree. Currently only zip-format is supported. Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-02-05cgit v0.2Lars Hjemli Main changes since v0.1: -list tags in repo summary -allow search in log-view -read repository paths from cgitrc Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-02-05Add support for prefix and gitsrc arguments to 'make'Lars Hjemli This should improve the installation a little, especially since the new options are mentioned in the README. Also, add a make-rule to build the git binaries if necessary + a dependency between cgit and libgit.a. Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-02-04Update cgitrc templateLars Hjemli Make the descriptions more helpfull. Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-02-04Add support for lightweight tagsLars Hjemli There is nothing bad about a tag that has no tag-object, but the old code didn't handle such tags correctly. Fix it. Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-02-04Read repo-info from /etc/cgitrcLars Hjemli This makes cgit read all repo-info from the configfile, instead of scanning for possible git-dirs below a common root path. This is primarily done to get better security (separate physical path from logical repo-name). In /etc/cgitrc each repo is registered with the following keys: repo.url repo.name repo.path repo.desc repo.owner Note: *Required keys are repo.url and repo.path, all others are optional *Each occurrence of repo.url starts a new repository registration *Default value for repo.name is taken from repo.url *The value of repo.url cannot contain characters with special meaning for urls (i.e. one of /?%&), while repo.name can contain anything. Example: repo.url=cgit-pub repo.name=cgit/public repo.path=/pub/git/cgit repo.desc=My public cgit repo repo.owner=Lars Hjemli repo.url=cgit-priv repo.name=cgit/private repo.path=/home/larsh/src/cgit/.git repo.desc=My private cgit repo repo.owner=Lars Hjemli Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-02-04Do not die if tag has no messageLars Hjemli Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-02-03Fix search for non-virtual urlsLars Hjemli When cgit don't use virtual urls, the current repo and page url parameters must be included in the search form as hidden input fields. Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2007-01-28Update README with install/config informationLars Hjemli