File: libirc.c

package info (click to toggle)
ayttm 0.6.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 8,368 kB
  • ctags: 8,851
  • sloc: ansic: 65,755; sh: 10,810; cpp: 3,092; makefile: 561; yacc: 294; lex: 53; sed: 16
file content (417 lines) | stat: -rw-r--r-- 8,867 bytes parent folder | download | duplicates (2)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 * IRC protocol implementation
 *
 * Copyright (C) 2008, Siddhesh Poyarekar and the Ayttm Team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "libirc.h"
#include "ctcp.h"

static char irc_modes[] = {
	'a',
	'i',
	'w',
	'r',
	'o',
	'O',
	's'
};

/* LOGIN */
void irc_login(const char *password, int mode, irc_account *ia)
{
	char buff[BUF_LEN];
	memset(buff, 0, BUF_LEN);

	if (password && password[0]) {
		sprintf(buff, "PASS %s\n", password);
		ia->callbacks->irc_send_data(buff, strlen(buff), ia);
	}
	if (ia->nick) {
		sprintf(buff, "NICK %s\n", ia->nick);
		ia->callbacks->irc_send_data(buff, strlen(buff), ia);
	}
	if (ia->user) {
		sprintf(buff, "USER %s %d * :Ayttm user %s\n", ia->user, mode,
			ia->user);
		ia->callbacks->irc_send_data(buff, strlen(buff), ia);
	}
}

/* LOGOUT */
void irc_logout(irc_account *ia)
{
	char buff[BUF_LEN];
	memset(buff, 0, BUF_LEN);

	sprintf(buff, "QUIT :Ayttm logging off\n");
	ia->callbacks->irc_send_data(buff, strlen(buff), ia);
}

/* Send a PRIVMSG */
int irc_send_privmsg(const char *recipient, char *message, irc_account *ia)
{
	char *out_msg;
	int offset = 0;
	char buff[BUF_LEN];
	memset(buff, 0, BUF_LEN);
	int type = 0;

	if (!message)
		return IRC_NOECHO;

	while (message[offset] == ' ' || message[offset] == '\t')
		offset++;

	if (message[offset] == '/') {
		char *param_offset = NULL;

		/* It is some kind of command */

		message += offset + 1;

		param_offset = strchr(message, ' ');

		if (param_offset) {
			*param_offset = '\0';
			param_offset++;
		}

		type = irc_get_command_string(buff, recipient, message, param_offset,
			ia);

		/* reinstate the space so that we can put the message in our window intact */
		if (param_offset) {
			*(param_offset - 1) = ' ';
		}
	} else {
		out_msg = ctcp_encode(message, strlen(message));
		snprintf(buff, sizeof(buff), "PRIVMSG %s :%s\n", recipient,
			out_msg);

		if (out_msg)
			free(out_msg);
	}

	if (*buff)
		ia->callbacks->irc_send_data(buff, strlen(buff), ia);

	return type;
}

/* Send a NOTICE */
void irc_send_notice(const char *recipient, char *message, irc_account *ia)
{
	char *out_msg;
	char buff[BUF_LEN];
	memset(buff, 0, BUF_LEN);

	out_msg = ctcp_encode(message, strlen(message));

	sprintf(buff, "NOTICE %s :%s\n", recipient, out_msg);
	ia->callbacks->irc_send_data(buff, strlen(buff), ia);
}

/* Send WHOIS query */
void irc_send_whois(const char *target, const char *mask, irc_account *ia)
{
	char buff[BUF_LEN];
	memset(buff, 0, BUF_LEN);

	if (target)
		sprintf(buff, "WHOIS %s ", target);
	else
		sprintf(buff, "WHOIS ");

	strcat(buff, mask);
	strcat(buff, "\n");

	ia->callbacks->irc_send_data(buff, strlen(buff), ia);
}

/* Set Away */
void irc_set_away(char *message, irc_account *ia)
{
	char buff[BUF_LEN];
	memset(buff, 0, BUF_LEN);

	if (message) {
		/* Actually set away */
		sprintf(buff, "AWAY :%s\n", message);
		ia->callbacks->irc_send_data(buff, strlen(buff), ia);

	} else {
		/* Unset away */
		sprintf(buff, "AWAY\n");
		ia->callbacks->irc_send_data(buff, strlen(buff), ia);
	}
}

/* Set Modes */
void irc_set_mode(int irc_mode, irc_account *ia)
{
	char buff[BUF_LEN];
	memset(buff, 0, BUF_LEN);

	sprintf(buff, "MODE %s +%c\n", ia->nick, irc_modes[irc_mode]);
	ia->callbacks->irc_send_data(buff, strlen(buff), ia);
}

/* Unset modes */
void irc_unset_mode(int irc_mode, irc_account *ia)
{
	char buff[BUF_LEN];
	memset(buff, 0, BUF_LEN);

	sprintf(buff, "MODE %s -%c\n", ia->nick, irc_modes[irc_mode]);
	ia->callbacks->irc_send_data(buff, strlen(buff), ia);
}

/* Join a Channel */
void irc_join(const char *room, irc_account *ia)
{
	char buff[BUF_LEN];
	memset(buff, 0, BUF_LEN);

	sprintf(buff, "JOIN :%s\n", room);

	ia->callbacks->irc_send_data(buff, strlen(buff), ia);
}

