2015-02-14 13:35:02 -05:00
/*
2026-01-05 23:12:38 -07:00
* SysInfo - sysinfo plugin for ZoiteChat
2015-02-14 13:35:02 -05:00
* Copyright (c) 2012 Berke Viktor.
2015-01-03 15:26:05 -08:00
*
2015-02-14 13:35:02 -05:00
* xsys.c - main functions for X-Sys 2
* by mikeshoup
* Copyright (C) 2003, 2004, 2005 Michael Shoup
* Copyright (C) 2005, 2006, 2007 Tony Vroon
2015-01-03 15:26:05 -08:00
*
2015-02-14 13:35:02 -05:00
* 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.
2015-01-03 15:26:05 -08:00
*
2015-02-14 13:35:02 -05:00
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
2015-01-03 15:26:05 -08:00
*/
2015-02-14 13:35:02 -05:00
#include "config.h"
2015-01-03 15:26:05 -08:00
2015-02-14 13:35:02 -05:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2015-01-03 15:26:05 -08:00
#include <glib.h>
2026-01-05 23:12:38 -07:00
#include "zoitechat-plugin.h"
2015-02-14 13:35:02 -05:00
#include "sysinfo-backend.h"
#include "sysinfo.h"
2015-01-03 15:26:05 -08:00
2026-01-05 23:12:38 -07:00
#define _(x) zoitechat_gettext(ph,x)
2015-02-14 13:35:02 -05:00
#define DEFAULT_ANNOUNCE TRUE
2015-01-03 15:26:05 -08:00
2026-01-05 23:12:38 -07:00
static zoitechat_plugin * ph ;
2015-01-03 15:26:05 -08:00
2015-02-14 13:35:02 -05:00
static char name [] = "Sysinfo" ;
static char desc [] = "Display info about your hardware and OS" ;
static char version [] = "1.0" ;
2026-03-29 11:25:44 -04:00
static char sysinfo_help [] = "SysInfo Usage: \n /SYSINFO [-e|-o] [CLIENT|UI|OS|CPU|RAM|STORAGE|GPU|CHIPSET|SOUND|ETHERNET|UPTIME], print various details about your system or print a summary without arguments \n /SYSINFO SET <variable> \n " ;
2015-01-03 15:26:05 -08:00
2015-02-14 13:35:02 -05:00
typedef struct
2015-01-03 15:26:05 -08:00
{
2015-02-14 13:35:02 -05:00
const char * name ; /* Lower case name used for prefs */
const char * title ; /* Used for the end formatting */
char * ( * callback ) ( void );
gboolean def ; /* Hide by default? */
} hwinfo ;
static char *
get_client ( void )
2015-01-03 15:26:05 -08:00
{
2026-03-25 14:00:12 -04:00
const char * ver = zoitechat_get_info ( ph , "version" );
2026-02-01 15:23:36 -07:00
char * out ;
out = g_strdup_printf ( "ZoiteChat %s" , ver );
return out ;
2015-01-03 15:26:05 -08:00
}
2015-02-14 13:35:02 -05:00
static hwinfo hwinfos [] = {
{ "client" , "Client" , get_client },
2026-02-01 15:23:36 -07:00
{ "ui" , "UI" , sysinfo_backend_get_ui },
2015-02-14 13:35:02 -05:00
{ "os" , "OS" , sysinfo_backend_get_os },
{ "cpu" , "CPU" , sysinfo_backend_get_cpu },
{ "memory" , "Memory" , sysinfo_backend_get_memory },
2026-03-29 11:27:11 -04:00
{ "storage" , "Storage" , sysinfo_backend_get_disk , TRUE },
2026-03-25 14:33:27 -04:00
{ "gpu" , "GPU" , sysinfo_backend_get_gpu },
2026-03-25 14:32:37 -04:00
{ "chipset" , "CHIPSET" , sysinfo_backend_get_chipset , TRUE },
2015-02-14 13:35:02 -05:00
{ "sound" , "Sound" , sysinfo_backend_get_sound , TRUE },
{ "ethernet" , "Ethernet" , sysinfo_backend_get_network , TRUE },
{ "uptime" , "Uptime" , sysinfo_backend_get_uptime },
{ NULL , NULL },
};
static gboolean sysinfo_get_bool_pref ( const char * pref , gboolean def );
static gboolean
should_show_info ( hwinfo info )
2015-01-03 15:26:05 -08:00
{
2015-02-14 13:35:02 -05:00
char hide_pref [ 32 ];
2015-01-03 15:26:05 -08:00
2015-02-14 13:35:02 -05:00
g_snprintf ( hide_pref , sizeof ( hide_pref ), "hide_%s" , info . name );
return ! sysinfo_get_bool_pref ( hide_pref , info . def );
2015-01-03 15:26:05 -08:00
}
2015-02-14 13:35:02 -05:00
static void
print_summary ( gboolean announce )
2015-01-04 15:08:45 -08:00
{
2015-02-14 13:35:02 -05:00
char ** strings = g_new0 ( char * , G_N_ELEMENTS ( hwinfos ));
int i , x ;
char * output ;
2015-01-04 16:54:15 -08:00
2015-02-14 13:35:02 -05:00
for ( i = 0 , x = 0 ; hwinfos [ i ]. name != NULL ; i ++ )
2015-01-03 15:26:05 -08:00
{
2015-02-14 13:35:02 -05:00
if ( should_show_info ( hwinfos [ i ]))
2015-01-03 15:26:05 -08:00
{
2015-02-14 13:35:02 -05:00
char * str = hwinfos [ i ]. callback ();
if ( str )
{
strings [ x ++ ] = g_strdup_printf ( " \002 %s \002 : %s" , hwinfos [ i ]. title , str );
g_free ( str );
}
2015-01-03 15:26:05 -08:00
}
}
2015-02-14 13:35:02 -05:00
output = g_strjoinv ( " \002\342\200\242\002 " , strings );
2026-01-05 23:12:38 -07:00
zoitechat_commandf ( ph , "%s %s" , announce ? "SAY" : "ECHO" , output );
2015-01-03 15:26:05 -08:00
2015-02-14 13:35:02 -05:00
g_strfreev ( strings );
g_free ( output );
2015-01-04 15:08:45 -08:00
}
2015-01-03 15:26:05 -08:00
2015-02-14 13:35:02 -05:00
static void
print_info ( char * info , gboolean announce )
2015-01-04 15:08:45 -08:00
{
2015-01-03 15:26:05 -08:00
int i ;
2015-02-14 13:35:02 -05:00
for ( i = 0 ; hwinfos [ i ]. name != NULL ; i ++ )
2015-01-03 15:26:05 -08:00
{
2015-02-14 13:35:02 -05:00
if ( ! g_ascii_strcasecmp ( info , hwinfos [ i ]. name ))
2015-01-03 15:26:05 -08:00
{
2015-02-14 13:35:02 -05:00
char * str = hwinfos [ i ]. callback ();
if ( str )
2015-01-03 15:26:05 -08:00
{
2026-01-05 23:12:38 -07:00
zoitechat_commandf ( ph , "%s \002 %s \002 : %s" , announce ? "SAY" : "ECHO" ,
2015-02-14 13:35:02 -05:00
hwinfos [ i ]. title , str );
g_free ( str );
2015-01-03 15:26:05 -08:00
}
2015-02-14 13:35:02 -05:00
else
2026-01-05 23:12:38 -07:00
zoitechat_print ( ph , _ ( "Sysinfo: Failed to get info. Either not supported or error." ));
2015-02-14 13:35:02 -05:00
return ;
2015-01-04 15:08:45 -08:00
}
2015-01-03 15:26:05 -08:00
}
2026-01-05 23:12:38 -07:00
zoitechat_print ( ph , _ ( "Sysinfo: No info by that name \n " ));
2015-01-03 15:26:05 -08:00
}
2015-02-14 13:35:02 -05:00
static gboolean
sysinfo_get_bool_pref ( const char * pref , gboolean def )
2015-01-04 15:08:45 -08:00
{
2026-01-05 23:12:38 -07:00
int value = zoitechat_pluginpref_get_int ( ph , pref );
2015-01-04 15:08:45 -08:00
2015-02-14 13:35:02 -05:00
if ( value != - 1 )
return value ;
2015-01-04 15:08:45 -08:00
2015-02-14 13:35:02 -05:00
return def ;
}
2015-01-04 15:08:45 -08:00
2015-02-14 13:35:02 -05:00
static void
sysinfo_set_pref_real ( const char * pref , char * value , gboolean def )
{
if ( value && value [ 0 ])
2015-01-04 15:08:45 -08:00
{
2015-02-14 13:35:02 -05:00
guint64 i = g_ascii_strtoull ( value , NULL , 0 );
2026-01-05 23:12:38 -07:00
zoitechat_pluginpref_set_int ( ph , pref , i != 0 );
zoitechat_printf ( ph , _ ( "Sysinfo: %s is set to: %d \n " ), pref , i != 0 );
2015-01-03 15:26:05 -08:00
}
else
{
2026-01-05 23:12:38 -07:00
zoitechat_printf ( ph , _ ( "Sysinfo: %s is set to: %d \n " ), pref ,
2015-02-14 13:35:02 -05:00
sysinfo_get_bool_pref ( pref , def ));
2015-01-04 15:08:45 -08:00
}
}
2015-02-14 13:35:02 -05:00
static void
sysinfo_set_pref ( char * key , char * value )
2015-01-04 15:08:45 -08:00
{
2015-02-14 13:35:02 -05:00
if ( ! key || ! key [ 0 ])
2015-01-04 15:08:45 -08:00
{
2026-01-05 23:12:38 -07:00
zoitechat_print ( ph , _ ( "Sysinfo: Valid settings are: announce and hide_* for each piece of information. e.g. hide_os. Without a value it will show current (or default) setting. \n " ));
2015-02-14 13:35:02 -05:00
return ;
2015-01-04 15:08:45 -08:00
}
2015-02-14 13:35:02 -05:00
if ( ! strcmp ( key , "announce" ))
2015-01-05 03:57:59 -08:00
{
2015-02-14 13:35:02 -05:00
sysinfo_set_pref_real ( key , value , DEFAULT_ANNOUNCE );
return ;
2015-01-05 03:57:59 -08:00
}
2015-02-14 13:35:02 -05:00
else if ( g_str_has_prefix ( key , "hide_" ))
2015-01-05 03:57:59 -08:00
{
2015-02-14 13:35:02 -05:00
int i ;
for ( i = 0 ; hwinfos [ i ]. name != NULL ; i ++ )
{
if ( ! strcmp ( key + 5 , hwinfos [ i ]. name ))
{
sysinfo_set_pref_real ( key , value , hwinfos [ i ]. def );
return ;
}
}
2015-01-05 03:57:59 -08:00
}
2026-01-05 23:12:38 -07:00
zoitechat_print ( ph , _ ( "Sysinfo: Invalid variable name \n " ));
2015-02-14 13:35:02 -05:00
}
2015-01-05 03:57:59 -08:00
2015-02-14 13:35:02 -05:00
static int
sysinfo_cb ( char * word [], char * word_eol [], void * userdata )
{
gboolean announce = sysinfo_get_bool_pref ( "announce" , DEFAULT_ANNOUNCE );
int offset = 0 , channel_type ;
char * cmd ;
2015-01-05 03:57:59 -08:00
2015-02-14 13:35:02 -05:00
if ( ! strcmp ( "-e" , word [ 2 ]))
2015-01-05 03:57:59 -08:00
{
2015-02-14 13:35:02 -05:00
announce = FALSE ;
offset ++ ;
2015-01-05 03:57:59 -08:00
}
2015-02-14 13:35:02 -05:00
else if ( ! strcmp ( "-o" , word [ 2 ]))
2015-01-05 03:57:59 -08:00
{
2015-02-14 13:35:02 -05:00
announce = TRUE ;
offset ++ ;
2015-01-05 03:57:59 -08:00
}
2026-01-05 23:12:38 -07:00
channel_type = zoitechat_list_int ( ph , NULL , "type" );
2015-02-14 13:35:02 -05:00
if ( channel_type != 2 /* SESS_CHANNEL */ && channel_type != 3 /* SESS_DIALOG */ )
announce = FALSE ;
cmd = word [ 2 + offset ];
if ( ! g_ascii_strcasecmp ( "SET" , cmd ))
sysinfo_set_pref ( word [ 3 + offset ], word_eol [ 4 + offset ]);
else if ( ! cmd || ! cmd [ 0 ])
print_summary ( announce );
else
print_info ( cmd , announce );
2015-01-05 03:57:59 -08:00
2026-01-25 16:13:47 -07:00
return ZOITECHAT_EAT_ALL ;
2015-01-05 03:57:59 -08:00
}
2015-02-14 13:35:02 -05:00
int
2026-01-05 23:12:38 -07:00
zoitechat_plugin_init ( zoitechat_plugin * plugin_handle , char ** plugin_name , char ** plugin_desc , char ** plugin_version , char * arg )
2015-01-03 15:26:05 -08:00
{
2015-02-14 13:35:02 -05:00
ph = plugin_handle ;
* plugin_name = name ;
* plugin_desc = desc ;
* plugin_version = version ;
2015-01-03 15:26:05 -08:00
2026-01-25 16:13:47 -07:00
zoitechat_hook_command ( ph , "SYSINFO" , ZOITECHAT_PRI_NORM , sysinfo_cb , sysinfo_help , NULL );
2015-01-04 15:08:45 -08:00
2026-02-01 15:23:36 -07:00
zoitechat_command ( ph , "MENU ADD \" Window/Display System Info \" \" SYSINFO \" " );
2026-01-05 23:12:38 -07:00
zoitechat_printf ( ph , _ ( "%s plugin loaded \n " ), name );
2015-02-14 13:35:02 -05:00
return 1 ;
2015-01-04 15:08:45 -08:00
}
2015-01-05 03:57:59 -08:00
2015-02-14 13:35:02 -05:00
int
2026-01-05 23:12:38 -07:00
zoitechat_plugin_deinit ( void )
2015-01-05 03:57:59 -08:00
{
2026-02-01 15:23:36 -07:00
zoitechat_command ( ph , "MENU DEL \" Window/Send System Info \" " );
2026-01-05 23:12:38 -07:00
zoitechat_command ( ph , "MENU DEL \" Window/Display System Info \" " );
zoitechat_printf ( ph , _ ( "%s plugin unloaded \n " ), name );
2015-02-14 13:35:02 -05:00
return 1 ;
2015-01-05 03:57:59 -08:00
}