Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/dll/nap/napfunc.c
1072 views
1
#define IN_MODULE
2
#include "irc.h"
3
#include "struct.h"
4
#include "dcc.h"
5
#include "ircaux.h"
6
#include "misc.h"
7
#include "output.h"
8
#include "lastlog.h"
9
#include "screen.h"
10
#include "status.h"
11
#include "window.h"
12
#include "vars.h"
13
#include "input.h"
14
#include "module.h"
15
#include "hook.h"
16
#include "list.h"
17
#include "modval.h"
18
#include "./napster.h"
19
#include "md5.h"
20
21
#define NEMPTY ""
22
#define EMPTY_STRING m_strdup(NEMPTY)
23
#define RETURN_EMPTY return EMPTY_STRING
24
#define RETURN_IF_EMPTY(x) if (empty( x )) RETURN_EMPTY
25
#define GET_INT_ARG(x, y) {RETURN_IF_EMPTY(y); x = atol(new_next_arg(y, &y));}
26
#define GET_STR_ARG(x, y) {RETURN_IF_EMPTY(y); x = new_next_arg(y, &y);RETURN_IF_EMPTY(x);}
27
#define RETURN_STR(x) return m_strdup(x ? x : EMPTY)
28
#define RETURN_INT(x) return m_strdup(ltoa(x))
29
30
31
/*
32
* given a "time in seconds" we convert this to a pretty time format
33
*/
34
35
BUILT_IN_FUNCTION(func_mp3_time)
36
{
37
unsigned long t;
38
t = my_atol(input);
39
return m_strdup(mp3_time(t));
40
}
41
42
/*
43
* return topic for the channel or empty
44
*/
45
46
BUILT_IN_FUNCTION(func_topic)
47
{
48
char *chan;
49
ChannelStruct *ch;
50
GET_STR_ARG(chan, input);
51
ch = (ChannelStruct *)find_in_list((List **)&nchannels, chan, 0);
52
return ch ? m_strdup(ch->topic) : m_strdup("");
53
}
54
55
/*
56
* are we on "channel"
57
*/
58
59
BUILT_IN_FUNCTION(func_onchan)
60
{
61
char *chan;
62
ChannelStruct *ch;
63
GET_STR_ARG(chan, input);
64
ch = (ChannelStruct *)find_in_list((List **)&nchannels, chan, 0);
65
return ch ? m_strdup("1") : m_strdup("0");
66
}
67
68
/*
69
* given a channel, returns the nicks on that channel.
70
* given a channel and a "nick speed files" returns the nick else empty.
71
*/
72
BUILT_IN_FUNCTION(func_onchannel)
73
{
74
char *chan;
75
ChannelStruct *ch;
76
NickStruct *n;
77
char *nick = NULL;
78
GET_STR_ARG(chan, input);
79
if ((ch = (ChannelStruct *)find_in_list((List **)&nchannels, chan, 0)))
80
{
81
char *ret = NULL;
82
char buffer[200];
83
if (input && *input)
84
{
85
while ((nick = next_arg(input, &input)))
86
{
87
for (n = ch->nicks; n; n = n->next)
88
{
89
if (!my_stricmp(nick, n->nick))
90
{
91
sprintf(buffer, "%s %d %lu", n->nick, n->speed, n->shared);
92
m_s3cat(&ret, " ", buffer);
93
}
94
}
95
}
96
}
97
else
98
{
99
for (n = ch->nicks; n; n = n->next)
100
m_s3cat(&ret, " ", n->nick);
101
}
102
return ret ? ret : m_strdup("");
103
}
104
RETURN_EMPTY;
105
}
106
107
BUILT_IN_FUNCTION(func_connected)
108
{
109
if (nap_socket > -1)
110
{
111
int len;
112
struct sockaddr_in name;
113
len = sizeof (name);
114
if (getpeername(nap_socket, (struct sockaddr *)&name, &len))
115
return m_strdup("-1");
116
return m_sprintf("%s %d", inet_ntoa(name.sin_addr), ntohs(name.sin_port));
117
}
118
return m_strdup("");
119
}
120
121
BUILT_IN_FUNCTION(func_hotlist)
122
{
123
char *nick = NULL;
124
char *ret = NULL;
125
NickStruct *n;
126
if (input && *input)
127
{
128
char buffer[200];
129
while ((nick = next_arg(input, &input)))
130
{
131
for (n = nap_hotlist; n; n = n->next)
132
{
133
if (!my_stricmp(nick, n->nick))
134
{
135
sprintf(buffer, "%s %d %lu", n->nick, n->speed, n->shared);
136
m_s3cat(&ret, " ", buffer);
137
}
138
}
139
}
140
}
141
else
142
{
143
for (n = nap_hotlist; n; n = n->next)
144
m_s3cat(&ret, " ", n->nick);
145
146
}
147
return ret ? ret : m_strdup("");
148
}
149
150
BUILT_IN_FUNCTION(func_raw)
151
{
152
int l;
153
_N_DATA n_data = {0, 0};
154
GET_INT_ARG(l, input);
155
n_data.command = l;
156
if (input && *input)
157
n_data.len = strlen(input);
158
if (nap_socket < 0)
159
return m_strdup("-1");
160
write(nap_socket, &n_data, 4);
161
if (n_data.len)
162
return m_strdup(ltoa(write(nap_socket, input, n_data.len)));
163
else
164
return m_strdup("0");
165
}
166
167
BUILT_IN_FUNCTION(func_napchannel)
168
{
169
return nap_current_channel ? m_strdup(nap_current_channel) : m_strdup(empty_string);
170
}
171
172
BUILT_IN_FUNCTION(func_md5)
173
{
174
int l;
175
unsigned long size = 0;
176
GET_INT_ARG(l, input);
177
if (input && *input)
178
size = my_atol(input);
179
return calc_md5(l, size);
180
}
181
182