/* Leave a Channel */
void irc_leave_chat_room(const char *room, irc_account *ia)
{
	char buff[BUF_LEN];
	memset(buff, 0, BUF_LEN);

	sprintf(buff, "PART :%s\n", room);

	ia->callbacks->irc_send_data(buff, strlen(buff), ia);
}

/* Send an Invite */
void irc_send_invite(const char *user, const char *room,
	const char *message, irc_account *ia)
{
	char buff[BUF_LEN];
	memset(buff, 0, BUF_LEN);

	if (*message) {
		sprintf(buff, "PRIVMSG %s :%s\n", user, message);

		ia->callbacks->irc_send_data(buff, strlen(buff), ia);
	}

	sprintf(buff, "INVITE %s %s\n", user, room);
	ia->callbacks->irc_send_data(buff, strlen(buff), ia);
}

void irc_get_names_list(const char *channel, irc_account *ia)
{
	char buff[BUF_LEN];
	memset(buff, 0, BUF_LEN);

	sprintf(buff, "NAMES %s\n", channel);
	ia->callbacks->irc_send_data(buff, strlen(buff), ia);
}

void irc_request_list(const char *channel, const char *target, irc_account *ia)
{
	char buff[BUF_LEN];
	memset(buff, 0, BUF_LEN);

	sprintf(buff, "LIST");
	if (channel) {
		strcat(buff, " ");
		strcat(buff, channel);
	}

	if (target) {
		strcat(buff, " ");
		strcat(buff, target);
	}

	strcat(buff, "\n");

	ia->callbacks->irc_send_data(buff, strlen(buff), ia);
}

/* Send a PONG message response. Called by the PING handler */
void irc_send_pong(const char *message, irc_account *ia)
{
	char buff[BUF_LEN];
	memset(buff, 0, BUF_LEN);

	sprintf(buff, "PONG :%s\n", message);
	ia->callbacks->irc_send_data(buff, strlen(buff), ia);
}

/* Get PRIVMSG. CTCP implementation called here. */
void irc_process_privmsg(const char *to, const char *message,
	irc_message_prefix *prefix, irc_account *ia)
{
	/* 
	 * This only decodes. You will want to call ctcp_get_extended_data() to actually 
	 * get the complete break-up of the string into constituent commands
	 */
	char *out_msg = ctcp_decode(message, strlen(message));

	ia->callbacks->got_privmsg(to, out_msg, prefix, ia);

	if (out_msg) {
		free(out_msg);
	}
}

/* Get NOTICE. CTCP implementation called here. */
void irc_process_notice(const char *to, const char *message,
	irc_message_prefix *prefix, irc_account *ia)
{
	/* 
	 * This only decodes. You will want to call ctcp_get_extended_data() to actually 
	 * get the complete break-up of the string into constituent commands
	 */
	char *out_msg = ctcp_decode(message, strlen(message));

	ia->callbacks->incoming_notice(to, out_msg, prefix, ia);

	if (out_msg) {
		free(out_msg);
	}
}

irc_name_list *irc_gen_name_list(char *message)
{
	char *offset = NULL;
	irc_name_list *list = NULL;
	irc_name_list *head = NULL;

	while (message && *message) {
		offset = strchr(message, ' ');

		if (list) {
			list->next =
				(irc_name_list *)calloc(1,
				sizeof(irc_name_list));
			list = list->next;
		} else if (!list) {
			list = (irc_name_list *)calloc(1,
				sizeof(irc_name_list));
			head = list;
		}

		if (offset) {
			*offset = '\0';
		}

		if (*message == '+' || *message == '@') {
			list->attribute = *message;
			message++;
		} else
			list->attribute = '\0';

		list->name = strdup(message);

		/* We're at the end of the list... get out now... */
		if (!offset)
			break;

		message = offset + 1;
	}

	return head;
}

/* Add a parameter to the list and return its new head */
irc_param_list *irc_param_list_add(irc_param_list *param_list,
	const char *param)
{
	irc_param_list *head = param_list;
	irc_param_list *new =
		(irc_param_list *)calloc(1, sizeof(irc_param_list));

	if (!param_list) {
		new->param = strdup(param);
		new->next = NULL;

		return new;
	}

	while (param_list->next)
		param_list = param_list->next;

	param_list->next = new;

	new->param = strdup(param);
	new->next = NULL;

	return head;
}

/* Free the parameter list */
void irc_param_list_free(irc_param_list *param_list)
{
	irc_param_list *to_be_freed = NULL;

	while (param_list) {
		to_be_freed = param_list;
		param_list = param_list->next;
		free(to_be_freed);
		to_be_freed = NULL;
	}
}

/* Get nth paramater in the list */
char *irc_param_list_get_at(irc_param_list *list, int position)
{
	int cur = 0;

	if (!list)
		return NULL;

	while (list->next && cur < position) {
		list = list->next;
		cur++;
	}

	if (list)
		return list->param;
	else
		return NULL;
}

int irc_recv(irc_account *ia, char *buf, int len)
{
	if (buf[len] == '\n') {
		char *fbuf;

		if (buf[len - 1] != '\r')
			return 0;

		fbuf = strdup(buf);

		fbuf[len - 1] = '\0';
		irc_message_parse(fbuf, ia);

		free(fbuf);

		return 1;
	}

	return 0;
}