]> git.mxchange.org Git - friendica-addons.git/blob - irc/irc.php
Move Config::get() to DI::config()->get()
[friendica-addons.git] / irc / irc.php
1 <?php
2 /**
3 * Name: IRC Chat Addon
4 * Description: add an Internet Relay Chat chatroom on freenode
5 * Version: 1.1
6 * Author: tony baldwin <https://free-haven.org/profile/tony>
7 * Author: Tobias Diekershoff <https://f.diekershoff.de/u/tobias>
8 */
9
10 use Friendica\Core\Config;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Renderer;
13 use Friendica\DI;
14
15 function irc_install() {
16         Hook::register('app_menu', 'addon/irc/irc.php', 'irc_app_menu');
17         Hook::register('addon_settings', 'addon/irc/irc.php', 'irc_addon_settings');
18         Hook::register('addon_settings_post', 'addon/irc/irc.php', 'irc_addon_settings_post');
19 }
20
21 function irc_uninstall() {
22         Hook::unregister('app_menu', 'addon/irc/irc.php', 'irc_app_menu');
23         Hook::unregister('addon_settings', 'addon/irc/irc.php', 'irc_addon_settings');
24
25 }
26
27
28 function irc_addon_settings(&$a,&$s) {
29         if(! local_user())
30                 return;
31
32     /* Add our stylesheet to the page so we can make our settings look nice */
33
34 //      DI::page()['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->getBaseURL() . '/addon/irc/irc.css' . '" media="all" />' . "\r\n";
35
36     /* setting popular channels, auto connect channels */
37         $sitechats = DI::pConfig()->get( local_user(), 'irc','sitechats'); /* popular channels */
38         $autochans = DI::pConfig()->get( local_user(), 'irc','autochans');  /* auto connect chans */
39
40         $t = Renderer::getMarkupTemplate( "settings.tpl", "addon/irc/" );
41         $s .= Renderer::replaceMacros($t, [
42                 '$header' => DI::l10n()->t('IRC Settings'),
43                 '$info' => DI::l10n()->t('Here you can change the system wide settings for the channels to automatically join and access via the side bar. Note the changes you do here, only effect the channel selection if you are logged in.'),
44                 '$submit' => DI::l10n()->t('Save Settings'),
45                 '$autochans' => [ 'autochans', DI::l10n()->t('Channel(s) to auto connect (comma separated)'), $autochans, DI::l10n()->t('List of channels that shall automatically connected to when the app is launched.')],
46                 '$sitechats' => [ 'sitechats', DI::l10n()->t('Popular Channels (comma separated)'), $sitechats, DI::l10n()->t('List of popular channels, will be displayed at the side and hotlinked for easy joining.') ]
47         ]);
48
49
50         return;
51
52 }
53
54 function irc_addon_settings_post(&$a, &$b) {
55         if(!local_user())
56                 return;
57
58         if(!empty($_POST['irc-submit'])) {
59                 if (isset($_POST['autochans'])) {
60                         DI::pConfig()->set(local_user(), 'irc', 'autochans', trim(($_POST['autochans'])));
61                 }
62                 if (isset($_POST['sitechats'])) {
63                         DI::pConfig()->set(local_user(), 'irc', 'sitechats', trim($_POST['sitechats']));
64                 }
65                 /* upid pop-up thing */
66                 info(DI::l10n()->t('IRC settings saved.') . EOL);
67         }
68 }
69
70 function irc_app_menu($a,&$b) {
71         $b['app_menu'][] = '<div class="app-title"><a href="irc">' . DI::l10n()->t('IRC Chatroom') . '</a></div>';
72 }
73
74
75 function irc_module() {
76         return;
77 }
78
79
80 function irc_content(&$a) {
81
82         $baseurl = DI::baseUrl()->get() . '/addon/irc';
83         $o = '';
84
85         /* set the list of popular channels */
86         if (local_user()) {
87             $sitechats = DI::pConfig()->get( local_user(), 'irc', 'sitechats');
88             if (!$sitechats)
89                 $sitechats = DI::config()->get('irc', 'sitechats');
90         } else {
91             $sitechats = DI::config()->get('irc','sitechats');
92         }
93         if($sitechats)
94                 $chats = explode(',',$sitechats);
95         else
96                 $chats = ['friendica','chat','chatback','hottub','ircbar','dateroom','debian'];
97
98
99         DI::page()['aside'] .= '<div class="widget"><h3>' . DI::l10n()->t('Popular Channels') . '</h3><ul>';
100         foreach($chats as $chat) {
101                 DI::page()['aside'] .= '<li><a href="' . DI::baseUrl()->get() . '/irc?channels=' . $chat . '" >' . '#' . $chat . '</a></li>';
102         }
103         DI::page()['aside'] .= '</ul></div>';
104
105         /* setting the channel(s) to auto connect */
106         if (local_user()) {
107             $autochans = DI::pConfig()->get(local_user(), 'irc', 'autochans');
108             if (!$autochans)
109                 $autochans = DI::config()->get('irc','autochans');
110         } else {
111             $autochans = DI::config()->get('irc','autochans');
112         }
113         if($autochans)
114                 $channels = $autochans;
115         else
116                 $channels = ($_GET['channels'] ?? '') ?: 'friendica';
117
118 /* add the chatroom frame and some html */
119   $o .= <<< EOT
120 <h2>IRC chat</h2>
121 <p><a href="http://tldp.org/HOWTO/IRC/beginners.html" target="_blank">A beginner's guide to using IRC. [en]</a></p>
122 <iframe src="//webchat.freenode.net?channels=$channels" style="width:100%; max-width:900px; height: 600px;"></iframe>
123 EOT;
124
125 return $o;
126
127 }
128
129 function irc_addon_admin_post (&$a) {
130         if(! is_site_admin())
131                 return;
132
133         if($_POST['irc-submit']) {
134                 Config::set('irc','autochans',trim($_POST['autochans']));
135                 Config::set('irc','sitechats',trim($_POST['sitechats']));
136                 /* stupid pop-up thing */
137                 info(DI::l10n()->t('IRC settings saved.') . EOL);
138         }
139 }
140 function irc_addon_admin (&$a, &$o) {
141         $sitechats = DI::config()->get('irc','sitechats'); /* popular channels */
142         $autochans = DI::config()->get('irc','autochans');  /* auto connect chans */
143         $t = Renderer::getMarkupTemplate( "admin.tpl", "addon/irc/" );
144         $o = Renderer::replaceMacros($t, [
145                 '$submit' => DI::l10n()->t('Save Settings'),
146                 '$autochans' => [ 'autochans', DI::l10n()->t('Channel(s) to auto connect (comma separated)'), $autochans, DI::l10n()->t('List of channels that shall automatically connected to when the app is launched.')],
147                 '$sitechats' => [ 'sitechats', DI::l10n()->t('Popular Channels (comma separated)'), $sitechats, DI::l10n()->t('List of popular channels, will be displayed at the side and hotlinked for easy joining.') ]
148         ]);
149 }