1 /********************************************************************\
2   * BitlBee -- An IRC to other IM-networks gateway                     *
3   *                                                                    *
4   * Copyright 2002-2013 Wilmer van der Gaast and others                *
5   \********************************************************************/
6 
7 /* The IRC-based UI (for now the only one)                              */
8 
9 /*
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License as published by
12   the Free Software Foundation; either version 2 of the License, or
13   (at your option) any later version.
14 
15   This program is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   GNU General Public License for more details.
19 
20   You should have received a copy of the GNU General Public License with
21   the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22   if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23   Fifth Floor, Boston, MA  02110-1301  USA
24 */
25 
26 #ifndef _IRC_H
27 #define _IRC_H
28 
29 #include <sys/socket.h>
30 
31 #define IRC_MAX_LINE 512
32 #define IRC_MAX_ARGS 16
33 #define IRC_WORD_WRAP 425
34 
35 #define IRC_LOGIN_TIMEOUT 60
36 #define IRC_PING_STRING "PinglBee"
37 
38 #define UMODES "abisw"     /* Allowed umodes (although they mostly do nothing) */
39 #define UMODES_PRIV "Ro"   /* Allowed, but not by user directly */
40 #define UMODES_KEEP "R"    /* Don't allow unsetting using /MODE */
41 #define CMODES "ntC"       /* Allowed modes */
42 #define CMODE "t"          /* Default mode */
43 #define UMODE "s"          /* Default mode */
44 
45 #define CTYPES "&#"        /* Valid channel name prefixes */
46 
47 typedef enum {
48 	USTATUS_OFFLINE = 0,
49 	USTATUS_AUTHORIZED = 1, /* Gave the correct server password (PASS). */
50 	USTATUS_LOGGED_IN = 2,  /* USER+NICK(+PASS) finished. */
51 	USTATUS_IDENTIFIED = 4, /* To NickServ (root). */
52 	USTATUS_SHUTDOWN = 8,   /* Now used to indicate we're shutting down.
53 	                           Currently just blocks irc_vawrite(). */
54 	USTATUS_CAP_PENDING = 16,
55 	USTATUS_SASL_PLAIN_PENDING = 32,
56 
57 	/* Not really status stuff, but other kinds of flags: For slightly
58 	   better password security, since the only way to send passwords
59 	   to the IRC server securely (i.e. not echoing to screen or written
60 	   to logfiles) is the /OPER command, try to use that command for
61 	   stuff that matters. */
62 	OPER_HACK_IDENTIFY = 0x100,
63 	OPER_HACK_IDENTIFY_NOLOAD = 0x01100,
64 	OPER_HACK_IDENTIFY_FORCE  = 0x02100,
65 	OPER_HACK_REGISTER = 0x200,
66 	OPER_HACK_ACCOUNT_PASSWORD = 0x400,
67 	OPER_HACK_ANY = 0x3700, /* To check for them all at once. */
68 
69 	IRC_UTF8_NICKS = 0x10000, /* Disable ASCII restrictions on buddy nicks. */
70 } irc_status_t;
71 
72 typedef enum {
73 	CAP_SASL = (1 << 0),
74 	CAP_MULTI_PREFIX = (1 << 1),
75 	CAP_EXTENDED_JOIN = (1 << 2),
76 	CAP_AWAY_NOTIFY = (1 << 3),
77 	CAP_USERHOST_IN_NAMES = (1 << 4),
78 	CAP_SERVER_TIME = (1 << 5),
79 } irc_cap_flag_t;
80 
81 struct irc_user;
82 
83 typedef struct irc {
84 	int fd;
85 	irc_status_t status;
86 	double last_pong;
87 	int pinging;
88 	GString *sendbuffer;
89 	char *readbuffer;
90 	GIConv iconv, oconv;
91 
92 	struct irc_user *root;
93 	struct irc_user *user;
94 
95 	char *password; /* HACK: Used to save the user's password, but before
96 	                   logging in, this may contain a password we should
97 	                   send to identify after USER/NICK are received. */
98 	char *auth_backend;
99 
100 	char umode[8];
101 
102 	struct query *queries;
103 	GSList *file_transfers;
104 
105 	GSList *users G_GNUC_DEPRECATED;
106 	GSList *channels;
107 	struct irc_channel *default_channel;
108 	GHashTable *nick_user_hash;
109 	GHashTable *watches; /* See irc_cmd_watch() */
110 
111 	gint r_watch_source_id;
112 	gint w_watch_source_id;
113 	gint ping_source_id;
114 	gint login_source_id; /* To slightly delay some events at login time. */
115 
116 	struct otr *otr; /* OTR state and book keeping, used by the OTR plugin.
117 	                    TODO: Some mechanism for plugindata. */
118 
119 	struct bee *b;
120 	guint32 caps;
121 } irc_t;
122 
123 typedef enum {
124 	/* Replaced with iu->last_channel IRC_USER_PRIVATE = 1, */
125 	IRC_USER_AWAY = 2,
126 
127 	IRC_USER_OTR_ENCRYPTED = 0x10000,
128 	IRC_USER_OTR_TRUSTED   = 0x20000,
129 } irc_user_flags_t;
130 
131 typedef struct irc_user {
132 	irc_t *irc;
133 
134 	char *nick;
135 	char *user;
136 	char *host;
137 	char *fullname;
138 
139 	/* Nickname in lowercase for case insensitive searches */
140 	char *key;
141 
142 	irc_user_flags_t flags;
143 	struct irc_channel *last_channel;
144 
145 	GString *pastebuf; /* Paste buffer (combine lines into a multiline msg). */
146 	guint pastebuf_timer;
147 	time_t away_reply_timeout; /* Only send a 301 if this time passed. */
148 
149 	struct bee_user *bu;
150 
151 	const struct irc_user_funcs *f;
152 } irc_user_t;
153 
154 struct irc_user_funcs {
155 	gboolean (*privmsg)(irc_user_t *iu, const char *msg);
156 	gboolean (*ctcp)(irc_user_t *iu, char * const* ctcp);
157 };
158 
159 extern const struct irc_user_funcs irc_user_root_funcs;
160 extern const struct irc_user_funcs irc_user_self_funcs;
161 
162 typedef enum {
163 	IRC_CHANNEL_JOINED = 1, /* The user is currently in the channel. */
164 	IRC_CHANNEL_TEMP = 2,   /* Erase the channel when the user leaves,
165 	                           and don't save it. */
166 
167 	/* Hack: Set this flag right before jumping into IM when we expect
168 	   a call to imcb_chat_new(). */
169 	IRC_CHANNEL_CHAT_PICKME = 0x10000,
170 } irc_channel_flags_t;
171 
172 typedef struct irc_channel {
173 	irc_t *irc;
174 	char *name;
175 	char mode[8];
176 	int flags;
177 
178 	char *topic;
179 	char *topic_who;
180 	time_t topic_time;
181 
182 	GSList *users; /* struct irc_channel_user */
183 	struct irc_user *last_target;
184 	struct set *set;
185 
186 	GString *pastebuf; /* Paste buffer (combine lines into a multiline msg). */
187 	guint pastebuf_timer;
188 
189 	const struct irc_channel_funcs *f;
190 	void *data;
191 } irc_channel_t;
192 
193 struct irc_channel_funcs {
194 	gboolean (*privmsg)(irc_channel_t *ic, const char *msg);
195 	gboolean (*join)(irc_channel_t *ic);
196 	gboolean (*part)(irc_channel_t *ic, const char *msg);
197 	gboolean (*topic)(irc_channel_t *ic, const char *new_topic);
198 	gboolean (*invite)(irc_channel_t *ic, irc_user_t *iu);
199 	void (*kick)(irc_channel_t *ic, irc_user_t *iu, const char *msg);
200 
201 	gboolean (*_init)(irc_channel_t *ic);
202 	gboolean (*_free)(irc_channel_t *ic);
203 };
204 
205 typedef enum {
206 	IRC_CHANNEL_USER_OP = 1,
207 	IRC_CHANNEL_USER_HALFOP = 2,
208 	IRC_CHANNEL_USER_VOICE = 4,
209 	IRC_CHANNEL_USER_NONE = 8,
210 } irc_channel_user_flags_t;
211 
212 typedef struct irc_channel_user {
213 	irc_user_t *iu;
214 	int flags;
215 } irc_channel_user_t;
216 
217 typedef enum {
218 	IRC_CC_TYPE_DEFAULT  = 0x00001,
219 	IRC_CC_TYPE_REST     = 0x00002, /* Still not implemented. */
220 	IRC_CC_TYPE_GROUP    = 0x00004,
221 	IRC_CC_TYPE_ACCOUNT  = 0x00008,
222 	IRC_CC_TYPE_PROTOCOL = 0x00010,
223 	IRC_CC_TYPE_MASK     = 0x000ff,
224 	IRC_CC_TYPE_INVERT   = 0x00100,
225 } irc_control_channel_type_t;
226 
227 struct irc_control_channel {
228 	irc_control_channel_type_t type;
229 	struct bee_group *group;
230 	struct account *account;
231 	struct prpl *protocol;
232 	char modes[5];
233 };
234 
235 extern const struct bee_ui_funcs irc_ui_funcs;
236 
237 typedef enum {
238 	IRC_CDU_SILENT,
239 	IRC_CDU_PART,
240 	IRC_CDU_KICK,
241 } irc_channel_del_user_type_t;
242 
243 /* These are a glued a little bit to the core/bee layer and a little bit to
244    IRC. The first user is OTR, and I guess at some point we'll get to shape
245    this a little bit more as other uses come up. */
246 typedef struct irc_plugin {
247 	/* Called at the end of irc_new(). Can be used to add settings, etc. */
248 	gboolean (*irc_new)(irc_t *irc);
249 	/* At the end of irc_free(). */
250 	void (*irc_free)(irc_t *irc);
251 
252 	/* Problem with the following two functions is ordering if multiple
253 	   plugins are handling them. Let's keep fixing that problem for
254 	   whenever it becomes important. */
255 
256 	/* Called by bee_irc_user_privmsg_cb(). Return NULL if you want to
257 	   abort sending the msg. */
258 	char* (*filter_msg_out)(irc_user_t * iu, char *msg, int flags);
259 	/* Called by bee_irc_user_msg(). Return NULL if you swallowed the
260 	   message and don't want anything to go to the user. */
261 	char* (*filter_msg_in)(irc_user_t * iu, char *msg, int flags);
262 
263 	/* From storage.c functions. Ideally these should not be used
264 	   and instead data should be stored in settings which will get
265 	   saved automatically. Consider these deprecated! */
266 	void (*storage_load)(irc_t *irc);
267 	void (*storage_save)(irc_t *irc);
268 	void (*storage_remove)(const char *nick);
269 } irc_plugin_t;
270 
271 extern GSList *irc_plugins; /* struct irc_plugin */
272 
273 /* irc.c */
274 extern GSList *irc_connection_list;
275 
276 irc_t *irc_new(int fd);
277 void irc_set_hosts(irc_t *irc, const struct sockaddr *remote_addr, const socklen_t remote_addrlen);
278 void irc_abort(irc_t *irc, int immed, char *format, ...) G_GNUC_PRINTF(3, 4);
279 void irc_free(irc_t *irc);
280 void irc_setpass(irc_t *irc, const char *pass);
281 
282 void irc_process(irc_t *irc);
283 char **irc_parse_line(char *line);
284 char *irc_build_line(char **cmd);
285 
286 void irc_write(irc_t *irc, char *format, ...) G_GNUC_PRINTF(2, 3);
287 void irc_write_all(int now, char *format, ...) G_GNUC_PRINTF(2, 3);
288 void irc_vawrite(irc_t *irc, char *format, va_list params);
289 
290 void irc_flush(irc_t *irc);
291 void irc_switch_fd(irc_t *irc, int fd);
292 void irc_sync(irc_t *irc);
293 void irc_desync(irc_t *irc);
294 
295 int irc_check_login(irc_t *irc);
296 
297 void irc_umode_set(irc_t *irc, const char *s, gboolean allow_priv);
298 
299 void register_irc_plugin(const struct irc_plugin *p);
300 
301 /* irc_channel.c */
302 irc_channel_t *irc_channel_new(irc_t *irc, const char *name);
303 irc_channel_t *irc_channel_by_name(irc_t *irc, const char *name);
304 irc_channel_t *irc_channel_get(irc_t *irc, char *id);
305 int irc_channel_free(irc_channel_t *ic);
306 void irc_channel_free_soon(irc_channel_t *ic);
307 int irc_channel_add_user(irc_channel_t *ic, irc_user_t *iu);
308 int irc_channel_del_user(irc_channel_t *ic, irc_user_t *iu, irc_channel_del_user_type_t type, const char *msg);
309 irc_channel_user_t *irc_channel_has_user(irc_channel_t *ic, irc_user_t *iu);
310 struct irc_channel *irc_channel_with_user(irc_t *irc, irc_user_t *iu);
311 int irc_channel_set_topic(irc_channel_t *ic, const char *topic, const irc_user_t *who);
312 void irc_channel_user_set_mode(irc_channel_t *ic, irc_user_t *iu, irc_channel_user_flags_t flags);
313 void irc_channel_set_mode(irc_channel_t *ic, const char *s);
314 void irc_channel_auto_joins(irc_t *irc, struct account *acc);
315 void irc_channel_printf(irc_channel_t *ic, char *format, ...) G_GNUC_PRINTF(2, 3);
316 gboolean irc_channel_name_ok(const char *name);
317 void irc_channel_name_strip(char *name);
318 int irc_channel_name_cmp(const char *a_, const char *b_);
319 char *irc_channel_name_gen(irc_t *irc, const char *name);
320 gboolean irc_channel_name_hint(irc_channel_t *ic, const char *name);
321 void irc_channel_update_ops(irc_channel_t *ic, char *value);
322 char irc_channel_user_get_prefix(irc_channel_user_t *icu);
323 char *set_eval_irc_channel_ops(struct set *set, char *value);
324 gboolean irc_channel_wants_user(irc_channel_t *ic, irc_user_t *iu);
325 
326 /* irc_commands.c */
327 void irc_exec(irc_t *irc, char **cmd);
328 
329 /* irc_send.c */
330 void irc_send_num(irc_t *irc, int code, char *format, ...) G_GNUC_PRINTF(3, 4);
331 void irc_send_login(irc_t *irc);
332 void irc_send_motd(irc_t *irc);
333 const char *irc_user_msgdest(irc_user_t *iu);
334 void irc_rootmsg(irc_t *irc, char *format, ...) G_GNUC_PRINTF(2, 3);
335 void irc_usermsg(irc_user_t *iu, char *format, ...) G_GNUC_PRINTF(2, 3);
336 void irc_usernotice(irc_user_t *iu, char *format, ...) G_GNUC_PRINTF(2, 3);
337 void irc_send_join(irc_channel_t *ic, irc_user_t *iu);
338 void irc_send_part(irc_channel_t *ic, irc_user_t *iu, const char *reason);
339 void irc_send_quit(irc_user_t *iu, const char *reason);
340 void irc_send_kick(irc_channel_t *ic, irc_user_t *iu, irc_user_t *kicker, const char *reason);
341 void irc_send_names(irc_channel_t *ic);
342 void irc_send_topic(irc_channel_t *ic, gboolean topic_change);
343 void irc_send_whois(irc_user_t *iu);
344 void irc_send_who(irc_t *irc, GSList *l, const char *channel);
345 void irc_send_msg(irc_user_t *iu, const char *type, const char *dst, const char *msg, const char *prefix);
346 void irc_send_msg_raw(irc_user_t *iu, const char *type, const char *dst, const char *msg);
347 void irc_send_msg_f(irc_user_t *iu, const char *type, const char *dst, const char *format, ...) G_GNUC_PRINTF(4, 5);
348 void irc_send_nick(irc_user_t *iu, const char *new_nick);
349 void irc_send_channel_user_mode_diff(irc_channel_t *ic, irc_user_t *iu,
350                                      irc_channel_user_flags_t old_flags, irc_channel_user_flags_t new_flags);
351 void irc_send_invite(irc_user_t *iu, irc_channel_t *ic);
352 void irc_send_cap(irc_t *irc, char *subcommand, char *body);
353 void irc_send_away_notify(irc_user_t *iu);
354 
355 G_GNUC_INTERNAL void irc_send_msg_ts(irc_user_t *iu, const char *type, const char *dst, const char *msg, const char *prefix, time_t ts);
356 G_GNUC_INTERNAL void irc_send_msg_raw_tags(irc_user_t *iu, const char *type, const char *dst, const char* tags, const char *msg);
357 
358 /* irc_user.c */
359 irc_user_t *irc_user_new(irc_t *irc, const char *nick);
360 int irc_user_free(irc_t *irc, irc_user_t *iu);
361 irc_user_t *irc_user_by_name(irc_t *irc, const char *nick);
362 int irc_user_set_nick(irc_user_t *iu, const char *new_nick);
363 gint irc_user_cmp(gconstpointer a_, gconstpointer b_);
364 const char *irc_user_get_away(irc_user_t *iu);
365 void irc_user_quit(irc_user_t *iu, const char *msg);
366 
367 /* irc_util.c */
368 char *set_eval_timezone(struct set *set, char *value);
369 char *irc_format_timestamp(irc_t *irc, time_t msg_ts);
370 G_GNUC_INTERNAL char *irc_format_servertime(irc_t *irc, time_t msg_ts);
371 char *set_eval_self_messages(struct set *set, char *value);
372 
373 /* irc_im.c */
374 void bee_irc_channel_update(irc_t *irc, irc_channel_t *ic, irc_user_t *iu);
375 void bee_irc_user_nick_reset(irc_user_t *iu);
376 
377 /* irc_cap.c */
378 void irc_cmd_cap(irc_t *irc, char **cmd);
379 
380 #endif
381