aboutsummaryrefslogtreecommitdiffstats
path: root/examples/link.c
blob: 73af3b8277c59b7be891e5d1cc01d292a85fd07f (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <irc.h>

#define mywrite(a,b) write(a,b,strlen(b))

int *fds;
char **chans;

void extra_handler(int fd) {
 return;
}

void sendto_others(int fd,char *action,char *msg) {
 int i;
 char tmp[512];
 for(i=0;fds[i] != -1;i++) {
  if(fds[i] != fd) {
   snprintf(tmp,sizeof(tmp)-1,"%s %s :%s\r\n",action,chans[fdtoi(fds[i])],msg);
   write(fds[i],tmp,strlen(tmp));
  }
 }
}

void message_handler(int fd,char *from,struct user *user,char *line) {
 char tmp[512];
 if(!strcmp(from,chans[fdtoi(fd)])) {//don't want to be forwarding PMs. :P
  if(line[0] == '\x01' && strlen(line) > 9 && !strncmp(line+1,"ACTION ",7) && line[strlen(line)-1] == '\x01') {
   snprintf(tmp,sizeof(tmp)-1,"%cACTION %s %s",1,user->nick,line+8);
  } else {
   snprintf(tmp,sizeof(tmp)-1,"<%s> %s",user->nick,line);
  }
  sendto_others(fd,"PRIVMSG",tmp);
 }
}

void line_handler(int fd,char *line) {//this should be built into the libary?
 char *temp;
 char **a;
 char tmp[512];
 struct user *user=malloc(sizeof(struct user));
 printf("line: '%s'\n",line);
 a=line_cutter(fd,line,user);
 if(!user->user && a[0]) {
  if(!strcmp(a[0],"004")) {
   snprintf(tmp,sizeof(tmp)-1,"JOIN %s\r\n",chans[fdtoi(fd)]);
   temp=strchr(chans[fdtoi(fd)],' ');
   if(temp) *temp=0;
   mywrite(fd,tmp);
  }
 }
 if(a[0] && a[1] && a[2]) {
  if(!strcmp(a[0],"PRIVMSG")) {
   message_handler(fd,*a[1]=='#'?a[1]:user->nick,user,a[2]);
  }
 }
 if(a[0] && user->nick && a[1]) {
  if(!strcmp(a[0],"JOIN")) {
   snprintf(tmp,sizeof(tmp)-1,"%s has joined %s",user->nick,a[1]+(*a[1]==':'));
   sendto_others(fd,"NOTICE",tmp);
  }
  if(!strcmp(a[0],"PART")) {
   snprintf(tmp,sizeof(tmp)-1,"%s has parted %s",user->nick,a[1]+(*a[1]==':'));
   sendto_others(fd,"NOTICE",tmp);
  }
  if(!strcmp(a[0],"QUIT")) {
   snprintf(tmp,sizeof(tmp)-1,"%s has quited %s",user->nick,a[1]+(*a[1]==':'));
   sendto_others(fd,"NOTICE",tmp);
  }
 }
 free(user);
}

int fdtoi(int fd) {
 int i;
 for(i=0;fds[i] != -1;i++) {
  if(fds[i] == fd) return i;
 }
 return -1;
}

int main(int argc,char *argv[]) {
 fds=malloc(sizeof(int) * (argc+3) / 3);
 chans=malloc(sizeof(char *) * (argc+3) / 3);
 int i=0;
 printf("%d\n",argc);
 for(i=0;((i*3)+3)<argc;i++) {
  printf("%d server: %s port: %s channel: %s\n",i,argv[(i*3)+1],argv[(i*3)+2],argv[(i*3)+3]);
  fds[i]=serverConnect(argv[(i*3)+1],argv[(i*3)+2]);
  if(fds[i] == -1) return 1;
  chans[i]=strdup(argv[(i*3)+3]);
  mywrite(fds[i],"NICK link8239\r\nUSER a b c :d\r\n");
 }
 fds[i]=-1;
 //heh. you can write your own code for picking a different nick per server. fuck you.
 runem(fds,line_handler,extra_handler);
}