segmented irc.c from main for more usefulness within parse.c
This commit is contained in:
parent
3d4a6cf8ff
commit
21a45f1d4a
2
build.sh
2
build.sh
@ -9,7 +9,7 @@ PREFIX=${PREFIX:-$DIR}
|
||||
|
||||
CC=${CC-cc}
|
||||
CFLAGS='-std=c99 -Wall -Wextra -Wpedantic'
|
||||
CPPFLAGS="-I/usr/bin/ircclient/ -Iinclude -D_GNU_SOURCE -DPROGN=\"$PROGN\""
|
||||
CPPFLAGS="-I/usr/bin/ircclient/ -Iinclude -D_GNU_SOURCE -DDELC=static -DPROGN=\"$PROGN\""
|
||||
LDFLAGS='-lircclient'
|
||||
|
||||
mkdir -p $PREFIX && echo "Made directory: $PREFIX"
|
||||
|
@ -1,9 +0,0 @@
|
||||
#ifndef CONFIG_H_
|
||||
|
||||
#define SERVER "irc.rizon.net"
|
||||
#define PORT 6667
|
||||
#define CHANNEL "#/g/chad"
|
||||
#define USERNAME "probotic"
|
||||
|
||||
#define CONFIG_H_
|
||||
#endif
|
11
include/irc.h
Normal file
11
include/irc.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef IRC_H_
|
||||
|
||||
#include <libircclient.h>
|
||||
|
||||
extern irc_session_t * session;
|
||||
extern irc_callbacks_t callbacks;
|
||||
|
||||
extern char const * channel;
|
||||
|
||||
#define IRC_H_
|
||||
#endif
|
143
src/irc.c
Normal file
143
src/irc.c
Normal file
@ -0,0 +1,143 @@
|
||||
/* irc.c - IRC interface
|
||||
|
||||
Probotic is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 3 only as
|
||||
published by the Free Software Foundation.
|
||||
|
||||
Probotic 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 version 3 for more details.
|
||||
|
||||
The above copyright notice, this permission notice and the word
|
||||
"NIGGER" shall be included in all copies or substantial portions
|
||||
of the Software.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
version 3 + NIGGER along with Probotic.
|
||||
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
irc_session_t * session;
|
||||
irc_callbacks_t callbacks;
|
||||
|
||||
const char * channel;
|
||||
|
||||
#define IRCMSG(msg) irc_cmd_msg(session, channel, msg)
|
||||
|
||||
DELC char *
|
||||
get_username(const char * origin)
|
||||
{
|
||||
const char USERNAME_TERMINATOR = '!';
|
||||
int i = 0;
|
||||
char * r;
|
||||
while (origin[i] != USERNAME_TERMINATOR)
|
||||
{ i++; }
|
||||
r = (char *) malloc(i + 1);
|
||||
strncpy(r, origin, i);
|
||||
r[i] = '\00';
|
||||
return r;
|
||||
}
|
||||
|
||||
DELC void
|
||||
msg_wrapper(const char* fmt,
|
||||
...)
|
||||
{
|
||||
va_list args;
|
||||
char * fmtdmsg;
|
||||
|
||||
va_start(args, fmt);
|
||||
if(vasprintf(&fmtdmsg, fmt, args) == -1)
|
||||
{ exit(1); }
|
||||
|
||||
puts(fmtdmsg);
|
||||
irc_cmd_msg(session, channel, fmtdmsg);
|
||||
|
||||
free(fmtdmsg);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
DELC void
|
||||
event_connect(irc_session_t * session,
|
||||
const char * event,
|
||||
const char * origin,
|
||||
const char ** params,
|
||||
unsigned int count)
|
||||
{
|
||||
(void) event;
|
||||
(void) origin;
|
||||
(void) params;
|
||||
(void) count;
|
||||
irc_cmd_join(session, CHANNEL, 0);
|
||||
}
|
||||
|
||||
DELC void
|
||||
event_channel(irc_session_t * session,
|
||||
const char * event,
|
||||
const char * origin,
|
||||
const char ** params,
|
||||
unsigned int count)
|
||||
{
|
||||
size_t i = 0, len;
|
||||
const char * channel = params[0];
|
||||
char * message = params[1];
|
||||
char * arg;
|
||||
char * swp;
|
||||
(void) session;
|
||||
(void) event;
|
||||
(void) origin;
|
||||
(void) channel;
|
||||
(void) message;
|
||||
(void) count;
|
||||
swp = get_username(origin);
|
||||
/* msg_wrapper("%s, you are a faggot for this opinion.", swp); */
|
||||
/* parses the command */
|
||||
len = strlen(message);
|
||||
while (message[i] != ' ')
|
||||
{ ++i; }
|
||||
message[i] = '\0';
|
||||
arg = message + i + 1;
|
||||
|
||||
if (*message == '!')
|
||||
{
|
||||
++message;
|
||||
/* if (strcmp(message, "stop") == 0) { exit(1); } */
|
||||
if (strcmp(message, "remind") == 0)
|
||||
{ msg_wrapper("%s: No current assignment", swp); }
|
||||
else if (strcmp(message, "next") == 0)
|
||||
{ msg_wrapper("%s: No future assignments", swp); }
|
||||
else if (strcmp(message, "raw") == 0)
|
||||
{ msg_wrapper("%s: Executing SQL `%s'", arg); }
|
||||
else if (strcmp(message, "dump") == 0)
|
||||
{ msg_wrapper("%s: All projects"); }
|
||||
else if (strcmp(message, "submit") == 0)
|
||||
{ msg_wrapper("%s: Submitting project link '%s' to <random janny>", swp, arg); }
|
||||
else if (strcmp(message, "reroll") == 0)
|
||||
{ msg_wrapper("%s: No more rerolls possible.", swp); }
|
||||
#ifdef NDEBUG
|
||||
else
|
||||
{ msg_wrapper("No such command '%s'", message); }
|
||||
#endif /* NDEBUG */
|
||||
}
|
||||
free(swp);
|
||||
}
|
||||
|
||||
DELC int
|
||||
init(char const * username, char const * server, int port)
|
||||
{
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
callbacks.event_connect = event_connect;
|
||||
callbacks.event_channel = event_channel;
|
||||
|
||||
session = irc_create_session(&callbacks);
|
||||
if (!session)
|
||||
{ ERR(1, "Error creating IRC session"); }
|
||||
irc_connect(session, server, port, 0, username, username, username);
|
||||
/* We should figure out how the failure happens so we can tell the user that. */
|
||||
if (irc_run(session) != 0)
|
||||
{ ERR(1, "Error running IRC session\nPossible issue: bad URL, no network connection, bad port, refused connection."); }
|
||||
return 0;
|
||||
}
|
131
src/main.c
131
src/main.c
@ -19,136 +19,13 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <libircclient.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "config.h"
|
||||
#include "irc.h"
|
||||
|
||||
irc_session_t * session;
|
||||
irc_callbacks_t callbacks;
|
||||
|
||||
const char * channel;
|
||||
|
||||
char * get_username(const char * origin)
|
||||
{
|
||||
const char USERNAME_TERMINATOR = '!';
|
||||
int i = 0;
|
||||
char * r;
|
||||
while (origin[i] != USERNAME_TERMINATOR)
|
||||
{ i++; }
|
||||
r = (char *) malloc(i + 1);
|
||||
strncpy(r, origin, i);
|
||||
r[i] = '\00';
|
||||
return r;
|
||||
}
|
||||
|
||||
void
|
||||
msg_wrapper(const char* fmt,
|
||||
...)
|
||||
{
|
||||
va_list args;
|
||||
char * fmtdmsg;
|
||||
|
||||
va_start(args, fmt);
|
||||
if(vasprintf(&fmtdmsg, fmt, args) == -1)
|
||||
{ exit(1); }
|
||||
|
||||
puts(fmtdmsg);
|
||||
irc_cmd_msg(session, channel, fmtdmsg);
|
||||
|
||||
free(fmtdmsg);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void
|
||||
event_connect(irc_session_t * session,
|
||||
const char * event,
|
||||
const char * origin,
|
||||
const char ** params,
|
||||
unsigned int count)
|
||||
{
|
||||
(void) event;
|
||||
(void) origin;
|
||||
(void) params;
|
||||
(void) count;
|
||||
irc_cmd_join(session, CHANNEL, 0);
|
||||
}
|
||||
|
||||
void
|
||||
event_channel(irc_session_t * session,
|
||||
const char * event,
|
||||
const char * origin,
|
||||
const char ** params,
|
||||
unsigned int count)
|
||||
{
|
||||
size_t i = 0, len;
|
||||
const char * channel = params[0];
|
||||
char * message = params[1];
|
||||
char * arg;
|
||||
char * swp;
|
||||
(void) session;
|
||||
(void) event;
|
||||
(void) origin;
|
||||
(void) channel;
|
||||
(void) message;
|
||||
(void) count;
|
||||
swp = get_username(origin);
|
||||
/* msg_wrapper("%s, you are a faggot for this opinion.", swp); */
|
||||
/* parses the command */
|
||||
len = strlen(message);
|
||||
while (message[i] != ' ')
|
||||
{ ++i; }
|
||||
message[i] = '\0';
|
||||
arg = message + i + 1;
|
||||
|
||||
if (*message == '!')
|
||||
{
|
||||
++message;
|
||||
/* if (strcmp(message, "stop") == 0) { exit(1); } */
|
||||
if (strcmp(message, "remind") == 0)
|
||||
{ msg_wrapper("%s: No current assignment.", swp); }
|
||||
else if (strcmp(message, "next") == 0)
|
||||
{ msg_wrapper("%s: No future assignments", swp); }
|
||||
else if (strcmp(message, "raw") == 0)
|
||||
{ msg_wrapper("%s: executing sql %s", arg); }
|
||||
else if (strcmp(message, "dump") == 0)
|
||||
{ msg_wrapper("%s: all projects... they don't seem to exist!"); }
|
||||
else if (strcmp(message, "submit") == 0)
|
||||
{ msg_wrapper("%s: Submitting project link '%s' to <random janny>", swp, arg); }
|
||||
else if (strcmp(message, "reroll") == 0)
|
||||
{ msg_wrapper("%s: No more rerolls possible.", swp); }
|
||||
#ifdef NDEBUG
|
||||
else
|
||||
{ msg_wrapper("No such command '%s'", message); }
|
||||
#endif /* NDEBUG */
|
||||
}
|
||||
free(swp);
|
||||
}
|
||||
|
||||
int
|
||||
init(char const * username, char const * server, int port)
|
||||
{
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
callbacks.event_connect = event_connect;
|
||||
callbacks.event_channel = event_channel;
|
||||
|
||||
session = irc_create_session(&callbacks);
|
||||
if (!session)
|
||||
{ ERR(1, "Error creating IRC session"); }
|
||||
irc_connect(session, server, port, 0, username, username, username);
|
||||
/* We should figure out how the failure happens so we can tell the user that. */
|
||||
if (irc_run(session) != 0)
|
||||
{ ERR(1, "Error running IRC session\nPossible issue: bad URL, no network connection, bad port, refused connection."); }
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* args: server port channel [username] - defaults to probotic */
|
||||
|
||||
/* I'd have to give up constification for server:port */
|
||||
/* args: server port channel [username]
|
||||
username defaults to probotic */
|
||||
int
|
||||
main(int argc,
|
||||
char const ** argv)
|
||||
|
10
src/parse.c
10
src/parse.c
@ -29,21 +29,23 @@
|
||||
|
||||
/* these are closer described as 'setters', no? */
|
||||
|
||||
int parse_remind(char const * arg)
|
||||
DELC int
|
||||
parse_remind(char const * arg)
|
||||
{
|
||||
(void) arg;
|
||||
ERRMSG("not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parse_repo(char const * arg)
|
||||
DELC int
|
||||
parse_repo(char const * arg)
|
||||
{
|
||||
(void) arg;
|
||||
ERRMSG("not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
DELC int
|
||||
parse_creds(creds_t * creds,
|
||||
char const * creds_file)
|
||||
{
|
||||
@ -72,7 +74,7 @@ parse_creds(creds_t * creds,
|
||||
fclose(stream);
|
||||
return 0;
|
||||
|
||||
// Releasing everything in cause of a failure
|
||||
/* Releasing everything in cause of a failure */
|
||||
fail:
|
||||
fclose(stream);
|
||||
clean_creds(creds);
|
||||
|
Reference in New Issue
Block a user