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