Contiki 3.x
ircc.c
1 /*
2  * Copyright (c) 2004, Adam Dunkels.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Adam Dunkels <adam@sics.se>
32  *
33  */
34 
35 #include "contiki.h"
36 #include "ircc.h"
37 
38 #include "ircc-strings.h"
39 
40 #include "lib/petsciiconv.h"
41 
42 #include <string.h>
43 
44 #ifdef IRC_CONF_SYSTEM_STRING
45 #define IRC_SYSTEM_STRING IRC_CONF_SYSTEM_STRING
46 #else
47 #define IRC_SYSTEM_STRING "Contiki"
48 #endif
49 
50 #define PORT 6667
51 
52 #define SEND_STRING(s, str) PSOCK_SEND(s, (uint8_t *)str, (unsigned int)strlen(str))
53 
54 #define ISO_space 0x20
55 #define ISO_bang 0x21
56 #define ISO_at 0x40
57 #define ISO_cr 0x0d
58 #define ISO_nl 0x0a
59 #define ISO_colon 0x3a
60 #define ISO_O 0x4f
61 
62 enum {
63  COMMAND_NONE,
64  COMMAND_JOIN,
65  COMMAND_PART,
66  COMMAND_MSG,
67  COMMAND_ACTIONMSG,
68  COMMAND_LIST,
69  COMMAND_QUIT
70 };
71 
72 /*---------------------------------------------------------------------------*/
73 void
74 ircc_init(void)
75 {
76 
77 }
78 /*---------------------------------------------------------------------------*/
79 static char *
80 copystr(char *dest, const char *src, size_t n)
81 {
82  size_t len;
83 
84  len = strlen(src);
85  strncpy(dest, src, n);
86 
87  if(len > n) {
88  return dest + n;
89  } else {
90  return dest + len;
91  }
92 }
93 /*---------------------------------------------------------------------------*/
94 static
95 PT_THREAD(setup_connection(struct ircc_state *s))
96 {
97  char *ptr;
98 
99 
100  PSOCK_BEGIN(&s->s);
101 
102  ptr = s->outputbuf;
103  ptr = copystr(ptr, ircc_strings_nick, sizeof(s->outputbuf));
104  ptr = copystr(ptr, s->nick, (int)sizeof(s->outputbuf) - (ptr - s->outputbuf));
105  ptr = copystr(ptr, ircc_strings_crnl_user, sizeof(s->outputbuf) - (ptr - s->outputbuf));
106  ptr = copystr(ptr, s->nick, sizeof(s->outputbuf) - (ptr - s->outputbuf));
107  ptr = copystr(ptr, ircc_strings_contiki, sizeof(s->outputbuf) - (ptr - s->outputbuf));
108  ptr = copystr(ptr, s->server, sizeof(s->outputbuf) - (ptr - s->outputbuf));
109  ptr = copystr(ptr, ircc_strings_colon_contiki, sizeof(s->outputbuf) - (ptr - s->outputbuf));
110 
111  SEND_STRING(&s->s, s->outputbuf);
112 
113  PSOCK_END(&s->s);
114 }
115 /*---------------------------------------------------------------------------*/
116 static
117 PT_THREAD(join_channel(struct ircc_state *s))
118 {
119  PSOCK_BEGIN(&s->s);
120 
121  SEND_STRING(&s->s, ircc_strings_join);
122  SEND_STRING(&s->s, s->channel);
123  SEND_STRING(&s->s, ircc_strings_crnl);
124 
125  ircc_sent(s);
126 
127  PSOCK_END(&s->s);
128 }
129 /*---------------------------------------------------------------------------*/
130 static
131 PT_THREAD(part_channel(struct ircc_state *s))
132 {
133  PSOCK_BEGIN(&s->s);
134 
135  SEND_STRING(&s->s, ircc_strings_part);
136  SEND_STRING(&s->s, s->channel);
137  SEND_STRING(&s->s, ircc_strings_crnl);
138 
139  ircc_sent(s);
140 
141  PSOCK_END(&s->s);
142 }
143 /*---------------------------------------------------------------------------*/
144 static
145 PT_THREAD(list_channel(struct ircc_state *s))
146 {
147  PSOCK_BEGIN(&s->s);
148 
149  SEND_STRING(&s->s, ircc_strings_list);
150  SEND_STRING(&s->s, s->channel);
151  SEND_STRING(&s->s, ircc_strings_crnl);
152 
153  ircc_sent(s);
154 
155  PSOCK_END(&s->s);
156 }
157 /*---------------------------------------------------------------------------*/
158 static
159 PT_THREAD(send_message(struct ircc_state *s))
160 {
161  char *ptr;
162 
163  PSOCK_BEGIN(&s->s);
164 
165  ptr = s->outputbuf;
166  ptr = copystr(ptr, ircc_strings_privmsg, sizeof(s->outputbuf));
167  ptr = copystr(ptr, s->channel, sizeof(s->outputbuf) - (ptr - s->outputbuf));
168  ptr = copystr(ptr, ircc_strings_colon, sizeof(s->outputbuf) - (ptr - s->outputbuf));
169  ptr = copystr(ptr, s->msg, sizeof(s->outputbuf) - (ptr - s->outputbuf));
170  ptr = copystr(ptr, ircc_strings_crnl, sizeof(s->outputbuf) - (ptr - s->outputbuf));
171 
172  SEND_STRING(&s->s, s->outputbuf);
173 
174  ircc_sent(s);
175 
176  PSOCK_END(&s->s);
177 }
178 /*---------------------------------------------------------------------------*/
179 static
180 PT_THREAD(send_actionmessage(struct ircc_state *s))
181 {
182  char *ptr;
183 
184  PSOCK_BEGIN(&s->s);
185 
186  ptr = s->outputbuf;
187  ptr = copystr(ptr, ircc_strings_privmsg, sizeof(s->outputbuf));
188  ptr = copystr(ptr, s->channel, sizeof(s->outputbuf) - (ptr - s->outputbuf));
189  ptr = copystr(ptr, ircc_strings_colon, sizeof(s->outputbuf) - (ptr - s->outputbuf));
190  ptr = copystr(ptr, ircc_strings_action, sizeof(s->outputbuf) - (ptr - s->outputbuf));
191  ptr = copystr(ptr, s->msg, sizeof(s->outputbuf) - (ptr - s->outputbuf));
192  ptr = copystr(ptr, ircc_strings_ctcpcrnl, sizeof(s->outputbuf) - (ptr - s->outputbuf));
193 
194 
195  SEND_STRING(&s->s, s->outputbuf);
196 
197  ircc_sent(s);
198 
199  PSOCK_END(&s->s);
200 }
201 /*---------------------------------------------------------------------------*/
202 struct parse_result {
203  char *msg;
204 
205  char *user;
206  char *host;
207  char *name;
208  char *command;
209  char *middle;
210  char *trailing;
211 };
212 static struct parse_result r;
213 static void
214 parse_whitespace(void)
215 {
216  while(*r.msg == ISO_space) ++r.msg;
217 }
218 static void
219 parse_word(void)
220 {
221  char *ptr;
222  ptr = strchr(r.msg, ISO_space);
223  if(ptr != NULL) {
224  r.msg = ptr;
225  }
226 }
227 static void
228 parse_user(void)
229 {
230  parse_whitespace();
231  r.user = r.msg;
232  parse_word();
233  *r.msg = 0;
234  ++r.msg;
235 }
236 static void
237 parse_host(void)
238 {
239  parse_whitespace();
240  r.host = r.msg;
241  parse_word();
242  *r.msg = 0;
243  ++r.msg;
244 }
245 
246 static void
247 parse_name(void)
248 {
249  parse_whitespace();
250  r.name = r.msg;
251  parse_word();
252  *r.msg = 0;
253  ++r.msg;
254 }
255 
256 static void
257 parse_prefix(void)
258 {
259  parse_name();
260  if(*r.msg == ISO_bang) {
261  ++r.msg;
262  parse_user();
263  }
264  if(*r.msg == ISO_at) {
265  ++r.msg;
266  parse_host();
267  }
268 }
269 
270 static void
271 parse_command(void)
272 {
273  parse_whitespace();
274  r.command = r.msg;
275  parse_word();
276  *r.msg = 0;
277  ++r.msg;
278 }
279 
280 /*static void
281 parse_trailing(void)
282 {
283  r.trailing = r.msg;
284  while(*r.msg != 0 && *r.msg != ISO_cr && *r.msg != ISO_nl) ++r.msg;
285  *r.msg = 0;
286  ++r.msg;
287 }*/
288 
289 static void
290 parse_params(void)
291 {
292  char *ptr;
293 
294  parse_whitespace();
295  ptr = strchr(r.msg, ISO_colon);
296  if(ptr != NULL) {
297  r.trailing = ptr + 1;
298  ptr = strchr(ptr, ISO_cr);
299  if(ptr != NULL) {
300  *ptr = 0;
301  }
302  }
303 }
304 
305 static void
306 parse(char *msg, struct parse_result *dummy)
307 {
308  r.msg = msg;
309  if(*r.msg == ISO_cr || *r.msg == ISO_nl) {
310  return;
311  }
312  if(*r.msg == ISO_colon) {
313  ++r.msg;
314  parse_prefix();
315  }
316 
317  parse_command();
318  parse_params();
319 
320  /* printf("user %s host %s name %s command %s middle %s trailing %s\n",
321  r.user, r.host, r.name, r.command, r.middle, r.trailing);*/
322 }
323 
324 /*---------------------------------------------------------------------------*/
325 static
326 PT_THREAD(handle_input(struct ircc_state *s))
327 {
328  char *ptr;
329  /* struct parse_result r;*/
330 
331  PSOCK_BEGIN(&s->s);
332 
333  PSOCK_READTO(&s->s, ISO_nl);
334 
335  if(PSOCK_DATALEN(&s->s) > 0) {
336 
337  s->inputbuf[PSOCK_DATALEN(&s->s)] = 0;
338 
339  if(strncmp(s->inputbuf, ircc_strings_ping, 5) == 0) {
340  strncpy(s->outputbuf, s->inputbuf, sizeof(s->outputbuf));
341 
342  /* Turn "PING" into "PONG" */
343  s->outputbuf[1] = ISO_O;
344  SEND_STRING(&s->s, s->outputbuf);
345  } else {
346 
347  memset(&r, 0, sizeof(r));
348 
349  parse(s->inputbuf, &r);
350 
351  if(r.name != NULL) {
352  ptr = strchr(r.name, ISO_bang);
353  if(ptr != NULL) {
354  *ptr = 0;
355  }
356  }
357 
358  if(r.command != NULL && strncmp(r.command, ircc_strings_join, 4) == 0) {
359  ircc_text_output(s, "Joined channel", r.name);
360  } else if(r.command != NULL && strncmp(r.command, ircc_strings_part, 4) == 0) {
361  ircc_text_output(s, "Left channel", r.name);
362  } else if(r.trailing != NULL) {
363  if(strncmp(r.trailing, ircc_strings_action,
364  strlen(ircc_strings_action)) == 0) {
365  ptr = strchr(&r.trailing[1], 1);
366  if(ptr != NULL) {
367  *ptr = 0;
368  }
369  ptr = &r.trailing[strlen(ircc_strings_action)];
370  petsciiconv_topetscii(r.name, strlen(r.name));
371  petsciiconv_topetscii(ptr, strlen(ptr));
372  ircc_text_output(s, r.name, ptr);
373  } else if(strncmp(r.trailing, ircc_strings_version_query,
374  strlen(ircc_strings_version_query)) == 0) {
375  if(r.name != NULL) {
376  strncpy(s->outputbuf, r.name, sizeof(s->outputbuf));
377  SEND_STRING(&s->s, ircc_strings_notice);
378  /* user is temporarily stored in outputbuf. */
379  SEND_STRING(&s->s, s->outputbuf);
380  SEND_STRING(&s->s, ircc_strings_colon);
381  SEND_STRING(&s->s, ircc_strings_version);
382  SEND_STRING(&s->s, ircc_strings_version_string);
383  SEND_STRING(&s->s, IRC_SYSTEM_STRING);
384  SEND_STRING(&s->s, ircc_strings_ctcpcrnl);
385  }
386  } else {
387  petsciiconv_topetscii(r.name, strlen(r.name));
388  petsciiconv_topetscii(r.trailing, strlen(r.trailing));
389  ircc_text_output(s, r.name, r.trailing);
390  }
391  }
392  }
393  }
394 
395  PSOCK_END(&s->s);
396 }
397 /*---------------------------------------------------------------------------*/
398 static
399 PT_THREAD(data_or_command(struct ircc_state *s))
400 {
401  PSOCK_BEGIN(&s->s);
402 
403  PSOCK_WAIT_UNTIL(&s->s, PSOCK_NEWDATA(&s->s) ||
404  (s->command != COMMAND_NONE));
405 
406  PSOCK_END(&s->s);
407 }
408 /*---------------------------------------------------------------------------*/
409 static
410 PT_THREAD(handle_connection(struct ircc_state *s))
411 {
412  PT_BEGIN(&s->pt);
413 
414  PSOCK_INIT(&s->s, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
415 
416  PT_WAIT_THREAD(&s->pt, setup_connection(s));
417 
418  while(1) {
419 
420  PT_WAIT_UNTIL(&s->pt, data_or_command(s));
421 
422  if(PSOCK_NEWDATA(&s->s)) {
423  PT_WAIT_THREAD(&s->pt, handle_input(s));
424  }
425 
426  if(s->command == COMMAND_JOIN) {
427  s->command = COMMAND_NONE;
428  PT_WAIT_THREAD(&s->pt, join_channel(s));
429  } else if(s->command == COMMAND_PART) {
430  s->command = COMMAND_NONE;
431  PT_WAIT_THREAD(&s->pt, part_channel(s));
432  } else if(s->command == COMMAND_MSG) {
433  s->command = COMMAND_NONE;
434  PT_WAIT_THREAD(&s->pt, send_message(s));
435  } else if(s->command == COMMAND_ACTIONMSG) {
436  s->command = COMMAND_NONE;
437  PT_WAIT_THREAD(&s->pt, send_actionmessage(s));
438  } else if(s->command == COMMAND_LIST) {
439  s->command = COMMAND_NONE;
440  PT_WAIT_THREAD(&s->pt, list_channel(s));
441  } else if(s->command == COMMAND_QUIT) {
442  s->command = COMMAND_NONE;
443  tcp_markconn(uip_conn, NULL);
444  PSOCK_CLOSE(&s->s);
445  process_post(PROCESS_CURRENT(), PROCESS_EVENT_EXIT, NULL);
446  PT_EXIT(&s->pt);
447  }
448  }
449 
450  PT_END(&s->pt);
451 }
452 /*---------------------------------------------------------------------------*/
453 void
454 ircc_appcall(void *s)
455 {
456  if(uip_closed() || uip_aborted() || uip_timedout()) {
457  ircc_closed(s);
458  } else if(uip_connected()) {
459  ircc_connected(s);
460  PT_INIT(&((struct ircc_state *)s)->pt);
461  memset(((struct ircc_state *)s)->channel, 0,
462  sizeof(((struct ircc_state *)s)->channel));
463  ((struct ircc_state *)s)->command = COMMAND_NONE;
464  handle_connection(s);
465  } else if(s != NULL) {
466  handle_connection(s);
467  }
468 }
469 /*---------------------------------------------------------------------------*/
470 struct ircc_state *
471 ircc_connect(struct ircc_state *s, char *servername, uip_ipaddr_t *ipaddr,
472  char *nick)
473 {
474  s->conn = tcp_connect((uip_ipaddr_t *)ipaddr, UIP_HTONS(PORT), s);
475  if(s->conn == NULL) {
476  return NULL;
477  }
478  s->server = servername;
479  s->nick = nick;
480  return s;
481 }
482 /*---------------------------------------------------------------------------*/
483 void
484 ircc_list(struct ircc_state *s)
485 {
486  s->command = COMMAND_LIST;
487 }
488 /*---------------------------------------------------------------------------*/
489 void
490 ircc_join(struct ircc_state *s, char *channel)
491 {
492  strncpy(s->channel, channel, sizeof(s->channel));
493  s->command = COMMAND_JOIN;
494 }
495 /*---------------------------------------------------------------------------*/
496 void
497 ircc_part(struct ircc_state *s)
498 {
499  s->command = COMMAND_PART;
500 }
501 /*---------------------------------------------------------------------------*/
502 void
503 ircc_quit(struct ircc_state *s)
504 {
505  s->command = COMMAND_QUIT;
506 }
507 /*---------------------------------------------------------------------------*/
508 void
509 ircc_msg(struct ircc_state *s, char *msg)
510 {
511  s->msg = msg;
512  s->command = COMMAND_MSG;
513 }
514 /*---------------------------------------------------------------------------*/
515 void
516 ircc_actionmsg(struct ircc_state *s, char *msg)
517 {
518  s->msg = msg;
519  s->command = COMMAND_ACTIONMSG;
520 }
521 /*---------------------------------------------------------------------------*/
#define PROCESS_CURRENT()
Get a pointer to the currently running process.
Definition: process.h:402
#define PT_WAIT_THREAD(pt, thread)
Block and wait until a child protothread completes.
Definition: pt.h:191
#define PSOCK_READTO(psock, c)
Read data up to a specified character.
Definition: psock.h:291
#define PSOCK_BEGIN(psock)
Start the protosocket protothread in a function.
Definition: psock.h:164
Representation of a uIP TCP connection.
Definition: uip.h:1336
CCIF struct uip_conn * tcp_connect(uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Open a TCP connection to the specified IP address and port.
#define uip_aborted()
Has the connection been aborted by the other end?
Definition: uip.h:781
#define NULL
The null pointer.
#define PT_INIT(pt)
Initialize a protothread.
Definition: pt.h:79
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
Definition: uip.h:1238
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
Definition: process.c:322
#define PT_THREAD(name_args)
Declaration of a protothread.
Definition: pt.h:99
#define uip_connected()
Has the connection just been connected?
Definition: uip.h:761
#define PT_WAIT_UNTIL(pt, condition)
Block and wait until condition is true.
Definition: pt.h:147
#define PSOCK_WAIT_UNTIL(psock, condition)
Wait until a condition is true.
Definition: psock.h:395
PETSCII/ASCII conversion functions.
#define PSOCK_INIT(psock, buffer, buffersize)
Initialize a protosocket.
Definition: psock.h:150
#define PSOCK_CLOSE(psock)
Close a protosocket.
Definition: psock.h:241
#define PSOCK_END(psock)
Declare the end of a protosocket&#39;s protothread.
Definition: psock.h:348
#define PT_BEGIN(pt)
Declare the start of a protothread inside the C function implementing the protothread.
Definition: pt.h:114
#define uip_timedout()
Has the connection timed out?
Definition: uip.h:791
#define PSOCK_NEWDATA(psock)
Check if new data has arrived on a protosocket.
Definition: psock.h:362
#define PT_END(pt)
Declare the end of a protothread.
Definition: pt.h:126
#define PSOCK_DATALEN(psock)
The length of the data that was previously read.
Definition: psock.h:304
#define uip_closed()
Has the connection been closed by the other end?
Definition: uip.h:771
#define PT_EXIT(pt)
Exit the protothread.
Definition: pt.h:245